《C++大作业:学生信息管理系统(附代码).docx》由会员分享,可在线阅读,更多相关《C++大作业:学生信息管理系统(附代码).docx(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、学生信息管理系统(附代码)题目要求: 设计一个类CStudent,类中包含一个学生的基本数据如下:编号,姓名,性别,年龄,数学成果,计算机成果,外语成果。并假设编号为整数,且从 1号往后连续编码;姓名为字符串,性别为字符。如:1 LiPing m 18 89 98 94请接受binary文件形式,并运用随机读写处理方式,对自定义CStudent类的对象数据进行 存储与读写处理(即是说,总按具有连续编码的编号num为“序”来对文件中的各对象数据 进行随机读写处理)。并设计该类的成员函数,而且对输出运算符进行重载,使该运算 符能够完成将一个学生的信息输出到屏幕上。要求成员函数完成以下功能:(1)从
2、键盘输入一个学生的有关信息,并将它们存入到数据文件中(按编号来确定写出位 置)。(2)按编号对学生信息进行检索并将检索结果显示在屏幕上。(3)按姓名对学生信息进行检索并将检索结果显示在屏幕上。(4)计算某编号学生的总成果与平均成果。(5)列出全部总成果超过n分的性别为s同学的有关信息(n,s由用户从键盘输入)。Code:2. *名称:student.cpp*3. *描述:学生管理程序*4. *功能:添加,修改,按条件查询学生信息*5. *环境:Fedora Linux 11 & GCC & x86 *6. *备注:davelv 第一次 Class 于 2023-01-10*7. *更新:新建了
3、可复用的搜寻模板searchframe */.、/9.10. #include 11. #include 12. #include 364. case 3 :searchframe(姓名”,name,stu,comparebyname,false); break;365. case 4 :searchframe(年级”,grade,stu,comparebygrade,false); break;366. case 5 :searchframe(n分数和性别n,markandsex,stu,comparebymarkandsex,false); break;367. case 6 :retur
4、n ;368. default: cout输入有误n;cin.clear();cin.ignore(CIN_LEN,n);gotoIchoose;369. 370. 371. 372. Illlllllllllllllllllllllllllllll373. /信息保存函数374. Illlllllllllllllllllllllllllll375. int save(vector &stu)376. 377. fstream file;378.379. file.open(FILENAME, fstream:out|fstream:binary);二进制写打开文件380. if(!file)
5、推断打开是否成功381. return -1;382. 遍历全部对象383. for(vector:size_type ix =0; ix!=stu.size(); +ix)384. 推断当前对象是否已修改385. if(stuix-isaltered()386. 修改了则写入文件387. file.seekp(ix*sizeof(data);388. file.write(char*) stuix-getinfo(),sizeof(data);389. 写入是否成功390. if(file.fail()391. return -2;392. 393.394. 395. file.close(
6、);396. return 0;397. 398.399. Illllllllllllllllllllllllllllll400.主函数401. Illlllllllllllllllllllllllllll402.403. int main()404. (405. vector stu ;406.407.system(”clear);/清屏408.读入数据文件409.if(initial(stu)0)410.(411.cout”初始化失败,请检查数据文件“4111是否完好珀”;412.return -1;413.)414.415.int choose;416.主菜单循环417.while(tr
7、ue)418.(419.cout nn*学生信息管理系统*nn”420.n1 录入信息n”421.IV2 .修改信息n”422.113.检索学生n”423.it4.保存数据n”424.ft5.保存退出n”425.n6.不保存退出nn426.” *modify by davelv*n”;427.Ichoose:428.coutvv”输入您的选择:;429.choose=0;430.cinchoose;431.switch(choose)432.(433.case 1 :insert(stu);break;434.case 2 :searchframe(学号:choose,stu,compareb
8、yid,true);break;435.case 3 :fetch(stu);break;436.case 4 :if(save(stu) 0 )437.(438.cout 保存失败,请检查数据文件 FILEN AME是否完好n”;439.;break;440.case 5 :if(save(stu) 0 )441.(442.coutvv”保存失败,请检查数据文件 FILENAME是否完好n”;443.return 0;444.case 6 :return 0;445.default:coutn 输入有误 rT;cin.clear();cin.ignore(CIN_LEN,n);gotoIch
9、oose;446.)447. 448. return 0;449. 13. #include 14. #include 15. using namespace std;16. #define CIN_LEN1024缓冲区长度17. #define FILENAME “data”/数据文件名18. Illllllllllllllllllllllllllllllllllll19. /结构和类20. Illllllllllllllllllllllllllllllllll21. struct data/学生个人信息22. 23. int id;学号24. char name20;名字25. char
10、major20;专业26. char sex;性别27. double ch,en,ma;成果28. int grade;年级29. ;30. class CStudent31. 32. protected:33. bool altered;是否修改34. data info;学生信息35. public:36. static int nowid ;新学生自增 id37. static void displayhead();显示表头38. static void displayshorthead();显示短表头39. CStudent();构造40. void displayinfo();显示
11、全部学生信息41. void displayshortinfo();显示学生短信息42. double getsum();取总成果43. double getave();取得平均分44. double getch();取语文成果45. double geten();取外语成果46. double getma();取数学成果47. int set(bool isnew);/设置学生信息48. int getgrade。;/取年级49. int getid();取学号50. bool isaltered();取是否修改51. char getsex();取性别52. char* getname()
12、;取姓名53. char* getmajor();取专业54. data* getinfo。;/取学生全部信息55. 定义友元函数以便重载运算符56. friend ostream& operator(ostream&,const CStudent&);57. friend istream& operator(istream&,CStudent&);58. ;59. int CStudent:nowid = 1;/初始化类静态成员60. CStudent:CStudent()基类构造61. 62. info.id=CStudent:nowid+;/子增 id63. strcpy(info.na
13、meJNone”); 名字64. info.ch=0; 语文成果65. info.en=0;/外语成果66. info.ma=0; 数学成果67. info.grade= 1;/年级68. altered=false;/未被修改69. )70. int CStudent: :getgrade()71. 72. return info.grade;73. 74. double CStudent: :getsum()75. 76. return info.ch+info.en+info.ma;77. )78. double CStudent:getave()79. 80. return (info
14、.ch+info.en+info.ma)/3;81. 82. double CStudent:getch()83. 84. return info.ch;85. 86. double CStudent:geten()87. 88. return info.en;89. 90. double CStudent:getma()91. (101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144r
15、eturn info.ma;int CStudent:getid()(return info.id;)char CStudent: :getsex()return info.sex;)char * CStudent: :getname()(return info.name;)bool CStudent:isaltered()(return altered;data *CStudent:getinfo()(return &info;)void CStudent: :displayinfo()(coutvv*thisvvtvvgetsum()vvtvvgetave()vvendl;利用重载运算符输
16、出)void CStudent:displayshortinfo()(cout *thisendl;)void CStudent: :displayhead()(coutMnt学号t姓名t性别t专业t年级t中文t英文t数学t总分t平均分n”; )void CStudent: :displayshorthead()(coutnnt学号t姓名t性别t专业t年级t中文t英文t数学n”;)int CStudent:set(bool isalter)cout”输入学生信息:n”;145146147148149150151152153154155156157158159160161162163164165
17、166167168169170171172173174175176177178179180181182183184185186187188displayshorthead();if (isalter)displayshortinfo();cout,t,info.idntn;cin.clear();cin *this;从标准输入获得学生信息altered=true; 已修改if(cin.fail()cout ”录入失败n” ;cin.clearQ;cin.ignore(CINJLEN,iT);这两行是用来清空输入缓冲 return -1;)else(cout ”录入成功n”;return 1;)
18、重载输出符ostream &operator(ostream& out,const CStudent &right)(输出学生的全部信息out ,tnright.info.id,t,right.info.name,tnright.info.sexnt right.info.majornt,right.info.grade,tnright.info.chntn right.info.enntnright.info.ma;return out;)/重载输入符istream& operator(istream& in,CStudent& right)(输入除ID外的其他信息right.info.gr
19、aderight.info.chright.info.enright.info.ma;return in;)lllllllllllllllllllllllllllllllll初始化函数llllllllllllllllllllllllllllllllint initial(vector &stu)189. (190. fstream file;输入文件191. CStudent *p;192. file.open(FILENAME, fstream:in|fstream:binary);二进制输入打开193. if(!file)文件是否打开成功194. return -1;195. while(
20、 file.peek()!=EOF )是否到文件末尾196. (197. p=new CStudent。;/新建一个学生对象198. file.read(char*) p-getinfo(),sizeof(data);读入学生对象199. if(file.fail()检查读入是否失败200. return -2;201. stu.push_back(p);对象力口入 vector202. 203. if(!stu.empty()假如从文件读入了对象204. CStudent:nowid = stu.back()-getid()+l;则自增id设置为最终一个学生 id+1205. file.cl
21、ose();关闭206. return stu.size。;/返回对象个数207. 208. IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII209. /信息增加函数210. IIIIIIIIIIIIIIIIIIIIIIIIIIIIIII211. void insert(vector &stu)212. 213. char c=y;/输入限制字符214. int flag = 1;标记位,1表示新增成功215. CStudent *p=new CStudent();216. while(c != H)是否接着新增217. 218. flag = p-set(false); 设置
22、学生信息219. if( flag = 1 )假如设置成功220. 221. stu.push_back(p);对象力口入 vector222. p = new CStudent。;/新建下一个对象223. 224. cout ”是否接着添加学生(any/n)?”;225. cin.clear();226. cin.ignore(CIN_LEN;n,);227. cin.get(c);228. 229. 删除最终一个新建的对象,因为没有运用它230. delete p;231. CStudent: :nowid;232. 233. Illllllllllllllllllllllllllllll
23、234. /查询全部信息函数235. /236. int comparebynone(const void *a, const void *b )237. 238. return 0;239. )240. Illlllllllllllllllllllllllllllll241. /按学号比较函数242. Illlllllllllllllllllllllllllll243. int comparebyid(const void *a, const void *b )244. 245. return *(const int *)a - (CStudent *)(b)-getid();246. 24
24、7. Illllllllllllllllllllllllllllll248. /按姓名比较函数249. Illlllllllllllllllllllllllllll250. int comparebyname(const void *a, const void *b )251. 252. return strcmp(const char *)a, (const char *)(CStudent *)b)-getname();253. 254. Illlllllllllllllllllllllllllllll255. /按年级比较函数256. Illlllllllllllllllllllllll
25、llll257. int comparebygrade(const void *a, const void *b)258. (259. return (*(const int *)a - (CStudent *)b)-getgrade();260. 261. Illllllllllllllllllllllllllllllllllll262. /按总分和性别比较函数263. Illllllllllllllllllllllllllllllllll264. int comparebymarkandsex(const void *a, const void *b)265. 266. double ma
26、rk;267. char sex;268. sscanf(const char)a,n%lf%cH,&mark,&sex);269. return !(270. (CStudent*)b)-getsum() = mark )271. & (sex =h) | (sex = (CStudent)b)-getsex()272. );273. )274. Illllllllllllllllllllllllllllll275. /搜寻模板276. Illllllllllllllllllllllllllll277. template 278. void searchframe(const char *i
27、nfb, T &condition ,vector &stu, int (*compare)(const void *a, const void *b) ,bool isalter)279. 280. char c-yf;281. int flag;282. while(c != h)283. (284. cin.clear();285. if(info != NULL)286. (287. cout 输入info1”;288. /cin.ignore(CIN_LEN;n);289. cincondition;290. 291. if(cin.fail()292. 293. cout 输入错误
28、n”;294. 295. else296. 297. /遍历vector查找298. for(vector:size_typeix=flag=0;ix!=stu.size();+ix)299. 推断是name是否相等300. if(compare(&condition,stuix) = 0)301. 302. if( isalter)303. stuix-set(isalter);304. else305. (306. if(flag = 0)307. CStudent: :displayhead();308. stuix-displayinfo();309. flag= 1;310.311.
29、 312. 313. if(flag=0)/没有查到314. (315. cout“没有”;316. if(info 二二 NULL)317. cout”符合条件”;318. else319. coutvvinfo 为condition;320. cout” 的学生 n”;321. 322. 323. cout ”是否接着(any/n)?”;324. cin.clear();325. 1);326. cin.get(c);327. 328. 339.340. Illlllllllllllllllllllllllllllll341. /信息检索函数342. Illllllllllllllllll
30、lllllllllll343. void fetch(vector &stu)344. 345. int choose,id,grade;346.char name20,markandsex20;347.348. while (true)349. (350. cout ”ntl .显示全部学生信息n”351. ”t2.按学号查学生信息n”352. %3.按姓名查学生信息n”353. ”t4.按年级查学生信息n”354. ”t5.按成果和性别查询n”355. 飞6 .返回上级菜单nn”;356. Ichoose:357. cout”输入您的选择:;358. choose=O;359. cinchoose;360. switch(choose)361. 362. case1 :searchframe(NULL,choose,stu,comparebynone,false); break;363. case2 :searchframe(学号”,id,stu,comparebyid,false);break;