#include <conio.h>
#include <stdlib.h>
//#include"FILE.h"
typedef struct shangpin
{
char name[20];
int biaohao;
int shuliang;
double jiage;
}Node;
typedef struct list
{
Node data;
struct list *next;
}List ,*Slist;
void charu(Slist &s,Node x);
int caidan()
{
int i;
while(1)
{
system("cls");
printf("\n\t\t\t 商店銷售管理系統\n\n");
printf("\t\t **********************************************\n\n");
printf("\t\t 1--添加商品 2--出售商品\n\n");
printf("\t\t 3--刪除商品 4--顯示商品\n\n");
printf("\t\t 5--查找商品 0--退出系統\n\n");
printf("\t\t **********************************************\n\n");
printf("請選擇(0-5): ");
scanf("%d",&i);
if(i<6&&i>=0)break;
}
return i;
}
void chushihua(Slist &s)
{
Slist head=new List;
head->next=NULL;
s=head;
}
void tianjia(Slist &s)
{
Node x;
Slist r,p,q;
int i=0;
r=new List;
r->next=NULL;
q=p=s->next;
while(q)
{
i++;
q=q->next;
}
printf("請輸入商品名稱、數量以及價格: ");
scanf("%s%d%lf",x.name,&x.shuliang,&x.jiage);
x.biaohao=i+1;
r->data=x;
s->next=r;
r->next=p;
}
void duchu(Slist &s)
{
FILE *cp;//定義文件指針
int i=0;
Node x;
if((cp=fopen("shangpin.txt","a+"))==NULL)//打開文件
{printf("文件打開失敗!");return ;}
fseek(cp,0L,SEEK_SET);//將文件指針移動到文件開頭
while(!feof(cp))
{
fscanf(cp,"%s%d%lf",x.name,&x.shuliang,&x.jiage);//讀取文件的內容
x.biaohao=++i;
charu(s,x);//把讀到的數據插入到鏈表上
}
fclose(cp);//關閉文件
}
void charu(Slist &s,Node x)
{
Slist r,p=s->next;
r=new List;
r->next=NULL;
r->data=x;
s->next=r;
r->next=p;
}
void xieru(Slist s)
{
FILE *cp;//同上
Slist p=s->next;
if((cp=fopen("shangpin.txt","w+"))==NULL)
{printf("文件打開失敗!");return ;}//同上
while(p)
{
fprintf(cp,"%s %d %.2lf ",p->data.name,p->data.shuliang,p->data.jiage);//寫入文件中,並且以空格隔開
p=p->next;
}
fclose(cp);//關閉文件
}
Slist chazhao(Slist s,int m)
{
Slist p=s->next;
if(p==0)return 0;
if(p->next==0)return p;
if(p->data.biaohao==m)return p;
while(p->next)
{
if(p->next->data.biaohao==m)return p;
p=p->next;
}
return 0;
}
void xianshi(Slist s)
{
Slist p;
p=s->next;
printf("%10s%20s%10s%10s\n","商品編號","商品名稱","商品數量","商品價格");
while(p)
{
printf("%10d%20s%10d%10.2f\n",p->data.biaohao,p->data.name,p->data.shuliang,p->data.jiage);
p=p->next;
}
}
void shanchu(Slist &s)
{
}
void chushou(Slist &s)
{
}
void main()
{
int num;
Slist s;
chushihua(s);
duchu(s);
num=caidan();
while(1)
{
switch(num)
{
case 0:exit(0);break;
case 1:tianjia(s);break;
case 2:chushou(s);break;
case 3:shanchu(s);break;
case 4:xianshi(s);break;
case 5:chazhao(s,1);break;
}
printf("按任意鍵繼續!");
getch();
num=caidan();
}
xieru(s);
}
//有些函數我在沒寫啊,妳自己寫寫吧,文件的讀取和寫入,還有鏈表的創建我都給妳寫好了,也就是上面的1,4我都寫好了,
//包括了文件,剩下的2,3,5,妳自己試試,並且我也給妳寫了壹個查找函數,妳看看能用不!