学生信息管理系统----(顺序表)
题目:学生信息管理系统--(顺序表)
作者:姜莹,汪凯双,信宇
主要功能:运用数据结构中的顺序表结构对学生信息进行存储,简单高效实现对学生信息的增删改查,文件操作,排序等。其中运用数据结构中哈希,二分等算法对学生信息进行快速高效查找。运用三种排序算法(直接插入排序,选择排序,快速排序)对学生信息进行排序。
#include <stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<stdlib.h> #define INITLISTSIZE 5 #define LISTINCREMENT 5 #define M 1000007 typedef struct student //学生信息存储结构 { char no[11]; char name[15]; int score[3]; int sum; float average; int order; //按成绩排序 } student; typedef struct SqList //顺序表存储结构 { student *elem; int listsize; int length; } SqList; student HashTable[M]; //哈希表 void Init(SqList &L); //初始化 int Menu_select(); //菜单 void Create(SqList &L); //顺序表建立 void Print(SqList L); //数据打印 void Compute(SqList &L); //数据计算 void Append(SqList &L); //数据追加 void Del(SqList &L); //数据删除 void Insert(SqList &L); //数据插入 void Search(SqList L); //数据查找 void Sort(SqList &L); //数据排序 void Save(SqList L); //保存文件 void Load(SqList &L); //读取文件 void Insert_Sort(SqList &L); //插入排序 void Select_Sort(SqList &L); //选择排序 void quick_sort(int l, int r,SqList &L);//快速排序 void Number_Sort(SqList &L); //按学号排序 void Binary_Search(SqList &L); //折半查找 void Hash_search(SqList L); //哈希查找 void Squence_Search(SqList L); //顺序查找 void Creathashtable(SqList L); //建立哈希表 /****************主程序*********************/ int main() { SqList L; Init(L); while(1) { switch(Menu_select()) { case 0: Init(L); break; case 1: Create(L); break; case 2: Print(L); break; case 3: Search(L); break; case 4: Compute(L); break; case 5: Del(L); break; case 6: Append(L); break; case 7: Insert(L); break; case 8: Save(L); break; case 9: Load(L); break; case 10: Sort(L); break; case 11: exit(0); } } return 0; } /****************菜单函数*******************/ int Menu_select() { int choice; 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(" 8 保存 \n"); printf(" 9 读入 \n"); printf(" 10 排序 \n"); printf(" 11 退出 \n"); printf("Enter your choice(0-11):"); scanf("%d",&choice); return choice; } /****************初始化*********************/ void Init(SqList &L) { L.elem=(student *)malloc(INITLISTSIZE*sizeof(student)); L.length=0; } /****************顺序表的建立***************/ void Create(SqList &L) { int i=0; while(1) { printf("请输入学号:"); scanf("%s",L.elem[i].no); if(L.elem[i].no[0]=='@') break; printf("请输入姓名:"); scanf("%s",L.elem[i].name); printf("请输入数学成绩:"); scanf("%d",&L.elem[i].score[0]); printf("请输入英语成绩:"); scanf("%d",&L.elem[i].score[1]); printf("请输入数据结构成绩:"); scanf("%d",&L.elem[i].score[2]); L.elem[i].sum=0; L.elem[i].average=0; L.elem[i].order=0; L.length++; i++; } } /****************信息打印*******************/ void Print(SqList L) { int i; for(i=0; i<L.length; i++) { printf("学号:%s 姓名:%s 数学:%d 英语:%d 数据结构:%d 总分:%d 平均分:%.2f 排名:%d\n",L.elem[i].no,L.elem[i].name,L.elem[i].score[0],L.elem[i].score[1],L.elem[i].score[2],L.elem[i].sum,L.elem[i].average,L.elem[i].order); } } /****************数据删除*******************/ void Del(SqList &L) { printf("输入要删除的学生学号:"); char number[11]; scanf("%s",number); int i=0; while(i<L.length&&strcmp(number,L.elem[i].no)) i++; while(i<L.length-1) { L.elem[i]=L.elem[i+1]; i++; } L.length-=1; } /****************数据追加*******************/ void Append(SqList &L) { printf("请输入要增加的学生信息:"); char no[11]; printf("请输入学号:"); scanf("%s",no); char name[15]; printf("请输入姓名:"); scanf("%s",name); int score[3]; printf("请输入数学成绩:"); scanf("%d",&score[0]); printf("请输入英语成绩:"); scanf("%d",&score[1]); printf("请输入数据结构成绩:"); scanf("%d",&score[2]); float sum=0; float average=0; strcpy(L.elem[L.length].no,no); strcpy(L.elem[L.length].name,name); memcpy(L.elem[L.length].score,score,sizeof(score)); L.elem[L.length].sum=sum; L.elem[L.length].average=average; L.elem[L.length].order=0; L.length+=1; } /****************数据插入*******************/ void Insert(SqList &L) { printf("请输入指定的学生学号:"); char no[11]; scanf("%s",no); student *p; p=L.elem; while(p&&strcmp(p->no,no)) p++; if(!p) printf("没有此人!\n"); else { student *q; for(q=L.elem+L.length; q>=p; q--) { *(q+1)=*q; } } printf("请输入学号:"); scanf("%s",p->no); printf("请输入姓名:"); scanf("%s",p->name); printf("请输入数学成绩:"); scanf("%d",&p->score[0]); printf("请输入英语成绩:"); scanf("%d",&p->score[1]); printf("请输入数据结构成绩:"); scanf("%d",&p->score[2]); L.length++; } /****************数据计算*******************/ void Compute(SqList &L) { student *p; for(p=L.elem; p<L.elem+L.length; p++) { p->sum=p->score[0]+p->score[1]+p->score[2]; p->average=p->sum/3.0; } } /****************文件保存*******************/ void Save(SqList L) { FILE *fp; if((fp=fopen("student.txt","w"))==NULL) { printf("文件打开失败\n"); getchar(); exit(1); } student *p; for(p=L.elem; p<L.elem+L.length; p++) { fprintf(fp,"%s %s %d %d %d %d %f %d\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); } fclose(fp); } /****************文件读取*******************/ void Load(SqList &L) { int i=0; FILE *fp; if((fp=fopen("student.txt","r"))==NULL) { printf("文件打开失败\n"); getchar(); exit(1); } student *p; p=L.elem; while(fscanf(fp,"%s%s%d%d%d%d%f%d",p->no,p->name,&p->score[0],&p->score[1],&p->score[2],&p->sum,&p->average,&p->order)==8) { p++; i++; } L.length=i; fclose(fp); } /****************插入排序*******************/ void Insert_Sort(SqList &L) { student p; int i, j; for (i = 1; i < L.length; i++) if (L.elem[i].sum > L.elem[i - 1].sum) { p = L.elem[i]; for (j = i - 1; j >= 0 && L.elem[j].sum < p.sum; j--) L.elem[j + 1] = L.elem[j]; L.elem[j + 1] = p; } student *t; i=0; for(t=L.elem; t<L.elem+L.length; t++) { L.elem[i].order=i+1; i++; } } /****************选择排序*******************/ void Select_Sort(SqList &L) { for (int i = 0; i <L.length - 1; i++) { int index = i; for (int j = i + 1; j < L.length; j++) { if (L.elem[index].sum <L.elem[j].sum) { index = j; } } student p; p = L.elem[index]; L.elem[index] = L.elem[i]; L.elem[i] = p; } student *t; int i=0; for(t=L.elem; t<L.elem+L.length; t++) { L.elem[i].order=i+1; i++; } } /****************快速排序*******************/ void Quick_sort(int s,int t,SqList &L) { int i=s,j=t,x=L.elem[(i+j)/2].sum; student y; do { while(L.elem[i].sum>x) i++; while(L.elem[j].sum<x) j--; if(i<=j) { y=L.elem[j]; L.elem[j]=L.elem[i]; L.elem[i]=y; i++; j--; } } while(i<j); if(j>s) Quick_sort(s,j,L); if(i<t) Quick_sort(i,t,L); student *p; i=0; for(p=L.elem; p<L.elem+L.length; p++) { L.elem[i].order=i+1; i++; } } /****************顺序查找*******************/ void Squence_Search(SqList L) { int i; char search_number[11]; printf("输入要查找的学生学号:"); scanf("%s",search_number); for( i=0; i<L.length; i++) { if(strcmp(search_number,L.elem[i].no)==0) { printf("%s %s %d %d %d %d %.2f %d\n",L.elem[i].no,L.elem[i].name,L.elem[i].score[0],L.elem[i].score[0],L.elem[1].score[2],L.elem[i].sum,L.elem[i].average,L.elem[i].order); break; } } if(i==L.length) printf("错误!!!没有此学生信息!\n"); } /****************二分查找*******************/ void Binary_Search(SqList &L) { Number_Sort(L); char search_number[11]; printf("输入要查找的学生学号:"); scanf("%s",search_number); int low=0,high=L.length-1; while(low<=high) { int mid=(low+high)/2; if(strcmp(L.elem[mid].no,search_number)==0) { printf("%s %s %d %d %d %d %.2f %d\n",L.elem[mid].no,L.elem[mid].name,L.elem[mid].score[0],L.elem[mid].score[0],L.elem[mid].score[2],L.elem[mid].sum,L.elem[mid].average,L.elem[mid].order); return; } else if(strcmp(L.elem[mid].no,search_number)<0) low=mid+1; else if(strcmp(L.elem[mid].no,search_number)>0) high=mid-1; } printf("错误!!!没有此学生信息!\n"); } /****************建立哈希表*****************/ void Creathashtable(SqList L) { int i=0; while(i<L.length) { int len=strlen(L.elem[i].no); int j=0; int sum=0; while(j<4&&len) { sum+=(L.elem[i].no[len-1]-'0')*pow(10,j); j++; len--; } if(strlen(HashTable[sum].no)==0) HashTable[sum]=L.elem[i]; else { while(strlen(HashTable[sum].no)!=0) { sum++; } HashTable[sum]=L.elem[i]; } i++; } } /****************哈希查找*******************/ void Hash_search(SqList L) { Creathashtable(L); char search_number[11]; printf("输入要查找的学生学号:"); scanf("%s",search_number); int len=strlen(search_number); int j=0; int sum=0; while(j<4&&len) { sum+=(search_number[len-1]-'0')*pow(10,j); j++; len--; } student *p=&HashTable[sum]; while(strcmp(p->no,search_number)!=0) { p++; } printf("%s %s %d %d %d %d %.2f %d\n",p->no,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order); } /****************按学号排序*****************/ void Number_Sort(SqList &L) { for (int i = 0; i <L.length - 1; i++) { int index = i; for (int j = i + 1; j < L.length; j++) { if (strcmp(L.elem[index].no,L.elem[j].no)>0) { index = j; } } student p; p = L.elem[index]; L.elem[index] = L.elem[i]; L.elem[i] = p; } } /****************排序函数*******************/ void Sort(SqList &L) { int select; printf("1:插入排序\n2: 选择排序\n3: 快速排序\n4: 学号排序\n"); scanf("%d",&select); if(select==1) Insert_Sort(L); else if(select==2) Select_Sort(L); else if(select==3) Quick_sort(0,L.length-1,L); else if(select==4) Number_Sort(L); } /****************查找函数*******************/ void Search(SqList L) { int opt; printf("1:顺序查找\n2:二分查找\n3:哈希查找\n"); scanf("%d",&opt); if(opt==1) Squence_Search(L); else if(opt==2) Binary_Search(L); else if(opt==3) Hash_search(L); }