1、 设计一个学生信息管理系统,链表中每一结点包括:学号、姓名、年龄、性别、出生年月、地址、电话、电子邮件。程序能实现以下功能:
从文件中加载信息
从键盘输入信息
显示学生的信息
修改学生的信息
查找链表中是否存在某个元素,并显示这个元素的所有信息,若没有这个元素则显示“无此记录!”的信息。
删除链表中指定学号的结点。
添加学生记录
要求:程序运行中,先显示实现以上功能所构成的菜单,然后根据选项调用相应程序及显示其对应的结果。
完整的代码如下:
#include "stdio.h" #include "stdlib.h" #include "string.h" typedef struct student { int id; //学号 char name[20]; //姓名 int age; //年龄 char sex; //性别(f或m) char birthday[30]; //出生年月 char address[20]; //地址 char phone[15]; //电话 char email[30]; //email struct student *next; }student; student *head=NULL; int length; //链表的长度 void create() { student *p1,*p2; length=0; p1=(student *)malloc(sizeof(student)); p1->id=-1; if(head==NULL) head=p1; printf("请输入学生的学号、姓名、年龄、性别、出生年月、地址、电话、电子邮箱:/n"); while(1) //学号为0的时候退出 { p2=(student *)malloc(sizeof(student)); scanf("%d %s %d %c %s %s %s %s",&p2->id,p2->name,&p2->age,&p2->sex,p2->birthday,p2->address,p2->phone,p2->email); //输入学生信息 if(p2->id==0) { printf("链表创建完成!/n"); break; } length++; //链表的长度 p1->next=p2; p2->next=NULL; p1=p1->next; } return ; } void LoadStudentInfoFromFile() { student *p,*q; int c; FILE* f; f= fopen("input.txt","r"); if (f == NULL) return; fseek(f, 0, SEEK_SET); p=(student *)malloc(sizeof(student)); p->next=NULL; head=p; while(!feof(f)) { c = fgetc(f); if (c != -1) fseek(f, -1, SEEK_CUR); else return ; q=(student *)malloc(sizeof(student)); fscanf(f,"%d",&q->id); fscanf(f,"%s",q->name); fscanf(f,"%d",&q->age); fscanf(f,"%c",&q->sex); fscanf(f,"%c",&q->sex); fscanf(f,"%s",q->birthday); fscanf(f,"%s",q->address); fscanf(f,"%s",q->phone); fscanf(f,"%s",q->email); q->next=NULL; p->next=q; p=p->next; length++; //链表的长度 } } void ModifyStudentInfo() { student *p=head->next; int num; printf("请输入要修改的学生的学号:"); scanf("%d",&num); while(p!=NULL) { if(p->id==num) { printf("修改前,学号为%d的学生的信息如下:/n",num); printf("%d %s %d %c %s %s %s %s/n",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email); printf("请输入学生的新电话:"); getchar(); gets(p->phone); printf("请输入学生的新地址:"); gets(p->address); printf("修改后,学号为%d的学生的信息如下:/n",num); printf("%d %s %d %c %s %s %s %s/n",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email); return ; } p=p->next; } if(p==NULL) { printf("该学号不存在!/n"); return ; } } void display() { student *p=head->next; printf("链表中所有的学生信息如下:/n"); while(p!=NULL) { printf("%d %s %d %c %s %s %s %s/n",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email); //printf("/n"); p=p->next; } return ; } void search() { int num,x; char name[20]; student *p=head->next; printf("请选择查询的方式:/n"); printf("1、按学号查询/t 2、按姓名查询/n"); scanf("%d",&x); if(x==1) { printf("需要查找的学生学号为:"); scanf("%d",&num); while(p!=NULL) { if(p->id==num) { printf("学生学号为%d的学生的信息如下:/n",num); printf("%d %s %d %c %s %s %s %s/n",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email); return; } p=p->next; } if(p==NULL) printf("无此记录!/n"); } else if(x==2) { printf("需要查找的学生姓名为:"); getchar(); gets(name); //scanf(); p=head->next; while(p!=NULL) { if(strcmp(p->name,name)==0) { printf("学生姓名为%s的学生的信息如下:/n",name); printf("%d %s %d %c %s %s %s %s/n",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email); return; } p=p->next; } if(p==NULL) printf("无此记录!/n"); } return ; } void insert() { int num,i; student *p,*q; p=head; printf("请输入你要插入位置: "); scanf("%d",&num); if(num>length) { printf("找不到要插入的位置/n"); return ; } else { printf("请输入你要插入的学生的信息:/n"); q=(student *)malloc(sizeof(student)); scanf("%d %s %d %c %s %s %s %s",&q->id,q->name,&q->age,&q->sex,q->birthday,q->address,q->phone,q->phone); while(p!=NULL) { if(p->id==q->id) { printf("该学号已经存在,无法插入!/n"); return ; } p=p->next; } p=head; for(i=0;i<num;i++) p=p->next; q->next=p->next; p->next=q; length++; printf("插入成功!/n"); return ; } } void Delete() { int num; student *p,*q; q=head,p=head->next; printf("请输入要删除的学生的学号:/n"); scanf("%d",&num); while(p!=NULL) { if(p->id==num) { q->next=p->next; free(p); length--; printf("删除成功!/n"); return ; } p=p->next; q=q->next; } if(p==NULL) { printf("找不到要删除的编号!/n"); return ; } } void menu() { printf("________________________________________________________________/n"); printf("| 学生信息管理系统 |/n"); printf("| 0、 退出系统 |/n"); printf("| 1、 录入学生信息 |/n"); printf("| 2、 建立链表 |/n"); printf("| 3、 显示链表 |/n"); printf("| 4、 查找链表中的某个元素 |/n"); printf("| 5、 删除链表中指定学号的结点 |/n"); printf("| 6、 指定的位置上插入一个新结点 |/n"); printf("| 7、 修改学生信息 |/n"); printf("________________________________________________________________/n"); return ; } int main(void) { int a; menu(); while(1) { printf("请选择相应的功能:"); scanf("%d",&a); switch(a) { case 0: return 0; case 1: LoadStudentInfoFromFile(); //从文件中加载学生信息 menu(); break; case 2: create(); //从键盘输入学生信息 menu(); break; case 3: if(head) { display(); menu(); } else { printf("链表为空,请先建立链表!/n"); menu(); } break; case 4: if(head) { search(); menu(); } else { printf("链表为空,请先建立链表!/n"); menu(); } break; case 5: if(head) { Delete(); menu(); } else { printf("链表为空,请先建立链表!/n"); menu(); } break; case 6: if(head) { insert(); menu(); } else { printf("链表为空,请先建立链表!/n"); menu(); } break; case 7: if(head) { ModifyStudentInfo(); menu(); } else { printf("链表为空,请先建立链表!/n"); menu(); } break; default: break; } } system("pause"); return 0; }
832

被折叠的 条评论
为什么被折叠?



