#include<fstream> #include<string> #include<iomanip> using namespace std; typedef struct node { string stunum,stuname; int math,english,computer; int averagescore; node *next; }*ptrn; int length(ptrn &e) { int count=0; ptrn te; te=e; while(1) { if(te==0) { break; } else { count++; te=te->next; } } return count; } void averagescorerank(ptrn &e) { for(int i=0;i<length(e);i++) { ptrn temm; temm=e; for(int j=0;j<length(e)-i-1;j++) { if(temm->averagescore<temm->next->averagescore) { string tem,temp; int temath,temen,temco,temaverage; tem=temm->next->stunum; temp=temm->next->stuname; temath=temm->next->math; temen=temm->next->english; temco=temm->next->computer; temaverage=temm->next->averagescore; temm->next->stunum=temm->stunum; temm->next->stuname=temm->stuname; temm->next->math=temm->math; temm->next->english=temm->english; temm->next->computer=temm->computer; temm->next->averagescore=temm->averagescore; temm->stunum=tem; temm->stuname=temp; temm->math=temath; temm->english=temen; temm->computer=temco; temm->averagescore=temaverage; } temm=temm->next; } } } void mathrank(ptrn &e) { for(int i=0;i<length(e);i++) { ptrn temm; temm=e; for(int j=0;j<length(e)-i-1;j++) { if(temm->math<temm->next->math) { string tem,temp; int temath,temen,temco,temaverage; tem=temm->next->stunum; temp=temm->next->stuname; temath=temm->next->math; temen=temm->next->english; temco=temm->next->computer; temaverage=temm->next->averagescore; temm->next->stunum=temm->stunum; temm->next->stuname=temm->stuname; temm->next->math=temm->math; temm->next->english=temm->english; temm->next->computer=temm->computer; temm->next->averagescore=temm->averagescore; temm->stunum=tem; temm->stuname=temp; temm->math=temath; temm->english=temen; temm->computer=temco; temm->averagescore=temaverage; } temm=temm->next; } } } void enlishrank(ptrn &e) { for(int i=0;i<length(e);i++) { ptrn temm; temm=e; for(int j=0;j<length(e)-i-1;j++) { if(temm->english<temm->next->english) { string tem,temp; int temath,temen,temco,temaverage; tem=temm->next->stunum; temp=temm->next->stuname; temath=temm->next->math; temen=temm->next->english; temco=temm->next->computer; temaverage=temm->next->averagescore; temm->next->stunum=temm->stunum; temm->next->stuname=temm->stuname; temm->next->math=temm->math; temm->next->english=temm->english; temm->next->computer=temm->computer; temm->next->averagescore=temm->averagescore; temm->stunum=tem; temm->stuname=temp; temm->math=temath; temm->english=temen; temm->computer=temco; temm->averagescore=temaverage; } temm=temm->next; } } } void computerrank(ptrn &e) { for(int i=0;i<length(e);i++) { ptrn temm; temm=e; for(int j=0;j<length(e)-i-1;j++) { if(temm->computer<temm->next->computer) { string tem,temp; int temath,temen,temco,temaverage; tem=temm->next->stunum; temp=temm->next->stuname; temath=temm->next->math; temen=temm->next->english; temco=temm->next->computer; temaverage=temm->next->averagescore; temm->next->stunum=temm->stunum; temm->next->stuname=temm->stuname; temm->next->math=temm->math; temm->next->english=temm->english; temm->next->computer=temm->computer; temm->next->averagescore=temm->averagescore; temm->stunum=tem; temm->stuname=temp; temm->math=temath; temm->english=temen; temm->computer=temco; temm->averagescore=temaverage; } temm=temm->next; } } } void section(ptrn &e) { ptrn tem; tem=e; int ms6=0,ms6_9=0,ms7_9=0,ms8_9=0,ms9=0,es6=0,es6_9=0,es7_9=0,es8_9=0,es9=0,cs6=0,cs6_9=0,cs7_9=0,cs8_9=0,cs9=0; while(1) { if(tem!=0) { if(tem->math<60)ms6++; if(tem->math>=60&&tem->math<=69)ms6_9++; if(tem->math>=70&&tem->math<=79)ms7_9++; if(tem->math>=80&&tem->math<=89)ms8_9++; if(tem->math>=90)ms9++; if(tem->english<60)es6++; if(tem->english>=60&&tem->english<=69)es6_9++; if(tem->english>=70&&tem->english<=79)es7_9++; if(tem->english>=80&&tem->english<=89)es8_9++; if(tem->english>=90)es9++; if(tem->computer<60)cs6++; if(tem->computer>=60&&tem->computer<=69)cs6_9++; if(tem->computer>=70&&tem->computer<=79)cs7_9++; if(tem->computer>=80&&tem->computer<=89)cs8_9++; if(tem->computer>=90)cs9++; tem=tem->next; } else break; } cout<<std::left<<setw(20)<<"數學不及格人數爲 :"<<std::right<<setw(5)<<ms6<<endl; cout<<std::left<<setw(20)<<"60-69人數爲 :"<<std::right<<setw(5)<<ms6_9<<endl; cout<<std::left<<setw(20)<<"70-79人數爲 :"<<std::right<<setw(5)<<ms7_9<<endl; cout<<std::left<<setw(20)<<"80-89人數爲 :"<<std::right<<setw(5)<<ms8_9<<endl; cout<<std::left<<setw(20)<<"90以上人數爲 :"<<std::right<<setw(5)<<ms9<<endl; cout<<std::left<<setw(20)<<"英語不及格人數爲 :"<<std::right<<setw(5)<<es6<<endl; cout<<std::left<<setw(20)<<"60-69人數爲 :"<<std::right<<setw(5)<<es6_9<<endl; cout<<std::left<<setw(20)<<"70-79人數爲 :"<<std::right<<setw(5)<<es7_9<<endl; cout<<std::left<<setw(20)<<"80-89人數爲 :"<<std::right<<setw(5)<<es8_9<<endl; cout<<std::left<<setw(20)<<"90以上人數爲 :"<<std::right<<setw(5)<<es9<<endl; cout<<std::left<<setw(20)<<"計算機不及格人數爲 :"<<std::right<<setw(5)<<cs6<<endl; cout<<std::left<<setw(20)<<"60-69人數爲 :"<<std::right<<setw(5)<<cs6_9<<endl; cout<<std::left<<setw(20)<<"70-79人數爲 :"<<std::right<<setw(5)<<cs7_9<<endl; cout<<std::left<<setw(20)<<"80-89人數爲 :"<<std::right<<setw(5)<<cs8_9<<endl; cout<<std::left<<setw(20)<<"90以上人數爲 :"<<std::right<<setw(5)<<cs9<<endl; } void serch(ptrn &e) { cout<<" | (1)學號查詢 |"<<endl; cout<<" | (2)姓名查詢 |"<<endl; cout<<" | 請輸入序號 |"<<endl; cout<<"_______________________________________________________"<<endl; int m; cin>>m; if(m==1) { cout<<" 請輸入學號"<<endl; string ch1; cin>>ch1; ptrn temn; temn=e; while(1) { if(temn!=0&&temn->stunum==ch1) { cout<<" 所查學生的信息以下"<<endl; cout<<std::left<<setw(10)<<"學號"<<std::right<<setw(10)<<"姓--名"<<std::right<<setw(10)<<"數學"<<std::right<<setw(10)<<"英語"<<std::right<<setw(10)<<"計算機"<<endl; cout<<std::left<<setw(10)<<temn->stunum<<std::right<<setw(10)<<temn->stuname<<std::right<<setw(10)<<temn->math<<std::right<<setw(10)<<temn->english<<std::right<<setw(10)<<temn->computer<<endl;break; } else if(temn!=0) { temn=temn->next; } else { break; } } cout<<"______________________________________________________________"<<endl; } else { cout<<" 請輸入姓名"<<endl; string ch2; cin>>ch2; ptrn temn; temn=e; while(1) { if(temn!=0&&temn->stuname==ch2) { cout<<" 所查學生的信息以下"<<endl; cout<<std::left<<setw(10)<<"學號"<<std::right<<setw(10)<<"姓--名"<<std::right<<setw(10)<<"數學"<<std::right<<setw(10)<<"英語"<<std::right<<setw(10)<<"計算機"<<endl; cout<<std::left<<setw(10)<<temn->stunum<<std::right<<setw(10)<<temn->stuname<<std::right<<setw(10)<<temn->math<<std::right<<setw(10)<<temn->english<<std::right<<setw(10)<<temn->computer<<endl; break; } else if(temn!=0) { temn=temn->next; } else { break; } } cout<<"______________________________________________________________"<<endl; } } void main() { ptrn head,tem,newnode; string stunum,stuname,math,english,computer; ifstream inf("input.dat"); head=0; int time=0,mathaverage=0,enaverage=0,coaverage=0,mathtop,entop,cotop,mathmin,enmin,comin; while(!inf.eof())//********************************************************* { if(time==0) { time=1; //跳過文件第一行 ptrn newnode1=new node; inf>>stunum>>stuname>>math>>english>>computer; continue; }//*********************************************************************** newnode=new node; inf>>stunum>>stuname>>math>>english>>computer; newnode->stunum=stunum;newnode->stuname=stuname; newnode->math=atoi(math.c_str());//***************************** newnode->english=atoi(english.c_str()); //將字符串轉換爲數字 newnode->computer=atoi(computer.c_str());//******************** newnode->averagescore=(newnode->math+newnode->english+newnode->computer)/3; if(head==0) { head=newnode; tem=head; } else { tem->next=newnode; tem=newnode; } } if(head)tem->next=0; inf.close(); ofstream ouf("averagerank.dat"); averagescorerank(head); ouf<<" 按照平均成績排名由高到低爲"<<endl; ptrn tem1; tem1=head; while(1) { if(tem1!=0) { ouf<<tem1->stunum<<" "<<tem1->stuname<<" "<<tem1->averagescore<<endl; tem1=tem1->next; } else break; } ouf.close(); ofstream ouf1("mathrank.dat"); mathrank(head); ouf1<<" 按照數學成績排名由高到低爲"<<endl; ptrn tem2; tem2=head; mathtop=head->math; while(1) { if(tem2!=0) { ouf1<<tem2->stunum<<" "<<tem2->stuname<<" "<<tem2->math<<endl; mathaverage+=tem2->math; if(tem2->next==0) { mathmin=tem2->math; } tem2=tem2->next; } else { mathaverage=mathaverage/length(head);break; } } ouf1.close(); ofstream ouf2("englishrank.dat"); enlishrank(head); ouf2<<" 按照英語成績排名由高到低爲"<<endl; ptrn tem3; tem3=head; entop=head->english; while(1) { if(tem3!=0) { ouf2<<tem3->stunum<<" "<<tem3->stuname<<" "<<tem3->english<<endl; enaverage+=tem3->english; if(tem3->next==0) { enmin=tem3->english; } tem3=tem3->next; } else { enaverage=enaverage/length(head); break; } } ouf2.close(); ofstream ouf3("computerrank.dat"); computerrank(head); ouf3<<" 按照計算機成績排名由高到低爲"<<endl; ptrn tem4; tem4=head; cotop=head->computer; while(1) { if(tem4!=0) { ouf3<<tem4->stunum<<" "<<tem4->stuname<<" "<<tem4->computer<<endl; coaverage+=tem4->computer; if(tem4->next==0) { comin=tem4->computer; } tem4=tem4->next; } else { coaverage=coaverage/length(head); break; } } ouf3.close(); cout<<std::left<<setw(20)<<"數學平均分爲 :"<<std::right<<setw(5)<<mathaverage<<endl; cout<<std::left<<setw(20)<<"英語平均分爲 :"<<std::right<<setw(5)<<enaverage<<endl; cout<<std::left<<setw(20)<<"計算機平均分爲:"<<std::right<<setw(5)<<coaverage<<endl; cout<<std::left<<setw(20)<<"數學最高分爲 :"<<std::right<<setw(5)<<mathtop<<endl; cout<<std::left<<setw(20)<<"英語最高分爲 :"<<std::right<<setw(5)<<entop<<endl; cout<<std::left<<setw(20)<<"計算機最高分爲:"<<std::right<<setw(5)<<cotop<<endl; section(head); serch(head); }