版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、深圳大学实验报告实验课程名称:程序设计实验与课程设计实验项目名称:实验10 C+流输入与流输出学院:计软专业:计科报告人:学号:班级:_3_同组人:无指导教师:朱安民实验时间:2014年6月16日 提交时间:2014年6月16声明:本次实验内容由报告人和同组人独立完成,所有涉及到他人的工作均已说明。报告人和同组人均同意教师及学校为教学活动而引用本实验的内容,且无需事先征得同意和特别说明。教务处制、实验目的1 .掌握标准输入输出(iostream库中标准对象cin、cout)的使用2 掌握 IO 流 类成 员函 数输 入输 出( cin.get, cin.getline, cin.read; c
2、out.put, cout.write)的使用3. 掌握输出格式(标准控制符、IO 流类成员函数、 iomanip 头文件中的控制符) 控制方法4. 掌握磁盘文件的输入输出方法二、实验说明和实验环境1. 在奥特曼类的基础上,编写一个程序,重载运算符“”和“”,使得用户可 以直接(格式化)输出奥特曼的状态。在主程序中输入若干个( 3 个) 奥特曼 的状态,并分别将它们保存到文件 RecordU.txt 中。然后读取并显示文件中的内 容。2. 奥特曼和怪物都具有属性:等级 , 生命, 攻击 , 经验 , 金钱,都具有方法:初 始化 initial 和显示状态 display在奥特曼的初始化中,需要
3、接受外来等级参数,生命 , 攻击的数值初始化为 等级的 10倍,金钱为等级的 100 倍,经验恒为 0在怪兽的初始化中,需要接受外来等级参数,生命 , 攻击的数值初始化为等 级的 8 倍,经验为等级的 80倍,金钱为等级的 800倍对怪兽和奥特曼的状态输出采用运算符重载的方法,并结合 display 方法 使用,注意本题目要求怪兽和奥特曼的状态输出必须使用重载运算符, 不能直 接使用 display 方法。注意:为了实现运算符重载,需要包含头文件 <iomanip> 经常天降怪石会砸中奥特曼或怪兽,如果被幸运石砸中,就会处于 “鸿运当 头”状态,被砸对象除等级外其它属性全部翻倍;
4、如果被厄运石砸中,就会陷入“倒 霉透顶”状态,被砸对象除等级外其它属性全部减半。把 “鸿运当头 ”封装成操作, 并用重载运算符 +来实现; “倒霉透顶”封装成操作,并用重载运算符 -来实现。 两个重载都需要用友元方法。3. 奥特曼和怪物经过一番战斗斗,需要中场休息,休息时把各自的属性值存放 到一个文件中,休息结束时需要从文件中读取之前的状态继续战斗。奥特曼和怪物都具有属性: 等级, 生命, 攻击, 经验 , 金钱,都具有方法: 初 始化 initial 和显示状态 display奥特曼和怪兽的属性值均来自于文件 (文件的第一行是奥特曼的数据, 初始 状态为:等级是 8,生命 , 攻击的数值初始
5、化为等级的 10 倍,金钱为等级的 100 倍,经验恒为 0,文件的第二行是怪兽的数据,初始状态为等级是4,生命 , 攻击的数值初始化为等级的 8 倍,经验为等级的 80倍,金钱为等级的 800倍) 对怪兽和奥特曼的状态输出采用运算符重载的方法,并结合display 方法使用,注意本题目要求怪兽和奥特曼的状态输出必须使用重载运算符, 不能直 接使用 display 方法。 把信息直接从文件读出来并赋值给奥特曼和怪兽,需要重载运算符作为友元; 把奥特曼和怪兽的值写入文件,要求重载运算符作为友元三、实验分析设计"-一一主函数int nain<)< "定义对象U師M俵
6、示奥捷曼和怪兽"定义甦数变量tftk,字样变量夕淀舊勰尊麴级,幷调用皿卩两方法输出狀态cout«M1:cin»t; while(t)cin»s>>k:"如具£等于坐与字母6黒持曩被砸中 "如果*倉禺裁丄«Wt*+"否则,U哦行"打掰血p列方迭显徑to"如果三芈于小写早母叫專曹涵斜 "如果遑範丄也扌姑“"否则i附顷行一cout<<M1;return 9;jFriend ifstream operator>>(ifstreama in
7、 ;Monsterfriend ofstredmfi operator<<(ofstrea川& out;Monsterttiriclude <iotream>Uincludc <iomanip> ttincludG <fstream> uing namespace 若七8;|/主函数int itiain() UltraMan U1: Monster M1: int t# K; char c;U1.initial(dj* cout<<U1; Ml.initial(4): cout«Hl ;iFetreaa fin(&q
8、uot;c: 123um. txt*') i 打开文件准备读取數据 /* fin»U1 :从文件中读第一行数掳赋值给奧計曼对彖cout«U1 ;:从文件中读第二行数据赋值给奥特曼対象cout<<H1;fin.cloec();whiU(t)(cin>>>>k;if (e = U) if ( k%2 = 0 )+U1 ;else-U1 ;CDUt<<U1;)if (s = = 'm*) if ( k%2 = G )+M1 ;else-Ml ;cout<<M1;t-;ofstrean刊上;"打开
9、文件准备写入数据 fout<<U1 ;护.奨持辱对象的倍写入文件fout«M1 ;把怪兽对歸的值写入文件fout ” cloce();ifstrean f in2( "c : 1 23um1 .txf);"打开文件准备写入数据fin2»U2;从文件读取奥特曼对象的億fin2»M2;从文件读取怪兽对彖的值fin2.close();cout<<U2. getmoney ()->-M2 .get moneys )<<endl ;return 0;四、核心代码说明第一题#in clude<fstream&
10、gt;#in clude<iostream>using n amespace std;/类的定义与实现class Ultrama npublic:Ultrama n();frie nd istream & operator >>(istream&in put,Ultrama n&u )i nput>>u.ra nk>>u.hp>>u.damage>>u.exp>>u.m on ey;return in put;frie nd ostream & operator <<
11、(ostream&output,Ultrama n&u)output<<u.ra nk<<" "<<u.hp<<" "<<u.damage<<" "<<u.exp<<" "<<u.m on ey<<" "return output;int rank;int hp;int damage;int exp;int money;Ultraman:Ultraman()ra
12、nk=0;hp=0;damage=0;exp=0;money=0;/主函数int main()Ultraman u1,u2,u3;ofstream outfile("RecordU.txt");cin>>u1>>u2>>u3;outfile<<u1<<u2<<u3;outfile.close();ifstream infile("RecordU.txt");infile>>u1>>u2>>u3;cout<<u1<<endl&
13、lt;<u2<<endl<<u3<<endl;infile.close();return 0;第二题#include<iostream>using namespace std;/奥特曼类class Ultramanpublic:void initial(int);void display();friend ostream& operator<<(ostream& output,Ultraman& u)output<<"u rank="<<u.rank<&l
14、t;" hp="<<u.hp<<" damage="<<u.damage<<" exp="<<u.exp<<" money="<<u.money<<endl;return output;Ultraman operator +()rank=rank;hp=2*hp;damage=2*damage;exp=2*exp;money=2*money;return*this;Ultraman operator -()rank=ra
15、nk;hp=0.5*hp;damage=0.5*damage;exp=0.5*exp;money=0.5*money;return*this;private:int rank;int hp;int damage;int exp;int money;/奥特曼类的定义void Ultraman:initial(int a)rank=a;hp=10*a;damage=10*a;exp=0;money=100*a;void Ultraman:display()cout<<"u rank="<<rank<<" hp="<&
16、lt;hp<<" damage="<<damage<<" exp="<<exp<<" money="<<money<<endl;/怪兽类class Monsterpublic:void initial(int);void display();friend ostream& operator<<(ostream& output,Monster&m)output<<"m rank="<
17、;<m.rank<<" hp="<<m.hp<<" damage="<<m.damage<<" exp="<<m.exp<<" money="<<m.money<<endl;return output;Monster operator +()rank=rank;hp=2*hp; damage=2*damage;exp=2*exp;money=2*money;return*this;Monster ope
18、rator -()rank=rank;hp=0.5*hp;damage=0.5*damage;exp=0.5*exp;money=0.5*money;return*this;private:int rank;int hp;int damage;int exp;int money;/怪兽类的定义void Monster:initial(int b)rank=b;hp=8*b;damage=8*b;exp=80*b;money=800*b;void Monster:display()cout<<"m rank="<<rank<<"
19、hp="<<hp<<" damage="<<damage<<" exp="<<exp<<" money="<<money<<endl;/主函数int main()Ultraman U;Monster M;char s;int k;U.initial(8);M.initial(4); U.display();cout<<M; int t;cin>>t;while(t-) cin>>s>>
20、;k; if(s='u') if(k%2=0) +U; else -U; U.display(); if(s='m') if(k%2=0) +M; else -M; M.display(); return 0;第三题#include<iostream> #include<fstream> #include<iomanip> using namespace std;/奥特曼类 class Ultraman public: int getmoney()return money; void initial(int); void di
21、splay();friend istream& operator>>(istream& input,Ultraman& u)input>>u.rank>>u.hp>>u.damage>>u.exp>>u.money;return input;friend ostream& operator<<(ostream& output,Ultraman& u)output<<"u rank="<<u.rank<<&qu
22、ot; hp="<<u.hp<<" damage="<<u.damage<<" exp="<<u.exp<<" money="<<u.money<<endl;return output;Ultraman operator +() rank=rank; hp=2*hp;文档来源为 :从网络收集整理 .word 版本可编辑 .欢迎下载支持 . damage=2*damage;exp=2*exp;money=2*money; retur
23、n*this;Ultraman operator -()rank=rank;hp=0.5*hp; damage=0.5*damage;exp=0.5*exp; money=0.5*money;return*this;private:int rank;int hp;int damage;int exp;int money;/奥特曼类的定义void Ultraman:initial(int a)rank=a;hp=10*a; damage=10*a;exp=0; money=100*a;void Ultraman:display()cout<<"u rank="&l
24、t;<rank<<" hp="<<hp<<" damage="<<damage<<" exp="<<exp<<" money="<<money<<endl;/怪兽类class Monsterpublic:int getmoney()return money;void initial(int);void display();friend istream& operator>>(istr
25、eam& input,Monster& m)input>>m.rank>>m.hp>>m.damage>>m.exp>>m.money;return input;friend ostream& operator<<(ostream& output,Monster&m)output<<"m文档来源为 :从网络收集整理 .word 版本可编辑 .欢迎下载支持 .rank="<<m.rank<<" hp="<
26、<m.hp<<" damage="<<m.damage<<" exp="<<m.exp<<" money="<<m.money<<endl;return output;Monster operator +()rank=rank;hp=2*hp;damage=2*damage;exp=2*exp;money=2*money;return*this;Monster operator -()rank=rank;hp=0.5*hp;damage=0.5*
27、damage;exp=0.5*exp;money=0.5*money;return*this;private:int rank;int hp;int damage;int exp;int money;/怪兽类的定义void Monster:initial(int b)rank=b;hp=8*b;damage=8*b;exp=80*b;money=800*b;void Monster:display()cout<<"m rank="<<rank<<" hp="<<hp<<" damage
28、="<<damage<<" exp="<<exp<<" money="<<money<<endl;/主函数 int main() Ultraman U1,U2; Mo nster M1,M2; char s;int t,k;U1.i ni tial(8); cout«U1;M1.i ni tial(4); cout<<M1;cin> >t;while(t-)cin> >s>>k;if(s='u')if
29、(k%2=0) +U1;else -U1;cout<<U1;if(s='m')if(k%2=0) +M1;else -M1;cout<<M1;ofstream fout("c:123um.txt");fout<<U1;fout<<M1;fout.close();ifstream fin 2("c:123um.txt");fin 2>>U2;fin 2>>M2;fin 2.close();cout<<U1.getm on ey()+M1.getm on ey()<<e ndl;return 0;五、测试与结果1和2题通过网上测评系统;1062352012150254AAccepted4 CIE血692 B2014-06*1616:31:7100281201215025 417684 C卄伽2375 B20t4-GC-lSrt)2 44第三题时间时间未掌握好,没有进行测评,但通过 vc6.0LIrank=8 r<nk=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 浙江省嘉兴市2025-2026学年高三下学期二模技术试题(含答案)
- 2025昆明理工大学津桥学院教师招聘考试题目及答案
- 2025江西工商职业技术学院教师招聘考试题目及答案
- 2026年经济学基础模拟考试试题及答案
- 考研农业工程试题及答案
- 2026北京大学材料科学与工程学院招聘2位劳动合同制人员建设考试参考题库及答案解析
- 2026浙江中国小商品城集团股份有限公司市场化选聘11人建设笔试备考试题及答案解析
- 河南新科起重机股份有限公司2026届校园招聘建设笔试备考题库及答案解析
- 2026河南南阳师范学院招聘高层次人才168人建设考试参考试题及答案解析
- 2025年阿勒泰地区阿勒泰市法院书记员招聘考试试题及答案解析
- 新质生产力:个人发展的新机遇
- 2025年江西省高考思想政治试卷真题(含标准答案)
- 露天采矿汛期安全培训课件
- 咨询费居间协议合同范本
- 《流体力学》课件(共十三章)
- 化工厂消防设施培训课件
- 2024年云南省元阳县民政局公开招聘试题带答案详解
- 介入诊疗技术管理规范实施细则及质量控制流程
- 2025至2030年中国油气回收行业市场运营态势及未来趋势研判报告
- 2025年中级银行从业资格之《中级个人理财》题库及答案详解【历年真题】
- T/CNFAGS 15-2024绿色合成氨分级标准(试行)
评论
0/150
提交评论