C语言链表实现学生信息管理系统程序设计
作者:不想实训啦
这篇文章主要为大家详细介绍了C语言链表实现学生信息管理系统程序设计,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C语言链表实现学生信息管理系统的具体代码,供大家参考,具体内容如下
事先存入的数据:
菜单
创建链表并倒序输出
输出链表中的全部信息
写入信息并保存至文件中(覆盖原有文件)
随机读取
指定查找
添加信息
指定删除
特殊查找
特殊删除
退出系统
#include<iostream> #include<string.h> #include<stdlib.h> #include<iomanip> #include<fstream> #include<time.h> int length=0; using namespace std; char a[10],b[10],c[10],d[10],e[10],f[10],g[10]; //定义学生信息的结构体类型,包括:学号、姓名、专业、班级、3门成绩 typedef struct StuNode { char grad[10];//学号 char name[10];//姓名 char spec[10];//专业 char stu_class[10];//班级 int score1; int score2; int score3; struct StuNode *next; }student, *StuLink; void Sort(StuLink &head)//从小到大进行冒泡排序 { StuLink tmp,pre,p,q; if (head->next) { p = head->next->next; head->next->next = NULL; while (p) { pre = head; //pre是q的前驱 q = pre->next; while (q && strcmp(q->grad,p->grad)<0)//从链表第二个结点开始找比当前插入值大的结点 { pre = pre->next; q = q->next; } tmp = p->next;//将p插入到结点pre和q之间 p->next = q; pre->next = p; p = tmp; } } } student *CreateList()//初始化:创建链表 { void Output(StuLink &p); StuLink head = (student*)malloc(sizeof(student)); StuLink p,q; p = head; q = head; char grad[10];//学号 char name[10];//姓名 char spec[10];//专业 char stu_class[10];//班级 int score1; int score2; int score3; FILE *r= fopen("2.txt","r"); if(r==NULL) { printf("打开文件失败!"); return NULL; } fscanf(r,"%s%s%s%s%s%s%s",&a,&b,&c,&d,&e,&f,&g);//读取标题 while(fscanf(r,"%s%s%s%s%d%d%d",grad,name,spec,stu_class,&score1,&score2,&score3)!=EOF) { q = (student*)malloc(sizeof(student)); strcpy(q->grad,grad); strcpy(q->name,name); strcpy(q->spec,spec); strcpy(q->stu_class,stu_class); q->score1 = score1; q->score2 = score2; q->score3 = score3; p->next = q; p = q; length++; } p->next = NULL; Sort(head); //倒序输出 StuLink k = head,t; while(k->next) k = k->next; while(k!=head) { //倒序输出 t = head; while(t->next!=k) t = t->next;//t为k前驱 k = t; } return head; } void Output(StuLink &p)//输出信息 { printf("%5s %-5s %-3s %s %3d %3d %3d\n",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); } void Print_List(StuLink &head)//打印整个链表 { StuLink p = head->next; while(p) { Output(p); p = p->next; } } void Save(StuLink &head)//写入文件。 { StuLink p = (student*)malloc(sizeof(student)),q = head->next; char grad[10],name[10],spec[10],stu_class[10]; int score1,score2,score3; printf("请输入学生信息:\n"); scanf("%s%s%s%s%d%d%d",p->grad,p->name,p->spec,p->stu_class,&p->score1,&p->score2,&p->score3); FILE *w =fopen("2.txt","a"); if(w==NULL) { printf("打开文件失败!\n"); return; } else printf("写入成功!\n"); fprintf(w,"\n%s %s %s %s %d %d %d",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); fclose(w); //存入链表 p->next = q; head->next = p; Sort(head); } void Fetch(StuLink &H)//随机读取某个学生的信息。 { StuLink p = H->next; int i = time(NULL) % length; int j = i; while(j) { p = p->next; j--; } printf("第%d名学生\n",i+1); printf(" 学号 姓名 专业 班级 成绩1 成绩2 成绩3\n"); Output(p); } student *Search_num(StuLink &H) { //查找指定学号的学生,返回指向该学生结点的指针。 char grad[10]; printf("请输入查询信息的学号:"); scanf("%s",grad); StuLink p = H->next; while(p) { if(strcmp(p->grad,grad)==0) return p; p = p->next; } return NULL; } void InsertList(StuLink &H) { //在函数中输入一个学生的信息,将该学生信息插入到链表中的相应位置,并保持此链表按学号的有序性。 StuLink p = H->next, q = H; StuLink insert = (student*)malloc(sizeof(student)); printf("请输入学生信息:\n"); scanf("%s%s%s%s%d%d%d",insert->grad,insert->name,insert->spec,insert->stu_class,&insert->score1,&insert->score2,&insert->score3); while(p) { if(strcmp(p->grad,insert->grad) > 0 ) { q->next = insert;//应插入q和p之间 insert->next = p; break; } q = q->next;//q是p的前驱 p = p->next; } if(!p)//insert的学号大于所有已知值 { q->next = insert; insert->next = NULL; } p = H->next; printf("\n"); Print_List(H); } void Delete_num(StuLink &H)//从链表中删除指定学号的学生。 { StuLink p = H->next, q = H; char grad[10]; printf("请输入想删除的学生的学号:\n"); scanf("%s",grad); while(p) { if(strcmp(p->grad,grad)==0) { q->next = p->next; free(p); break; } q = q->next; p = p->next; } FILE *w =fopen("2.txt","w"); p=H->next; while(p) { fprintf(w,"%5s %-5s %-3s %s %3d %3d %3d\n",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); p = p->next; } fclose (w); Print_List(H); } student *Search_major_subject_score(StuLink &H) { //查找某个专业的、某门课程的成绩小于某个分数的学生,返回指向该学生结点的指针。 char spec[10]; float score; StuLink p = H->next; int lesson; printf("请输入专业、课程序号和门限分数:\n"); scanf("%s%d%f",spec,&lesson,&score); while(p) { if(strcmp(p->spec,spec)==0) if(lesson==1 && p->score1<score || lesson==2 && p->score2<score || lesson==3 && p->score3<score) return p; p = p->next; } return NULL; } void Delete_major_subject(StuLink &H) { //从链表中删除某个专业的、某门课程的成绩小于某个分数的学生。 char spec[10]; float score; StuLink p = H->next, q = H; int lesson,flag = 0; printf("请输入专业、课程序号和门限分数:\n"); scanf("%s%d%f",spec,&lesson,&score); while(p) { if(strcmp(p->spec,spec)==0 && lesson == 1 && p->score1 < score) // cs 1 94 { Output(p); q->next = p->next; free(p); p = q->next; flag = 1; } else if(strcmp(p->spec,spec)==0 && lesson == 2 && p->score2 < score) // cs 1 94 { Output(p); q->next = p->next; free(p); p = q->next; flag = 1; } else if(strcmp(p->spec,spec)==0 && lesson == 3 && p->score3 < score) // cs 1 94 { Output(p); q->next = p->next; free(p); p = q->next; flag = 1; } else { q = p; p = p->next; } } if(flag==0) printf("不存在此学生!\n"); else printf("成功删除\n"); FILE *w =fopen("2.txt","w"); p=H->next; while(p) { fprintf(w,"%5s %-5s %-3s %s %3d %3d %3d\n",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); p = p->next; } fclose (w); Print_List(H); } void write(StuLink &H)//写入其他文件 { StuLink p = H->next; FILE *w = fopen("other.txt","w"); fprintf(w,"%s %s %s %s %s %s %s",a,b,c,d,e,f,g); while(p) { fprintf(w,"\n%s %s %s %s %d %d %d",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); p = p->next; } fclose(w); printf("写入成功!"); } char menu() { char ch1; printf(" \t\t\t\t 欢迎访问学生信息登记系统! \n"); printf("\t\t\t ___________________________________________________________\n"); printf("\t\t\t\t\t 1 Creatlist 写入信息创建链表并倒序输出\n"); printf("\t\t\t\t\t 2 Output 输出全部信息\n"); printf("\t\t\t\t\t 3 save 保存文件\n"); printf("\t\t\t\t\t 4 Fetch 随机读取\n"); printf("\t\t\t\t\t 5 Search num 指定查找\n"); printf("\t\t\t\t\t 6 Insertlist 添加信息 \n"); printf("\t\t\t\t\t 7 Delete num 指定删除\n"); printf("\t\t\t\t\t 8 Search_major _subject_score 特殊查找\n"); printf("\t\t\t\t\t 9 Delete_major _subject_score 特殊删除\n"); printf("\t\t\t\t\t 10 Exit 退出系统\n"); printf("\t\t\t ___________________________________________________________\n"); } void read(StuLink &head)//读取文件函数 { StuLink p = (student*)malloc(sizeof(student)),q = head->next; char grad[10],name[10],spec[10],stu_class[10]; int score1,score2,score3; FILE *w =fopen("2.txt","a"); if(w==NULL) { printf("打开文件失败!\n"); return; } else { StuLink p = head->next; while(p) { Output(p); p = p->next; } printf("输出成功!\n"); fprintf(w,"\n%s %s %s %s %d %d %d",p->grad,p->name,p->spec,p->stu_class,p->score1,p->score2,p->score3); } fclose(w); //存入链表 } int main() { StuLink H = CreateList(),temp; int k; menu(); scanf("%d",&k); k = int(k); while(k) { k = int(k); switch(k) { case 0: break; case 1: { StuLink head = (student*)malloc(sizeof(student)); StuLink p,q; p = head; q = head; char grad[10];//学号 char name[10];//姓名 char spec[10];//专业 char stu_class[10];//班级 int score1; int score2; int score3; FILE *r= fopen("2.txt","r"); if(r==NULL) { printf("打开文件失败!"); } fscanf(r,"%s%s%s%s%s%s%s",&a,&b,&c,&d,&e,&f,&g);//读取标题 while(fscanf(r,"%s%s%s%s%d%d%d",grad,name,spec,stu_class,&score1,&score2,&score3)!=EOF) { q = (student*)malloc(sizeof(student)); strcpy(q->grad,grad); strcpy(q->name,name); strcpy(q->spec,spec); strcpy(q->stu_class,stu_class); q->score1 = score1; q->score2 = score2; q->score3 = score3; p->next = q; p = q; length++; } p->next = NULL; Sort(head); StuLink k = head,t; while(k->next) k = k->next; printf("倒序输出:\n 学号 姓名 专业 班级 成绩1 成绩2 成绩3\n"); while(k!=head) { //倒序输出 t = head; while(t->next!=k) t = t->next;//t为k前驱 Output(k); k = t; } } menu(); break; case 2: Print_List(H); menu(); break; case 3: Save(H); menu(); break; case 4: Fetch(H); menu(); break; case 5: temp = Search_num(H); if(temp) { printf("指针为:%d\n",temp); printf(" 学号 姓名 专业 班级 成绩1 成绩2 成绩3\n"); Output(temp); } else printf("不存在此学生!\n"); menu(); break; case 6: InsertList(H); menu(); break; case 7: Delete_num(H); menu(); break; case 8: temp = Search_major_subject_score(H); if(temp) { printf("指针为:%d\n",temp); printf(" 学号 姓名 专业 班级 成绩1 成绩2 成绩3\n"); Output(temp); } else printf("不存在此学生!\n"); menu(); break; case 9: Delete_major_subject(H); menu(); break; case 99: read(H); menu(); break; case 10: printf("\n ~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); //退出提示 printf(" Goodbye! \n"); printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); exit(0);//将程序退出 default: printf("输入有误,请重新输入!\n"); } printf("请输入选项:"); scanf("%d",&k); } return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。