




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、#include <iostream.h> #include <iomanip.h> #include <fstream> #include <vector> #include <malloc.h> #include <stdlib.h> #include <string> #include <process.h> #include <stdio.h> #define LEN sizeof(struct student) using namespace std; int n=0; /定義
2、一個全局變量統計學生人數 struct student/定義一個學生信息的結構體 char name20; /用來存放姓名的 char sex20; /用來存放性別的 char yuanxi20;/用來存放院系的 long int id; /用來存放學號的 int score4; /用來存放分數的 int total; /用來存放總分數的 struct student *next; ; vector <student> stu; class Information public: Information() ; /構造函數. Information() ; /析構函數. stude
3、nt *creat();/建立鏈表函數。 void output(student *head); int count(student *head);/定義函數count()統計考生總數 student *insert(student*head);/指針函數*insert()用來添加考生信息. student *cancel(student *head,long int num);/指針函數*cancel()用來刪除考生信息. student *find(student *head,long int num); /指針函數*find()用來查找考生信息. void inorder(student
4、 *head);/定義inorder()函數將考生的總分從大到小排列并輸出 void average( student *head);/求學生成績的平均分的函數 void save(student *head);/保存函數 student *Read();/讀取函數 private: student *p1,*p2,*p3,*head,st; ; Information:Information() /構造函數 cout<<" *n" cout<<" -<<歡迎您使用學生信息管理系統>>-n" cout<
5、;<" *nn" Information:Information() /構造函數 cout<<" *n" cout<<" -<<謝謝您使用學生信息管理系統>>-n" cout<<" *n" student *Information:creat(void) /定義一個指向struct student的結構體指針函數*creat()用來增加學生信息. char ch20;n=0; /用來存放姓名的 p1=p2=(student *)malloc(LEN)
6、;/調用malloc()函數用來開辟一個新的存儲單元 cout<<" -<<請建立學生考試信息表,在姓名處輸入 ! 結束輸入。>>-"<<endl; cout<<"姓名:" cin>>ch; head=NULL; /給指針head賦初值 while (strcmp(ch,"!")!=0) /調用字符比較函數strcmp()用來判斷是否繼續輸入 char str10; int flag=0; p1=(student *)malloc(LEN);/調用malloc()
7、函數用來開辟一個新的存儲單元 strcpy(p1->name,ch); /將循環結構前面輸入的姓名復制到結構體名為p1的數組name中 cout<<" 性別:" cin>>p1->sex; cout<<"院系:" ; cin>>str; cout<<" 學號(9位):" do cin>>str; if(atol(str)>999999999 | atol(str)<1) cout<<"對不起,請正確輸入!n"
8、 else p1->id=atol(str); flag=1; while(flag=0); flag=0; cout<<" 語文成績:" do cin>>str; if(atoi(str)>100 | atoi(str)<1) cout<<"對不起,請輸入1-100之間的數字!n" else p1->score0=atoi(str); flag=1; while(flag=0); flag=0; cout<<" 外語成績:" do cin>>str;
9、 if(atoi(str)>100 | atoi(str)<1) cout<<"對不起,請輸入1-100之間的數字!n" else p1->score1=atoi(str); flag=1; while(flag=0); flag=0; cout<<" 數學成績:" do cin>>str; if(atoi(str)>100 | atoi(str)<1) cout<<"對不起,請輸入1-100之間的數字!n" else p1->score2=atoi(
10、str); flag=1; while(flag=0); flag=0; cout<<" C+成績:" do cin>>str; if(atoi(str)>100 | atoi(str)<1) cout<<"對不起,請輸入1-100之間的數字!n" else p1->score3=atoi(str); flag=1; while(flag=0); flag=0; p1->total=p1->score0+p1->score1+p1->score2+p1->score3;/
11、計算總分 if(n=0)head=p1;/如果是輸入第一組學生考試信息就將指針p1賦給指針head else p2->next=p1;/否則將p1賦給p2所指結構體的next指針 p2=p1;/將指針p1賦給指針p2 n+; /將n的值加1 cout<<" 姓名:" cin>>ch;/將輸入的姓名存放到字符數組ch中 p2->next=NULL;/將p2所指結構體的next指針重新賦空值 return (head);/將輸入的第一組學生考試信息返回 void Information:output(student *head) /定義out
12、put()函數將學生的信息從頭指針所指內容開始輸出 if(head=NULL) cout<<" 這是一個空表,請先輸入考生成績.n" else cout<<"-n" cout<<" *學生成績信息表*n" cout<<"-n" cout<<"學號 姓 名 性別 院系 語文 英語 數學 C+ 平均成績 總分n" cout<<"-n" p1=head;/將頭指針賦給p do cout<<setw(
13、8)<<p1->id <<setw(9)<<p1->name <<setw(8)<<p1->sex <<setw(13)<<p1->score0 <<setw(16)<<p1->score1 <<setw(10)<<p1->score2 <<setw(9)<<p1->score3 <<setw(6)<<p1->total/4.0 <<setw(11)<
14、<p1->total<<endl; cout<<"-n" p1=p1->next;/將下一組學生信息的next指針賦給p while(p1!=NULL);/若指針p非空則繼續,目的是把所有的學生信息都傳給指針p然后輸出. /統計學生人數的函數 int Information:count(struct student *head)/定義函數count()統計考生總數 if(head=NULL) /若指針head為空返回值為0 return(0); else return(1+count(head->next);/函數的遞歸調用
15、/插入學生的成績信息的函數 student *Information:insert( student *head) /插入新結點定義一個指向struct student的結構體指針函數*insert()用來添加考生信息. char str10; int flag=0; cout<<"t-<<請輸入新增學生成績信息>>-n"<<endl; p1=(student *)malloc(LEN); /使p1指向插入的新結點 cout<<" 姓名:" cin>>p1->name; /將輸
16、入的姓名存放到結構體名為p1的數組name中 cout<<" 性別:" cin>>p1->sex; cout<<" 學號(9位):" do cin>>str; if(atol(str)>99999999 | atol(str)<1) cout<<"對不起,請請正確輸入!n" else p1->id=atol(str); flag=1; while(flag=0); flag=0; cout<<" 語文成績:" do ci
17、n>>str; if(atoi(str)>100 | atoi(str)<1) cout<<"對不起,請輸入1-100之間的數字!n" else p1->score0=atoi(str); flag=1; while(flag=0); flag=0; cout<<" 外語成績:" do cin>>str; if(atoi(str)>100 | atoi(str)<1) cout<<"對不起,請輸入1-100之間的數字!n" else p1->
18、;score1=atoi(str); flag=1; while(flag=0); flag=0; cout<<" 數學成績:" do cin>>str; if(atoi(str)>100 | atoi(str)<1) cout<<"對不起,請輸入1-100之間的數字!n" else p1->score2=atoi(str); flag=1; while(flag=0); flag=0; cout<<" C+成績:" do cin>>str; if(atoi
19、(str)>100 | atoi(str)<1) cout<<"對不起,請輸入1-100之間的數字!n" else p1->score3=atoi(str); flag=1; while(flag=0); flag=0; p1->total=p1->score0+p1->score1+p1->score2+p1->score3;/計算總分 p2=head;/將頭指針賦給p2 if(head=NULL) /若沒調用次函數以前的頭指針head為空 head=p1;p1->next=NULL;/則將p1賦給頭指針h
20、ead并將p1所指結構體成員指針next賦空值 else while(p1->id>p2->id)&&(p2->next!=NULL) p3=p2;/p3指向原p2指向的結點 p2=p2->next; /p2后移一個結點 if(p1->id<=p2->id) if(head=p2) p1->next=head; head=p1; /插入到第一個結點之前 else p3->next=p1; p1->next=p2; /插入到p3所指結點之后 else p2->next=p1; p1->next=NULL
21、; /插入到尾結點之后 n+;/將學生人數加1 cout<<"t你輸入的學生信息已經成功插入"<<endl; return (head); /刪除學生信息函數 student *Information:cancel(student *head,long int num)/定義一個指向struct student的結構體指針函數*delete()用來刪除考生信息. if(head=NULL)/若調用次函數以前的頭指針head為空 return(head); else p1=head;/否則將頭指針賦給p1 while(num!=p1->id&am
22、p;&p1->next!=NULL)/尋找要刪除的結點當p1所指的學號不是輸入的學號并且p1所指的next指針不為空 p2=p1; p1=p1->next; /p2指向原p1指向的結點p1后移一個結點 if(num=p1->id)/如果輸入的學生準考證號是p1所指的學號結點找到后刪除 if(p1=head) head=p1->next;/如果head指針和p1指針相等則將下一個結點賦給指針head else p2->next=p1->next;/否則將p1所指結點賦給p2所指結點將要刪除的學生信息跳過去 cout<<" 刪除學號
23、為"<<num<<"的學生n" n-;/將學生人數減1 return(head);/將頭指針返回 /查找學生信息函數 student *Information:find(student *head,long int num) /定義一個指向struct student的結構體指針函數*find()用來查找學生信息. if(head=NULL)/若調用次函數以前的頭指針head為空 cout<<" 這是一個空表,請先輸入考生成績.n" return(head); else p1=head;/否則將頭指針賦給p1
24、 while(num!=p1->id&&p1->next!=NULL) /尋找結點當p1所指的學號不是輸入的學生學號并且p1所指的next指針不為空 p1=p1->next; /p1后移一個結點 if(num=p1->id)/如果要查找的學號是p1所指的學號 cout<<"-n" cout<<"學號 姓名 性別 院系 語文 英語 數學 C+ 平均分 總分 n" cout<<"-n" cout<<setw(8)<<p1->id &l
25、t;<setw(9)<<p1->name <<setw(8)<<p1->sex <<setw(13)<<p1->score0 <<setw(16)<<p1->score1 <<setw(10)<<p1->score2 <<setw(9)<<p1->score3 <<setw(6)<<p1->total/4.0 <<setw(11)<<p1->total<&l
26、t;endl; cout<<"-n" else cout<<" 沒找到學號為"<<num<<"的學生.n" /結點沒找到 return(head); void Information:inorder(student *head) /定義inorder()函數將考生的總分從大到小排列并輸出 int i,k,m=0,j; student *p20;/定義一個指向struct student的結構體指針數組p if(head!=NULL)/如果頭指針是空則繼續 m=count(head); c
27、out<<"-n" cout<<"學生成績統計表n" cout<<"-n" cout<<"學號 姓 名 性別 院系 語文 英語 數學 C+ 平均分 總分 名次n" cout<<"-n" p1=head; for(k=0;k<m;k+) pk=p1; p1=p1->next; for(k=0;k<m-1;k+) /選擇排序法 for(j=k+1;j<m;j+) if(pk->total<pj->t
28、otal) p2=pk; pk=pj; pj=p2; for(i=0;i<m;i+) cout<<setw(8)<<p1->id <<setw(9)<<p1->name <<setw(8)<<p1->sex <<setw(13)<<p1->score0 <<setw(16)<<p1->score1 <<setw(10)<<p1->score2 <<setw(9)<<p1->scor
29、e3 <<setw(6)<<p1->total/4.0 <<setw(11)<<p1->total<<endl; cout<<"-n" void Information:average(student *head) /求各科平均成績的函數 int k,m; float arg1=0,arg2=0,arg3=0,arg4=0; if(head=NULL)/如果頭指針是空則繼續 cout<<" 這是一個空表,請先輸入學生成績.n" else m=count(hea
30、d); p1=head; for(k=0;k<m;k+) arg1+=p1->score0; arg2+=p1->score1; arg3+=p1->score2; arg4+=p1->score3; p1=p1->next; arg1/=m;arg2/=m;arg3/=m;arg4/=m; cout<<"全班單科成績平均分n" cout<<"-n" cout<<" 語文平均分:"<<setw(7)<<arg1 <<"
31、 英語平均分:"<<setw(7)<<arg2 <<" 數學平均分:"<<setw(7)<<arg3 <<" C+平均分:"<<setw(7)<<arg4<<endl; cout<<"-n" void Information:save(student *head) /保存函數. ofstream out("data.txt",ios:out); out<<count(head
32、)<<endl; while(head!=NULL) out<<head->name<<"t" <<head->id<<"t"<<"t" <<head->sex<<"t" <<head->score0<<"t" <<head->score1<<"t" <<head->score2<
33、<"t" <<head->score3<<"t" <<head->total<<endl; head=head->next; student *Information:Read() /讀取函數的實現 int i=0; p1=p2=( student *)malloc(LEN); head=NULL; ifstream in("data.txt",ios:out); in>>i; if(i=0)cout<<" data.txt 文件
34、中的數據為空,請先輸入數據。"<<endl; return 0; else cout<<" "<<endl; for(;i>0;i-) p1=(student *)malloc(LEN); cin>>>>st.id>>st.sex >>st.score0>>st.score1>>st.score2>>st.score3 >>st.total; strcpy(p1->name,); p1->
35、id=st.id; strcpy(p1->sex,st.sex); p1->score0=st.score0; p1->score1=st.score1; p1->score2=st.score2; p1->score3=st.score3; p1->total=st.total; if(n=0)head=p1;/如果是輸入第一組學生考試信息就將指針p1賦給指針head else p2->next=p1;/否則將p1賦給p2所指結構體的next指針 p2=p1;/將指針p1賦給指針p2 n+; /將n的值加1 cout<<" &q
36、uot;<<p1->name<<"t" /顯示讀入數據 <<p1->id<<"t"<<"t" <<p1->sex<<"t" <<p1->score0<<"t" <<p1->score1<<"t" <<p1->score2<<"t" <<p1->sco
37、re3<<"t" <<p1->total<<endl; cout<<" "<<endl; / cout<<" 數據已經成功讀取完畢。"<<endl; p2->next=NULL; return (head); int main() /主函數. system("color 4f");/改變登陸界面顏色 Information person; student *head=NULL; char str5; int flag=0
38、; int choice; long int i; char admin20; char pass30; cout<<"請輸入賬號n" cin>>admin; cout<<"請輸入密碼n" cin>>pass; if(strcmp(admin,"1111")=0&&strcmp(pass,"222")=0) do cout<<" *"<<endl; cout<<" 學生信息管理系統主菜單
39、界面 *"<<endl ; cout<<" "<<endl; cout<<""<<endl; cout<<" .輸入學生信息 "<<endl; cout<<" .顯示學生信息 "<<endl; cout<<" .排序統計成績 "<<endl; cout<<" .查找學生信息 "<<endl; cout<<"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 車輛過戶手續辦理及產權轉移稅務籌劃合同
- 食品安全倉儲租賃服務及質量監管協議
- 車輛租賃公司售后服務合同范本
- 陳紈婚姻解除協議及財產分配合同
- 2025建筑工程合同管理法規詳解
- 2025年政府土地使用權收購協議范本
- 2025《家具銷售合同范本》
- 2025年1月河南高考適應性測試歷史試題及答案
- 2025合同范本酒店合作協議樣本
- 死亡邊境測試題及答案
- 2025設備租賃合同版本范文
- 2025年四川省成都市初中學業水平考試生物試題(無答案)
- 醫院感染教學課件
- 民航危險品運輸典型案例55課件
- 倉庫管理制度及流程
- 四川省綿陽市名校聯盟2025屆八年級物理第二學期期末復習檢測試題含解析
- 2025-2030中國烘焙食品行業市場發展分析與發展趨勢及投資風險研究報告
- 2024北京西城區五年級(下)期末數學試題及答案
- AI技術在智能護理中的應用和挑戰
- 2025年瑜伽教練資格證考試題庫:瑜伽教練職業培訓教學案例試題
- 陜西2025年陜西楊凌示范區事業單位招聘15人筆試歷年參考題庫附帶答案詳解
評論
0/150
提交評論