当前位置:文档之家› C++学生管理系统代码

C++学生管理系统代码

#include<iostream>#include<fstream>#include<string>#include <iomanip>#include<windows.h>#include<ctime>using namespace std;const int NAME_NUM=30;struct student{char name[NAME_NUM];float num;float chinaNum;float englishNum;float mathNum;float average;float result;int pos;student *next;};void Print(student *head);void InsertFront(student* &head, student *pNew);void InsertRear(student* &head, student *pNew);student* Find(student *head, char *findStr, char type); student* Read();void Write(student* head);void ShowList(student* head);int GetLength(student* head);void Delete(student* &head, char *delStr,int delNum);void FindMaxOrMin(student *head,char type,char maxOrMin); void Reword(student *pStd);void Sort(student *&head, char type,char maxOrMin);void Count(student *&head);void DeleteAll(student *&head);bool Enter(char type);void SetTitle(bool isLoad);void AboutMe();void ChaXun(string str,student *head);void PaiMing(string str, student* head);void ShanChu(string str, student *&head);void XianShi(string str, student *head);void XuiGai(string str, student *&head);void ZengJia(string str, student* &head);int Run();bool Enter(char type){ofstream out("Password.pwd",ios::app);ifstream in("Password.pwd");string s[2];int num=0;string zhangHao;string miMa;while(!in.eof()){in>>s[num];num++;if(num==2){break;}}if(s[0].compare("")==0 || type=='2' ){if(s[0].compare("")==0 && type!='2'){cout<<"你是第一次使用本程序,请设置帐号和密码."<<endl; }else{bool isLoad=false;isLoad=Enter('1');if(isLoad==true){cout<<"修改用户."<<endl;out.close();out.open("Password.pwd",ios::trunc);}else{cout<<"你输入的密码错误."<<endl;cout<<"你不是管理员不能修改密码."<<endl; return false;}}cout<<"请输入您的新帐号: ";cin>>s[0];cout<<"请输入您的新密码: ";cin>>s[1];string s1,s2;for(int i=0; i<s[0].size(); i++){s1+=char(int(s[0][i])+11);}for( i=0; i<s[1].size(); i++){s2+=char(int(s[1][i])+11);}s[0]=s1;s[1]=s2;for( i=0; i<=1; i++){out<<s[i]<<" ";}out.close();}string s1,s2;for(int i=0; i<s[0].size(); i++){s1+=char(int(s[0][i])-11);}for( i=0; i<s[1].size(); i++){s2+=char(int(s[1][i])-11);}cout<<"请输入您的帐号: ";cin>>zhangHao;cout<<"请输入您的密码: ";cin>>miMa;if(pare(s1)==0 && pare(s2)==0) {return true;}else{return false;}return false;}void Print(student *head){student *pHead=head;int num=strlen(head->name);while(head){if(num<strlen(head->name)){num=strlen(head->name);}head=head->next;}head=pHead;cout<<setw(num)<<head->name<<setw(8)<<head->num<<setw(10)<<head->chinaNum<<setw(10)<<head->mathNum<<setw(10)<<head->englishNum<<setw(8)<<head->result<<setw(8)<<head->average<<setw(5)<<head->pos<<endl; }void ShowList(student* head){cout<<"姓名:"<<setw(8)<<"座号:"<<setw(10)<<"语文分数:"<<setw(10) <<"数学分数:"<<setw(10)<<"英语分数:"<<setw(8)<<"总分数:"<<setw(8)<<"平均分:"<<setw(6)<<"名次:"<<endl<<endl; while(head){Count(head);Print(head);head=head->next;}cout<<endl;}void Write(student* head){ofstream out("Student.dat",ios::trunc);while(head){Count(head);out.write((char*)head,sizeof(student)); head=head->next;}out.close();}student* Read(){ifstream in("Student.dat");student *head;student *pP;student *pEnd;pP=new student;pEnd=head=pP;student *pS=new student;memset(pS->name,0,NAME_NUM); in.read((char*)pS,sizeof(student));if(strcmp(pS->name,"\0")==0){return NULL;}strcpy(pP->name,pS->name);pP->num=pS->num;pP->chinaNum=pS->chinaNum;pP->englishNum=pS->englishNum;pP->mathNum=pS->mathNum;while(!in.eof()){in.read((char*)pS,sizeof(student));pEnd->next=pP;pEnd=pP;pP=new student;strcpy(pP->name,pS->name);pP->num=pS->num;pP->chinaNum=pS->chinaNum;pP->englishNum=pS->englishNum;pP->mathNum=pS->mathNum;}pEnd->next=NULL;delete pP;return head;}student* Find(student *head,char *findStr, char type){/*参数说明:type=='1' 按名字查找type=='2' 按座号查找type=='3' 按语文查找type=='4' 按数学查找type=='5' 按英语查找type=='6' 按总分查找type=='7' 按平均分查找type=='8' 按排名查找 //没有实现*/bool isFind=false;student *firstStd=NULL;cout<<"姓名:"<<setw(8)<<"座号:"<<setw(10)<<"语文分数:"<<setw(10) <<"数学分数:"<<setw(10)<<"英语分数:"<<setw(8)<<"总分数:"<<setw(8)<<"平均分:"<<setw(5)<<"名次:"<<endl<<endl;while(head){if(type=='1'){if(strcmp(head->name,findStr)==0){if(firstStd==NULL){firstStd=head;}Print(head);isFind=true;if(head->next==NULL){return firstStd;}}}else if(type=='2'){if(int(head->num)==int(atof(findStr))) {if(firstStd==NULL){firstStd=head;}Print(head);isFind=true;if(head->next==NULL){return firstStd;}}}else if(type=='3'){if(int(head->chinaNum)==int(atof(findStr))) {if(firstStd==NULL){firstStd=head;Print(head);isFind=true;if(head->next==NULL){return firstStd;}}}else if(type=='4'){if(int(head->mathNum)==int(atof(findStr))) {if(firstStd==NULL){firstStd=head;}Print(head);isFind=true;if(head->next==NULL){return firstStd;}}}else if(type=='5'){if(int(head->englishNum)==int(atof(findStr))) {if(firstStd==NULL){firstStd=head;}Print(head);isFind=true;if(head->next==NULL){return firstStd;}}}else if(type=='6'){if(int(head->result)==int(atof(findStr))) {if(firstStd==NULL){firstStd=head;}Print(head);isFind=true;if(head->next==NULL){return firstStd;}}}else if(type=='7'){if(int(head->average)==int(atof(findStr))) {if(firstStd==NULL){firstStd=head;}Print(head);isFind=true;if(head->next==NULL){return firstStd;}}}else if(type=='8'){if(int(head->pos)==int(atof(findStr))) {if(firstStd==NULL){firstStd=head;}Print(head);isFind=true;if(head->next==NULL){return firstStd;}}}head=head->next;}if(isFind==false){cout<<"找不到你要找的记录."<<endl;return NULL;}return firstStd;}void InsertRear(student* &head,student *pNew) {student *pEnd,*pHead,*pS=new student;if(head==NULL){InsertFront(head,pNew);return ;}pHead=head;while(head){pEnd=head;if(head->next==NULL){break;}head=head->next;}strcpy(pS->name,pNew->name);pS->num=pNew->num;pS->chinaNum=pNew->chinaNum;pS->englishNum=pNew->englishNum;pS->mathNum=pNew->mathNum;pS->next=NULL;pEnd->next=pS;head=pHead;}void InsertFront(student* &head, student *pNew) {student *pHead=new student;strcpy(pHead->name,pNew->name);pHead->num=pNew->num;pHead->chinaNum=pNew->chinaNum;pHead->englishNum=pNew->englishNum; pHead->mathNum=pNew->mathNum;pHead->next=head;head=pHead;}int GetLength(student *head){int len=0;while(head){len++;head=head->next;}return len;}void Delete(student* &head, char *delStr,int delNum) {student* pDel,*pNew,*pHead;bool isFind=false;pHead=head;if(strcmp(head->name,delStr)==0){pNew=head->next;delete head;head=pNew;if(delNum==2)cout<<endl<<"因为要删除的记录在前面,所有只删除第一记录."<<endl; }return ;}while(head->next){if(delNum==2){if(strcmp(head->next->name,delStr)==0){pNew=head->next->next;pDel=head->next;delete pDel;head->next=pNew;head=pHead;isFind=true;}}else{if(strcmp(head->next->name,delStr)==0){pNew=head->next->next;pDel=head->next;delete pDel;head->next=pNew;head=pHead;return ;}}head=head->next;}head=pHead;if(isFind==false){cout<<"找不到你要删除的记录."<<endl;}else{cout<<"删除记录成功."<<endl;}void FindMaxOrMin(student *head,char type,char maxOrMin) {/*参数说明:type=='1' 按语文查找type=='2' 按数学查找type=='3' 按英语查找type=='4' 按总分查找type=='5' 按平均分查找type=='6' 按排名查找*/int maxNum;student *pHead=head;if(type=='1'){maxNum=int(head->chinaNum);}else if (type=='2'){maxNum=int(head->mathNum);}else if (type=='3'){maxNum=int(head->englishNum);}else if (type=='4'){maxNum=int(head->result);}else if (type=='5'){maxNum=int(head->average);}else if (type=='6'){maxNum=head->pos;}else{cout<<"你输入错误,请重新输入."<<endl; return ;}while(head){if(maxOrMin=='1'){if(type=='1'){if(maxNum<int(head->chinaNum)){maxNum=int(head->chinaNum);}}else if (type=='2'){if(maxNum<int(head->mathNum)){maxNum=int(head->mathNum);}}else if (type=='3'){if(maxNum<int(head->englishNum)){maxNum=int(head->englishNum);}}else if (type=='4'){if(maxNum<int(head->result)){maxNum=int(head->result);}}else if (type=='5'){if(maxNum<int(head->average)){maxNum=int(head->average);}else if (type=='6'){if(maxNum<head->pos){maxNum=head->pos;}}}else{if(type=='1'){if(maxNum>int(head->chinaNum)) {maxNum=int(head->chinaNum);}}else if (type=='2'){if(maxNum>int(head->mathNum)) {maxNum=int(head->mathNum);}}else if (type=='3'){if(maxNum>int(head->englishNum)) {maxNum=int(head->englishNum); }}else if (type=='4'){if(maxNum>int(head->result)){maxNum=int(head->result);}}else if (type=='5'){if(maxNum>int(head->average))maxNum=int(head->average);}}else if (type=='6'){if(maxNum>head->pos){maxNum=head->pos;}}}head=head->next;}head=pHead;cout<<"姓名:"<<setw(8)<<"座号:"<<setw(10)<<"语文分数:"<<setw(10) <<"数学分数:"<<setw(10)<<"英语分数:"<<setw(8)<<"总分数:"<<setw(8)<<"平均分:"<<setw(5)<<"名次:"<<endl<<endl;while(head){if(type=='1'){if(int(head->chinaNum)==maxNum){Print(head);}}else if (type=='2'){if(int(head->mathNum)==maxNum){Print(head);}}else if (type=='3'){if(int(head->englishNum)==maxNum){Print(head);}else if (type=='4'){if(int(head->result)==maxNum){Print(head);}}else if (type=='5'){if(int(head->average)==maxNum){Print(head);}}else if (type=='6'){if(head->pos==maxNum){Print(head);}}head=head->next;}}void Reword(student *pStd){if(pStd!=NULL){cout<<"请输入学生的新名字:"<<endl; cin>>pStd->name;cout<<"请输入学生的座号"<<endl; cin>>pStd->num;cout<<"请输入学生的语文分数"<<endl; cin>>pStd->chinaNum;cout<<"请输入学生的数学分数"<<endl; cin>>pStd->mathNum;cout<<"请输入学生的英语分数"<<endl; cin>>pStd->englishNum;}{return ;}}void Sort(student *&head, char type,char maxOrMin) {/*参数说明:type=='1' 按语文排列type=='2' 按数学排列type=='3' 按英语排列type=='4' 按总分排列type=='5' 按平均分排列type=='6' 按座号排列*/student *pHead,*pH;pHead=pH=head;int len=GetLength(head);float *array=new float[len];int i;int x=0;float num=0;while(head){Count(head);if(type=='1'){num=head->chinaNum;}else if(type=='2'){num=head->mathNum;}else if(type=='3'){num=head->englishNum;}else if(type=='4'){num=head->result;}else if(type=='5'){num=head->average; }else if(type=='6'){num=head->num;}array[x]=num;x++;head=head->next;}head=pHead;if(maxOrMin=='1') {for( i=1; i<len; i++) {for(int j=0; j<len-i; j++) {if(array[j]<array[j+1]) {float num;num=array[j];array[j]=array[j+1]; array[j+1]=num;}}}}else{for( i=1; i<len; i++) {for(int j=0; j<len-i; j++) {if(array[j]>array[j+1]) {float num;num=array[j];array[j]=array[j+1]; array[j+1]=num;}}}}int pos=1;for(i=0; i<len; i++){head=pHead;while(head){if(type=='1'){num=head->chinaNum;}else if(type=='2'){num=head->mathNum;}else if(type=='3'){num=head->englishNum;}else if(type=='4'){num=int(head->result);}else if(type=='5'){num=int(head->average);}else if(type=='6'){num=int(head->num);}int n=0;if(int(array[i])==int(num)){if(int(array[i])!=int(array[i+1])) {if(n==0){}head->pos=pos;pos++;}else{head->pos=n;}}head=head->next;}}head=pH;delete []array;}void Count(student *&head){head->result=head->chinaNum+head->englishNum+head->mathNum;head->average=head->result/3;}void DeleteAll(student* &head){student *cp,*np;cp=head;while(cp){np=cp->next;delete cp;cp=np;}head=NULL;}void ChaXun(string str,student *head){Sort(head,'4','1');cout<<"欢迎使用查询功能"<<endl<<endl;cout<<"请输入你要按什么查询 1->一般查询 2->查找最多 3->查找最少"<<endl; string s;while(s[0]!='1'&&s[0]!='2'&&s[0]!='3'){cout<<"你输入错误,请重新输入."<<endl;cin>>s;}if(s[0]=='1'){cout<<"按什么查询?"<<endl;cout<<"1->姓名 2->座号 3->语文成绩 4->数学成绩 " <<"5->英语成绩 6->总分 7->平均分 8->排名"<<endl; cin>>str;while(str[0]!='1' && str[0]!='2' &&str[0]!='3' && str[0]!='4' &&str[0]!='5' && str[0]!='6' &&str[0]!='7' && str[0]!='8' ){cout<<"你输入错误,请重新输入."<<endl;cin>>str;}char findStr[30];cout<<"请输入要查找的关键字或关键数:"<<endl; cin>>findStr;switch(str[0]){case '1':Find(head,findStr,'1');break;case '2':Find(head,findStr,'2');break;case '3':Find(head,findStr,'3');break;case '4':Find(head,findStr,'4');break;case '5':Find(head,findStr,'5');break;case '6':Find(head,findStr,'6');case '7':Find(head,findStr,'7');break;case '8':Find(head,findStr,'8');break;}}else if(s[0]=='2'){cout<<"请输入要按什么查询?"<<endl;cout<<"1->语文成绩 2->数学成绩 "<<"3->英语成绩 4->总分 5->平均分 6->排名"<<endl; string s;cin>>s;switch(s[0]){case '1':FindMaxOrMin(head,'1','1');break;case '2':FindMaxOrMin(head,'2','1');break;case '3':FindMaxOrMin(head,'3','1');break;case '6':FindMaxOrMin(head,'6','1');break;case '5':FindMaxOrMin(head,'5','1');break;default:FindMaxOrMin(head,'4','1');break;}}else if(s[0]=='3'){cout<<"请输入要按什么查询?"<<endl;cout<<"1->语文成绩 2->数学成绩 "<<"3->英语成绩 4->总分 5->平均分 6->排名"<<endl; string s;switch(s[0]){case '1':FindMaxOrMin(head,'1','2');break;case '2':FindMaxOrMin(head,'2','2');break;case '3':FindMaxOrMin(head,'3','2');break;case '6':FindMaxOrMin(head,'6','2');break;case '5':FindMaxOrMin(head,'5','2');break;default:FindMaxOrMin(head,'4','2');break;}}}void ZengJia(string str, student* &head){student *pNew=new student;cout<<"欢迎使用增加功能"<<endl<<endl;cout<<"请输入新学生的名字 :"<<endl;cin>>pNew->name;cout<<"请输入新学生的座号 :"<<endl;cin>>pNew->num;cout<<"请输入他的语文分数 :"<<endl;cin>>pNew->chinaNum;cout<<"请输入他的数学分数"<<endl;cin>>pNew->mathNum;cout<<"请输入他的英语分数"<<endl;cin>>pNew->englishNum;cout<<"插入记录的 (1->最前面 2->最后面)"<<endl; cin>>str;while(str[0]!='1' && str[0]!='2'){cout<<"你输入错误,请重新输入."<<endl;cout<<"插入记录的 (1->最前面 2->最后面)"<<endl;cin>>str;}if(str[0]=='1'){InsertFront(head,pNew);}else if(str[0]=='2'){InsertRear(head,pNew);}cout<<"新学生增加成功."<<endl;}void ShanChu(string str, student *&head){char delStr[30];cout<<"欢迎使用删除功能"<<endl<<endl;cout<<"1->查询删除 2->全部删除"<<endl;cin>>str;while(str[0]!='1' && str[0]!='2'){cout<<"输入错误,请重新输入."<<endl;cin>>str;}if(str[0]=='1'){cout<<"请输入要删除的关键字"<<endl;cin>>delStr;cout<<"1->删除第一条找到的记录 2->删除所有找到的记录"<<endl; cin>>str;while(str[0]!='1'&&str[0]!='2'){cout<<"你输入错误,请重新输入."<<endl;cin>>str;}cout<<"你真的要删除吗? 1->删除 2->取消"<<endl;string s;cin>>s;if(str[0]=='1'){if(str[0]=='1'){Delete(head,delStr,1);}else{Delete(head,delStr,2);}}else{cout<<"你已经取消删除了."<<endl;}}else{cout<<"你真的要删除全部数据吗?这样会使你的数据全部丢失哦."<<endl; cout<<"1->全部删除 2->取消删除"<<endl;cin>>str;if(str[0]=='1'){DeleteAll(head);}else{cout<<"你已经取消删除了."<<endl;}}}void PaiMing(string str, student* head){string s;cout<<"欢迎使用排名功能"<<endl<<endl;cout<<"排名选择: 1->升序 2->降序"<<endl;cin>>s;cout<<"请输入要按什么排名?"<<endl;cout<<"1->语文成绩 2->数学成绩 3->英语成绩 "<<"4->总分 5->平均分 6->座号"<<endl;cin>>str;while(str[0]!='1' && str[0]!='2' &&str[0]!='3' && str[0]!='4' &&str[0]!='5' && str[0]!='6' ){cout<<"你输入错误,请重新输入."<<endl;cin>>str;}cout<<"姓名:"<<setw(8)<<"座号:"<<setw(10)<<"语文分数:"<<setw(10) <<"数学分数:"<<setw(10)<<"英语分数:"<<setw(8)<<"总分数:"<<setw(8)<<"平均分:"<<setw(6)<<"名次:"<<endl<<endl; if(s[0]=='2'){switch(str[0]){case '1':Sort(head,'1','1');break;case '2':Sort(head,'2','1');break;case '3':Sort(head,'3','1');break;case '4':Sort(head,'4','1');break;case '5':Sort(head,'5','1');break;case '6':Sort(head,'6','1');break;}}else{switch(str[0]){case '1':Sort(head,'1','2');break;case '2':Sort(head,'2','2');break;case '3':Sort(head,'3','2');break;case '4':Sort(head,'4','2');break;case '5':Sort(head,'5','2');break;case '6':Sort(head,'6','2');break;}}ShowList(head);return ;}void XianShi(string str, student *head){Sort(head,'4','1');string s;cout<<"欢迎使用显示功能"<<endl;cout<<"1->显示全部记录 2->显示记录数目"<<endl; cin>>s;if(s[0]=='2'){cout<<"记录的数目是:"<<GetLength(head)<<endl;}else{ShowList(head);}}void XuiGai(string str, student *&head){string s;student *std;cout<<"欢迎使用修改功能"<<endl;cout<<"请输入你要按什么查询"<<endl;cout<<"1->姓名 2->座号 3->语文成绩 4->数学成绩 " <<"5->英语成绩 "<<endl;cin>>str;while(str[0]!='1' && str[0]!='2' &&str[0]!='3' && str[0]!='4' &&str[0]!='5' ){cout<<"你输入错误,请重新输入."<<endl;cin>>str;}char findStr[30];cout<<"请输入要查找的关键字或关键数:"<<endl; cin>>findStr;switch(str[0]){case '1':std=Find(head,findStr,'1');Reword(std);break;case '2':std=Find(head,findStr,'2');Reword(std);break;case '3':std=Find(head,findStr,'3');Reword(std);break;case '4':std=Find(head,findStr,'4');Reword(std);break;case '5':std=Find(head,findStr,'5');Reword(std);break;}Write(head);if(std!=NULL){cout<<"修改成功."<<endl;}}int Run(){bool isLoad=false;student* head=NULL;student *pNew=new student;head=Read();SetTitle(false);if(head!=NULL){ Sort(head,'5','1');Count(head);}string str;SetTitle(false);cout<<" 欢迎使用学生管理系统 "<<endl<<endl;cout<<" 1->用户登陆 2->退出程序 "<<endl;cin>>str;if(str[0]=='2'){AboutMe();return 0;}else{isLoad=Enter('1');system("cls");if(isLoad==true){SetTitle(true);cout<<" 恭喜,您输入的密码正确.可以对本系统的进行任何操作."<<endl; }else{cout<<" Sorry,您输入的密码错误.你不能修改本系统的任何内容."<<endl; }begin:cout<<endl<<endl;cout<<" 欢迎使用学生管理系统 "<<endl<<endl; cout<<" 1->增加功能 2-查询功能"<<endl;cout<<" 3->删除功能 4-排名功能"<<endl;cout<<" 5->显示功能 6-修改功能"<<endl;cout<<" 7->用户设置 8-退出程序"<<endl;cout<<"请输入您的选择: "<<endl;cin>>str;while(str[0]!='8'){if(isLoad==true && head!=NULL){cout<<endl<<endl;if(str[0]=='1'){ZengJia(str, head);Sort(head,'4','1');Write(head);}else if(str[0]=='2'){ChaXun(str,head);}else if(str[0]=='3'){ShanChu(str,head);Sort(head,'4','1');Write(head);}else if(str[0]=='4'){PaiMing(str,head);}else if(str[0]=='5'){XianShi(str,head);}else if(str[0]=='6'){XuiGai(str,head);Write(head);else if(str[0]=='7'){cout<<"欢迎使用用户修改功能"<<endl;isLoad=Enter('2');}else if(str[0]=='8'){AboutMe();return 0;}else{cout<<"你输入错误,请重新输入."<<endl;goto begin;}}else if(isLoad==false && head!=NULL){if(str[0]=='2'){ChaXun(str,head);}else if(str[0]=='4'){PaiMing(str,head);}else if(str[0]=='5'){XianShi(str,head);}else{cout<<"你不是管理员,不能进行此项功能."<<endl;cout<<"你只能进行查询功能显示功能排名功能"<<endl;}}else if( head==NULL && isLoad==true){cout<<"系统检查到你没有任何记录,不能进行任何操作,只能增加记录."<<endl; ZengJia(str, head);Write(head);head=Read();}else if( head==NULL && isLoad==false){cout<<"因为你没有登陆,系统又检查到你没有任何记录,你不能进行任何操作."<<endl; }cout<<endl<<endl;cout<<"按任何键继续进行操作."<<endl;getchar();getchar();system("cls");goto begin;}AboutMe();return 0;}void SetTitle(bool isLoad){HWND hwnd=GetForegroundWindow();if(isLoad==false){SetWindowText(hwnd," 学生管理系统(没有登陆)");}else{SetWindowText(hwnd," 学生管理系统(已经登陆)");}system("color a");}void AboutMe(){char*pStr= " ┃ \n"" ┃ \n"。

相关主题