|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
花了两天时间终于把系统完善啦~~~至今写过的最长代码
感谢小甲鱼老师的<C语言程序设计>:057第十章 结构体与共用体05(新版).mp4
努力学习! 热爱鱼C^_^~~~如有BUG,还望大牛指正!
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include<windows.h> // Sleep函数
- #define LEN sizeof(struct student) // student结构的大小
- void Commands_menu(void);
- struct student *Create(struct student *head);
- struct student *Insert(struct student *head, struct student *stu_2);
- struct student *Del(struct student *head);
- void Print(struct student *head);
- void Quit(void);
- void SlowDisplay(char *p);
- struct student
- {
- int num;
- float score;
- struct student *next;
- };
- int n=0; // 链表节点数
- int choose_number; // 命令选择指向默认
- int is_num; // 检查输入是否为数字
- char proof; // 判断 Y/N
- int main()
- {
- SlowDisplay("\n\t学 生 成 绩 管 理 系 统\n");
- Commands_menu();
- return 0;
- }
- //*******************************命令菜单*******************************
- void Commands_menu(void)
- {
- struct student *head=NULL, stu_2;
- while (1)
- {
- puts("\n************************************************************************");
- printf("****\t1 : 创建数据\t< 友情提醒: 执行'创建数据'命令会覆盖旧数据库! >\n");
- printf("****\t2 : 插入数据\n");
- printf("****\t3 : 删除数据\n");
- printf("****\t4 : 打印数据\n");
- printf("****\t0 : 退出!\n");
- puts("************************************************************************");
- SlowDisplay("\n请输入执行命令 : ");
- choose_number = 9;
- switch(scanf("%d", &choose_number), choose_number)
- {
- case 1:
- // 一次过滤所有非法字符输入
- while(getchar() != '\n')
- {
- ;
- }
- SlowDisplay("\n确定创建新数据库, 覆盖旧数据库? < Y / N > : ");
- proof = getchar();
- if(proof == 'y' || proof == 'Y')
- {
- SlowDisplay("\n数据库重建中...\t< 学号、成绩输入 '0' 则退出! >\n");
- head = Create(head);
- }
- else if(proof == 'n' || proof == 'N')
- {
- SlowDisplay("\n取消创建! 继续!\n");
- }
- else
- {
- SlowDisplay("\n创建失败! 输入非法!\n");
- }
- Print(head);
- break;
- case 2:
- head = Insert(head, &stu_2);
- Print(head);
- break;
- case 3:
- head = Del(head);
- Print(head);
- break;
- case 4:
- Print(head);
- break;
- case 0:
- Quit();
- break;
- default:
- SlowDisplay("\n输入非法!\n\n");
- }
- // 一次过滤所有非法字符输入
- while(getchar() != '\n')
- {
- ;
- }
- }
- }
- //*******************************创建数据*******************************
- struct student *Create(struct student *head)
- {
- struct student *p1, *p2;
- n = 0;
- head = NULL;
- p1 = p2 = (struct student *)malloc(LEN);
- printf("\n\t录入第 %d 位学生学号 : ", n+1);
- // 检查非法输入
- while(!(is_num = scanf("%d", &p1->num)))
- {
- printf("\n\t请重新输入学生学号(纯数字) : ");
- while(getchar() != '\n')
- {
- ;
- }
- }
- if(p1->num == 0)
- {
- SlowDisplay("\n结束创建!\n");
- return head;
- }
- printf("\n\t录入第 %d 位学生成绩 : ", n+1);
- // 检查非法输入
- while(!(is_num = scanf("%f", &p1->score)) || p1->score < 0 || p1->score >100)
- {
- printf("\n\t请重新输入学生成绩(0~100的数字) : ");
- while(getchar() != '\n')
- {
- ;
- }
- }
-
- while (p1->num)
- {
- n++;
- if (1 == n)
- {
- head = p1;
- }
- else
- {
- p2->next = p1;
- }
- p2 = p1;
- p1 = (struct student *)malloc(LEN);
- getchar();
- printf("\n\t请继续录入第 %d 位学生学号 : ", n+1);
- // 检查非法输入
- while(!(is_num = scanf("%d", &p1->num)))
- {
- printf("\n\t请重新输入学生学号(纯数字) : ");
- while(getchar() != '\n')
- {
- ;
- }
- }
- if(p1->num == 0)
- {
- SlowDisplay("\n结束创建!\n");
-
- p2->next = NULL;
- return head;
- }
- printf("\n\t请继续录入第 %d 位学生成绩 : ", n+1);
- // 检查非法输入
- while(!(is_num = scanf("%f", &p1->score)) || p1->score < 0 || p1->score >100)
- {
- printf("\n\t请重新输入学生成绩(0~100的数字) : ");
- while(getchar() != '\n')
- {
- ;
- }
- }
- }
- p2->next = NULL;
- SlowDisplay("\n创建成功!\n");
- return head;
- }
- //*******************************插入数据*******************************
- struct student *Insert(struct student *head, struct student *stu_2)
- {
- struct student *p1, *p2;
- stu_2 = (struct student *)malloc(LEN);
- printf("\n请录入要插入学生学号(输入'0'取消) : ");
- // 检查非法输入
- while(!(is_num = scanf("%d", &stu_2->num)))
- {
- printf("\n\t请重新输入学生学号(纯数字) : ");
- while(getchar() != '\n')
- {
- ;
- }
- }
- if(stu_2->num == 0)
- {
- SlowDisplay("\n取消插入!\n");
- return head;
- }
- printf("\n请录入要插入学生成绩 : ");
- // 检查非法输入
- while(!(is_num = scanf("%f", &stu_2->score)) || stu_2->score < 0 || stu_2->score > 100)
- {
- printf("\n\t请重新输入学生成绩(0~100的数字) : ");
- while(getchar() != '\n')
- {
- ;
- }
- }
- p1 = head;
- if(head == NULL)
- {
- head = stu_2;
- stu_2->next = NULL;
- }
- else
- {
- while (p1->next != NULL && stu_2->num > p1->num)
- {
- p2 = p1;
- p1 = p1->next;
- }
- if (stu_2->num <= p1->num)
- {
- if (head == p1)
- {
- head = stu_2;
- }
- else
- {
- p2->next = stu_2;
- }
- stu_2->next = p1;
- }
- else
- {
- p1->next = stu_2;
- stu_2->next = NULL;
- }
- }
- n++;
- SlowDisplay("\n插入成功!");
- printf("\n插入学号 : %d\n", stu_2->num);
- return head;
- }
- //*******************************删除数据*******************************
- struct student *Del(struct student *head)
- {
- struct student *p1, *p2;
- int num;
- getchar();
- printf("\n请输入您想删除的学生学号(输入'0'取消) : ");
- // 检查非法输入
- while(!(is_num = scanf("%d", &num)))
- {
- printf("\n\t请重新输入学生学号(纯数字) : ");
- while(getchar() != '\n')
- {
- ;
- }
- }
- if(num == 0)
- {
- SlowDisplay("\n取消删除!\n");
- return head;
- }
- p1 = head;
- if(head == NULL)
- {
- SlowDisplay("\n删除失败!");
- printf("\n数据库为空!\n");
- }
- else
- {
- while (p1->next != NULL && num != p1->num)
- {
- p2 = p1;
- p1 = p1->next;
- }
- if (num == p1->num)
- {
- if (p1 == head)
- {
- head = p1->next;
- }
- else
- {
- p2->next = p1->next;
- }
- SlowDisplay("\n删除成功!");
- printf("\n删除学号 : %d\n", num);
- n--;
- }
- else
- {
- SlowDisplay("\n删除失败!");
- printf("\n无匹配学号 : %d\n", num);
- }
- }
- return head;
- }
- //*******************************打印数据*******************************
- void Print(struct student *head)
- {
- struct student *p;
- SlowDisplay("\n数据库打印中...");
- puts("\n************************************************************************");
- printf("\n****\t数据库中共计有 %d 条记录!\n", n);
- p = head;
- while (p)
- {
- printf("\n\t学号 : %d , 成绩 : %5.2f", p->num, p->score);
- p = p->next;
- }
- putchar('\n');
- }
- //*******************************退出*******************************
- void Quit(void)
- {
- // 一次过滤所有非法字符输入
- while(getchar() != '\n')
- {
- ;
- }
- SlowDisplay("\n确定退出? < Y / N > : ");
- proof = getchar();
- if(proof == 'y' || proof == 'Y')
- {
- SlowDisplay("\n数据库保存中...\n");
- // 执行数据库保存...
- SlowDisplay("\n * * * *\n");
- SlowDisplay("\n * * * *\n");
- SlowDisplay("\n * * * *\n");
- SlowDisplay("\n数据库已保存, 系统退出!\t");
- exit(0);
- }
- else if(proof == 'n' || proof == 'N')
- {
- SlowDisplay("\n取消退出! 继续!\n");
- }
- else
- {
- SlowDisplay("\n输入非法! 继续!\n");
- }
- }
- //*******************************慢显*******************************
- void SlowDisplay(char *p)
- {
- while(1)
- {
- if(*p!=0)
- printf("%c",*p++);
- else
- break;
- Sleep(50);
- }
- }
复制代码 |
评分
-
查看全部评分
|