实验四 输入输出流
实验课程名:面向对象程序设计(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输入姓名后,必须用回车结束姓名输入) 实验数据: 1001 Zhou Zhi 3000 200 1002 Chen Hua 4000 400 1003 Wang Fan 5000 500 实验代码: #include #include #include using namespace std; class Laborage { public: Laborage(){} void input();void display(); private: int num; char name[10]; float Ssalary; float Security; float Fsalary; }; void Laborage::input() { cin>> num; cin.get(name,10,'\\n'); cin>> Ssalary; cin>> Security; Fsalary = Ssalary - Security; } void Laborage::display() { cout<< resetiosflags(ios::right)<代码分析: 1)在输入时调用getline()以换行符作为输入姓名时的结束标志,已达到输入空格的目的 2)输出时采用resetiosflags(ios::right)实现结束向左对齐,用setw(8)实现输出域宽为8,使用一系列的格式控制字符来实现输出格式的控制。 2. 重载运算符“<<”和“>>”,使其能够输入一件商品的信息和输出这件商品的信息。商品的信息由编号、商品名和价格。假如商品类Merchandise的框架如下: class merchandise{ public: Merchandiss(); ~Merchandiss(); friend istream& operator>>(istream& in,Merchandiss& s); friend ostream&operator<<(ostream& out,Merchandiss& s); private: int no; char *name; double price; }; 要求实现该类,并编写以下的main函数对该类进行操作。 int main() { Merchandise mer; cin>>mer; cout< using namespace std; class merchandise{public: merchandise(){no=0;name[0]='\\0';price=1;} ~merchandise(){} friend istream& operator>>(istream& in,merchandise& s); friend ostream&operator<<(ostream& out,merchandise& s); private: int no; char name[100]; double price; }; istream& operator>>(istream& in,merchandise& s) { } ostream& operator<<(ostream& out,merchandise& s) { cout<<\"商品信息显示如下:\"<>s.no; in.getline(s.name,100,'\\n'); in>>s.price; return in;} out<>mer; cout<>时使用getline函数,输入name以’\\n’作为结束标记 重载<<时直接输出。在主函数中调用这些函数实现输入输出的功能。 3.将一个源文件复制为两个不同名目的文件,源文件与目的文件均用构造函数打开,使用成员函数get与put复制第一个目的文件,使用getline与插入运算符复制第二个目的文件。(提示:用get函数将输入文件流对象的指针指向文件尾后,无法将该指针移到文件首位置。所以只能定义两个输入文件流对象打开同一源文件,用于两种方式的文件复制。) 实验数据: 源文件:e:\\ex\\a.txt,文件内容为souce file 目的文件1:e:\\ex\\b.txt 目的文件2:e:\\ex\\c.txt 实验代码: #include #include #include using namespace std; void createfile() { } void copyfile_b() { ofstream outfile(\"b.txt\"); if(!outfile) { } ifstream infile(\"a.txt\"); if(!infile) { } char ch; while(infile.get(ch)) { } outfile<} outfile.close(); infile.close(); void copyfile_c() { } void display(char *filename) { } int main() { createfile(); ifstream infile(filename); if(!infile) { } char ch; while(infile.get(ch)) { } cout<} 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\"); return 0; 实验结果: 定义几个函数分别实现:创建文件、复制文件、读取文件中的内容到显示器 在主函数中调用创建函数,创建一个文件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,内容如下: 1001 90 85 80 0 1002 80 70 60 0 1003 85 80 75 0 目的文件:e:\\ex\\array2.txt 实验代码: #include #include using namespace std; void createfile() { ofstream outfile(\"array1.txt\"); int a[3][4]; int i,j; for(i=0;i<3;i++) { cout<<\"请?输º?入¨?第̨²\"<>a[i][j]; } for(i=0;i<3;i++) { } for(j=0;j<4;j++) { } outfile<<'\\n'; outfile<}//创ä¡ä建¡§文?件tarray1 void sort()//排?序¨°并¡é创ä¡ä建¡§文?件tarray2 { ifstream infile(\"array1.txt\"); int a[3][5]; int i,j,t; double s=0; for(i=0;i<3;i++) { } for(j=0;j<2;j++) { } ofstream outfile(\"array2.txt\"); if(!outfile) { } for(i=0;i<3;i++) { for(j=0;j<5;j++) { outfile<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; for(j=0;j<4;j++) { } s=(s-a[i][0])/3; a[i][4]=s; s=0; infile>>a[i][j]; s=s+a[i][j];} } } outfile<<' '; outfile<<'\\n'; void display_file(char *filename) { } int main() { createfile(); sort(); display_file(\"array2.txt\"); return 0; } ifstream infile(filename); if(!infile) { } int a[3][5]; int i,j; for(i=0;i<3;i++) { } for(j=0;j<5;j++) { } cout<>a[i][j]; cout<实验分析: 定义三个函数分别实现:创建文件,排序,输出文件。排序采用冒泡排序将平均成绩排序,若前面的大于后面的,将各个对应项交换,达到排序的目的。 在主函数中调用这三个函数,实现程序的功能 5. 编写一个程序,将两个文本文件连接成一个文件,然后将此文件中所有小写字母转换成大写字母,并打印出来。 实验代码: #include #include #include using namespace std; void createfile(char *filename) { ofstream outfile(filename); if(!outfile) { } char str[80]; cin.getline(str,80); int i=0; cerr<<\"open file error!\"<} while(str[i]) { } outfile.close(); outfile<=97&&str<=122) str=str-32; cerr<<\"open file error!\"<} infile.close(); //关?闭À?文?件t流¢¡Â outfile.close(); cout<实验分析: 定义三个函数: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 using namespace std; int judge(int a)//求a是否是素数如果是则输出 { } void createfile()/创建文件a将素数存入 { ofstream outfile(\"1.dat\"); if(!outfile) { } int i; cerr<<\"open file error!\"<} int c = 0; for(i=201;i<=400;i++) { } cout<代码分析:定义两个函数judge、create。分别实现判断素数,及创建文件的功能。Create中调用judge函数,判断judge为1时该数是素数,将次数存入文件,同时定义一个计数的变量,当变量为5的倍数时换行,从而保证文件中是5个数一行。在主函数中调用函数。 7.编写一个程序,可以读入一个C++语言的源文件,每一行加上行号后保存到另一个后缀为.prn的同名文件中,同时输出到屏幕上。 实验代码: #include #include #include using namespace std; void keep() { string s,name,name1; cout<<\"请输入c++源文件的名称(不含有后缀名)\"; cin>>name; name1=name+\".prn\"; name+=\".cpp\"; ifstream read(name.c_str()); fstream write; write.open(name1.c_str(),ios::trunk |ios::out ); int i=0; if(!read) { } if(!write) cout<<\"Cannot open input file\\n\";} { } while (getline(read,s)) cout<代码分析: 定义一个函数主要实现打开、输出、保存的功能。在函数中定义一个name的字符指针,用来存储文件名,打开相应的文件时只需在name后加上后缀.cpp就能打开相应得文件。定义一个字符串变量写入时一行一行的写入存到字符串中,并输出到显示器加入行号后保存。 三、结论 通过本次实验,我掌握了输入输出的几种基本的格式控制符。他们在cout<<或cin>>后面控制输入输出的格式。其中有函数get,getline函数进行字符的输入输出,getline(字符指针,字符个数,结束标志); 学会了文件的基本操作,文件流操作函数主要在头文件fstream中,输入流在头文件ifstream,输出流在头文件ofstream中,在定义文件操作流的对象时要加上这两个类