慧远
#include<iostream> #include<cstring> #include<fstream> using namespace std; int num=0; const int MAX_NUM_OF_STUDENTS=1000; struct Student { char name[20],sex[10],bonetime[20],address[20],mail[20]; int no,age,phone; }; Student students[MAX_NUM_OF_STUDENTS]; Student name_students[MAX_NUM_OF_STUDENTS]; char choose; extern int Input_project(); extern int Search_project(); extern int Delete_project(); extern int Modify_project(); int Face()//主界面 { cout<<"***********欢迎使用学生信息管理系统************"<<endl<<endl; cout<<"◇ 录入(I),"<<endl<<"◇ 查询(S),"<<endl<<"◇ 删除(D),"<<endl <<"◇ 修改(M),"<<endl<<"◇ 退出(E),"<<endl; cout<<"☆ 请输入您的选择(I,S,D,E):"; cin>>choose; switch(choose) { case 'I': case 'i': system("cls");Input_project();break; case 'S': case 's': system("cls");Search_project();break; case 'D': case 'd': system("cls");Delete_project();break; case 'M': case 'm': system("cls");Modify_project();break; case 'E': case 'e': system("cls");return -1;break; default: cout<<" 输入错误!"<<endl; } return 0; } int Input_project()//输入功能 { choose='C'; while( choose=='C'||choose=='c') { num++; cout<<"请输入学号:"; cin>>students[num].no; cout<<"请输入姓名:"; cin>>students[num].name; cout<<"请输入年龄:"; cin>>students[num].age; cout<<"请输入性别:"; cin>>students[num].sex; cout<<"请输入出生年月:"; cin>>students[num].bonetime; cout<<"请输入地址:"; cin>>students[num].address; cout<<"请输入电话号码:"; cin>>students[num].phone; cout<<"请输入E-mail:"; cin>>students[num].mail; cout<<"☆ 继续或返回主菜单(C/E):"; cin>>choose; if(choose=='E'||choose=='e') break; } num+=1; int i=num;//以下是按no冒泡排序 bool exchange; while(i>1) { exchange=false; for( int k=0;k<i-1;k++) { if(students[k].no>students[k+1].no) { Student temp=students[k]; students[k]=students[k+1]; students[k+1]=temp; exchange=true; } } if (!exchange) break; i--; } for (int t=0;t<num;t++)//结构体数组各项一一复制 { name_students[t].no=students[t].no; *strcpy(name_students[t].name,students[t].name); name_students[t].age=students[t].age; *strcpy(name_students[t].sex,students[t].sex); *strcpy(name_students[t].bonetime,students[t].bonetime); *strcpy(name_students[t].address,students[t].address); name_students[t].phone=students[t].phone; *strcpy(name_students[t].mail,students[t].mail); } while(i>1)//以下是按name冒泡排序 { exchange=false; for( int k=0;k<i-1;k++) { if(strcmp(name_students[k].name,name_students[k+1].name)>0) { Student temp=name_students[k]; name_students[k]=name_students[k+1]; name_students[k+1]=temp; exchange=true; } } if (!exchange) break; i--; } FILE *fp=fopen("c:\\name.txt","a+");//存储两结构体数组 FILE *fp1=fopen("c:\\number.txt","a+"); if (fp == NULL||fp1 == NULL)//判断文件打开是否成功 { cout<<"打开文件失败!"<<endl; return -1; } for(i=0;i<num;i++) { fwrite(&students[i],sizeof(Student),1,fp); fwrite(&name_students[i],sizeof(Student),1,fp1); } fclose(fp); fclose(fp1); system("cls");return 0; } int Search_project()//查询功能 { FILE *fp=fopen("c:\\name.txt","r"); FILE *fp1=fopen("c:\\number.txt","r"); if (fp == NULL||fp1 == NULL)//判断文件打开是否成功 { cout<<"打开文件失败!"<<endl; return -1; } num=0; fread(&students[num],sizeof(Student),1,fp); fread(&name_students[num],sizeof(Student),1,fp1); while((!feof(fp))&&(!feof(fp1))) { num++;//计算已存储的资料的人数num fread(&students[num],sizeof(Student),1,fp); fread(&name_students[num],sizeof(Student),1,fp1); } fclose(fp); fclose(fp1); cout<<'\t'<<"按学号查询(A)"<<'\t'<<"按姓名查询(B)"<<'\t'<<"返回主菜单(E)"<<endl; cout<<"Ο 请选择:"; char input; cin>>input; switch(input) { case 'A': case 'a': { cout<<"请输入学号:"; int number; cin>>number;//以下是折半查询: int low=0; int high=num-1; while (low<=high) { int mid=(low+high)/2; if (students[mid].no==number) { cout<<" 学号:"<<students[mid].no<<endl; cout<<" 姓名:"<<students[mid].name<<endl; cout<<" 年龄: "<<students[mid].age<<endl; cout<<" 性别: "<<students[mid].sex<<endl; cout<<"出生年月: "<<students[mid].bonetime<<endl; cout<<" 地址: "<<students[mid].address<<endl; cout<<"电话号码: "<<students[mid].phone<<endl; cout<<" E-mail: "<<students[mid].mail<<endl;break; } else if (number>students[mid].no) low=mid+1; else high=mid-1; } if(low>high) cout<<" 无相关资料!"<<endl; } break; case 'B': case 'b': { cout<<"请输入姓名:"; char name[20]; cin>>name; int low=0; int high=num-1;//以下是折半查询: while (low<=high) { int mid=(low+high)/2; if (strcmp(name_students[mid].name,name)==0) { cout<<" 学号:"<<name_students[mid].no<<endl; cout<<" 姓名:"<<name_students[mid].name<<endl; cout<<" 年龄: "<<name_students[mid].age<<endl; cout<<" 性别: "<<name_students[mid].sex<<endl; cout<<" 出生年月: "<<name_students[mid].bonetime<<endl; cout<<" 地址: "<<name_students[mid].address<<endl; cout<<" 电话号码: "<<name_students[mid].phone<<endl; cout<<" E-mail: "<<name_students[mid].mail<<endl; break; } else if (strcmp(name_students[mid].name,name)<0) low=mid+1; else high=mid-1; } if(low>high) cout<<" 无相关资料!"<<endl; } break; case 'E': case 'e': system("cls"); return -1;break; } cout<<"☆ 继续或返回主菜单(C/E):"; cin>>choose; if(choose=='C'||choose=='c') { system("cls"); Search_project(); } else { system("cls"); return -1; } return 0; }; int Delete_project() { cout<<'\t'<<"按学号删除(A)"<<'\t'<<"按姓名删除(B)"<<'\t'<<"返回主菜单(E)"<<endl; cout<<" 请选择:"; char input; cin>>input; switch(input) { case 'A': case 'a': cout<<" 需删除的学生的学号是:"; int number; cin>>number; for(int i=0;i<num;i++) { if( students[i].no==number) break; } for(int j=i;j<num;j++)//将要删除的学生调至数组最后一位,前num-1位仍按no排列 { Student temp =students[j]; students[j]=students[j+1]; students[j+1]=temp; } case 'B': case 'b': cout<<" 需删除的学生的姓名是:"; char name[20]; cin>>name; for(int t=0;t<num;t++) { if(strcmp(name_students[t].name,name)==0) break; } for(int j=t;j<num;j++)//将要删除的学生调至数组最后一位,前num-1位仍按name排列 { Student temp =name_students[j]; name_students[j]=name_students[j+1]; name_students[j+1]=temp; } num-=1;//总人数减一,即删除最后一位 FILE *fp=fopen("c:\\name.txt","w"); FILE *fp1=fopen("c:\\number.txt","w"); if (fp == NULL||fp1 == NULL)//判断文件打开是否成功 { cout<<"打开文件失败!"<<endl; return -1; } for(i=0;i<num;i++) { fwrite(&students[i],sizeof(Student),1,fp); fwrite(&name_students[i],sizeof(Student),1,fp1); } fclose(fp); fclose(fp1); bool p=true;//以下查找并判断删除是否成功 int low=0; int high=num-1; while (low<=high) { int mid=(low+high)/2; if (students[mid].no==number||name_students[mid].no==number) { cout<<" 删除失败!"<<endl; p=false; break; } else if (number>students[mid].no) low=mid+1; else high=mid-1; } if(p) cout<<" 删除成功!"<<endl; cout<<"☆ 继续或返回主菜单(C/E):"; cin>>choose; if(choose=='C'||choose=='c') { system("cls");Delete_project(); } else { system("cls");return -1; } return 0; }; int Modify_project() { FILE *fp=fopen("c:\\name.txt","r"); FILE *fp1=fopen("c:\\number.txt","r"); if (fp == NULL||fp1 == NULL)//判断文件打开是否成功 { cout<<"打开文件失败!"<<endl; return -1; } num=0; fread(&students[num],sizeof(Student),1,fp); fread(&name_students[num],sizeof(Student),1,fp1); while((!feof(fp))&&(!feof(fp1))) { num++;//计算已存储的资料的人数num fread(&students[num],sizeof(Student),1,fp); fread(&name_students[num],sizeof(Student),1,fp1); } fclose(fp); fclose(fp1); cout<<" 需修改的学生的学号是:"; int number; cin>>number; int t,math,english,physics,computer; cout<<" 输入修改结果:"<<endl; cout<<" 姓名:"; cin>>name; cout<<" 年龄:"; cin>>age; cout<<" 性别:"; cin>>sex; cout<<" 出生年月:"; cin>>bonetime; cout<<" 地址:"; cin>>address; cout<<" 电话号码:"; cin>>phone; cout<<" E-mail:"; cin>>mail; for(int i=0;i<num;i++) { if( students[i].no==number) break; } students[i].name=name; students[i].age=age; students[i].sex=sex; students[i].bonetime=bonetime; students[i].address=address; students[i].phone=phone; students[i].mail=mail; i=num;//以下是按no冒泡排序 bool exchange; while(i>1) { exchange=false; for( int k=0;k<i-1;k++) { if(students[k].no>students[k+1].no) { Student temp=students[k]; students[k]=students[k+1]; students[k+1]=temp; exchange=true; } } if (!exchange) break; i--; } for (t=0;t<num;t++)//结构体数组各项一一复制 { name_students[t].no=students[t].no; *strcpy(name_students[t].name,students[t].name); name_students[t].age=students[t].age; name_students[t].sex=students[t].sex; name_students[t].bonetime=students[t].bonetime; name_students[t].address=students[t].address; name_students[t].phone=students[t].phone; name_students[t].mail=students[t].mail; } while(i>1)//以下是按name冒泡排序 { exchange=false; for( int k=0;k<i-1;k++) { if(strcmp(name_students[k].name,name_students[k+1].name)>0) { Student temp=name_students[k]; name_students[k]=name_students[k+1]; name_students[k+1]=temp; exchange=true; } } if (!exchange) break; i--; } fp=fopen("c:\\name.txt","w+");//存储两结构体数组,w+表示清空已有文件的内容 fp1=fopen("c:\\number.txt","w+"); if (fp == NULL||fp1 == NULL)//判断文件打开是否成功 { cout<<"打开文件失败!"<<endl; return -1; } for(i=0;i<num;i++) { fwrite(&students[i],sizeof(Student),1,fp); fwrite(&name_students[i],sizeof(Student),1,fp1); } fclose(fp); fclose(fp1); cout<<"☆ 继续或返回主菜单(C/E):"; cin>>choose; if(choose=='C'||choose=='c') { system("cls");Delete_project();} else {system("cls");return -1;} return 0; }; int main() { system("cls"); do {Face();} while (choose=='e'||choose=='E'); return 0; system("pause"); }啥时候要!