C语言链表实现班级管理系统
2017-02-25 10:27
435 查看
上次用结构体数组做了一个图书管理系统,这次决定用链表实现一个简单的班级管理系统,还是先用create()函数第一次输入内容,之后使用可以直接从文件中读取。
#include<stdio.h> #include<stdlib.h> #define M 20 //定义学生的结构体 typedef struct noab { int id; char name[M]; char classroom[M]; float score; struct node *next; }student; student *init() { student *p,*q,*head; int i,a; int n=0; printf("输入学生信息的个数\n"); scanf("%d",&a); head=NULL; for(i=0;i<a;i++) { p=(student *)malloc(sizeof(student)); printf("输入第%d个学生的信息\n",i+1); printf("学号:"); scanf("%d",&p->id); printf("姓名:"); getchar(); gets(p->name); printf("班级:"); gets(p->classroom); printf("成绩:"); scanf("%f",&p->score); n=n+1; if(n==1) { head=p; q=p; q->next=NULL; }else { q->next=p; q=p; p->next=NULL; } } return head; } void display (student *head) { student *p=head; while(p) { printf("学生的学号为%d,姓名为%s,班级为%s,分数为%f\n",p->id,p->name,p->classroom,p->score); p=p->next; } } student *addStudent(student *head) { student *p,*q; q=head; while(q->next) q=q->next; p=(student *)malloc(sizeof(student)); printf("学号:"); scanf("%d",&p->id); printf("姓名:"); getchar(); gets(p->name); printf("班级:"); gets(p->classroom); printf("成绩:"); scanf("%f",&p->score); p->next=NULL; q->next=p; return head; } student *deleteStudent(student *head) { int a; student *pre=NULL,*p; printf("请输入要删除的id"); scanf("%d",&a); if(!head) {printf("系统中已经没有数据可以删除\n");return head;} p=head; while(p&&p->id!=a) { pre=p; p=p->next; } if(p) { if(!pre) head =head->next; else pre->next=p->next; free(p); } return head; } student *correctStudent(student *head) { int a; student *p; printf("请输入要修改的学生的id"); scanf("%d",&a); if(!head) {printf("系统中已经没有数据可以修改\n");return head;} p=head; while(p&&p->id!=a) { p=p->next; } if(p==NULL){printf("没有查到要修改的学生的id\n");return head;} else { printf("学号:"); scanf("%d",&p->id); printf("姓名:"); getchar(); gets(p->name); printf("班级:"); gets(p->classroom); printf("成绩:"); scanf("%f",&p->score); } return head; } void writeFile(student *head) { FILE *outf; student *p=head; if((outf = fopen("data.txt","wb")) == NULL) { printf("不能打开数据文件。\n"); exit(1); } while(p) { fwrite(p,sizeof(student),1,outf); p = p->next; } fclose(outf); } student *readFile(student *head) { int n=0; student temp; FILE *inf; student *p=NULL,*q; if((inf = fopen("data.txt","rb")) == NULL) { printf("不能打开数据文件。\n"); exit(1); } while(fread(&temp,sizeof(student),1,inf)!=0) { p=(student *)malloc(sizeof(student)); p->id=temp.id; p->score=temp.score; strcpy(p->name,temp.name); strcpy(p->classroom,temp.classroom); n++; if(n==1){head=p;q=p;} else { q->next=p; q=q->next; } } q->next=NULL; fclose(inf); return head; } void main() { int a; student *head,*p; //head=init(); head=readFile(head); do { printf("欢迎使用学生管理系统\n"); printf("1:增加学生\n2:删除学生\n3:修改信息\n4:显示所有信息\n5:退出系统\n"); printf("请输入您想进行的操作是:\n"); scanf("%d",&a); switch(a) { case 1:head=addStudent(head);break; case 2:head=deleteStudent(head);break; case 3:head=correctStudent(head);break; case 4:display(head);break; } }while(a != 5); writeFile(head); }
相关文章推荐
- c语言使用链表编写一个可以实现班级学生管理系统,增加,删除,修改学生信息
- C语言链表实现的简易学生成绩管理系统
- C语言链表实现图书管理系统
- <C语言>使用一个二维数组实现学生姓名管理系统,要求不能使用链表
- C语言简单用链表实现学生管理系统
- C语言使用链表实现学生信息管理系统
- C语言链表实现的控制台程序员信息管理系统
- 学生成绩管理系统课程设计(C语言,链表实现)
- C语言实现学生信息管理系统(单链表)
- c语言 学生管理系统(数组实现)
- 小型图书馆管理系统(C语言实现)
- 用C语言实现一个类似学籍管理系统的 e协 管理系统beta ( 正在完善中...)
- c语言实现的学生信息管理系统
- C语言实现的工资管理系统
- <4>c语言实现ATM存取款系统(链表)
- 实验一、链表实现学生管理系统
- C语言实现学生信息管理系统
- c语言制作的学生成绩管理系统 加链表
- C语言实现学生信息管理系统
- C语言实现一个学生信息管理系统