C++输入输出流实验报告_第1页
C++输入输出流实验报告_第2页
C++输入输出流实验报告_第3页
C++输入输出流实验报告_第4页
C++输入输出流实验报告_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验名称,实验人姓名,页码深 圳 大 学 实 验 报 告实验课程名称: 程序设计实验与课程设计 实验项目名称: 实验10 C+流输入与流输出 学院: 计软 专业: 计科 报告人: 学号: 班级: 3 同组人: 无 指导教师: 朱安民 实验时间: 2014年6月16日 提交时间: 2014年6月16 声明:本次实验内容由报告人和同组人独立完成,所有涉及到他人的工作均已说明。报告人和同组人均同意教师及学校为教学活动而引用本实验的内容,且无需事先征得同意和特别说明。教务处制一、实验目的1掌握标准输入输出(iostream库中标准对象cin、cout)的使用2掌握IO流类成员函数输入输出(cin.ge

2、t, cin.getline, cin.read; cout.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,生命, 攻击的数值初始化为等级的10倍,金钱为等级的100倍,经验恒为0,文件的第二行是怪兽的数据,初始状态为等级

5、是4,生命, 攻击的数值初始化为等级的8倍,经验为等级的80倍,金钱为等级的800倍)对怪兽和奥特曼的状态输出采用运算符重载的方法,并结合display方法使用,注意本题目要求怪兽和奥特曼的状态输出必须使用重载运算符,不能直接使用display方法。把信息直接从文件读出来并赋值给奥特曼和怪兽,需要重载运算符作为友元;把奥特曼和怪兽的值写入文件,要求重载运算符作为友元 三、实验分析设计. 四、核心代码说明第一题#include<fstream>#include<iostream>using namespace std;/类的定义与实现class Ultramanpubli

6、c:Ultraman();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<<" "<<

7、u.hp<<" "<<u.damage<<" "<<u.exp<<" "<<u.money<<" "return output;int rank;int hp;int damage; int exp;int money;Ultraman:Ultraman()rank=0; hp=0; damage=0; exp=0; money=0;/主函数int main()Ultraman u1,u2,u3;ofstream outfile(&

8、quot;RecordU.txt");cin>>u1>>u2>>u3;outfile<<u1<<u2<<u3;outfile.close();ifstream infile("RecordU.txt");infile>>u1>>u2>>u3;cout<<u1<<endl<<u2<<endl<<u3<<endl;infile.close();return 0;第二题#include<

9、iostream>using namespace std;/奥特曼类class Ultramanpublic:void initial(int);void display();friend ostream& operator<<(ostream& output,Ultraman& u)output<<"u rank="<<u.rank<<" hp="<<u.hp<<" damage="<<u.damage<<&

10、quot; 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=rank;hp=0.5*hp;damage=0.5*damage;exp=0.5*exp;money=0.5*money;return*this;private:int

11、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="<<hp<<" damage="<<damage<<" exp="<<exp

12、<<" money="<<money<<endl;/怪兽类class Monsterpublic:void initial(int);void display();friend ostream& operator<<(ostream& output,Monster&m)output<<"m rank="<<m.rank<<" hp="<<m.hp<<" damage="<<

13、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*damage;exp=0.5*exp;money=0.5*money;return*th

14、is;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="<<damage<<" exp=&quo

15、t;<<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>>k;if(s='u')if(k%2=0) +U;else -U;U.display();if(s='m')if(k%2=0) +M;else

16、 -M;M.display();return 0;第三题#include<iostream>#include<fstream>#include<iomanip>using namespace std;/奥特曼类class Ultramanpublic:int getmoney()return money;void initial(int);void display();friend istream& operator>>(istream& input,Ultraman& u)input>>u.rank>&

17、gt;u.hp>>u.damage>>u.exp>>u.money;return input;friend ostream& operator<<(ostream& output,Ultraman& u)output<<"u rank="<<u.rank<<" hp="<<u.hp<<" damage="<<u.damage<<" exp="<<u.

18、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=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

19、 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="<<hp<<" damage="<<damage<<" exp="<<exp<<" money="

20、;<<money<<endl;/怪兽类class Monsterpublic:int getmoney()return money;void initial(int);void display();friend istream& operator>>(istream& input,Monster& m)input>>m.rank>>m.hp>>m.damage>>m.exp>>m.money;return input;friend ostream& operator&

21、lt;<(ostream& output,Monster&m)output<<"m rank="<<m.rank<<" hp="<<m.hp<<" damage="<<m.damage<<" exp="<<m.exp<<" money="<<m.money<<endl;return output;Monster operator +()rank

22、=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*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;voi

23、d Monster:display()cout<<"m rank="<<rank<<" hp="<<hp<<" damage="<<damage<<" exp="<<exp<<" money="<<money<<endl;/主函数int main()Ultraman U1,U2;Monster M1,M2;char s;int t,k;U1.initial(8);cout<<U1;M1.initial(4);cout<<M1;cin>>t;while(t-)cin>>s>>k;if(s='u')if(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&qu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论