c++输入输出流实验报告.docx

上传人:b****7 文档编号:16444287 上传时间:2023-07-13 格式:DOCX 页数:24 大小:272.11KB
下载 相关 举报
c++输入输出流实验报告.docx_第1页
第1页 / 共24页
c++输入输出流实验报告.docx_第2页
第2页 / 共24页
c++输入输出流实验报告.docx_第3页
第3页 / 共24页
c++输入输出流实验报告.docx_第4页
第4页 / 共24页
c++输入输出流实验报告.docx_第5页
第5页 / 共24页
c++输入输出流实验报告.docx_第6页
第6页 / 共24页
c++输入输出流实验报告.docx_第7页
第7页 / 共24页
c++输入输出流实验报告.docx_第8页
第8页 / 共24页
c++输入输出流实验报告.docx_第9页
第9页 / 共24页
c++输入输出流实验报告.docx_第10页
第10页 / 共24页
c++输入输出流实验报告.docx_第11页
第11页 / 共24页
c++输入输出流实验报告.docx_第12页
第12页 / 共24页
c++输入输出流实验报告.docx_第13页
第13页 / 共24页
c++输入输出流实验报告.docx_第14页
第14页 / 共24页
c++输入输出流实验报告.docx_第15页
第15页 / 共24页
c++输入输出流实验报告.docx_第16页
第16页 / 共24页
c++输入输出流实验报告.docx_第17页
第17页 / 共24页
c++输入输出流实验报告.docx_第18页
第18页 / 共24页
c++输入输出流实验报告.docx_第19页
第19页 / 共24页
c++输入输出流实验报告.docx_第20页
第20页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

c++输入输出流实验报告.docx

《c++输入输出流实验报告.docx》由会员分享,可在线阅读,更多相关《c++输入输出流实验报告.docx(24页珍藏版)》请在冰点文库上搜索。

c++输入输出流实验报告.docx

c++输入输出流实验报告

实验四输入输出流

实验课程名:

面向对象程序设计(C++)

专业班级:

学号:

实验时间:

实验地点:

指导教师:

一、实验目的和要求

(1)理解类和对象的概念,掌握声明类和定义对象的方法。

(2)掌握构造函数和析构函数的实现方法。

(3)初步掌握使用类和对象编制C++程序。

(4)掌握对象数组、对象指针和string类的使用方法。

(5)掌握使用对象、对象指针和对象引用作为函数参数的方法。

(6)掌握类对象作为成员的使用方法。

(7)掌握静态数据成员和静态成员函数的使用方法。

(8)理解友元的概念和掌握友元的使用方法。

 

二、实验容

1.定义描述职工工资的类Laborage,数据成员为职工号(No)、(Name[8])、应发工资(Ssalary)、社保金(Security)、实发工资(Fsalary)。

定义公有成员函数Input(),在Input()函数输入职工号、、应发工资、社保金,实发工资由公式:

Fsalary=Ssalary-Security计算。

定义输出职工工资的成员函数Show()。

在显示函数Show()中,职工号、的输出域宽为8、左对齐,其余数据的输出域宽为10、右对齐、保留小数点后两位,输出格式均用预定义格式控制函数设置。

在主函数中用Laborage类定义职工对象数组a[3]。

用Input()输入职工工资,用Show()显示每个职工的工资。

(提示:

用getline输入后,必须用回车结束输入)

实验数据:

1001ZhouZhi3000200

1002ChenHua4000400

1003WangFan5000500

实验代码:

#include

#include

#include

usingnamespacestd;

classLaborage

{

public:

Laborage(){}

voidinput();

voiddisplay();

private:

intnum;

charname[10];

floatSsalary;

floatSecurity;

floatFsalary;

};

voidLaborage:

:

input()

{

cin>>num;

cin.get(name,10,'\n');

cin>>Ssalary;

cin>>Security;

Fsalary=Ssalary-Security;

}

voidLaborage:

:

display()

{

cout<

:

right)<

:

left)<

cout<

:

left)<

cout<

:

right)<

:

fixed)<

(2)<

cout<

:

right)<

:

fixed)<

(2)<

cout<

:

right)<

:

fixed)<

(2)<

cout<

}

intmain(void)

{

Laboragelab[3];

inti;

for(i=0;i<3;i++)

{

cout<<"请输入第"<

";

lab[i].input();

}

for(i=0;i<3;i++)

{

lab[i].display();

}

return0;

}

实验结果:

代码分析:

1)在输入时调用getline()以换行符作为输入时的结束标志,已达到输入空格的目的

2)输出时采用resetiosflags(ios:

:

right)实现结束向左对齐,用setw(8)实现输出域宽为8,使用一系列的格式控制字符来实现输出格式的控制。

2.重载运算符“<<”和“>>”,使其能够输入一件商品的信息和输出这件商品的信息。

商品的信息由编号、商品名和价格。

假如商品类Merchandise的框架如下:

classmerchandise{

public:

Merchandiss();

~Merchandiss();

friendistream&operator>>(istream&in,Merchandiss&s);

friendostream&operator<<(ostream&out,Merchandiss&s);

private:

intno;

char*name;

doubleprice;

};

要现该类,并编写以下的main函数对该类进行操作。

intmain()

{Merchandisemer;

cin>>mer;

cout<

return0;

}

实验代码:

#include

usingnamespacestd;

classmerchandise{

public:

merchandise(){no=0;name[0]='\0';price=1;}

~merchandise(){}

friendistream&operator>>(istream&in,merchandise&s);

friendostream&operator<<(ostream&out,merchandise&s);

private:

intno;

charname[100];

doubleprice;

};

istream&operator>>(istream&in,merchandise&s)

{

cout<<"请输入商品信息(编号、名称、价格):

";

in>>s.no;

in.getline(s.name,100,'\n');

in>>s.price;

returnin;

}

ostream&operator<<(ostream&out,merchandise&s)

{

cout<<"商品信息显示如下:

"<

out<

returnout;

}

intmain()

{

merchandisemer;

cin>>mer;

cout<

return0;

}

实验结果:

实验分析:

先定义一个商品的类,构造函数定义为无参函数在函数体中将name置为空,其他为0.在重载>>时使用getline函数,输入name以’\n’作为结束标记

重载<<时直接输出。

在主函数中调用这些函数实现输入输出的功能。

3.将一个源文件复制为两个不同名目的文件,源文件与目的文件均用构造函数打开,使用成员函数get与put复制第一个目的文件,使用getline与插入运算符复制第二个目的文件。

(提示:

用get函数将输入文件流对象的指针指向文件尾后,无法将该指针移到文件首位置。

所以只能定义两个输入文件流对象打开同一源文件,用于两种方式的文件复制。

实验数据:

源文件:

e:

\ex\a.txt,文件容为soucefile

目的文件1:

e:

\ex\b.txt

目的文件2:

e:

\ex\c.txt

实验代码:

#include

#include

#include

usingnamespacestd;

voidcreatefile()

{

ofstreamoutfile("a.txt");

if(!

outfile)

{

cerr<<"opena.txterror!

"<

exit

(1);

}

charstr[100];

cin.getline(str,100,'\n');

outfile<

outfile.close();

}

voidcopyfile_b()

{

ofstreamoutfile("b.txt");

if(!

outfile)

{

cerr<<"openb.txterror!

"<

exit

(1);

}

ifstreaminfile("a.txt");

if(!

infile)

{

cerr<<"opena.txterror!

"<

exit

(1);

}

charch;

while(infile.get(ch))

{

outfile<

}

outfile.close();

infile.close();

}

voidcopyfile_c()

{

ofstreamoutfile("c.txt");

if(!

outfile)

{

cerr<<"openc.txterror!

"<

exit

(1);

}

ifstreaminfile("a.txt");

if(!

infile)

{

cerr<<"opena.txterror!

"<

exit

(1);

}

charch;

while(infile.get(ch))

{

outfile<

}

outfile.close();

infile.close();

}

voiddisplay(char*filename)

{

ifstreaminfile(filename);

if(!

infile)

{

cerr<<"openthefileerror!

"<

exit

(1);

}

charch;

while(infile.get(ch))

{

cout.put(ch);

}

cout<

infile.close();

}

intmain()

{

createfile();

copyfile_b();

copyfile_c();

cout<<"a文?

件t中D的Ì?

¨²容¨Y为a:

êo";

display("a.txt");

cout<<"b文?

件t中D的Ì?

¨²容¨Y为a:

êo";

display("b.txt");

cout<<"c文?

件t中D的Ì?

¨²容¨Y为a:

êo";

display("c.txt");

return0;

}

实验结果:

定义几个函数分别实现:

创建文件、复制文件、读取文件中的容到显示器

在主函数中调用创建函数,创建一个文件a,调用复制文件的函数将a中的容复制到文件b,c中在调用读取文件的函数将a、b、c中的容输出到显示器中。

4.将存放在源文件(e:

\ex\array1.txt)中学生成绩读入二维整型数组a[3][5]中,数组a的第0列存放学号,第4列存放平均成绩。

计算出每个学生的平均成绩,用擂台法对数组a按平均成绩升序排序后,存放在目的文件(e:

\ex\array2.txt)中。

学生的学号与成绩如实验数据所示。

编写程序实现上述要求。

实验数据:

源文件:

e:

\ex\array1.txt,容如下:

10019085800

10028070600

10038580750

目的文件:

e:

\ex\array2.txt

实验代码:

#include

#include

usingnamespacestd;

voidcreatefile()

{

ofstreamoutfile("array1.txt");

inta[3][4];

inti,j;

for(i=0;i<3;i++)

{

cout<<"请?

输º?

入¨?

第̨²"<

学¡ì生¦¨²的Ì?

信?

息¡é:

êo";

for(j=0;j<4;j++)

{

cin>>a[i][j];

}

}

for(i=0;i<3;i++)

{

for(j=0;j<4;j++)

{

outfile<

outfile<<'';

}

outfile<<'\n';

}

}//创ä¡ä建¡§文?

件tarray1

voidsort()//排?

序¨°并¡é创ä¡ä建¡§文?

件tarray2

{

ifstreaminfile("array1.txt");

inta[3][5];

inti,j,t;

doubles=0;

for(i=0;i<3;i++)

{

for(j=0;j<4;j++)

{

infile>>a[i][j];

s=s+a[i][j];

}

s=(s-a[i][0])/3;

a[i][4]=s;

s=0;

}

for(j=0;j<2;j++)

{

for(i=0;i<2-j;i++)

{

if(a[i][4]>a[i+1][4])

{

for(t=0;t<5;t++)

{

s=a[i][t];

a[i][t]=a[i+1][t];

a[i+1][t]=s;

}

}

}

}

ofstreamoutfile("array2.txt");

if(!

outfile)

{

cerr<<"openfileerror!

";

exit

(1);

}

for(i=0;i<3;i++)

{

for(j=0;j<5;j++)

{

outfile<

outfile<<'';

}

outfile<<'\n';

}

}

voiddisplay_file(char*filename)

{

ifstreaminfile(filename);

if(!

infile)

{

cerr<<"openfileerror!

"<

exit

(1);

}

inta[3][5];

inti,j;

for(i=0;i<3;i++)

{

for(j=0;j<5;j++)

{

infile>>a[i][j];

cout<

}

cout<

}

cout<

}

intmain()

{

createfile();

sort();

display_file("array2.txt");

return0;

}

实验结果:

实验分析:

定义三个函数分别实现:

创建文件,排序,输出文件。

排序采用冒泡排序将平均成绩排序,若前面的大于后面的,将各个对应项交换,达到排序的目的。

在主函数中调用这三个函数,实现程序的功能

5.编写一个程序,将两个文本文件连接成一个文件,然后将此文件中所有小写字母转换成大写字母,并打印出来。

实验代码:

#include

#include

#include

usingnamespacestd;

voidcreatefile(char*filename)

{

ofstreamoutfile(filename);

if(!

outfile)

{

cerr<<"openfileerror!

"<

exit

(1);

}

charstr[80];

cin.getline(str,80);

inti=0;

while(str[i])

{

outfile<

i++;

}

outfile.close();

}

voidchange()

{

ifstreaminfile("c.txt");

if(!

infile)

{

cerr<<"openfileerror!

"<

exit

(1);

}

ofstreamoutfile("d.txt");

if(!

outfile)

{

cerr<<"openfileerror!

"<

exit

(1);

}

charstr;

while(infile.get(str))

{

if(str>=97&&str<=122)

str=str-32;

outfile.put(str);

cout<

}

cout<

infile.close();

outfile.close();

}

voidcopyfile()

{

charch;

ifstreaminfile("d.txt");//读¨¢取¨?

磁ä?

盘¨¬的Ì?

文?

本À?

文?

件t

ofstreamoutfile("c.txt");//创ä¡ä建¡§文?

本À?

文?

件t

while(infile.get(ch))//读¨¢取¨?

文?

本À?

中D的Ì?

¨²容¨Y

{

cout<

出?

文?

本À?

¨²容¨Y到Ì?

¨²存ä?

outfile<

¨²容¨Y到Ì?

文?

件t

}

infile.close();//关?

闭À?

文?

件t流¢¡Â

outfile.close();

cout<

}

intmain()

{

cout<<"请?

输º?

入¨?

a文?

件t中D的Ì?

数ºy据Y:

êo";

createfile("a.txt");

cout<<"请?

输º?

入¨?

b文?

件t中D的Ì?

数ºy据Y";

createfile("b.txt");

system("copya.txt+b.txtc.txt");

change();

copyfile();

return0;

}

实验结果:

实验分析:

定义三个函数:

create,change,copy。

分别实现创建文件,改变大小写,拷贝,程序中先建立文件a,b,在用库函数system将两个文件成一个文件c,在调用change函数,将文件c中的字母改为大写,并保存到d文件中,最后调用copy函数将d中的容拷贝到c中并覆盖。

从而使c中的字母都为大写。

6.产生一个二进制数据文件,将n~m之间的所有素数写入文件data.dat中。

从数据文件中读取二进制数据,并在显示器上以每行5个数的形式显示。

实验数据:

n=100

m=400

实验代码:

#include

#include

#include

usingnamespacestd;

intjudge(inta)//求a是否是素数如果是则输出

{

inttemp=sqrt(a*1.0);

inti;

for(i=2;i

{

if(a%i==0)

return0;

}

cout<

return1;

}

voidcreatefile()/创建文件a将素数存入

{

ofstreamoutfile("1.dat");

if(!

outfile)

{

cerr<<"openfileerror!

"<

exit

(1);

}

inti;

intc=0;

for(i=201;i<=400;i++)

{

if(judge(i))//若i是素数,则将i存入文件中

{

outfile<

c++;

if(c%5==0)

{

cout<

outfile<

}

}

}

cout<

outfile.close();

}

 

intmain()

{

createfile();

return0;

}

实验结果:

代码分析:

定义两个函数judge、create。

分别实现判断素数,及创建文件的功能。

Create中调用judge函数,判断judge为1时该数是素数,将次数存入文件,同时定义一个计数的变量,当变量为5的倍数时换行,从而保证文件中是5个数一行。

在主函数中调用函数。

7.编写一个程序,可以读入一个C++语言的源文件,每一行加上行号后保存到另一个后缀为.prn的同名文件中,同时输出到屏幕上。

实验代码:

#include

#include

#include

usingnamespacestd;

voidkeep()

{

strings,name,name1;

cout<<"请输入c++源文件的名称(不含有后缀名)";

cin>>name;

name1=name+".prn";

name+=".cpp";

ifstreamread(name.c_str());

fstreamwrite;

write.open(name1.c_str(),ios:

:

trunk|ios:

:

out);

inti=0;

if(!

read)

{

cout<<"Cannotopeninputfile\n";

}

if(!

write)

{

cout<<"Cannotopenoutputfile\n";

}

while(getline(read,s))

cout<

write<<++i<

read.close();

write.close();

cout<<"目标文件生成成功(和源文件同目录)"<

}

intmain()

{

keep();

return0;

}

代码分析:

定义一个函数主要实现打开、输出、保存的功能。

在函数中定义一个name的字符指针,用来存储文件名,打开相应的文件时只需在name后加上后缀.cpp

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

当前位置:首页 > 成人教育 > 电大

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

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