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 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
| #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <time.h> #include <windows.h>
#define SIZE1 100 #define SIZE2 200
typedef struct _node { char name[SIZE1]; char restaurant[SIZE1]; float price; int fraction; char comment[SIZE2]; struct _node *next; } Node; typedef struct _list //方便对链表操作 { Node *head; Node *pa; Node *pb; Node *pc; } List;
void menu(void); void menu_inquire(List *list); void help(void); int menu_sort(List *list);
void add(List *list); void delete (List *list); void revise(List *list);
char *s_gets(char *st, int n); int get_int(void); float get_float(void);
void inquire_all(List *list); void inquire_one(List *list); void inquire_restaurant(List *list); void inquire_part_price(List *list); void inquire_part_fraction(List *list); void average_percentage(List *list); void output(Node *p); void random_menu(List *list); void empty(List *list);
void save(List *list); void read_txt(List *list);
void sort_ascending(List *list); void sort_descending(List *list); void sort_score(List *list); int calculate(List *list); void swap1(List *list, List *list_swap); void swap2(List *list, List *list_swap);
int random_number(List *list); void color(int a);
int main() { menu(); return 0; }
void menu() { int options, z = 1; List list; list.head = NULL; FILE *fp = fopen("Menu.txt", "a"); fclose(fp); read_txt(&list); while (z) { system("cls"); printf("\n"); color(2); printf("|-----------------------------------------------------\n"); color(14); printf("| 菜品信息系统 \n"); color(2); printf("|-----------------------------------------------------\n"); color(11); printf("|********************系统功能菜单*********************\n"); color(2); printf("|-----------------------------------------------------\n"); color(15); printf("|*******************|1、使用帮助| \n"); printf("|*******************|2、添加菜品信息| \n"); printf("|*******************|3、删除菜品信息| \n"); printf("|*******************|4、查询菜品信息| \n"); printf("|*******************|5、修改菜品信息| \n"); printf("|*******************|6、今天吃什么| \n"); printf("|*******************|7、清空所有数据| \n"); printf("|*******************|8、保存|\n"); color(2); printf("|-----------------------------------------------------\n"); color(15); printf("|*******************|0、退出系统| \n"); color(12); printf("|!!!前请先保存再退出系统,否则数据会丢失!!!\n"); color(2); printf("|-----------------------------------------------------\n"); color(11); printf("请输入功能序号:"); color(15); options = get_int(); switch (options) { case 0: z = 0; printf("即将退出系统。\n"); break; case 1: help(); break; case 2: add(&list); break; case 3: delete (&list); break; case 4: menu_inquire(&list); break; case 5: revise(&list); break; case 6: random_menu(&list); break; case 7: empty(&list); break; case 8: save(&list); break; default: color(12); printf("无该功能,请重新输入\n"); color(11); system("pause"); color(15); } } system("pause"); } void help() { system("cls"); printf("\n"); color(2); printf("|-----------------------------------------------------------------\n"); color(15); printf("|使用帮助:\n"); printf("|1、本菜品信息系统可供用户自行修改内容\n"); printf("|2、因为考虑到不同饭店有同样名称的菜品,本系统允许菜品名称有重复\n"); printf("|3、请勿输入错误信息,以免造成程序出错\n"); printf("|4、关闭系统前记得保存!否则数据会丢失\n"); printf("|5、有bug联系1934009145@qq.com\n"); printf("|6、github地址:github.com/qxchuckle/Dishes-Management-System\n"); color(2); printf("|-----------------------------------------------------------------\n"); printf("\n"); color(11); system("pause"); } void menu_inquire(List *list) { int options, z = 1; List *p = list; if (list->head == NULL) { color(12); printf("还未存入任何菜品!无法查询!\n"); color(11); system("pause"); return; } while (z) { system("cls"); printf("\n"); color(2); printf("|-----------------------------------------------------\n"); color(15); printf("|1、查询全部菜品信息\n"); printf("|2、按菜名查询菜品信息\n"); printf("|3、查询饭店或食堂的全部菜品信息\n"); printf("|4、查询某价格以下的全部菜品\n"); printf("|5、查询某评分以上的全部菜品\n"); printf("|6、查询某饭店或食堂的平均价格、评分和价格区间占比\n"); printf("|0、返回主菜单\n"); color(2); printf("|-----------------------------------------------------\n"); color(11); printf("请输入功能序号:"); color(15); options = get_int(); switch (options) { case 0: z = 0; break; case 1: if (menu_sort(p)) inquire_all(p); return; case 2: inquire_one(p); return; case 3: if (menu_sort(p)) inquire_restaurant(p); return; case 4: if (menu_sort(p)) inquire_part_price(p); return; case 5: if (menu_sort(p)) inquire_part_fraction(p); return; case 6: average_percentage(p); return; default: color(12); printf("无该功能,请重新输入\n"); color(11); system("pause"); } } } int menu_sort(List *list) { int options, z = 1, n = 1; List *p = list; while (z) { system("cls"); printf("\n"); color(2); printf("|-----------------------------------------------------\n"); color(15); printf("|1、按价格从低到高排序\n"); printf("|2、按价格从高到低排序\n"); printf("|3、按评分从高到低排序\n"); printf("|4、不排序\n"); printf("|0、返回主菜单\n"); color(2); printf("|-----------------------------------------------------\n"); color(11); printf("请输入功能序号:"); color(15); options = get_int(); switch (options) { case 0: z = 0; return z; case 1: sort_ascending(p); return n; case 2: sort_descending(p); return n; case 3: sort_score(p); return n; case 4: return n; default: color(12); printf("无该功能,请重新输入\n"); color(11); system("pause"); color(15); } } }
void add(List *list) { float k = 1; int n = 0, i = 0; Node *last = list->head; Node *p1 = (Node *)malloc(sizeof(Node)), *p; p1->next = NULL; system("cls"); puts("开始录入菜品:\n----------------\n"); puts("请输入菜品名称:"); do { s_gets(p1->name, SIZE1); } while (strlen(p1->name) == 0); puts("请输入菜品所属饭店or食堂名称:"); do { s_gets(p1->restaurant, SIZE1); } while (strlen(p1->restaurant) == 0); for (p = list->head; p; p = p->next) { if (!(strcmp(p1->name, p->name) || strcmp(p1->restaurant, p->restaurant))) { color(12); printf("该饭店/食堂内已存在同名菜品!\n"); color(11); system("pause"); return; } } puts("请输入菜品的价格:"); p1->price = get_float(); puts("请输入0-10的整数对菜品进行评分:"); do { n = get_int(); if (n >= 0 && n <= 10) { p1->fraction = n; i = 1; } else { color(12); puts("评分不在规定范围内,请重新输入:"); color(15); } } while (!i); puts("请对菜品进行评价:"); do { s_gets(p1->comment, SIZE2); } while (strlen(p1->comment) == 0); if (last) { while (last->next) { last = last->next; } last->next = p1; } else { list->head = p1; } color(11); puts("录入成功!"); system("pause"); } void delete (List *list) { int n = 0; char a[SIZE1]; char b[SIZE1]; Node *p, *q; if (list->head == NULL) { printf("还未存入任何菜品!删除失败!\n"); system("pause"); return; } system("cls"); printf("请输入你要删除的菜品名称:"); s_gets(a, SIZE1); printf("请输入菜品所属饭店名称:"); s_gets(b, SIZE1); for (q = NULL, p = list->head; p; q = p, p = p->next) { if (!(strcmp(a, p->name) || strcmp(b, p->restaurant))) { n++; if (q) { q->next = p->next; } else { list->head = p->next; } } } if (!n) { printf("\n没有这道菜!\n"); } else { printf("\n删除成功%d道菜。\n", n); } color(11); system("pause"); } void revise(List *list) { float k = 1; int n = 0, m = 0, i = 0; char a[SIZE1]; char b[SIZE1]; Node *p, *q; if (list->head == NULL) { printf("还未存入任何菜品!无法修改!\n"); system("pause"); return; } system("cls"); printf("请输入要修改的菜品的名称:"); s_gets(a, SIZE1); printf("请输入菜品所属饭店名称:"); s_gets(b, SIZE1); for (q = NULL, p = list->head; p; q = p, p = p->next) { if (!(strcmp(a, p->name) || strcmp(b, p->restaurant))) { n++; printf("\n原来的菜品信息:\n"); printf("|菜名:%s\n", p->name); printf("|所属:%s\n", p->restaurant); printf("|价格:%.2f\n", p->price); printf("|评分:%d\n", p->fraction); printf("|评价:%s\n", p->comment); printf("\n"); puts("请修改菜品的价格:"); p->price = get_float(); puts("请输入0-10的整数以修改菜品的评分:"); do { m = get_int(); if (m >= 0 && m <= 10) { p->fraction = m; i = 1; } else { color(12); puts("评分不在规定范围内,请重新输入:"); color(15); } } while (!i); puts("请修改对菜品的评价:"); do { s_gets(p->comment, SIZE2); } while (strlen(p->comment) == 0); } } if (n) { printf("\n修改完成。\n"); } else { printf("\n没有这道菜!\n"); } color(11); system("pause"); }
char *s_gets(char *m, int n) { char *p; char *find; p = fgets(m, n, stdin); if (p) { find = strchr(m, '\n'); if (find) *find = '\0'; else while (getchar() != '\n') continue; } return p; } int get_int() { int n; char ch; while (scanf("%d%*c", &n) != 1) { while ((ch = getchar()) != '\n') putchar(ch); color(4); printf(" 不是一个正整数!\n请您重新输入:\n"); color(15); } return n; } float get_float() { float n = 1; char ch; do { if (n <= 0) { color(12); puts("请重新输入一个正数!"); color(15); } while (scanf("%f%*c", &n) != 1) { while ((ch = getchar()) != '\n') putchar(ch); color(12); printf(" 不是一个正数!\n请您重新输入:\n"); color(15); } } while (n <= 0); return n; }
void inquire_all(List *list) { Node *p; int n = 1; system("cls"); for (p = list->head; p; p = p->next) { printf("\n第%d道菜:", n); output(p); n++; } color(11); printf("\n所有菜品查询完毕\n"); system("pause"); } void inquire_one(List *list) { int n = 0; char a[SIZE1]; Node *p; system("cls"); printf("请输入你要找的菜品名称:"); s_gets(a, SIZE1); for (p = list->head; p; p = p->next) { if (!strcmp(a, p->name)) { output(p); n++; } } color(11); printf("\n共查找到%d道菜\n", n); system("pause"); } void inquire_restaurant(List *list) { int n = 0; char a[SIZE1]; Node *p; system("cls"); printf("请输入你要找的饭店或食堂:"); s_gets(a, SIZE1); for (p = list->head; p; p = p->next) { if (!strcmp(a, p->restaurant)) { output(p); n++; } } color(11); printf("\n共查找到%d道菜\n", n); system("pause"); } void inquire_part_price(List *list) { int n = 0; float money, k = 1; char a[SIZE1]; Node *p; system("cls"); printf("请输入封顶价格:"); money = get_float(); for (p = list->head; p; p = p->next) { if (p->price <= money) { output(p); n++; } } color(11); printf("\n共查找到%d道菜\n", n); system("pause"); } void inquire_part_fraction(List *list) { int n = 0; int score; char a[SIZE1]; Node *p; system("cls"); printf("请输入最低评分:"); score = get_int(); for (p = list->head; p; p = p->next) { if (p->fraction >= score) { output(p); n++; } } printf("\n共查找到%d道菜\n", n); color(11); system("pause"); } void average_percentage(List *list) { int n = 0; float sum1 = 0, sum2 = 0, b[5] = {0}; char a[SIZE1]; Node *p; system("cls"); printf("请输入你要查的饭店或食堂:"); s_gets(a, SIZE1); for (p = list->head; p; p = p->next) { if (!strcmp(a, p->restaurant)) { sum1 += p->price; sum2 += p->fraction; if (p->price <= 5) { b[0]++; } else { if (p->price <= 10) { b[1]++; } else { if (p->price <= 15) { b[2]++; } else { if (p->price < 20) { b[3]++; } else { b[4]++; } } } } n++; } } color(11); if (n) { printf("该饭店/食堂的一共有%d道菜\n平均价格是:%.2f\t平均评分是:%.2f\n", n, sum1 / n, sum2 / n); printf("其中:\n0~5元占:%.1f%%\n5~10元占:%.1f%%\n10~15元占:%.1f%%\n", b[0] / n * 100, b[1] / n * 100, b[2] / n * 100); printf("15~20元占:%.1f%%\n20元以上占:%.1f%%\n\n", b[3] / n * 100, b[4] / n * 100); } else { color(12); printf("无该饭店或食堂!\n\n"); } color(11); system("pause"); } void output(Node *p) { printf("\n|菜名:%s\n", p->name); printf("|所属:%s\n", p->restaurant); printf("|价格:%.2f\n", p->price); printf("|评分:%d\n", p->fraction); printf("|评价:%s\n", p->comment); } void random_menu(List *list) { system("cls"); if (list->head == NULL) { color(12); printf("还未存入任何菜品!\n"); color(11); system("pause"); return; } List *q = list; Node *p = list->head; int n = random_number(q), i = 1; while (p != NULL) {
if (i == n) { output(p); color(11); system("pause"); return; } i++; p = p->next; } } void empty(List *list) { Node *p, *q; int n; system("cls"); color(12); printf("此操作将清空所有数据!\n输入 1 继续,输入 0 取消:\n"); do { n = get_int(); } while (!(n == 0 || n == 1)); if (n == 1) { for (p = list->head; p; p = q) { q = p->next; free(p); } list->head = NULL; printf("清除完成!\n"); system("pause"); return; } }
void save(List *list) { if (list->head == NULL) { color(12); printf("还未存入任何菜品!保存为空!\n"); color(11); system("pause"); return; } Node *p = list->head; FILE *fp = fopen("Menu.txt", "w"); if (fp) { while (p) { fprintf(fp, "\n%s %s %0.2f %d %s", p->name, p->restaurant, p->price, p->fraction, p->comment); p = p->next; } fclose(fp); color(11); printf("\n保存成功\n"); system("pause"); } else { color(12); printf("保存失败,请自查或联系作者"); } } void read_txt(List *list) { int i = 1; Node *last; Node *p1; int a = 1; char f[5]; FILE *fp = fopen("Menu.txt", "r"); fgets(f, 10, fp); while (!feof(fp)) { last = list->head; p1 = (Node *)malloc(sizeof(Node)); p1->next = NULL; fscanf(fp, "%s%s%f%d%s", p1->name, p1->restaurant, &p1->price, &p1->fraction, p1->comment); if (last) { while (last->next) { last = last->next; } last->next = p1; } else { list->head = p1; } } fclose(fp); }
void sort_ascending(List *list) { int n = 0, i, k, num; List *p = list; Node *p1 = list->head, *p2, *p3; List list_swap; n = calculate(p); list_swap.pa = p1; list_swap.pb = p2; list_swap.pc = p3; for (i = 0; i < n - 1; i++) { list_swap.pa = list->head; list_swap.pb = NULL; list_swap.pc = list_swap.pa->next; for (k = 0; k < n - i - 1; k++) { if (list_swap.pa->price > list_swap.pc->price) { swap1(p, &list_swap); } else { swap2(p, &list_swap); } } } } void sort_descending(List *list) { int n = 0, i, k, num; List *p = list; Node *p1 = list->head, *p2, *p3; List list_swap; n = calculate(p); list_swap.pa = p1; list_swap.pb = p2; list_swap.pc = p3; for (i = 0; i < n - 1; i++) { list_swap.pa = list->head; list_swap.pb = NULL; list_swap.pc = list_swap.pa->next; for (k = 0; k < n - i - 1; k++) { if (list_swap.pa->price < list_swap.pc->price) { swap1(p, &list_swap); } else { swap2(p, &list_swap); } } } } void sort_score(List *list) { int n = 0, i, k, num; List *p = list; Node *p1 = list->head, *p2, *p3; List list_swap; n = calculate(p); list_swap.pa = p1; list_swap.pb = p2; list_swap.pc = p3; for (i = 0; i < n - 1; i++) { list_swap.pa = list->head; list_swap.pb = NULL; list_swap.pc = list_swap.pa->next; for (k = 0; k < n - i - 1; k++) { if (list_swap.pa->fraction < list_swap.pc->fraction) { swap1(p, &list_swap); } else { swap2(p, &list_swap); } } } } int calculate(List *list) { int n = 0; Node *p = list->head; while (p->next != NULL) { n++; p = p->next; } return n + 1; } void swap1(List *list, List *list_swap) { if (list_swap->pb) { list_swap->pb->next = list_swap->pa->next; list_swap->pa->next = list_swap->pc->next; list_swap->pc->next = list_swap->pa; } else { list->head = list_swap->pa->next; list_swap->pa->next = list_swap->pc->next; list_swap->pc->next = list_swap->pa; } list_swap->pb = list_swap->pc; list_swap->pc = list_swap->pa->next; } void swap2(List *list, List *list_swap) { if (list_swap->pb) { list_swap->pa = list_swap->pa->next; list_swap->pc = list_swap->pa->next; list_swap->pb = list_swap->pb->next; } else { list_swap->pa = list_swap->pa->next; list_swap->pc = list_swap->pa->next; list_swap->pb = list->head; } }
int random_number(List *list) { int n = 0, m = 0, sum = 1, i = 0; List *q = list; srand((unsigned)time(NULL) * 12); m = calculate(q); i = m; while (m > 0) { sum = sum * 10; m = m / 10; } while (1) { n = rand() % sum; if (n <= i && n > 0) { i = n; break; } } return i; } void color(int a) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a); }
|