C++实验四.docx
- 文档编号:28749936
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:17
- 大小:24.26KB
C++实验四.docx
《C++实验四.docx》由会员分享,可在线阅读,更多相关《C++实验四.docx(17页珍藏版)》请在冰豆网上搜索。
C++实验四
实验 四C++的流类库与输入输出
实验课程名:
面向对象的程序设计
专业班级:
学号:
姓名:
实验时间:
实验地点:
指导教师:
一、实验目的与要求
(1)掌握C++格式化的输入输出方法、
(2)掌握重载运算符“〈<”与“〉〉”的方法。
(3)掌握磁盘文件的输入输出方法。
。
二、实验内容。
1、下面给出的shiyan4-1、cpp程序用于打印九九乘法表,但程序中存在错误、请上机调试,使得此程序运行后,能够输出如下所示的九九乘法表。
* 1 23 4 5 67 89
11
2 2 4
3 3 6 9
4 48 12 16
55 10 15 2025
6 6 1218 243036
77 14 2128 354249
8 816 24 3240 48 56 64
991827 36 45 546372 81
//shiyan4-1、cpp
#include<iostream>
#include<iomanip〉
usingnamespacestd;
intmain()
{
inti,j;
cout〈<”*”;
for(i=1;i〈=9;i++)
cout〈
cout〈<endl;
for(i=1;i〈=9;i++)
{ cout<〈i〈<” ”;
for(j=1;j<=i;j++)
{cout<<i*j〈<””;}
}
return0;
}
更正后:
#include <iostream>
#include<iomanip>
usingnamespacestd;
intmain()
{
inti,j;
cout<<"*”;
for(i=1;i〈=9;i++)
cout<〈i〈〈"";
cout〈<endl;
for(i=1;i<=9;i++)
{ cout<〈i<〈”";
for(j=1;j〈=i;j++)
{cout〈<i*j<<"”;}
cout<<endl;
}
return0;
}
运行结果:
2、下面的程序用于统计文件xyz、txt中的字符个数,请填空完成程序。
//shiyan4-2、cpp
#include<iostream>
#include〈fstream〉
using namespacestd;
intmain()
{char ch;
inti=0;
ifstreamfile;
(“xyz、txt",ios:
:
in);
if(
!
file)
{
cout<<"xyz、txt cannotopen”< abort(); } while(! ()) { (ch); i++; } cout〈<"文件字符个数: "〈〈i< ③(); return0; } 3、重载运算符“〈<"与“〉〉”,使其能够输入一件商品的信息与输出这件商品的信息。 商品的信息由编号、商品名与价格、假如商品类Merchandise的框架如下: classmerchandise{ public: Merchandiss(); ~Merchandiss(); friendistream& operator>〉(istream&in,Merchandiss&s); friendostream&operator〈〈(ostream&out,Merchandiss&s); private: int no; char*name; double price; }; 要求实现该类,并编写以下的main函数对该类进行操作。 intmain() {Merchandisemer; cin〉>mer; cout<〈mer; return0; } 程序代码: #include〈iostream〉 #include〈string> using namespacestd; classMerchandise{ public: Merchandise(int n=0,charna='a’,doublep=0、0) { ﻩno=n; name=na; ﻩprice=p; } ~Merchandise(){} friendostream& operator<<(ostream& out,Merchandise&s) { ﻩcout〈<”编号: "<〈s、no<〈endl; ﻩcout<<"名称: "<〈s、name<〈endl; ﻩcout<〈”价格: ”〈 returnout; } friendistream&operator〉>(istream&in,Merchandise& s) { ﻩcout〈〈”编号: ”; ﻩcin>>s、no; cout<<”名称: ”; ﻩcin〉〉s、name; ﻩcout〈〈"价格: "; ﻩcin〉〉s、price; ﻩreturnin; } private: intno; charname; doubleprice; }; int main() { ﻩMerchandisemer; ﻩcout〈〈”输入商品信息: "〈〈endl; ﻩcin>>mer; ﻩcout〈〈"输出商品信息: ”〈〈endl; ﻩcout〈<mer; return0; } 运行结果: 4、编写一个程序,将两个文本文件连接成一个文件,然后将此文件中所有小写字母转换成大写字母,并打印出来、 程序代码: #include〈iostream〉 #include<fstream> #include〈string〉 usingnamespacestd; int main() {ofstreamfout1("f1、txt",ios: : out); if(! fout1) { cout<<"f1、txt canntopen、\n”; exit (1); } fout1〈〈"Thisis a”; ofstream fout2(”f2、txt”,ios: : out); if(! fout2) {cout<〈"f2、txtcanntopen、\n”; exit (1); } fout2<〈”program"; fout1、close(); fout2、close(); ifstreamfin2("f2、txt”,ios: : in); ofstreamfout3(”f1、txt",ios: : app); stringstr; while (fin2〉〉str) {fout3<<str; } fin2、close(); fout3、close(); ifstreamfin1("f1、txt”,ios: : in); if(! fin1) {cout<〈”f1、txtcanntopen、\n”; exit(1); } char ch; while(! fin1、eof()) { fin1、get(ch); if(ch>=’a'&&ch〈='z') { ch=ch—32; } cout〈<ch; } cout〈<endl; fin1、close(); return0; } 运行结果: 5、将一个源文件复制为两个不同名目的文件,源文件与目的文件均用构造函数打开,使用成员函数get与put复制第一个目的文件,使用getline与插入运算符复制第二个目的文件、(提示: 用get函数将输入文件流对象的指针指向文件尾后,无法将该指针移到文件首位置、因此只能定义两个输入文件流对象打开同一源文件,用于两种方式的文件复制。 ) 实验数据: 源文件: e: \ex\a、txt,文件内容为soucefile 目的文件1: e: \ex\b、txt 目的文件2: e: \ex\c、txt 程序代码: #include〈iostream〉 #include〈fstream> #include〈string〉 using namespace std; void createfile() { ofstream outfile("a、txt"); if(! outfile) { cerr<<”opena、txterror! ”<〈endl; exit(1); } char str[100]; cin、getline(str,100,’\n’); outfile〈〈str; out(); } void copyfile_b() { ofstreamoutfile(”b、txt"); if(! outfile) { cerr〈〈"openb、txterror! ”<<endl; exit(1); } ifstreaminfile("a、txt"); if(! infile) { cerr<<”open a、txt error! ”<〈endl; exit (1); } charch; while (in(ch)) { outfile< } out(); in(); } void copyfile_c() { ofstreamoutfile("c、txt”); if (! outfile) { cerr<〈"openc、txterror! "<〈endl; exit (1); } ifstreaminfile(”a、txt”); if(! infile) { cerr<〈”open a、txterror! "〈 exit (1); } char ch; while (in(ch)) { outfile〈〈ch; } out(); in(); } voiddisplay(char*) { ifstreamin); if(! infile) { cerr〈<"openthe! "<<endl; exit (1); } charch; while (in(ch)) { cout、put(ch); } cout<〈endl; in(); } int main() { createfile(); copyfile_b(); copyfile_c(); cout〈〈”a文件t中D的内容为: ”; display(”a、txt”); cout〈〈"b文件t中D的内容为: ”; display(”b、txt”); cout<〈"c文件t中D的内容为: "; display(”c、txt”); return 0; } 运行结果: 6、 将存放在源文件(e: \ex\array1、txt)中学生成绩读入二维整型数组a[3][5]中,数组a的第0列存放学号,第4列存放平均成绩、计算出每个学生的平均成绩,用擂台法对数组a按平均成绩升序排序后,存放在目的文件(e: \ex\array2、txt)中。 学生的学号与成绩如实验数据所示、编写程序实现上述要求、 实验数据: 源文件: e: \ex\array1、txt,内容如下: 100190 8580 ﻩ0 10028070ﻩ60ﻩﻩ0 1003ﻩ8580ﻩﻩ75ﻩ0 目的文件: e: \ex\array2、txt 程序代码: #include〈iostream〉 #include〈fstream〉 usingnamespace std; void createfile() { ofstreamoutfile(”array1、txt”); inta[3][4]; inti,j; for (i=0;i<3;i++) { cout〈<”请输入第”〈〈i+1<〈”个学生的信息: "; for (j=0;j<4;j++) { cin>〉a[i][j]; } } ﻩfor (i=0;i<3;i++) { for (j=0;j<4;j++) { outfile<〈a[i][j]; outfile<<' ’; } outfile〈〈’\n'; } }//tarray1 void sort() //排序并创建文件tarray2 { ifstreaminfile("array1、txt”); inta[3][5]; inti,j,t; double s=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<〈”open ! ”; exit (1); } for (i=0;i<3;i++) { for (j=0;j〈5;j++) { outfile<<a[i][j]; outfile<<'’; } outfile<〈’\n'; } } voiddisplay_*) { ifstreamin); if(! infile) { cerr<〈"open! "〈<endl; exit (1); } inta[3][5]; int i,j; for (i=0;i〈3;i++) { for (j=0;j<5;j++) { infile>>a[i][j];
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 实验
![提示](https://static.bdocx.com/images/bang_tan.gif)