




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、學(xué)生信息管理系統(tǒng)(附代碼)題目要求:設(shè)計(jì)一個(gè)類 CStudent ,類中包含一個(gè)學(xué)生的基本數(shù)據(jù)如下:編號(hào),姓名,性別,年齡,數(shù)學(xué)成績,計(jì)算機(jī)成績,外語成績。 并假設(shè)編號(hào)為整數(shù),且從 1 號(hào)往后連續(xù)編碼;姓名為字符串,性別為字符。如: 1 LiPing m 18 89 98 94請(qǐng)采用 binary 文件形式,并使用隨機(jī)讀寫處理方式,對(duì)自定義 CStudent 類的對(duì)象數(shù)據(jù)進(jìn)行 存儲(chǔ)與讀寫處理(即是說,總按具有連續(xù)編碼的編號(hào) num 為“序 ”來對(duì)文件中的各對(duì)象數(shù)據(jù) 進(jìn)行隨機(jī)讀寫處理) 。并設(shè)計(jì)該類的成員函數(shù),而且對(duì)輸出運(yùn)算符“<<”進(jìn)行重載,使該運(yùn)算符能夠完成將一個(gè)學(xué)生的信息輸出到
2、屏幕上。要求成員函數(shù)完成以下功能:(1)從鍵盤輸入一個(gè)學(xué)生的有關(guān)信息, 并將它們存入到數(shù)據(jù)文件中 (按編號(hào)來確定寫出位 置)。(2)按編號(hào)對(duì)學(xué)生信息進(jìn)行檢索并將檢索結(jié)果顯示在屏幕上。(3)按姓名對(duì)學(xué)生信息進(jìn)行檢索并將檢索結(jié)果顯示在屏幕上。(4)計(jì)算某編號(hào)學(xué)生的總成績與平均成績。(5)列出所有總成績超過n分的性別為s同學(xué)的有關(guān)信息(n,s由用戶從鍵盤輸入)。Code:*2. * 名稱 :student.cpp*3. *描述:學(xué)生管理程序*4. * 功能:添加,修改,按條件查詢學(xué)生信息 *5. *環(huán)境: Fedora Linux 11 & GCC & x86 *6. * 備注:da
3、velv 第一次 Class 于 2010-01-10*7. * 更新:新建了可復(fù)用的搜索模板 searchframe *8.*9.10. #include <iostream>11. #include <cstring>12. #include <vector>5.56.#include <fstre
4、am> #include <cstdlib>using namespace std;#define CIN_LEN1024/ 緩沖區(qū)長度#define"data"/ 數(shù)據(jù)文件名/ 結(jié)構(gòu)和類 / / struct data/ 學(xué)生個(gè)人信息int id;/ 學(xué)號(hào)char name20;/ 名字 char major20;/ 專業(yè) char sex;/ 性別 double ch,en,ma;/ 成績 int grade;/ 年級(jí);class CStudent protected:bool altered;/ 是否修改 data info;/ 學(xué)生信息 publi
5、c:static int nowid ;/ 新學(xué)生自增 id static void displayhead();/ 顯示表頭 static void displayshorthead();/ 顯示短表頭 CStudent();/ 構(gòu)造 void displayinfo();/ 顯示全部學(xué)生信息 void displayshortinfo();/ 顯示學(xué)生短信息 double getsum();/ 取總成績 double getave();/ 取得平均分 double getch();/ 取語文成績 double geten();/ 取外語成績 double getma();/ 取數(shù)學(xué)成績 i
6、nt set(bool isnew);/ 設(shè)置學(xué)生信息 int getgrade();/ 取年級(jí) int getid();/ 取學(xué)號(hào) bool isaltered();/ 取是否修改 char getsex();/ 取性別 char* getname();/ 取姓名 char* getmajor();/ 取專業(yè) data* getinfo();/ 取學(xué)生全部信息57. /定義友元函數(shù)以便重載運(yùn)算符58. friend ostream& operator<<(ostream&,const CStudent&);59. friend istream& o
7、perator>>(istream&,CStudent&);60.61. ;62.63. int CStudent:nowid = 1;/ 初始化類靜態(tài)成員64.65. CStudent:CStudent()/ 基類構(gòu)造66. 67. info.id=CStudent:nowid+;/ 子增 id68. strcpy(,"None");/ 名字69. info.ch=0;/ 語文成績70. info.en=0;/ 外語成績71. info.ma=0;/ 數(shù)學(xué)成績72. info.grade=1;/ 年級(jí)73. altered=f
8、alse;/ 未被修改74. 75.76. int CStudent:getgrade()77. 78. return info.grade;79. 80.81. double CStudent:getsum()82. 83. return info.ch+info.en+info.ma;84. 85. double CStudent:getave()86. 87. return (info.ch+info.en+info.ma)/3;88. 89. double CStudent:getch()90. 91. return info.ch;92. 93.94. double CStudent
9、:geten()95. 96. return info.en;97. 98.99. double CStudent:getma()100. 101. return info.ma;102. 103.104. int CStudent:getid()105. 106. return info.id;107. 108.109. char CStudent:getsex()110. 111. return info.sex;112. 113.114. char * CStudent:getname()115. 116. return ;117. 118. bool CStudent
10、:isaltered()119. 120. return altered;121. 122. data *CStudent:getinfo()123. 124. return &info;125. 126. void CStudent:displayinfo()127. 128. cout<<*this<<"t"<<getsum()<<"t"<<getave()<<endl;/ 利用重載運(yùn)算符輸出129. 130. void CStudent:displayshortinfo
11、()131. 132. cout << *this<<endl;133. 134. void CStudent:displayhead()135. 136. cout<<"nt學(xué)號(hào)t姓名t性別t專業(yè)t年級(jí)t中文t英文t數(shù)學(xué)t總分t平均分n"137. 138. void CStudent:displayshorthead()139. 140. cout<<"nt學(xué)號(hào)t姓名t性別t專業(yè)t年級(jí)t中文t英文t數(shù)學(xué)n"141. 142. int CStudent:set(bool isalter)143. 144.
12、cout<<" 輸入學(xué)生信息 :n"145.146.147.displayshortinfo();displayshorthead(); if (isalter)148. cout<<"t"<<info.id<<"t"149. cin.clear();150. cin>> *this;/ 從標(biāo)準(zhǔn)輸入獲取學(xué)生信息151. altered=true;/ 已修改152.153. if(cin.fail()154. 155. cout<<" 錄入失敗n"
13、;156. cin.clear();157. cin.ignore(CIN_LEN,'n');/ 這兩行是用來清空輸入緩沖158. return -1;159. 160. else161. 162. cout<<" 錄入成功 n"163. return 1;164. 165. 166.167. /重載輸出符168. ostream &operator<<(ostream& out,const CStudent &right)169. 170. /輸出學(xué)生的全部信息171. out <<"t
14、"<<.id<<"t"<<<<"t"<<.sex<<"t"172. <<.major<<"t"<<.grade<<"t"<<.ch<<"t"173. <<.en&l
15、t;<"t"<<.ma;174. return out;175. 176. /重載輸入符177. istream& operator>>(istream& in,CStudent& right)178. 179. /輸入除 ID 外的其他信息180. in >>>>.sex>>.major181. >>.grade>>.ch>&
16、gt;.en>>.ma;182. return in;183. 184.185. /186. / 初始化函數(shù) /187. /188. int initial(vector<CStudent*> &stu)fstream 輸入文件 CStudent *p;00022.2
17、32.(, fstream:in|fstream:binary);/ 二進(jìn)制輸入打開if (! 文件是否打開成功return -1;while( ()!=EOF )/ 是否到文件末尾 p=new CStudent();/ 新建一個(gè)學(xué)生對(duì)象(char*) p->getinfo(),sizeof(data);/ 讀入學(xué)生對(duì)象 if()/ 檢查讀入是否失敗return -2; stu.push_back(p);/ 對(duì)象加入 vector if(!stu.empty()/ 如果從文件讀入了對(duì)象id+1CStudent:now
18、id = stu.back()->getid()+1;/ 則自增 id 設(shè)置為最后一個(gè)學(xué)生 ();/關(guān)閉 return stu.size();/ 返回對(duì)象個(gè)數(shù) / / 信息增加函數(shù) / void insert(vector<CStudent*> &stu)char c='y'/ 輸入控制字符int flag = 1;/ 標(biāo)志位, 1 表示新增成功CStudent *p=new CStudent();while(c != 'n')/ 是否繼續(xù)新增flag = p->set(false);/ 設(shè)置學(xué)生信息 if( flag = 1 )
19、/ 如果設(shè)置成功 stu.push_back(p);/ 對(duì)象加入 vector p = new CStudent();/ 新建下一個(gè)對(duì)象cout << " 是否繼續(xù)添加學(xué)生 (any/n)?" cin.clear();cin.ignore(CIN_LEN,'n'); cin.get(c);233. 234. /刪除最后一個(gè)新建的對(duì)象,因?yàn)闆]有使用它235. delete p;236. CStudent:nowid-;237. 238.239. /240. / 查詢?nèi)啃畔⒑瘮?shù) /241. /242. int comparebynone(const
20、 void *a, const void *b )243. 244. return 0;245. 246. /247. / 按學(xué)號(hào)比較函數(shù) /248. /249. int comparebyid(const void *a, const void *b )250. 251. return *(const int *)a - (CStudent *)(b)->getid();252. 253. /254. / 按姓名比較函數(shù) /255. /256. int comparebyname(const void *a, const void *b )257. 258. return strcmp
21、(const char *)a, (const char *)(CStudent *)b)->getname();259. 260. /261. / 按年級(jí)比較函數(shù) /262. /263. int comparebygrade(const void *a, const void *b)264. 265. return (*(const int *)a - (CStudent *)b)->getgrade();266. 267. /268. / 按總分和性別比較函數(shù) /269. /270. int comparebymarkandsex(const void *a, const vo
22、id *b)271. 272. double mark;273. char sex;274. sscanf(const char*)a,"%lf%c",&mark,&sex);275. return !(276. ( (CStudent*)b)->getsum() >= mark )277. && ( (sex ='n') | (sex = (CStudent*)b)->getsex() )278. );279.280.281. 282. /283. / 搜索模板 /284. /285. template &
23、lt;typename T>286. void searchframe(const char *info, T &condition ,vector<CStudent *> &stu, (*compare)(const void *a, const void *b) ,bool isalter)287. int289.290.291.char c='y' int flag; while(c != 'n') 292.293.294.295.cin.clear();296.297.298.299.300.301.if(info !=
24、 NULL)cout<< " 輸入 "<<info<<":" /cin.ignore(CIN_LEN,'n'); cin>>condition;302.303.304.305.306.307.308.309.310.311.312.313.314.315.if(cin.fail()cout << " 輸入錯(cuò)誤 n"else/ 遍歷 vector 查找 for(vector<CStudent*>:size_type ix =flag=0; ix!=s
25、tu.size(); +ix) /判斷是 name 是否相等if(compare(&condition,stuix) = 0) if( isalter ) stuix->set(isalter);else288.318.319.316.317.if(flag = 0)CStudent:displayhead();stuix->displayinfo();320.321.322.323.324.325.326.327.328.329.330.331.332.333.334.335.336.337.338. 339.flag = 1; if (flag = 0)/ 沒有查到 c
26、out<<"沒有”; if(info = NULL) cout<<" 符合條件 ";else cout<<info<<" 為 "<<condition;cout<<" 的學(xué)生 n";cout << " 是否繼續(xù) (any/n)?"; cin.clear();cin.ignore(CIN_LEN,'n'); cin.get(c);340. /341. / 信息檢索函數(shù) /342. /343. void fet
27、ch(vector<CStudent*> &stu)344. 345. int choose,id,grade;346. char name20,markandsex20;347.347. while (true)348. 349. cout << "nt1. 顯示全部學(xué)生信息 n"351."t2. 按學(xué)號(hào)查學(xué)生信息n"352."t3. 按姓名查學(xué)生信息n"353."t4. 按年級(jí)查學(xué)生信息n"354."t5. 按成績和性別查詢n"355."t6. 返回
28、上級(jí)菜單 nn"356. lchoose:357. cout<<" 輸入您的選擇 :";358. choose=0;359. cin>>choose;360. switch(choose)361. 362. case1 :searchframe(NULL,choose,stu,comparebynone,false);break;363. case2 :searchframe(”學(xué)號(hào) ”,id,stu,comparebyid,false); break;364. case 3 :searchframe(”姓名",name,stu,
29、comparebyname,false); break;365. case 4 :searchframe(”年級(jí) ”,grade,stu,comparebygrade,false); break;366. case 5 :searchframe(" 分 數(shù) 和 性 別 ",markandsex,stu,comparebymarkandsex,false); break;367.case 6 :return ;368.default: cout<<"輸入有誤 n"cin.clear();cin.ignore(CIN_LEN,'n'
30、;);gotolchoose;369.370.371. 372. /373. /信息保存函數(shù)/374. /375. int save(vector<CStudent*> &stu)376. 377. fstream file;378.378. (, fstream:out|fstream:binary);/二進(jìn)制寫打開文件379. if (! 判斷打開是否成功380. return -1;381. /遍歷全部對(duì)象382. for(vector<CStudent*>:size_type ix =0;ix!=stu.size(); +ix)383. /判斷當(dāng)前對(duì)象是
31、否已修改384. if(stuix->isaltered()385. / 修改了則寫入文件386. (ix*sizeof(data);387. (char*) stuix->getinfo(),sizeof(data);388. / 寫入是否成功389. if()390. return -2;391. 393.392. 393. ();394. return 0;395. 398.396. /397. /主函數(shù)/398. /402.399. int main()400. 401. vector<CStudent *> stu ;406.407.system("clear");/ 清屏408./讀入
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年船用動(dòng)力堆及配套產(chǎn)品合作協(xié)議書
- 第18課 從九一八事變到西安事變(教學(xué)設(shè)計(jì))-2024-2025學(xué)年浙江省部編版歷史與社會(huì)八年級(jí)上冊(cè)
- 財(cái)務(wù)管理模擬試題與答案
- 第19課《大雁歸來》教學(xué)設(shè)計(jì) 2024-2025學(xué)年統(tǒng)編版語文七年級(jí)上冊(cè)
- 14《母雞》教學(xué)設(shè)計(jì)-2023-2024學(xué)年統(tǒng)編版四年級(jí)語文下冊(cè)
- 3《迢迢牽牛星》教學(xué)設(shè)計(jì)-2023-2024學(xué)年六年級(jí)下冊(cè)語文統(tǒng)編版
- 人教版七年級(jí)歷史與社會(huì)上冊(cè)2.1.2 海洋對(duì)人類的影響教學(xué)設(shè)計(jì)
- 2025年哈爾濱城市職業(yè)學(xué)院單招職業(yè)技能測(cè)試題庫審定版
- Unit7 What's the highest mountain in the world.寫作課教學(xué)設(shè)計(jì)-2024-2025學(xué)年人教版英語八年級(jí)下冊(cè)
- 江蘇省泰州市聯(lián)盟五校2023-2024學(xué)年高二上學(xué)期期中考試地理試卷(解析版)
- 《汽車電氣設(shè)備構(gòu)造與維修》 (第4版) 課件 第四章 發(fā)動(dòng)機(jī)電器
- 部編版語文六年級(jí)下冊(cè)第五單元大單元教學(xué)設(shè)計(jì)核心素養(yǎng)目標(biāo)
- 售后服務(wù)的常見挑戰(zhàn)及應(yīng)對(duì)策略
- 成人鼾癥的診斷與治療
- 智能環(huán)境設(shè)備的智能監(jiān)測(cè)與環(huán)境保護(hù)
- 2024年中車株洲電力機(jī)車研究所有限公司招聘筆試參考題庫含答案解析
- 文物保護(hù)概論課件
- 巴黎歐萊雅中國員工手冊(cè)
- 貨幣的起源發(fā)展演變和貨幣的面值課件
- 建筑業(yè)10項(xiàng)新技術(shù)概述
- 合肥的文化民俗
評(píng)論
0/150
提交評(píng)論