XMU C语言程序设计实践(5)

来源:互联网 发布:国家打击网络棋牌吗? 编辑:程序博客网 时间:2024/06/04 19:12

•       使用动态链表完成一个简单的商品库存信息管理系统。

•       商品信息包括如下字段:商品号、商品名称、商品库存

•       函数

create:接收用户输入的商品号和商品名称的信息,建立链表;库存初始化为0,没有进货之前不允许销售;商品号为0表示用户输入结束。本函数用于初始化,如果第二次被调用的时候,首先要执行destroy清除旧链表。

destroy:给定链表的头指针,删除链表的所有节点,并释放相应的空间。本函数在程序退出前应至少被调用一次。在调用此函数前,必须给予用户提示,使用户在删除前有反悔的机会。

sell:商品销售,由参数传入商品号和销售数量。如果不存在给定商品号的商品或销售数量大于相应商品的库存则出错;否则,从指定商品的库存中扣除相应的销售数量。当商品库存为0,则从链表中删除该商品。

stock:商品进货,由参数传入商品号和进货数量。如果不存在给定商品号的商品则在链表中插入新商品,并提示用户输入该商品名称;否则,增加指定商品的库存量。

list:列出所有商品的情况。

•       主程序

程序运行后,循环显示如下菜单:
1. 输入商品信息2. 销售3. 进货4. 列举商品信息5. 清除所有商品6.退出

根据用户的选择进一步提示用户输入并调用对应函数。




////by coolxxx//#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#define min(a,b) ((a)<(b)?(a):(b))#define max(a,b) ((a)>(b)?(a):(b))#define abs(a) ((a)>0?(a):(-(a)))#define lowbit(a) (a&(-a))#define sqr(a) ((a)*(a))#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))#define eps (1e-8)#define J 10000000#define MAX 0x7f7f7f7f#define PI 3.1415926535897#define N 104typedef long long LL;int cas,cass;int n,m,lll,ans;int mark1,mark2,mark5;//mark1=1商品非空 mark2=1 已经进货 mark5=1已经清空//typedef struct xxx xxx;struct xxx{int num,sum;char name[N];struct xxx * next;};void destroy(struct xxx *h){if(h->next==NULL){mark1=0;mark5=1;puts("清除完毕");return;}struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);struct xxx *q=(struct xxx *)malloc(sizeof(struct xxx)*1);p=h->next;while(p!=NULL){q=p->next;p=NULL;free(p);p=q;}h->next=NULL;mark1=0;mark5=1;puts("清除完毕");}void create(struct xxx *h){int xnum;puts("请输入商品号和商品名称,以商品号为0结束");while(scanf("%d",&xnum) && xnum){struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);p->next=h->next;p->num=xnum;p->sum=0;h->next=p;scanf("%s",p->name);}mark1=1;mark5=0;}void stock(struct xxx *h,int xnum,int xsum){struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);for(p=h->next;p!=NULL && p->num!=xnum;p=p->next);if(p!=NULL){p->sum+=xsum;printf("进货完成,现在%d号商品库存为%d",p->num,p->sum);return;}else{puts("商品不在清单内,现已加入清单,请输入商品名称");struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);p->next=h->next;h->next=p;p->num=xnum;p->sum=xsum;scanf("%s",p->name);printf("进货完成,现在%d号商品库存为%d",p->num,p->sum);}mark2=1;}void sell(struct xxx *h,int xnum,int xsum){struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);for(p=h->next;p!=NULL && p->num!=xnum;p=p->next);if(p!=NULL){if(p->sum<xsum){puts("商品库存小于销售数量,请核对后再进行销售");return;}p->sum-=xsum;printf("销售完成,%d商品剩余%d库存",p->num,p->sum);}else{puts("商品不在货物清单内,请核对后再进行销售");}}void list(struct xxx *h){if(h->next==NULL){puts("商品列表为空");return;}puts("商品列表如下:");struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);for(p=h->next;p!=NULL;p=p->next){printf("%-4d%-10s%-4d\n",p->num,p->name,p->sum);}puts("");}void work1(struct xxx *h){if(mark1){puts("当前商品列表非空,如果要生成商品列表需要先清空商品列表\n确定清空商品列表请输入1,取消请输入0");scanf("%d",&cas);if(cas==0){puts("您已取消清空列表");return;}destroy(h);}puts("现在开始输入货物清单");create(h);}void work2(struct xxx *h){int xnum,xsum;if(!mark2){puts("商品还未进货,请先进货再进行销售");return;}puts("请输入需要销售的商品号和销售数量");scanf("%d%d",&xnum,&xsum);sell(h,xnum,xsum);}void work3(struct xxx *h){int xnum,xsum;puts("请输入进货的商品号和商品数量");scanf("%d%d",&xnum,&xsum);stock(h,xnum,xsum);}void work4(struct xxx *h){list(h);}void work5(struct xxx *h){puts("即将删除所有商品!\n确定请输入1,取消请输入0");scanf("%d",&cas);if(cas==0){puts("您已取消删除所有商品");return;}destroy(h);}int main(){#ifndef ONLINE_JUDGE//freopen("1.txt","r",stdin);//freopen("2.txt","w",stdout);#endifint i,j;struct xxx *head=(struct xxx *)malloc(sizeof(struct xxx)*1);head->next=NULL;while(1){puts("1.输入商品信息\n2.销售\n3.进货\n4.列举商品信息\n5.清除所有商品\n6.退出");scanf("%d",&cass); if(cass==1)work1(head);else if(cass==2)work2(head);else if(cass==3)work3(head);else if(cass==4)work4(head);else if(cass==5)work5(head);else if(cass==6){if(mark5)break;puts("您还未清除所有商品,请清除所有商品后再退出");}puts("\n");}return 0;}/*////*/