#include <malloc.h>
#include <string.h>
typedef struct
{
char num[10];
char name[20];
float price;
int amount;
}Product;
typedef struct node
{
Product p;
struct node *pre;
struct node *next;
}node,*linklist;
linklist head,last;
void setData(linklist p)
{
printf("產品編號:");
scanf("%s",&p->p.num);
printf("產品名稱:");
scanf("%s",&p->p.name);
printf("產品單價:");
scanf("%f",&p->p.price);
printf("產品數量:");
scanf("%d",&p->p.amount);
}
void Insert(linklist p)
{
setData(p);
p->next=last;
last->pre->next=p;
p->pre=last->pre;
last->pre=p;
}
void Add()
{
char ch;
do
{
linklist p=(linklist)malloc(sizeof(node));
system("cls");
Insert(p);
printf("是否繼續?");
scanf(" %c",&ch);
}while(ch=='y'||ch=='Y');
}
linklist Qur()
{
char num[10];
linklist p=head->next;
printf("輸入產品編號:");
scanf("%s",num);
while(p!=last)
{
if(strcmp(num,p->p.num)==0) break;
p=p->next;
}
if(p==last) {printf("未找到\n");system("pause");}
return p;
}
void Del()
{
linklist p=Qur();
if(p==last) return;
p->pre->next=p->next;
p->next->pre=p->pre;
free(p);
printf("刪除成功\n");
system("pause");
}
void Modify()
{
linklist p=Qur();
if(p==last) return ;
setData(p);
}
void printTitle()
{
printf("編號\t名稱\t單價\t數量\n");
}
void show(linklist p)
{
printf("%s\t%s\t%.2f\t%d\n",p->p.num,p->p.name,p->p.price,p->p.amount);
}
void Tongji()
{
linklist p=head->next;
int i=0;
printTitle();
while(p!=last)
{
show(p);
p=p->next;
i++;
}
printf("***%d項商品\n",i);
system("pause");
}
void Sort()
{
linklist p,q;
for (p=head->next;p!=last;p=p->next)
{
for (q=p->next;q!=last;q=q->next)
{
if(strcmp(p->p.num,q->p.num)<0)
{
Product temp=p->p;
p->p=q->p;
q->p=temp;
}
}
}
printf("完成\n");
system("pause");
}
void Save()
{
linklist p=head->next;
FILE *fp=fopen("d:\\record.txt","w");
if(fp==NULL)
{
printf("保存失敗\n");
return;
}
fprintf(fp,"編號\t名稱\t單價\t數量\n");
while(p!=last)
{
fprintf(fp,"%s\t%s\t%.2f\t%d\n",p->p.num,p->p.name,p->p.price,p->p.amount);
p=p->next;
}
fclose(fp);
printf("完成\n");
system("pause");
}
void Disp()
{
linklist p=head->next;
printTitle();
while(p!=last)
{
show(p);
p=p->next;
}
system("pause");
}
void Wrong()
{
printf("輸入錯誤!\n");
system("pause");
}
void menu(void)
{
system("cls");
printf("********商品銷售管理系統*******\n");
printf("* *\n");
printf("* 1:添加 *\n");
printf("* 2:刪除 *\n");
printf("* 3:查詢 *\n");
printf("* 4:修改 *\n");
printf("* 5:插入 *\n");
printf("* 6:統計 *\n");
printf("* 7:降序排列 *\n");
printf("* 8:存儲 *\n");
printf("* 9:顯示數據 *\n");
printf("* 0:退出 *\n");
printf("* *\n");
printf("*******************************\n");
}
int select()
{
int choose;
scanf("%d",&choose);
switch(choose)
{
case 1:Add();break;
case 2:Del();break;
case 3:
{
linklist p=Qur();
if(p!=last) {show(p);system("pause");}break;
}
case 4:Modify();break;
case 5:
{
linklist p=(linklist)malloc(sizeof(node));
Insert(p);break;
}
case 6:Tongji();break;
case 7:Sort();break;
case 8:Save();break;
case 9:Disp();break;
case 0:break;
default:Wrong();break;
}
return choose;
}
void destroy()
{
linklist p=head->next;
while(p!=last)
{
head->next=p->next;
free(p);
p=head->next;
}
free(head);
free(last);
}
int main(void)
{
head=(linklist)malloc(sizeof(node));
last=(linklist)malloc(sizeof(node));
head->next=last;
last->next=NULL;
last->pre=head;
head->pre=NULL;
do
{
menu();
} while (select()!=0);
destroy();
return 0;
}