高分急求操作系统代码

2024-12-17 15:45:02
推荐回答(1个)
回答1:

#include
#include
#include
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;
}