编一个学生信息管理系统,求大神帮忙
题目要求: 1、从文件中读入学生相关信息存入链表(创建);2、实现对链表的插入/多关键字删除操作;
3、实现对链表的多关键字排序操作;
4、实现对链表的保存(原文件)和另存(新文件)功能;
5、将功能实现为菜单形式,方便用户进行任意操作序列。
[此贴子已经被作者于2017-2-13 10:40编辑过]
/* 1、从文件中读入学生相关信息存入链表(创建); 2、实现对链表的插入/多关键字删除操作; 3、实现对链表的多关键字排序操作; 4、实现对链表的保存(原文件)和另存(新文件)功能; 5、将功能实现为菜单形式,方便用户进行任意操作序列。 */ #include <stdio.h> #include <time.h> #include <string.h> #include <stdbool.h> struct students { char name[20];//学生名字 int number;//学生学号 int classes;//班级 float math;//数学成绩 float chinese;//语文成绩 float english;//英语成绩 struct students *next; }; struct students *student = NULL;//指向初始位置 bool yesorno = false; struct students *insert(void); struct students *find(char *str); void dele(void); void sort(void); void search(void); void save(void); void save_as(void); void read(void); void quit(void); void print(void); int main(void) { char code; for (;;) { printf("请选择您需要的操作\n"); printf("输入(I),删除(D),排序(X),搜索(S),保存(B),另存(L),读取(R),退出(Q):\n"); scanf(" %c", &code); while (getchar() != '\n') ; switch (code) { case 'i':case 'I':insert(); break; case 'd':case 'D':dele(); break; case 'x':case 'X':sort(); break; case 's':case 'S':search(); break; case 'b':case 'B':save(); break; case 'L':case 'l':save_as(); break; case 'r':case 'R':read(); break; case 'q':case 'Q':quit(); break; case 'p':case 'P':print(); break; default:printf("enter wrong.\n"); break; } } } struct students *find(char *str) { struct students *tmp; for (tmp = student; tmp; tmp = tmp->next) if ((strcmp(str, tmp->name) == 0) || (strcmp(str, tmp->number) == 0)) return tmp; if (tmp == NULL) { printf("Not found.\n"); return NULL; } } struct students *insert(void) { struct students *tmp = malloc(sizeof(struct students)); if (tmp == NULL) { fprintf(stderr, "No momory space.\n"); return; } printf("请输入学生所在班级:"); scanf("%d", &tmp->classes); while (getchar() != '\n') ; printf("请输入学生名字:"); gets(tmp->name); printf("请输入学生学号:"); scanf("%d", &tmp->number); printf("请输入学生数学成绩:"); scanf("%f", &tmp->math); printf("请输入学生语文成绩:"); scanf("%f", &tmp->chinese); printf("请输入学生英语成绩:"); scanf("%f", &tmp->english); tmp->next = student; student = tmp; yesorno = false; return student; } void dele(void) { char code[20], ch; struct students *tmp, *tmp1; printf("请输入您要删除的学生名字或学号:"); gets(code); tmp = find(code); if (tmp == NULL) return; printf("班级:%d\n", tmp->classes); printf("名字: "); puts(tmp->name); printf("学号: %d\n", tmp->number); printf("are you sure delete ?(y/n) : "); ch = getchar(); if (ch == 'y' || ch == 'Y') { if (tmp == student) student = student->next; else { for (tmp1 = student; tmp1->next != tmp; tmp1 = tmp1->next) ; tmp1->next = tmp->next; } free(tmp); printf("delete success.\n"); } yesorno = false; return; } void print(void) { struct students *tmp; printf("Students data:\n"); printf("classes name number math chinese english \n"); for (tmp = student; tmp; tmp = tmp->next) printf("%-12d%-12s%-12d%-12g%-12g%-12g\n", tmp->classes, tmp->name, tmp->number, tmp->math, tmp->chinese, tmp->english); printf("\n\n"); return; } void quit(void) { if (yesorno) exit(0); else { printf("尚未保存,是否直接退出?返回(y),退出(n):"); char code; for (;;) { scanf(" %c", &code); if (code == 'y' || code == 'Y') return; else if (code == 'n' || code == 'N') exit(0); else printf("Enter wrong,try again: "); } } } void save(void) { FILE *fp; if ((fp = fopen("studentsdata.txt", "w")) == NULL) { fprintf(stderr, "Can't build studentsdata.txt\n"); exit(0); } struct students *tmp; for (tmp = student; tmp; tmp = tmp->next) { fprintf(fp,"%d\n%s\n%d\n%.1f\n%.1f\n%.1f\n", tmp->classes, tmp->name, tmp->number, tmp->math, tmp->chinese, tmp->english); } fclose(fp); printf("存储完毕\n"); yesorno = true; return; } void save_as(void) { struct students *tmp; char str[20]; printf("请输入保存的文件名:"); gets(str); FILE *fp = fopen(str, "w"); if (fp == NULL) { fprintf(stderr, "Can't build %s\n", str); exit(0); } for (tmp = student; tmp; tmp = tmp->next) { fprintf(fp, "%d\n%s\n%d\n%.1f\n%.1f\n%.1f\n", tmp->classes, tmp->name, tmp->number, tmp->math, tmp->chinese, tmp->english); } fclose(fp); printf("另存储完毕\n"); yesorno = true; return; } void read(void) { FILE *fp; if ((fp = fopen("studentsdata.txt", "r")) == NULL) { fprintf(stderr, "Can't open studentsdata.txt\n"); exit(0); } for (;!feof(fp);) { struct students *tmp = malloc(sizeof(struct students)); if (tmp == NULL) { fprintf(stderr, "No momory space.\n"); return; } fscanf(fp, "%d\n", &tmp->classes); fscanf(fp, "%s\n", tmp->name); fscanf(fp, "%d\n%f\n%f\n%f\n", &tmp->number, &tmp->math, &tmp->chinese, &tmp->english); tmp->next = student; student = tmp; } fclose(fp); printf("读取完成\n"); yesorno = false; return; } void search(void) { char code[20]; struct students *tmp; printf("请输入您要查找的学生名字或学号:"); gets(code); tmp = find(code); if (tmp != NULL) { printf("Students data:\n"); printf("classes name number math chinese english \n"); printf("%-12d%-12s%-12d%-12.1f%-12.1f%-12.1f\n", tmp->classes, tmp->name, tmp->number, tmp->math, tmp->chinese, tmp->english); printf("\n"); } return; } void sort(void)//排序算法很粗糙,这是我自己的想出来的,改进也有,比如变成双链表,完成排序后销毁等 { char code, code1; bool x = true; printf("请选择排序关键字:学号(n),数学(m),语文(c):");//暂定3个,可继续添加 scanf(" %c", &code); while (getchar() != '\n') ; printf("升级排序(u),降级排序(d):"); scanf(" %c", &code1); while (getchar() != '\n') ; if (code1 == 'u') { if (code == 'n') { struct students *cur, *prev, *tmp; for (;;) { x = true; for (cur = student->next, prev = student; cur != NULL; prev = cur, cur = cur->next) { if (prev->number > cur->number) { prev->next = cur->next; cur->next = prev; if (prev == student)//保证student指向头 student = cur; else { for (tmp = student; tmp->next != prev; tmp = tmp->next)//保证调整位置前后指针连接 ; tmp->next = cur; } tmp = cur; cur = prev; prev = tmp; x = false;//只要进行了位置调整,x都为false,以保证继续循环 } } if (x) break; } } if (code == 'm') { struct students *cur, *prev, *tmp; for (;;) { x = true; for (cur = student->next, prev = student; cur != NULL; prev = cur, cur = cur->next) { if (prev->math > cur->math) { prev->next = cur->next; cur->next = prev; if (prev == student) student = cur; else { for (tmp = student; tmp->next != prev; tmp = tmp->next) ; tmp->next = cur; } tmp = cur; cur = prev; prev = tmp; x = false; } } if (x) break; } } if (code == 'c') { struct students *cur, *prev, *tmp; for (;;) { x = true; for (cur = student->next, prev = student; cur != NULL; prev = cur, cur = cur->next) { if (prev->chinese > cur->chinese) { prev->next = cur->next; cur->next = prev; if (prev == student) student = cur; else { for (tmp = student; tmp->next != prev; tmp = tmp->next) ; tmp->next = cur; } tmp = cur; cur = prev; prev = tmp; x = false; } } if (x) break; } } } if (code1 == 'd') { if (code == 'n') { struct students *cur, *prev, *tmp; for (;;) { x = true; for (cur = student->next, prev = student; cur != NULL; prev = cur, cur = cur->next) { if (prev->number < cur->number) { prev->next = cur->next; cur->next = prev; if (prev == student) student = cur; else { for (tmp = student; tmp->next != prev; tmp = tmp->next) ; tmp->next = cur; } tmp = cur; cur = prev; prev = tmp; x = false; } } if (x) break; } } if (code == 'm') { struct students *cur, *prev, *tmp; for (;;) { x = true; for (cur = student->next, prev = student; cur != NULL; prev = cur, cur = cur->next) { if (prev->math < cur->math) { prev->next = cur->next; cur->next = prev; if (prev == student) student = cur; else { for (tmp = student; tmp->next != prev; tmp = tmp->next) ; tmp->next = cur; } tmp = cur; cur = prev; prev = tmp; x = false; } } if (x) break; } } if (code == 'c') { struct students *cur, *prev, *tmp; for (;;) { x = true; for (cur = student->next, prev = student; cur != NULL; prev = cur, cur = cur->next) { if (prev->chinese < cur->chinese) { prev->next = cur->next; cur->next = prev; if (prev == student) student = cur; else { for (tmp = student; tmp->next != prev; tmp = tmp->next) ; tmp->next = cur; } tmp = cur; cur = prev; prev = tmp; x = false; } } if (x) break; } } } printf("排序完毕。\n"); return; }
[此贴子已经被作者于2017-2-18 00:17编辑过]