实验四流运算符的重载及文件的使用

上传人:人*** 文档编号:490830804 上传时间:2023-05-27 格式:DOC 页数:9 大小:174.50KB
返回 下载 相关 举报
实验四流运算符的重载及文件的使用_第1页
第1页 / 共9页
实验四流运算符的重载及文件的使用_第2页
第2页 / 共9页
实验四流运算符的重载及文件的使用_第3页
第3页 / 共9页
实验四流运算符的重载及文件的使用_第4页
第4页 / 共9页
实验四流运算符的重载及文件的使用_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《实验四流运算符的重载及文件的使用》由会员分享,可在线阅读,更多相关《实验四流运算符的重载及文件的使用(9页珍藏版)》请在金锄头文库上搜索。

1、实 验 报 告( 2014 / 2015 学年 第 二 学期)课程名称 面向对象程序设计及C+实验名称实验四:流运算符的重载及文件的使用实验时间2016年5月30日指导单位计算机研究中心指导教师陈景强学生姓名班级学号学院(系)专 业实 验 报 告实验名称流运算符的重载及文件的使用 指导教师陈景强实验类型验证 实验学时2 实验时间4一、 实验目的和要求(1)掌握在自定义的类中重载输入流运算符和输出流运算符和输出流运算符对象名”输入对象的值,通过“cout对象名”输出对象的值,输出复数值时将原来主函数中“对象名.print( )”改成“cout对象名”形式。在上一步完成的基础上,将复数类改成一个类

2、模板,只设一个模板参数,即实部和虚部用同一种类型,修改相应的代码,完成输入、输出功能。实验解答: (1)源代码参考多态性实验第2题。(2)记录重载operator函数的代码:ostream& operator(ostream &s,const Complex &c) cout0)cout+; coutc.imagi; coutendl; return s;(3) 将类改写成类模板:(多态性实验代码中的重载符号仅保留加法+,其他可省略)#includeusing namespace std;template class Complexprivate:T real;T imag;public:Co

3、mplex(T r=0,T i=0)real=r;imag=i;friend Complex operator+(const Complex &a,const Complex &b); friend Complex operator-(const Complex &a,const Complex &b);friend ostream &operator(istream &in,const Complex &a);Complex operator*(const Complex &a);Complex operator/(const Complex &a);template ostream &op

4、erator(ostream &out,const Complex &a)out0)out+;outa.imagi;outendl;return out;template istream &operator(istream &in,const Complex &a)ina.real;if(a.imag!=0)if(a.imag0)in+;ina.imagi;return in;template Complex operator+(const Complex &a,const Complex &b)Complex temp;temp.real=a.real+b.real;temp.imag=a.

5、imag+b.imag;return temp;template Complex operator-(const Complex &a,const Complex &b)Complex temp;temp.real=a.real-b.real;temp.imag=a.imag-b.imag;return temp;template Complex Complex :operator*(const Complex &a)Complex temp;temp.real=real*a.real;temp.imag=imag*a.imag;return temp;template Complex Com

6、plex :operator/(const Complex &a)Complex temp;temp.real=real/a.real;temp.imag=imag/a.imag;return temp;void main()Complex a1,a2,a3,a4,a5,a6;couta1;couta2;coutoriginal a1 is:a1endl;coutoriginal a2 is:a2endl;a3=a1+a2;couta3=a1+a2=a3endl; a4=a1-a2;couta4=a1-a2=a4endl;a5=a1*a2;couta5=a1*a2=a5endl;a6=a1/a

7、2;couta6=a1/a2=a6endl;实 验 报 告 实验题目2:事先用Windows的记事本建立一个文本文件ff.txt。 编写一个函数void ReadFile(char* s)实现读取以s串为文件名的文本文件的内容在屏幕上显示。 编写一个函数void Change(char *s1,char *s2)将文本文件中的小写字母全部改写成大写字母生成一个新文件ff2.txt。 主函数中调用ReadFile(ff.txt);显示ff.txt的内容,调用Change (ff.txt ,ff2.txt);根据ff.txt文件作修改生成一个新的文件ff2.txt,最后再调用ReadFile(ff

8、2.txt);显示新文件的内容。实验解答: void ReadFile(char *s)char ch100;ifstream inf(s);if(!inf)coutCannot open the file!n; return ; inf.get(ch);coutchendl;inf.close(); /请完成代码void Change(char *s1,char *s2)ifstream ifile(ff.txt);if(!ifile)coutff.txt cannot be openned!endl;return ;ofstream ofile(d:tempff2.txt);if(!ofi

9、le)coutff2.txt cannot be openned!endl;return ;char ch;while(ifile.get(ch)ch=ch-32;ofile.put(ch);ifile.close();ofile.close(); /请完成代码实验题目3(选做):定义学生类,该类包含学生的一些基本信息:学号、姓名、性别、成绩。定义流对象,实现用write函数将学生信息以二进制方式写到磁盘文件stu.dat中。再用read将磁盘中的学生信息读到内存显示在屏幕上。实验解答:(1)Student:Student(char *nu,char *na,char *se,int s) /构造函数ostream & operator(ostream &out,const Student &s) /重载输出运算符s1s2; /从键盘上输入要操作的文件名 Change(_s1,s2_); /调用Change函数将s1串为文件名的内容修改后保存到以s2串为文件名的文本文件中 ReadFile(_s2_

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号