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

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

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

•       函数

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

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

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

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

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

•       主程序

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

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

  1 //
  2 //by coolxxx
  3 //
  4 #include<stdio.h>
  5 #include<stdlib.h>
  6 #include<string.h>
  7 #include<math.h>
  8 #define min(a,b) ((a)<(b)?(a):(b))
  9 #define max(a,b) ((a)>(b)?(a):(b))
 10 #define abs(a) ((a)>0?(a):(-(a)))
 11 #define lowbit(a) (a&(-a))
 12 #define sqr(a) ((a)*(a))
 13 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
 14 #define eps (1e-8)
 15 #define J 10000000
 16 #define MAX 0x7f7f7f7f
 17 #define PI 3.1415926535897
 18 #define N 104
 19 typedef long long LL;
 20 int cas,cass;
 21 int n,m,lll,ans;
 22 int mark1,mark2,mark5;//mark1=1商品非空 mark2=1 已经进货 mark5=1已经清空
 23 //typedef struct xxx xxx;
 24 struct xxx
 25 {
 26     int num,sum;
 27     char name[N];
 28     struct xxx * next;
 29 };
 30 void destroy(struct xxx *h)
 31 {
 32     if(h->next==NULL)
 33     {
 34         mark1=0;
 35         mark5=1;
 36         puts("清除完毕");
 37         return;
 38     }
 39     struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);
 40     struct xxx *q=(struct xxx *)malloc(sizeof(struct xxx)*1);
 41     p=h->next;
 42     while(p!=NULL)
 43     {
 44         q=p->next;
 45         p=NULL;
 46         free(p);
 47         p=q;
 48     }
 49     h->next=NULL;
 50     mark1=0;
 51     mark5=1;
 52     puts("清除完毕");
 53 }
 54 void create(struct xxx *h)
 55 {
 56     int xnum;
 57     puts("请输入商品号和商品名称,以商品号为0结束");
 58     while(scanf("%d",&xnum) && xnum)
 59     {
 60         struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);
 61         p->next=h->next;
 62         p->num=xnum;
 63         p->sum=0;
 64         h->next=p;
 65         scanf("%s",p->name);
 66     }
 67     mark1=1;
 68     mark5=0;
 69 }
 70 void stock(struct xxx *h,int xnum,int xsum)
 71 {
 72     struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);
 73     for(p=h->next;p!=NULL && p->num!=xnum;p=p->next);
 74     if(p!=NULL)
 75     {
 76         p->sum+=xsum;
 77         printf("进货完成,现在%d号商品库存为%d",p->num,p->sum);
 78         return;
 79     }
 80     else
 81     {
 82         puts("商品不在清单内,现已加入清单,请输入商品名称");
 83         struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);
 84         p->next=h->next;
 85         h->next=p;
 86         p->num=xnum;
 87         p->sum=xsum;
 88         scanf("%s",p->name);
 89         printf("进货完成,现在%d号商品库存为%d",p->num,p->sum);
 90     }
 91     mark2=1;
 92 }
 93 void sell(struct xxx *h,int xnum,int xsum)
 94 {
 95     struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);
 96     for(p=h->next;p!=NULL && p->num!=xnum;p=p->next);
 97     if(p!=NULL)
 98     {
 99         if(p->sum<xsum)
100         {
101             puts("商品库存小于销售数量,请核对后再进行销售");
102             return;
103         }
104         p->sum-=xsum;
105         printf("销售完成,%d商品剩余%d库存",p->num,p->sum);
106     }
107     else
108     {
109         puts("商品不在货物清单内,请核对后再进行销售");
110     }
111 }
112 void list(struct xxx *h)
113 {
114     if(h->next==NULL)
115     {
116         puts("商品列表为空");
117         return;
118     }
119     puts("商品列表如下:");
120     struct xxx *p=(struct xxx *)malloc(sizeof(struct xxx)*1);
121     for(p=h->next;p!=NULL;p=p->next)
122     {
123         printf("%-4d%-10s%-4d
",p->num,p->name,p->sum);
124     }
125     puts("");
126 }
127 void work1(struct xxx *h)
128 {
129     if(mark1)
130     {
131         puts("当前商品列表非空,如果要生成商品列表需要先清空商品列表
确定清空商品列表请输入1,取消请输入0");
132         scanf("%d",&cas);
133         if(cas==0)
134         {
135             puts("您已取消清空列表");
136             return;
137         }
138         destroy(h);
139     }
140     puts("现在开始输入货物清单");
141     create(h);
142 }
143 void work2(struct xxx *h)
144 {
145     int xnum,xsum;
146     if(!mark2)
147     {
148         puts("商品还未进货,请先进货再进行销售");
149         return;
150     }
151     puts("请输入需要销售的商品号和销售数量");
152     scanf("%d%d",&xnum,&xsum);
153     sell(h,xnum,xsum);
154 }
155 void work3(struct xxx *h)
156 {
157     int xnum,xsum;
158     puts("请输入进货的商品号和商品数量");
159     scanf("%d%d",&xnum,&xsum);
160     stock(h,xnum,xsum);
161 }
162 void work4(struct xxx *h)
163 {
164     list(h);
165 }
166 void work5(struct xxx *h)
167 {
168     puts("即将删除所有商品!
确定请输入1,取消请输入0");
169     scanf("%d",&cas);
170     if(cas==0)
171     {
172         puts("您已取消删除所有商品");
173         return;
174     }
175     destroy(h);
176 }
177 int main()
178 {
179     #ifndef ONLINE_JUDGE
180 //    freopen("1.txt","r",stdin);
181 //    freopen("2.txt","w",stdout);
182     #endif
183     int i,j;
184     struct xxx *head=(struct xxx *)malloc(sizeof(struct xxx)*1);
185     head->next=NULL;
186     while(1)
187     {
188         puts("1.输入商品信息
2.销售
3.进货
4.列举商品信息
5.清除所有商品
6.退出");
189         scanf("%d",&cass);
190              if(cass==1)work1(head);
191         else if(cass==2)work2(head);
192         else if(cass==3)work3(head);
193         else if(cass==4)work4(head);
194         else if(cass==5)work5(head);
195         else if(cass==6)
196         {
197             if(mark5)break;
198             puts("您还未清除所有商品,请清除所有商品后再退出");
199         }
200         puts("
");
201     }
202     return 0;
203 }
204 /*
205 //
206 
207 //
208 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/7090837.html