C大作业学生信息管理系统附代码.docx

上传人:b****4 文档编号:4140417 上传时间:2023-05-06 格式:DOCX 页数:19 大小:20.11KB
下载 相关 举报
C大作业学生信息管理系统附代码.docx_第1页
第1页 / 共19页
C大作业学生信息管理系统附代码.docx_第2页
第2页 / 共19页
C大作业学生信息管理系统附代码.docx_第3页
第3页 / 共19页
C大作业学生信息管理系统附代码.docx_第4页
第4页 / 共19页
C大作业学生信息管理系统附代码.docx_第5页
第5页 / 共19页
C大作业学生信息管理系统附代码.docx_第6页
第6页 / 共19页
C大作业学生信息管理系统附代码.docx_第7页
第7页 / 共19页
C大作业学生信息管理系统附代码.docx_第8页
第8页 / 共19页
C大作业学生信息管理系统附代码.docx_第9页
第9页 / 共19页
C大作业学生信息管理系统附代码.docx_第10页
第10页 / 共19页
C大作业学生信息管理系统附代码.docx_第11页
第11页 / 共19页
C大作业学生信息管理系统附代码.docx_第12页
第12页 / 共19页
C大作业学生信息管理系统附代码.docx_第13页
第13页 / 共19页
C大作业学生信息管理系统附代码.docx_第14页
第14页 / 共19页
C大作业学生信息管理系统附代码.docx_第15页
第15页 / 共19页
C大作业学生信息管理系统附代码.docx_第16页
第16页 / 共19页
C大作业学生信息管理系统附代码.docx_第17页
第17页 / 共19页
C大作业学生信息管理系统附代码.docx_第18页
第18页 / 共19页
C大作业学生信息管理系统附代码.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C大作业学生信息管理系统附代码.docx

《C大作业学生信息管理系统附代码.docx》由会员分享,可在线阅读,更多相关《C大作业学生信息管理系统附代码.docx(19页珍藏版)》请在冰点文库上搜索。

C大作业学生信息管理系统附代码.docx

C大作业学生信息管理系统附代码

学生信息管理系统(附代码)

题目要求:

设计一个类CStudent,类中包含一个学生的基本数据如下:

编号,姓名,性别,年龄,数学成绩,计算机成绩,外语成绩。

并假设编号为整数,且从1号往后连续编码;姓名为字符串,性别为字符。

如:

1LiPingm18899894

请采用binary文件形式,并使用随机读写处理方式,对自定义CStudent类的对象数据进行存储与读写处理(即是说,总按具有连续编码的编号num为“序”来对文件中的各对象数据进行随机读写处理)。

并设计该类的成员函数,而且对输出运算符“<<”进行重载,使该运算符能够完成将一个学生的信息输出到屏幕上。

要求成员函数完成以下功能:

(1)从键盘输入一个学生的有关信息,并将它们存入到数据文件中(按编号来确定写出位置)。

(2)按编号对学生信息进行检索并将检索结果显示在屏幕上。

(3)按姓名对学生信息进行检索并将检索结果显示在屏幕上。

(4)计算某编号学生的总成绩与平均成绩。

(5)列出所有总成绩超过n分的性别为s同学的有关信息(n,s由用户从键盘输入)。

Code:

1./****************************************

2.*名称:

student.cpp*

3.*描述:

学生管理程序*

4.*功能:

添加,修改,按条件查询学生信息*

5.*环境:

FedoraLinux11&GCC&x86*

6.*备注:

davelv第一次Class于2010-01-10*

7.*更新:

新建了可复用的搜索模板searchframe*

8.****************************************/

9.

10.#include

11.#include

12.#include

13.#include

14.#include

15.

16.usingnamespacestd;

17.#defineCIN_LEN1024//缓冲区长度

18.#define"data"//数据文件名

19./////////////////////////////////////

20.//结构和类//

21.///////////////////////////////////

22.structdata//学生个人信息

23.{

24.intid;//学号

25.charname[20];//名字

26.charmajor[20];//专业

27.charsex;//性别

28.doublech,en,ma;//成绩

29.intgrade;//年级

30.};

31.

32.classCStudent

33.{

34.protected:

35.boolaltered;//是否修改

36.datainfo;//学生信息

37.public:

38.staticintnowid;//新学生自增id

39.staticvoiddisplayhead();//显示表头

40.staticvoiddisplayshorthead();//显示短表头

41.CStudent();//构造

42.voiddisplayinfo();//显示全部学生信息

43.voiddisplayshortinfo();//显示学生短信息

44.doublegetsum();//取总成绩

45.doublegetave();//取得平均分

46.doublegetch();//取语文成绩

47.doublegeten();//取外语成绩

48.doublegetma();//取数学成绩

49.intset(boolisnew);//设置学生信息

50.intgetgrade();//取年级

51.intgetid();//取学号

52.boolisaltered();//取是否修改

53.chargetsex();//取性别

54.char*getname();//取姓名

55.char*getmajor();//取专业

56.data*getinfo();//取学生全部信息

57.//定义友元函数以便重载运算符

58.friendostream&operator<<(ostream&,constCStudent&);

59.friendistream&operator>>(istream&,CStudent&);

60.

61.};

62.

63.intCStudent:

:

nowid=1;//初始化类静态成员

64.

65.CStudent:

:

CStudent()//基类构造

66.{

67.info.id=CStudent:

:

nowid++;//子增id

68.strcpy(info.name,"None");//名字

69.info.ch=0;//语文成绩

70.info.en=0;//外语成绩

71.info.ma=0;//数学成绩

72.info.grade=1;//年级

73.altered=false;//未被修改

74.}

75.

76.intCStudent:

:

getgrade()

77.{

78.returninfo.grade;

79.}

80.

81.doubleCStudent:

:

getsum()

82.{

83.returninfo.ch+info.en+info.ma;

84.}

85.doubleCStudent:

:

getave()

86.{

87.return(info.ch+info.en+info.ma)/3;

88.}

89.doubleCStudent:

:

getch()

90.{

91.returninfo.ch;

92.}

93.

94.doubleCStudent:

:

geten()

95.{

96.returninfo.en;

97.}

98.

99.doubleCStudent:

:

getma()

100.{

101.returninfo.ma;

102.}

103.

104.intCStudent:

:

getid()

105.{

106.returninfo.id;

107.}

108.

109.charCStudent:

:

getsex()

110.{

111.returninfo.sex;

112.}

113.

114.char*CStudent:

:

getname()

115.{

116.returninfo.name;

117.}

118.boolCStudent:

:

isaltered()

119.{

120.returnaltered;

121.}

122.data*CStudent:

:

getinfo()

123.{

124.return&info;

125.}

126.voidCStudent:

:

displayinfo()

127.{

128.cout<<*this<<"\t"<

129.}

130.voidCStudent:

:

displayshortinfo()

131.{

132.cout<<*this<

133.}

134.voidCStudent:

:

displayhead()

135.{

136.cout<<"\n\t学号\t姓名\t性别\t专业\t年级\t中文\t英文\t数学\t总分\t平均分\n";

137.}

138.voidCStudent:

:

displayshorthead()

139.{

140.cout<<"\n\t学号\t姓名\t性别\t专业\t年级\t中文\t英文\t数学\n";

141.}

142.intCStudent:

:

set(boolisalter)

143.{

144.cout<<"输入学生信息:

\n";

145.displayshorthead();

146.if(isalter)

147.displayshortinfo();

148.cout<<"\t"<

149.cin.clear();

150.cin>>*this;//从标准输入获取学生信息

151.altered=true;//已修改

152.

153.if(cin.fail())

154.{

155.cout<<"录入失败\n";

156.cin.clear();

157.cin.ignore(CIN_LEN,'\n');//这两行是用来清空输入缓冲

158.return-1;

159.}

160.else

161.{

162.cout<<"录入成功\n";

163.return1;

164.}

165.}

166.

167.//重载输出符

168.ostream&operator<<(ostream&out,constCStudent&right)

169.{

170.//输出学生的全部信息

171.out<<"\t"<

172.<

173.<

174.returnout;

175.}

176.//重载输入符

177.istream&operator>>(istream&in,CStudent&right)

178.{

179.//输入除ID外的其他信息

180.in>>right.info.name>>right.info.sex>>right.info.major

181.>>right.info.grade>>right.info.ch>>right.info.en>>right.info.ma;

182.returnin;

183.}

184.

185./////////////////////////////////

186.//初始化函数//

187.////////////////////////////////

188.intinitial(vector&stu)

189.{

190.fstream输入文件

191.CStudent*p;

192.

193.(,fstream:

:

in|fstream:

:

binary);//二进制输入打开

194.

195.if(!

文件是否打开成功

196.return-1;

197.while(()!

=EOF)//是否到文件末尾

198.{

199.p=newCStudent();//新建一个学生对象

200.((char*)p->getinfo(),sizeof(data));//读入学生对象

201.if(())//检查读入是否失败

202.return-2;

203.stu.push_back(p);//对象加入vector

204.}

205.if(!

stu.empty())//如果从文件读入了对象

206.CStudent:

:

nowid=stu.back()->getid()+1;//则自增id设置为最后一个学生id+1

207.();//关闭

208.returnstu.size();//返回对象个数

209.}

210.////////////////////////////////

211.//信息增加函数//

212.///////////////////////////////

213.voidinsert(vector&stu)

214.{

215.charc='y';//输入控制字符

216.

217.intflag=1;//标志位,1表示新增成功

218.

219.CStudent*p=newCStudent();

220.

221.while(c!

='n')//是否继续新增

222.{

223.flag=p->set(false);//设置学生信息

224.if(flag==1)//如果设置成功

225.{

226.stu.push_back(p);//对象加入vector

227.p=newCStudent();//新建下一个对象

228.}

229.cout<<"是否继续添加学生(any/n)?

";

230.cin.clear();

231.cin.ignore(CIN_LEN,'\n');

232.cin.get(c);

233.}

234.//删除最后一个新建的对象,因为没有使用它

235.deletep;

236.CStudent:

:

nowid--;

237.}

238.

239.///////////////////////////////

240.//查询全部信息函数//

241./////////////////////////////

242.intcomparebynone(constvoid*a,constvoid*b)

243.{

244.return0;

245.}

246.////////////////////////////////

247.//按学号比较函数//

248.//////////////////////////////

249.intcomparebyid(constvoid*a,constvoid*b)

250.{

251.return*(constint*)a-((CStudent*)(b))->getid();

252.}

253.///////////////////////////////

254.//按姓名比较函数//

255.//////////////////////////////

256.intcomparebyname(constvoid*a,constvoid*b)

257.{

258.returnstrcmp((constchar*)a,(constchar*)(((CStudent*)b)->getname()));

259.}

260.////////////////////////////////

261.//按年级比较函数//

262.//////////////////////////////

263.intcomparebygrade(constvoid*a,constvoid*b)

264.{

265.return(*(constint*)a-((CStudent*)b)->getgrade());

266.}

267./////////////////////////////////////

268.//按总分和性别比较函数//

269.///////////////////////////////////

270.intcomparebymarkandsex(constvoid*a,constvoid*b)

271.{

272.doublemark;

273.charsex;

274.sscanf((constchar*)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

286.voidsearchframe(constchar*info,T&condition,vector&stu,int(*compare)(constvoid*a,constvoid*b),boolisalter)

287.{

288.charc='y';

289.intflag;

290.while(c!

='n')

291.{

292.cin.clear();

293.

294.if(info!

=NULL)

295.{

296.cout<<"输入"<

";

297.//cin.ignore(CIN_LEN,'\n');

298.cin>>condition;

299.}

300.

301.if(cin.fail())

302.{

303.cout<<"输入错误\n";

304.}

305.else

306.{

307.//遍历vector查找

308.for(vector:

:

size_typeix=flag=0;ix!

=stu.size();++ix)

309.{//判断是name是否相等

310.if(compare(&condition,stu[ix])==0)

311.{

312.if(isalter)

313.stu[ix]->set(isalter);

314.else

315.{

316.if(flag==0)

317.CStudent:

:

displayhead();

318.stu[ix]->displayinfo();

319.}

320.flag=1;

321.}

322.}

323.if(flag==0)//没有查到

324.{

325.cout<<"没有";

326.if(info==NULL)

327.cout<<"符合条件";

328.else

329.cout<

330.cout<<"的学生\n";

331.}

332.}

333.cout<<"是否继续(any/n)?

";

334.cin.clear();

335.cin.ignore(CIN_LEN,'\n');

336.cin.get(c);

337.}

338.}

339.

340.////////////////////////////////

341.//信息检索函数//

342.//////////////////////////////

343.voidfetch(vector&stu)

344.{

345.intchoose,id,grade;

346.charname[20],markandsex[20];

347.

348.while(true)

349.{

350.cout<<"\n\t1.显示全部学生信息\n"

351."\t2.按学号查学生信息\n"

352."\t3.按姓名查学生信息\n"

353."\t4.按年级查学生信息\n"

354."\t5.按成绩和性别查询\n"

355."\t6.返回上级菜单\n\n";

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("学号",id,stu,comparebyid,false);break;

364.case3:

searchframe("姓名",name,stu,comparebyname,false);break;

365.case4:

searchframe("年级",grade,stu,comparebygrade,false);break;

366.c

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2