C语言大一课设:旭日苑菜品管理系统 您所在的位置:网站首页 菜单管理系统c语言 C语言大一课设:旭日苑菜品管理系统

C语言大一课设:旭日苑菜品管理系统

2024-07-14 23:53| 来源: 网络整理| 查看: 265

目录 引言做课设的经验代码

引言

大一上学习了C语言以后老师布置了一个课设任务,然后我写的是旭日苑菜品管理系统,旭日苑是我们学校最好吃的食堂哈哈哈(没有之一)。 顺便介绍一下我们学校,我们学校是全国第四的邮电大学,又称皇家邮电大学。(反正就是很厉害),那么话不多说就上代码啦。

做课设的经验

写一个模块测试一个模块(放到新的main函数里测试) 有良好的逻辑,建议先写菜单函数 链表使用时一定要注意边界情况,还有就是考虑要不要用头节点 展示功能先写,因为这个如果是对的你就可以用这个功能来测试其他函数(增删查改之类的) 链表的传参一定要清晰(因为最好不要使用全局变量) 记得free掉malloc来的空间,这是一个好习惯 防止暴力输入,吸收缓冲区多余字符 调试的时候可以设置断点或者在某个地方printf一个东西 一定要记得fclose!

代码 //linux版 #include #include #include #include #define SIZE 50 //用户信息主体(结构体实现) typedef struct user { char id[SIZE]; //账号 char pwd[SIZE]; //密码 long tele; //电话号码 }user; //菜品信息主体(链表实现) typedef struct Node { char name[SIZE]; //名称 int price; //价格 char restaurant[SIZE]; //菜馆名字 int star; //星级(好吃吗?) char comment[SIZE]; //评价 struct Node * next; }Node; //为了方便操作 typedef struct Dish { char name[SIZE]; int price; char restaurant[SIZE]; int star; char comment[SIZE]; }Dish; //为了安全的输入(防止暴力输入) char get_choice(); char get_manage(); long get_tele(); int get_int(); char get_first(); char getch(); char * s_gets(char * st, int n); void secret_pwd(char * pwd); //具体功能实现函数 void create_user_file(); void create_dish_file(); void register_user(); void login(); void find_pwd(); void show_author(); void press_to_continue(); void show_function(); void print_star(int n); void add_dish(Node **head); void delete_dish(Node *head); void search_by_name(Node *head); void search_by_price(Node *head); void search_by_restaurant(Node *head); void show_dish(Node *head); Node * init_dish(); void free_dish(Node **head); void save_dish(Node *head); void modify_dish(Node *head); //排序函数 Node * find_pmax_dish(Node *head); Node * find_pmin_dish(Node *head); Node * find_smax_dish(Node *head); Node * find_smin_dish(Node *head); void sort_sdown_dish(Node *head); void sort_pdown_dish(Node *head); void sort_sup_dish(Node *head); void sort_pup_dish(Node *head); void swap_dish(Node *p1, Node *p2); //加密解密函数 void encipher(char *original); void decipher(char *cipher); //菜单函数 void show_line(const char *line); void user_menu(); void internal_menu(); void dish_manage_menu(); void search_dish_menu(Node *head); void sort_dish_menu(Node *head); //看上去很短的主函数 int main() { user_menu(); press_to_continue(); puts("欢迎下次使用!"); return 0; } //保证文件users的存在,该文件存在时也不清空其中的数据 void create_user_file() { FILE * fp; if ((fp = fopen("users", "r")) == NULL) { if ((fp = fopen("users", "w+")) == NULL) { fprintf(stderr, "无法打开用户信息!\n"); system("sleep 3s"); return; } } fclose(fp); return; } //同上 void create_dish_file() { FILE * fp; if ((fp = fopen("dishes", "r")) == NULL) { if ((fp = fopen("dishes", "w+")) == NULL) { fprintf(stderr, "无法打开菜品信息!\n"); system("sleep 3s"); return; } } fclose(fp); return; } //获取用户键入的信息(一级&二级菜单使用) char get_choice(void) { char ch; ch =get_first(); while(ch != 'a' && ch != 'b' && ch != 'c' && ch != 'q') { puts("请输入正确选项"); ch =get_first(); } return ch; } //同上(三级菜单使用) char get_manage(void) { char ch; ch =get_first(); while(!(ch >= 'a' && ch char ch = 0; while (!isalpha(ch = getchar())) continue; while (getchar() != '\n') continue; return tolower(ch); //返回小写值 } //注册函数 void register_user() { FILE * fp; user new_, old; //这里new后面多'_'的原因是c++有个关键字是new char temp[SIZE]; show_line(" 请开始注册您的账号!"); if ((fp = fopen("users", "r")) == NULL) { fprintf(stderr, "读取用户信息失败!\n"); system("sleep 3s"); return; } puts("请输入您的账号:"); s_gets(new_.id, SIZE); while (fread(&old, sizeof(user), 1, fp) == 1) { decipher(old.pwd); if (!strcmp(new_.id, old.id)) { fprintf(stderr, "账号重复!\n"); system("sleep 3s"); return; } } puts("请输入您的密码:"); secret_pwd(new_.pwd); puts("请再次输入您的密码:"); secret_pwd(temp); while (1) { if (!strcmp(new_.pwd, temp)) break; puts("两次密码不一致!"); puts("请重新输入您的密码:"); secret_pwd(new_.pwd); puts("请再次输入您的密码:"); secret_pwd(temp); } puts("请输入您的电话号码以便找回密码:"); new_.tele = get_tele(); fp = fopen("users", "a"); encipher(new_.pwd); fwrite(&new_, sizeof(user), 1, fp); printf("账号%s已注册成功!\n", new_.id); system("sleep 3s"); fclose(fp); return; } //安全地获取电话号码 //64位架构下long最大值是9223372036854775807,用来存储电话号码很合适 long get_tele() { long input; char ch; while (scanf("%ld%*c", &input) != 1) { while ((ch =getchar()) != '\n') putchar(ch); printf(" 不是一个电话号码!\n请您重新输入:\n"); } return input; } //获取int类型的正数 int get_int() { int input; char ch; while (scanf("%d%*c", &input) != 1) { while ((ch =getchar()) != '\n') putchar(ch); printf(" 不是一个正整数!\n请您重新输入:\n"); } return input; } //登录函数 void login() { FILE * fp; user old; char temp[SIZE]; show_line(" 请开始登录您的账号!"); if ((fp = fopen("users", "r")) == NULL) { fprintf(stderr, "读取用户信息失败!\n"); system("sleep 3s"); return; } puts("请输入您的账号:"); s_gets(temp, SIZE); while (!feof(fp)) //判断是否到达文件结尾 { fread(&old, sizeof(user), 1, fp); decipher(old.pwd); if (!strcmp(temp, old.id)) { puts("请输入您的密码:"); secret_pwd(temp); if (!strcmp(temp, old.pwd)) { show_line(" 登录成功!"); system("sleep 3s"); //休眠,用起来比较带感哈哈哈哈 internal_menu(); return; } else { puts("密码不匹配!"); system("sleep 3s"); return; } } } printf("账号%s不存在!\n", temp); system("sleep 3s"); fclose(fp); return; } //找回密码函数 void find_pwd() { FILE * fp; user old; char temp[SIZE]; long key; show_line(" 请开始找回您的密码!"); if ((fp = fopen("users", "r")) == NULL) { fprintf(stderr, "读取用户信息失败!\n"); system("sleep 3s"); return; } puts("请输入您的账号:"); s_gets(temp, SIZE); while (!feof(fp)) { fread(&old, sizeof(user), 1, fp); decipher(old.pwd); if (!strcmp(temp, old.id)) { puts("请输入您的电话号码:"); if ((key = get_tele()) == old.tele) printf("您的密码是:%s\n", old.pwd); else puts("电话号码不匹配!"); system("sleep 3s"); return; } } printf("账号%s不存在!\n", temp); system("sleep 3s"); fclose(fp); return; } char * s_gets(char *st, int n) { char * ret_val; int i = 0; ret_val = fgets(st, n, stdin); if (ret_val != NULL) { while (st[i] != '\0' && st[i] != '\n') i++; if (st[i] == '\n') st[i] = '\0'; else while (getchar() != '\n') continue; } return ret_val; } //自定义的getch()函数 char getch() { char ch; system("stty -echo"); system("stty -icanon"); ch = getchar(); system("stty icanon"); system("stty echo"); return ch; } void secret_pwd(char *pwd) { char ch; int i = 0; while ((ch = getch()) != '\n') { if (ch == '\x7F') { putchar('\b'); putchar(' '); putchar('\b'); if (i) i--; else putchar('\a'); } else { putchar('*'); pwd[i++] = ch; } } pwd[i] = '\0'; putchar('\n'); return; } //加密函数 void encipher(char *original) { int key = 1; for (int i = 0; i int key = -1; for (int i = 0; i system("clear"); //清屏函数 for (int i =0; i switch(choice) { case 'a': register_user(); break; case 'b': login(); break; case 'c': find_pwd(); break; } show_line(" 欢迎使用旭日苑菜品管理系统!"); printf("\n %-20s%-20s\n %-20s%-20s\n\n", "a. 注册账号", "b. 登录账号", "c. 找回密码", "q. 退出系统"); for (int i =0; i switch(choice) { case 'a': dish_manage_menu(); break; case 'b': show_function(); break; case 'c': show_author(); break; case 'q': show_line(" 注销成功!"); system("sleep 3s"); return; } show_line(" 欢迎使用旭日苑菜品管理系统!"); printf("\n %-20s%-20s\n %-20s%-20s\n\n", "a. 菜品管理", "b. 功能介绍", "c. 开发者介绍", " q. 注销账号"); for (int i =0; i puts("\n请按任意键继续..."); getch(); return; } //菜品管理菜单(三级菜单) void dish_manage_menu() { char manage; Node * head; create_dish_file(); head = init_dish(); show_line(" 菜品管理"); printf("\n %-20s%-20s\n %-20s%-20s\n", "a. 录入菜品", "b. 删除菜品", "c. 查找菜品", "d. 浏览菜品"); printf(" %-20s%-20s\n %-20s%-20s\n\n", "e. 修改菜品", "f. 排序菜品", "g. 保存信息", "q. 返回上级"); for (int i =0; i case 'a': add_dish(&head); break; case 'b': delete_dish(head); break; case 'c': search_dish_menu(head); break; case 'd': show_dish(head); break; case 'e': modify_dish(head); break; case 'f': sort_dish_menu(head); break; case 'g': save_dish(head); break; } show_line(" 菜品管理"); printf("\n %-20s%-20s\n %-20s%-20s\n", "a. 录入菜品", "b. 删除菜品", "c. 查找菜品", "d. 浏览菜品"); printf(" %-20s%-20s\n %-20s%-20s\n\n", "e. 修改菜品", "f. 排序菜品", "g. 保存信息", "q. 返回上级"); for (int i =0; i if (ch == ' ') ispull = -ispull; if (ispull == -1) { show_line(" 功能介绍"); printf("%-20s%20s\n", "空格键展开/关闭", "Q键结束介绍"); } else { show_line(" 功能介绍"); printf("%-20s%20s\n", "空格键展开/关闭", "Q键结束介绍"); puts(" > Function 1: 登录系统"); puts(" > Function 2: 管理系统"); } } return; } //打印星星(最多打印5颗星星) void print_star(int n) { if (n > 5) n = 5; for (int i = 1; i fprintf(stderr, "读取菜品信息失败!\n"); free_dish(&p); system("sleep 3s"); return NULL; } while (fread(&temp, sizeof(Dish), 1, fp) == 1) { Node * p1, * p2 = p; //p1作为新指针,p2作为检索指针 if ((p1 = (Node*)malloc(sizeof(Node))) == NULL) { fprintf(stderr, "内存空间不足!\n"); free_dish(&p); system("sleep 3s"); return NULL; } p1->next = NULL; strcpy(p1->name, temp.name); p1->price = temp.price; p1->star = temp.star; strcpy(p1->restaurant, temp.restaurant); strcpy(p1->comment, temp.comment); if (p2 != NULL) { while (p2->next != NULL) p2 = p2->next; p2->next = p1; } else p = p1; } fclose(fp); return p; } //释放链表(防止内存泄露) void free_dish(Node **head) { Node *temp; Node **p = head; while(*head != NULL) { temp = *head; *head = (*head)->next; free(temp); } *p = NULL; return; } //录入菜品(尾插) void add_dish(Node **head) { Node * p1, * p2 = *head; show_line(" 录入菜品"); if ((p1 = (Node*)malloc(sizeof(Node))) == NULL) { fprintf(stderr, "内存空间不足!\n"); system("sleep 3s"); return; } puts("请输入菜品名称:"); s_gets(p1->name, SIZE); puts("请输入菜品价格:"); p1->price = get_int(); puts("请输入XUPTer们的评价:"); s_gets(p1->comment, SIZE); puts("请输入菜馆名称:"); s_gets(p1->restaurant, SIZE); puts("请输入该菜品的星级"); p1->star = get_int(); p1->next = NULL; if (*head != NULL) { while (p2->next != NULL) p2 = p2->next; p2->next = p1; } else *head = p1; show_line(" 录入成功!"); system("sleep 3s"); return; } //将菜品信息写入dishes void save_dish(Node *head) { Dish temp; FILE * fp; Node * p = head; show_line(" 写入数据中..."); if ((fp = fopen("dishes", "wb+")) == NULL) { fprintf(stderr, "写入菜品信息失败!\n"); system("sleep 3s"); return; } while (p) { strcpy(temp.name, p->name); temp.price = p->price; temp.star = p->star; strcpy(temp.restaurant, p->restaurant); strcpy(temp.comment, p->comment); if (fwrite(&temp, sizeof(Dish), 1, fp) != 1) { fprintf(stderr, "写入菜品信息失败!\n"); system("sleep 3s"); return; } p = p->next; } show_line(" 保存成功!"); system("sleep 3s"); fclose(fp); return; } //打印报表 void show_dish(Node *head) { Node * p = head; int count = 0; double total_p = 0.0, total_s = 0.0; show_line(" 《旭日苑菜品一览》"); while (p) { total_p += p->price; total_s += p->star; printf(" > 菜品 %-2d: %s\n", ++count, p->name); printf(" > 价格: %d\n", p->price); printf(" > 菜馆: %s\n", p->restaurant); printf(" > 星级: "); print_star(p->star); printf(" > 评价: %s\n", p->comment); p = p->next; for (int i = 0; i switch (choice) { case 'a': search_by_name(p); break; case 'b': search_by_restaurant(p); break; case 'c': search_by_price(p); break; } show_line(" 查找菜品"); printf("\n %-20s%-20s\n %-20s%-20s\n\n", "a. 按名称查找", "b. 按菜馆查找", "c. 按价格查找", "q. 返回上级"); for (int i =0; i if (!strcmp(temp, p->name)) { puts("查找成功!"); for (int i = 0; i name); printf(" > 价格: %d\n", p->price); printf(" > 菜馆: %s\n", p->restaurant); printf(" > 星级: "); print_star(p->star); printf(" > 评价: %s\n", p->comment); for (int i = 0; i next; } puts("未查找到符合条件的菜品!"); press_to_continue(); return; } //按价格范围查找 void search_by_price(Node *head) { Node * p = head; int min, max; int isfirst = 1; //是否打印头信息 puts("请输入需要查找的菜品价格范围:"); puts("最小值:"); min = get_int(); puts("最大值:"); max = get_int(); while (p) { if (p->price price >= min) { if (isfirst) { puts("查找成功!"); printf("下列是价格在%d和%d之间的菜品:\n", min, max); isfirst = 0; for (int i = 0; i name); printf(" > 价格: %d\n", p->price); printf(" > 菜馆: %s\n", p->restaurant); printf(" > 星级: "); print_star(p->star); printf(" > 评价: %s\n", p->comment); for (int i = 0; i next; } if (isfirst) //如果没有找到过 puts("未查找到符合条件的菜品!"); press_to_continue(); return; } //按所属菜馆查找 void search_by_restaurant(Node *head) { Node * p = head; char temp[SIZE]; int isfirst = 1; //是否打印头信息 puts("请输入需要查找的菜馆名:"); s_gets(temp, SIZE); while (p) { if (!strcmp(temp, p->restaurant)) { if (isfirst) { puts("查找成功!"); printf("下列是“%s”名下的菜品:\n", temp); isfirst = 0; for (int i = 0; i name); printf(" > 价格: %d\n", p->price); printf(" > 星级: "); print_star(p->star); printf(" > 评价: %s\n", p->comment); for (int i = 0; i next; } if (isfirst) //如果没有找到过 puts("未查找到符合条件的菜品!"); press_to_continue(); return; } //删除菜品 void delete_dish(Node *head) { Node * p = head, * prev = NULL; char temp[SIZE]; show_line(" 删除菜品"); puts("请输入需要删除的菜品名称:"); s_gets(temp, SIZE); while (p) { if (!strcmp(temp, p->name)) { prev->next = p->next; free(p); p = NULL; show_line(" 删除成功!"); system("sleep 3s"); return; } prev = p; p = p->next; } puts("未查找到符合条件的菜品!"); press_to_continue(); return; } //按价格升序排序 void sort_pup_dish(Node *head) { Node *p = head, *pmin; while (p) { pmin = find_pmin_dish(p); if (strcmp(pmin->name, p->name)) { swap_dish(pmin, p); } p = p->next; } return; } //按星级升序排序 void sort_sup_dish(Node *head) { Node *p = head, *pmin; while (p) { pmin = find_smin_dish(p); if (strcmp(pmin->name, p->name)) { swap_dish(pmin, p); } p = p->next; } return; } //按价格降序排序 void sort_pdown_dish(Node *head) { Node *p = head, *pmax; while (p) { pmax = find_pmax_dish(p); if (strcmp(pmax->name, p->name)) { swap_dish(pmax, p); } p = p->next; } return; } //按星级降序排序 void sort_sdown_dish(Node *head) { Node *p = head, *pmax; while (p) { pmax = find_smax_dish(p); if (strcmp(pmax->name, p->name)) { swap_dish(pmax, p); } p = p->next; } return; } //找到菜品中价格最小值节点 Node * find_pmin_dish(Node *head) { Node *pmin = head, *p = head->next; //p指向当前节点位置,pmin用于存储最小值节点 while (p) { if (p->price price) pmin = p; p = p->next; } return pmin; } //找到菜品中星级最小值节点 Node * find_smin_dish(Node *head) { Node *pmin = head, *p = head->next; while (p) { if (p->star star) pmin = p; p = p->next; } return pmin; } //找到菜品中星级最大值节点 Node * find_smax_dish(Node *head) { Node *pmax = head, *p = head->next; while (p) { if (p->star > pmax->star) pmax = p; p = p->next; } return pmax; } //找到菜品中价格最大值节点 Node * find_pmax_dish(Node *head) { Node *pmax = head, *p = head->next; while (p) { if (p->price > pmax->price) pmax = p; p = p->next; } return pmax; } //交换菜品信息 void swap_dish(Node *p1, Node *p2) { char name[SIZE]; int price; char restaurant[SIZE]; int star; char comment[SIZE]; price = p1->price; p1->price = p2->price; p2->price = price; star = p1->star; p1->star = p2->star; p2->star = star; strcpy(name, p1->name); strcpy(p1->name, p2->name); strcpy(p2->name, name); strcpy(restaurant, p1->restaurant); strcpy(p1->restaurant, p2->restaurant); strcpy(p2->restaurant, restaurant); strcpy(comment, p1->comment); strcpy(p1->comment, p2->comment); strcpy(p2->comment, comment); return; } //排序菜品菜单 void sort_dish_menu(Node *head) { Node * p = head; char choice; show_line(" 排序菜品"); printf("\n %-20s%-20s\n %-20s%-20s\n\n", "a. 按价格升序排序", "b. 按价格降序排序", "c. 按星级升序排序", "q. 按星级降序排序"); for (int i =0; i Node *p = head; char temp[SIZE]; show_line(" 修改菜品"); puts("请输入需要修改的菜品名称:"); s_gets(temp, SIZE); while (p) { if (!strcmp(temp, p->name)) { puts("请输入菜品价格:"); p->price = get_int(); puts("请输入XUPTer们的评价:"); s_gets(p->comment, SIZE); puts("请输入菜馆名称:"); s_gets(p->restaurant, SIZE); puts("请输入该菜品的星级"); p->star = get_int(); show_line(" 修改成功!"); system("sleep 3s"); return; } p = p->next; } puts("不存在该菜品!"); press_to_continue(); return; }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有