c++第五次实验(共20页)_第1页
c++第五次实验(共20页)_第2页
c++第五次实验(共20页)_第3页
c++第五次实验(共20页)_第4页
c++第五次实验(共20页)_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

1、计算机与通信工程学院天津理工大学计算机与通信工程学院实验报告2013 至 2014 学年 第 二 学期课程名称高级语言程序设计II实验( 5 )实验名称IO流类库模板实验时间 2014年 5月12 日 第 3 节 至 第 6 节学号姓名专业信息安全 主讲教师唐召东辅导教师唐召东软件环境VC+6硬件环境PC机实验目的1掌握标准输入输出流的使用方法和作用;2了解字符串流的定义与使用;3. 掌握运用成员函数和操纵符进行格式化输入/输出的方法;4. 掌握流的插入/提取操作的重载方法;5. 掌握文件流的打开、关闭及使用的使用方法;6了解文本文件流与二进制文件流在操作上的区别。7掌握MS Visual C

2、+6.0调试C+程序的基本方法、基本步骤。实验内容(应包括实验题目、实验要求、实验任务等)1设计一个职员类EMPLOYEE,主要包括工号、姓名、部门、年龄、工资等属性以及更换部门、年龄变更、工资增长等操作,为EMPLOYEE类提供输入/输出运算“”。(提示:类EMPLOYEE应有多个重载的构造函数)题目说明:1)类EMPLOYEE的定义可以参考如下:class EMPLOYEEprivate:long id;char nameMAX_LEN;char departmentMAX_LEN;int age;double salary;public:EMPLOYEE();EMPLOYEE(const

3、 EMPLOYEE& obj);EMPLOYEE(long idd, char namee, char depar, int agee, double sala);EMPLOYEE();void changeDepartment(char newDepar);void changeAge(int agee);void raiseSalary(double delta);/ 定义友元函数,提供输入/输出运算。friend ostream& operator(istream& stream, EMPLOYEE& obj); 2)主函数如下所示:void main()EMPLOYEE A(20070

4、1, Tom, DEPARTMENT A,34,2345.6);coutB;cout输入的信息:Bendl;2教材324页【7.11】有两个矩阵a和b,均为2行3列,编写程序求两个矩阵的和,重载插入运算符“”,使之能用于矩阵的输入和输出;重载“+”运算符使之能用于矩阵求和:c=a+b。3设计一个管理图书目的简单程序,提供的基本功能包括:可连续将新书存入文件book.dat中,新书信息加入到文件的尾部;也可以根据输入的书名进行查找;把文件book.dat中同书名的所有书目信息显示出来。(为简单起见,描述一本书的信息包括:书号,书名,出版社和作者)题目要求:1)/ 定义描述书信息的结构体struc

5、t Booklong id;char nameMAX_LEN;char publisherMAX_LEN;char authorMAX_LEN;2)定义函数实现:/ 向文件book.dat中添加新的一条书的信息void addBookInfo(Book aBook);/ 从文件book.dat中读出书名为namee的所有书的记录void selectBook(char namee);3)主函数中用一个演示程序完成对所设计的新类的功能测试,并给出测试数据和实验结果:void main() Book aBook=2001, Star, yahoo, Li Ming;Book bBook=2002,

6、 Boold, Sina, Tom Berry;Book cBook=2003, Star, google, Li Ming;addBookInfo(aBook);addBookInfo(bBook);addBookInfo(cBook);selectBook(Star);3建立两个磁盘文件f1.dat和f2.dat,编程序实现以下工作:从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数);从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面;从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat(不保留原来的数据)。4. 针对学生类,利用

7、本章中学到的文本文件的顺序、随机读写,二进制文件的顺序、随机读写函数,实现下列功能:1)学生基本信息由学号(长整型)、姓名(字符数组)、性别(字符型)、年龄(整型)、数学成绩(float)、语文成绩(float)、英语成绩(float)组成。学生类的定义可以参考如下:/定义Student类的子类class Studentpublic:long id;char name20;int age;float math;float chinese;float english;public:Student();Student(long l,char NAME,int a);Student(long l,c

8、har NAME,int a,float m, float c, float e);/ 特殊的构造函数:拷贝构造函数Student(const Student& other);/ 重载赋值运算符Student operator =(const Student& other);Student();void setId(long l);long getId();void setName(char NAME); void setAge(int a);int getAge();void setAll(float m, float c, float e);/ 利用友元函数:重载输出运算符friend o

9、stream& operator (istream& stream, Student& obj); ; 2)文本文件的顺序读写:定义fstream的对象ioFile,与文件“a.txt”建立关联,创建两个对象,Student s1(2007002,Jerry,18);Student s2(2007003,Jack,19,78.5,89.5,93.0);将s1、s2的学号、年龄、平均分的数据写入文件“a.txt”,并从文件“a.txt”中读出。3)二进制文件的顺序读写:定义ofstream 的对象outfile与文件“stud.dat”建立关联,创建三个对象,Student s1;Student

10、 s2(2007002,Jerry,18);Student s3(2007003,Jack,19,78.5,89.5,93.0);将s1、s2、s3的全部信息写入到文件“stud.dat”定义ifstream 的对象infile与文件“stud.dat”建立关联,从文件“stud.dat”依次读出三个对象的信息到数组Student stud3中,并将三个对象的信息在屏幕上输出:cout输入第 i+1 个学生的信息:nstudiendl;4)二进制文件的随机读写,编写程序实现下列操作:定义ofstream 的对象outfile与文件“stud1.dat”建立关联,创建4个对象,Student e

11、1(1,张三,23,10,20,30); Student e2(2,李四,32,20,30,40); Student e3(3,王五,34,30,40,50); Student e4(4,刘六,27,40,50,60);按e1,e2,e3,e4顺序写入文件;将e3(即王五)的年龄改为40岁;定义ifstream 的对象infile与文件“stud1.dat”建立关联;从文件中读出第3个人的数据,记录到对象Student s1中; 在屏幕上输出s1的信息:cout输出 s1 的信息:s1endl;实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)1. 代码#include#inc

12、ludeclass EMPLOYEEprivate:long id;/工号、姓名、部门、年龄、工资char name20;char department50;int age;double salary;public:EMPLOYEE();EMPLOYEE(const EMPLOYEE& obj);EMPLOYEE(long idd, char namee, char depar, int agee, double sala);EMPLOYEE();void changeDepartment(char newDepar);void changeAge(int agee);void raiseSa

13、lary(double delta);/ 定义友元函数,提供输入/输出运算。friend ostream& operator(istream& stream, EMPLOYEE& obj); EMPLOYEE:EMPLOYEE()id=0;name0=0;/字符数组的初始化department0=0;/字符数组的初始化age=0;salary=0;EMPLOYEE:EMPLOYEE(long idd, char namee, char depar, int agee, double sala)id=idd;strcpy(name,namee);strcpy(department,depar);

14、age=agee;salary=sala;EMPLOYEE:EMPLOYEE()coutdestructor is calledendl;void EMPLOYEE:changeDepartment(char newDepar)/修改部门void EMPLOYEE:changeAge(int agee)/修改年龄ostream& operator(ostream& stream, EMPLOYEE& obj)stream工号:obj.idendl;stream姓名:endl;/输出数组stream部门:obj.departmentendl;/输出数组stream年龄:obj.a

15、geendl;stream工资:obj.salary(istream& stream, EMPLOYEE& obj)coutobj.id;;coutobj.department;coutobj.age;coutobj.salary;return stream;void main()EMPLOYEE A(200701, Tom, DEPARTMENT A,34,2345.6);coutB;cout输入的信息:endlBendl;2.代码#includeclass Matrixpublic:Matrix();friend Matrix operator + (Matrix

16、&,Matrix &);friend ostream& operator(istream &,Matrix &);private:int mat23;/定义数组;Matrix:Matrix()/构造函数里对多维数组初始化for(int i=0;i2;i+)for(int j=0;j3;j+)matij=0;/数组的初始化Matrix operator + (Matrix &a,Matrix &b)Matrix c;for(int i=0;i2;i+)for(int j=0;j(istream & in,Matrix &m)cout输入矩阵的值:endl;for(int i=0;i2;i+)fo

17、r(int j=0;jm.matij;return in;ostream &operator(ostream &out,Matrix &m)for(int i=0;i2;i+)for(int j=0;j3;j+)outm.matij ;outa;cinb;coutendlMatrix a:endlaendl;coutendlMatrix b:endlbendl;c=a+b;coutendl两个矩阵相加:Matrix c=Matrix a+Matrix b:endlcendl;return 0;3代码#include#includestruct Booklong id;/idchar name3

18、0;/书名char publisher30;/出版社char author20;/作者;void addBookInfo(Book dBook4)/ 向文件book.dat中添加新的一条书的信息/Book aBook4=2001, Star, yahoo, Li Ming;ofstream add(book.dat,ios:binary|ios:app);if(!add)coutcannot open output file.n;/abort();for(int i=0;i4;i+)add.write(char *) &dBooki,sizeof(dBooki);/向Book传递参数adden

19、dl;add.close(); void selectBook()/从文件book.dat中读出书名为namee的所有书的记录Book dBook4;ifstream select(book.dat,ios:binary);if(!select)coutcannot open book.dat!n;/abort();for(int i=0;i3;i+)select.read(char *) &dBooki,sizeof(dBooki);/if(dB=namee)coutdBooki.id dB dBooki.publisher dBooki.authoren

20、dl;select.close();/*list list22;ifstream in(f4.dat,ios:binary);if(!in)coutcannot open input file.n;abort();for(int i=0;i2;i+)in.read(char *) &list2i,sizeof(list2i);/read写入内存coutlist2i.course list2i.scoreendl;in.close();*/void main() Book aBook4=2001, Star, yahoo, Li Ming;/Book bBook4=2002, Boold, Si

21、na, Tom Berry;/Book cBook4=2003, Star, google, Li Ming;addBookInfo(aBook);/添加图书信息/addBookInfo(bBook);/添加图书信息/addBookInfo(cBook);/添加图书信息selectBook();/选择图书4.代码#include#includeusing namespace std;int main()int i,t,j;int a10;int b21;ofstream outfile1(f1.dat,ios:out);cout请输入要存入f1中的10个整型变量endl;for(i = 0;i

22、 ai;outfile1ai ;outfile1.close();ofstream outfile2(f2.dat,ios:out);cout请输入要存入f2中的10个整型变量endl;for(i = 0;i ai;outfile2ai ;outfile2.close();ifstream infile1(f1.dat,ios:in);for(i = 0;i ai;infile1.close();ofstream outfile3(f2.dat,ios:app);for(i = 0;i 10;i+)outfile3ai ;outfile3.close();ifstream infile2(f2

23、.dat,ios:in);for(i = 0;i bi;infile2.close();for(j = 0;j 19;j+)for(i = 0;i bi+1)t = bi;bi = bi+1;bi+1 = t;ofstream outfile4(f2.dat,ios:out);for(i = 0;i 20;i+)outfile4bi ;outfile4.close();ifstream infile3(f2.dat,ios:in);coutf2中数据为:endl;for(i = 0;i bi;couttbi;infile3.close();return 0;5.代码#include#inclu

24、de#includeclass Studentpublic:long id;char name20;int age;float math;float chinese;float english;public:Student();Student(long l,char NAME,int a);Student(long l,char NAME,int a,float m, float c, float e);Student(const Student& other);/ 特殊的构造函数:拷贝构造函数Student operator =(const Student& other);/ 重载赋值运算符

25、Student();void setId(long l);long getId();void setName(char NAME); void setAge(int a);int getAge();void setAll(float m, float c, float e);friend ostream& operator (ostream& stream, Student& obj);/ 利用友元函数:重载输出运算符friend istream& operator (istream& stream, Student& obj); ;Student:Student()id=20070001;n

26、ame20=0;/字符数组初始化age=20;math=90;chinese=90;english=90;Student:Student(long l,char NAME,int a)id=l;strcpy(name,NAME);age=a;math=90;chinese=90;english=90;Student:Student(long l,char NAME,int a,float m, float c, float e)id=l;strcpy(name,NAME);age=a;math=m;chinese=c;english=e;Student:Student(const Studen

27、t& other)/拷贝构造函数id=other.id;strcpy(name,);age=other.age;math=other.math;chinese=other.chinese;english=other.english;Student Student:operator =(const Student& other)/ 重载赋值运算符id=other.id;strcpy(name,);age=other.age;math=other.math;chinese=other.chinese;english=other.english;return

28、*this;Student:Student()coutdestructor is called!endl;void Student:setId(long l)id=l;long Student:getId()return id;void Student:setName(char NAME)strcpy(name,NAME);void Student:setAge(int a)age=a;int Student:getAge()return age;void Student:setAll(float m, float c, float e)math=m;chinese=c;english=e;o

29、stream& operator (ostream& stream, Student& obj)/重载输出运算符streamobj.id;;streamobj.age;streamobj.math;streamobj.chinese;stream(istream& stream, Student& obj)/重载输入运算符streamobj.id;;streamobj.age;streamobj.math;streamobj.chinese;streamobj.english;return stream;int main()int a;char ch;Student s1(2007002,Jerry,18);Student s2(2007003,Jack,19,78.5,89.5,93.0);fstream ioFile(a.txt,ios:in|ios:out);if(!ioFile)coutcannot open a.txtn;/abort();ioFs1.age(s1.math+s1.chinese+s1.english)/3endl;ioFs2.age(s2.math+s2.chinese+s2.english)/3endl;ioFile.close();i

温馨提示

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

评论

0/150

提交评论