本文实例为大家分享了C语言实现餐饮管理与点餐系统的具体代码,供大家参考,具体内容如下
一、项目简介
1、本程序实现了用户的点餐功能,共包含五类菜品:凉菜、热菜、锅仔、主食、饮品。还有查看所点菜品与结束等功能。
2、后台管理员:按a可进入,主页面并未提示。
3、合理运用getch函数,清屏与暂停函数,页面整洁清爽。
4、菜品与txt文件对应,可实现对程序与txt的同步增删改查等功能。
5、使用了C语言很多基础语法:函数、指针、结构体、文件等。
二、程序说明
程序运行后在主页面按a进入后台管理页面,给相应的菜品信息添加菜品,这些信息会自动保存到txt中,下次打开的时候也会自动读取,做好菜品信息的创建工作就可以回到主页面进行相应的用户点餐操作。
三、功能演示
四、思维导图
五、程序代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> /*宏定义区*/ #define max_str 30 //最大字符串长度 #define max_num 30 //最大菜品数量 #define max_all 100 //今日售卖总计最大值 /*定义菜品结构体*/ struct dishes { char name[max_str]; int price; }; /*定义全局变量*/ struct dishes list1[max_num]; //存放凉菜菜品信息 int num1=0; //存放凉菜菜品数量 int *p1=&num1; struct dishes list2[max_num]; //存放热菜菜品信息 int num2=0; //存放热菜菜品数量 int *p2=&num2; struct dishes list3[max_num]; //存放锅仔菜品信息 int num3=0; //存放锅仔菜品数量 int *p3=&num3; struct dishes list4[max_num]; //存放主食菜品信息 int num4=0; //存放主食菜品数量 int *p4=&num4; struct dishes list5[max_num]; //存放饮品菜品信息 int num5=0; //存放饮品菜品数量 int *p5=&num5; struct dishes list6[max_num]; //存放顾客点餐菜品信息 int num6=0; //存放顾客点餐菜品数量 int *p6=&num6; struct dishes list7[max_all]; //存放今日所点总菜品信息 int num7=0; //存放今日所点总菜品数量 int *p7=&num7; /*全局声明*/ /*菜单的输出*/ //一级主菜单 char menu_m1(); // 管理员一级菜单 char menu_a1(); // 管理员二级菜单:添加或删除菜品的提示 char menu_a2(); //输出菜单1(包括提示输入语句) void output_1(struct dishes list[],int num); //输出菜品信息2(不含有提示输入语句) void output_2(struct dishes list[],int num); /*函数*/ //创建程序运行所需的txt文件 void creat_file(); //加载菜品信息到结构体数组,嵌套功能模块1:读取txt写入到单个结构体数组中 void load_dishes(struct dishes list1[],int *p1,struct dishes list2[],int *p2,struct dishes list3[],int *p3,struct dishes list4[],int *p4,struct dishes list5[],int *p5); //实现点餐的函数,嵌套功能模块2:实现点餐的结构体数组写入数据 void ordering(struct dishes list1[],int num1,struct dishes list2[],int *p2,struct dishes list3[],int *p3); // 查看与结算的输出函数 void settlement(struct dishes list[],int num); // 计算今日收入 void income(struct dishes list[],int num); // 实现菜品的价格修改,内嵌功能模块3:实现结构体菜品的价格修改与文件的重构 void change(struct dishes list[],int *p,char ch); // 实现菜品的删除,内嵌功能模块4:实现结构体菜品的删除与文件的重构 void del(struct dishes list[],int *p,char ch); // 函数:实现菜品的添加,内嵌功能模块5:实现结构体菜品的添加与文件的重构 void add(struct dishes list[],int *p,char ch); /*功能模块区*/ //功能模块1:读取txt写入到单个结构体数组中 int function_read(struct dishes list[],FILE *fp); //功能模块2:实现点餐的结构体数组写入数据 void function_ordering(struct dishes list1[],int num1,struct dishes list2[],int *p2,struct dishes list3[],int *p3,int choice); // 功能模块3:实现结构体菜品的价格修改与文件的重构,内嵌功能模块6:对经过修改的菜品结构体按价格从低到高排序 int funtion_change(struct dishes list[],int num,FILE *fp); //功能模块4:实现结构体菜品的删除与文件的重构 int function_del(struct dishes list[],int *p,FILE *fp); //功能模块5:实现结构体菜品的添加与文件的重构,,内嵌功能模块6:对经过修改的菜品结构体按价格从低到高排序 int function_add(struct dishes list[],int *p,FILE *fp); // 功能模块6:对经过修改的菜品结构体按价格从低到高排序 void function_sort(struct dishes list[],int num); int main() { creat_file(); load_dishes(list1,p1,list2,p2,list3,p3,list4,p4,list5,p5); int i,j,choice; char a,b,c; while(1) { a=menu_m1(); switch(a) { case '1': ordering(list1,num1,list6,p6,list7,p7); break; case '2': ordering(list2,num2,list6,p6,list7,p7); break; case '3': ordering(list3,num3,list6,p6,list7,p7); break; case '4': ordering(list4,num4,list6,p6,list7,p7); break; case '5': ordering(list5,num5,list6,p6,list7,p7); break; case '6': system("cls"),settlement(list6,num6),system("pause"),system("cls"); break; case 'y': system("cls"),settlement(list6,num6),*p6=0,system("pause"); break; case 'a': system("cls"); while(1) { b=menu_a1(); switch(b) { case '1': system("cls"),income(list7,num7),system("pause"),system("cls"); break; case '2': { system("cls"); c=menu_a2(); system("cls"); switch(c) { case '1': change(list1,p1,c); break; case '2': change(list2,p2,c); break; case '3': change(list3,p3,c); break; case '4': change(list4,p4,c); break; case '5': change(list5,p5,c); break; } system("cls"); } break; case '3': { system("cls"); c=menu_a2(); system("cls"); switch(c) { case '1': del(list1,p1,c); break; case '2': del(list2,p2,c); break; case '3': del(list3,p3,c); break; case '4': del(list4,p4,c); break; case '5': del(list5,p5,c); break; } system("cls"); } break; case '4': { system("cls"); c=menu_a2(); system("cls"); switch(c) { case '1': add(list1,p1,c); break; case '2': add(list2,p2,c); break; case '3': add(list3,p3,c); break; case '4': add(list4,p4,c); break; case '5': add(list5,p5,c); break; } system("cls"); } break; case '0': system("cls"); break; } if(b=='0') break; } break; } system("cls"); } } /*菜单的输出*/ //一级主菜单 char menu_m1() { char ch; printf(" ---------------------------n"); printf(" | 欢迎光临本餐厅 |n"); printf(" | 请按序号选择菜品 |n"); printf(" ---------------------------n"); printf(" | 1 凉菜 |n"); printf(" | 2 热菜 |n"); printf(" | 3 锅仔 |n"); printf(" | 4 主食 |n"); printf(" | 5 饮品 |n"); printf(" | 6 查看 |n"); printf(" | y 结算 |n"); printf(" ---------------------------n"); printf(" 输入序号:"); ch=getch(); return ch; } // 管理员一级菜单 char menu_a1() { char ch; printf("n-------------------------------n"); printf("t1t今日收入tn"); printf("t2t修改价格tn"); printf("t3t删除菜品tn"); printf("t4t添加菜品tn"); printf("t0t返回上一级tn"); printf("-------------------------------n"); printf("输入序号:"); ch=getch(); return ch; } // 管理员二级菜单:添加或删除菜品的提示 char menu_a2() { char ch; printf("n-------------------------------n"); printf("t1t凉菜tn"); printf("t2t热菜tn"); printf("t3t锅仔tn"); printf("t4t主食tn"); printf("t5t饮品tn"); printf("t0t返回上一级tn"); printf("-------------------------------n"); printf("输入序号:"); ch=getch(); return ch; } //输出菜单1(包括提示输入语句) void output_1(struct dishes list[],int num) { int i; printf("n-------------------------------------------------n"); printf("|t序号t菜品tt价格tt|n"); printf("-------------------------------------------------n"); for(i=0; i<num; i++) { if(strlen(list[i].name)<=6) printf("|t%dt%stt%dtt|n",i+1,list[i].name,list[i].price); else printf("|t%dt%st%dtt|n",i+1,list[i].name,list[i].price); } printf("|t0t返回tttt|n"); printf("-------------------------------------------------n"); printf("请输入序号:n"); } //输出菜品信息2(不含有提示输入语句) void output_2(struct dishes list[],int num) { int i; printf("n-------------------------------------------------n"); printf("|t序号t菜品tt价格tt|n"); printf("-------------------------------------------------n"); for(i=0; i<num; i++) { if(strlen(list[i].name)<=6) printf("|t%dt%stt%dtt|n",i+1,list[i].name,list[i].price); else printf("|t%dt%st%dtt|n",i+1,list[i].name,list[i].price); } printf("|t0t返回tttt|n"); printf("-------------------------------------------------n"); } /*函数*/ //创建程序运行所需的txt文件 void creat_file() { FILE *fp=fopen("凉菜.txt","a"); if(fp==NULL) return; fclose(fp); fp=fopen("热菜.txt","a"); if(fp==NULL) return; fclose(fp); fp=fopen("锅仔.txt","a"); if(fp==NULL) return; fclose(fp); fp=fopen("主食.txt","a"); if(fp==NULL) return; fclose(fp); fp=fopen("饮品.txt","a"); if(fp==NULL) return; fclose(fp); } //加载菜品信息到结构体数组,嵌套功能模块1:读取txt写入到单个结构体数组中 void load_dishes(struct dishes list1[],int *p1,struct dishes list2[],int *p2,struct dishes list3[],int *p3,struct dishes list4[],int *p4,struct dishes list5[],int *p5) { FILE *fp; fp=fopen("凉菜.txt","r"); *p1=function_read(list1,fp); fp=fopen("热菜.txt","r"); *p2=function_read(list2,fp); fp=fopen("锅仔.txt","r"); *p3=function_read(list3,fp); fp=fopen("主食.txt","r"); *p4=function_read(list4,fp); fp=fopen("饮品.txt","r"); *p5=function_read(list5,fp); fclose(fp); } //实现点餐的函数,嵌套功能模块2:实现点餐的结构体数组写入数据 void ordering(struct dishes list1[],int num1,struct dishes list2[],int *p2,struct dishes list3[],int *p3) { int choice; system("cls"); output_1(list1,num1); while(1) { scanf("%d",&choice); if(choice>=0&&choice<=num1) { if(choice==0) break; else function_ordering(list1,num1,list2,p2,list3,p3,choice); } else printf("输入序号有误,请重新输入:n"); } } // 查看与结算的输出函数 void settlement(struct dishes list[],int num) { int i,sum; for(i=0,sum=0; i<num; i++) sum+=list[i].price; printf("n-------------------------------------------------n"); printf("|t序号t菜品tt价格tt|n"); printf("-------------------------------------------------n"); for(i=0; i<num; i++) { if(strlen(list[i].name)<=6) printf("|t%dt%stt%dtt|n",i+1,list[i].name,list[i].price); else printf("|t%dt%st%dtt|n",i+1,list[i].name,list[i].price); } printf("-------------------------------------------------n"); printf("总价格为:%dnn",sum); } // 计算今日收入 void income(struct dishes list[],int num) { int i,sum; output_2(list,num); for(i=0,sum=0; i<num; i++) sum+=list[i].price; printf("今日总收入:%dn",sum); } // 实现菜品的价格修改,内嵌功能模块3:实现结构体菜品的价格修改与文件的重构 void change(struct dishes list[],int *p,char ch) { int i=1; FILE *fp; while(i!=0) { switch(ch) { case '1': output_2(list1,num1),fp=fopen("凉菜.txt","w"),i=funtion_change(list1,num1,fp),fclose(fp); break; case '2': output_2(list2,num2),fp=fopen("热菜.txt","w"),i=funtion_change(list2,num2,fp),fclose(fp); break; case '3': output_2(list3,num3),fp=fopen("锅仔.txt","w"),i=funtion_change(list3,num3,fp),fclose(fp); break; case '4': output_2(list4,num4),fp=fopen("主食.txt","w"),i=funtion_change(list4,num4,fp),fclose(fp); break; case '5': output_2(list5,num5),fp=fopen("饮品.txt","w"),i=funtion_change(list5,num5,fp),fclose(fp); break; } } } // 实现菜品的删除,内嵌功能模块4:实现结构体菜品的删除与文件的重构 void del(struct dishes list[],int *p,char ch) { int i=1; FILE *fp; while(i!=0) { switch(ch) { case '1': output_2(list1,num1),fp=fopen("凉菜.txt","w"),i=function_del(list1,p1,fp),fclose(fp); break; case '2': output_2(list2,num2),fp=fopen("热菜.txt","w"),i=function_del(list2,p2,fp),fclose(fp); break; case '3': output_2(list3,num3),fp=fopen("锅仔.txt","w"),i=function_del(list3,p3,fp),fclose(fp); break; case '4': output_2(list4,num4),fp=fopen("主食.txt","w"),i=function_del(list4,p4,fp),fclose(fp); break; case '5': output_2(list5,num5),fp=fopen("饮品.txt","w"),i=function_del(list5,p5,fp),fclose(fp); break; } } } // 函数:实现菜品的添加,内嵌功能模块5:实现结构体菜品的添加与文件的重构 void add(struct dishes list[],int *p,char ch) { int i=1; FILE *fp; while(i!=0) { switch(ch) { case '1': output_2(list1,num1),fp=fopen("凉菜.txt","w"),i=function_add(list1,p1,fp),fclose(fp); break; case '2': output_2(list2,num2),fp=fopen("热菜.txt","w"),i=function_add(list2,p2,fp),fclose(fp); break; case '3': output_2(list3,num3),fp=fopen("锅仔.txt","w"),i=function_add(list3,p3,fp),fclose(fp); break; case '4': output_2(list4,num4),fp=fopen("主食.txt","w"),i=function_add(list4,p4,fp),fclose(fp); break; case '5': output_2(list5,num5),fp=fopen("饮品.txt","w"),i=function_add(list5,p5,fp),fclose(fp); break; } } } /*功能模块区*/ //功能模块1:读取txt写入到单个结构体数组中 int function_read(struct dishes list[],FILE *fp) { int num=1;//索引从0开始 定义为1统计个数 char t,c[max_str]; int i; if(fp==NULL) { printf("Can't open this filen"); exit(1); } while(!feof(fp)) //检测流上的文件结束符,如果文件s结束,则返回非0值,否则返回0 { t=fgetc(fp); if(t=='n') num++; } rewind(fp); //将指针重置到第一行 for(i=0; i<num; i++) { fgets(c,max_str,fp); sscanf(c,"%s %d",list[i].name,&list[i].price); } fclose(fp); return num; } //功能模块2:实现点餐的结构体数组写入数据 void function_ordering(struct dishes list1[],int num1,struct dishes list2[],int *p2,struct dishes list3[],int *p3,int choice) { strcpy(list2[*p2].name,list1[choice-1].name); list2[*p2].price=list1[choice-1].price; strcpy(list3[*p3].name,list1[choice-1].name); list3[*p3].price=list1[choice-1].price; *p2=*p2+1; *p3=*p3+1; } // 功能模块3:实现结构体菜品的价格修改与文件的重构,内嵌功能模块6:对经过修改的菜品结构体按价格从低到高排序 int funtion_change(struct dishes list[],int num,FILE *fp) { if(fp==NULL) { printf("Can't open this filen"); exit(1); } int i,n; char temp[max_str],t[max_str]; printf("n输入ID:"); scanf("%d",&n); if(n==0) { for(i=0; i<num; i++) { strcpy(temp,list[i].name); strcat(temp," "); itoa(list[i].price,t,10); //itoa():将整型值转换为字符串,参数依次为:要复制的内容,复制给谁,以什么进制。 strcat(temp,t); strcat(temp,"n"); fputs(temp,fp); } return n; } printf("输入修改后的价格:"); scanf("%d",&list[n-1].price); function_sort(list,num); for(i=0; i<num; i++) { strcpy(temp,list[i].name); strcat(temp," "); itoa(list[i].price,t,10); //itoa():将整型值转换为字符串,参数依次为:要复制的内容,复制给谁,以什么进制。 strcat(temp,t); strcat(temp,"n"); fputs(temp,fp); } printf("修改完成n"); system("pause"); system("cls"); return 1; } //功能模块4:实现结构体菜品的删除与文件的重构 int function_del(struct dishes list[],int *p,FILE *fp) { if(fp==NULL) { printf("Can't open this filen"); exit(1); } int i,n; char temp[max_str],t[max_str]; printf("n输入想要修改的菜品ID:"); scanf("%d",&n); if(n==0) { for(i=0; i<*p; i++) { strcpy(temp,list[i].name); strcat(temp," "); itoa(list[i].price,t,10); //itoa():将整型值转换为字符串,参数依次为:要复制的内容,复制给谁,以什么进制。 strcat(temp,t); strcat(temp,"n"); fputs(temp,fp); } return 0; } for(i=n-1; i<*p; i++) { strcpy(list[i].name,list[i+1].name); list[i].price=list[i+1].price; } *p=*p-1; for(i=0; i<*p; i++) { strcpy(temp,list[i].name); strcat(temp," "); itoa(list[i].price,t,10); //itoa():将整型值转换为字符串,参数依次为:要复制的内容,复制给谁,以什么进制。 strcat(temp,t); strcat(temp,"n"); fputs(temp,fp); } printf("删除完成n"); system("pause"); system("cls"); return 1; } //功能模块5:实现结构体菜品的添加与文件的重构,内嵌功能模块6:对经过修改的菜品结构体按价格从低到高排序 int function_add(struct dishes list[],int *p,FILE *fp) { if(fp==NULL) { printf("Can't open this filen"); exit(1); } int i,n,x; char temp[max_str],t[max_str],in[max_str]; printf("n输入0 0返回上一级n"); printf("输入菜品名和价格,中间使用空格隔开:n"); scanf("%s%d",list[*p].name,&list[*p].price); if(list[*p].price == 0) { for(i=0; i<*p; i++) { strcpy(temp,list[i].name); strcat(temp," "); itoa(list[i].price,t,10); //itoa():将整型值转换为字符串,参数依次为:要复制的内容,复制给谁,以什么进制。 strcat(temp,t); strcat(temp,"n"); fputs(temp,fp); } return 0; } *p=*p+1; function_sort(list,*p); for(i=0; i<*p; i++) { strcpy(temp,list[i].name); strcat(temp," "); itoa(list[i].price,t,10); //itoa():将整型值转换为字符串,参数依次为:要复制的内容,复制给谁,以什么进制。 strcat(temp,t); strcat(temp,"n"); fputs(temp,fp); } printf("添加完成n"); system("pause"); system("cls"); return 1; } // 功能模块6:对经过修改的菜品结构体按价格从低到高排序 void function_sort(struct dishes list[],int num) { int i,j; struct dishes t; for(j=num-1; j>0; j--) { for(i=0; i<j; i++) if(list[i].price>list[i+1].price) { t=list[i]; list[i]=list[i+1]; list[i+1]=t; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是慈祥百褶裙最近收集整理的关于C语言实现餐饮管理与点餐系统的全部内容,更多相关C语言实现餐饮管理与点餐系统内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复