C++面向对象程序设计的课后实验的答案.doc_第1页
C++面向对象程序设计的课后实验的答案.doc_第2页
C++面向对象程序设计的课后实验的答案.doc_第3页
C++面向对象程序设计的课后实验的答案.doc_第4页
C++面向对象程序设计的课后实验的答案.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

实验八实验名称: C+的输入和输出 成绩: 实验目的和要求1 学习C+格式化的输入输出方法。2 学习C重载运算符“”的方法。3 学习C磁盘文件的输入输出方法。实验内容和步骤1. 下面给出的程序test9_1_1.cpp用于打印九九乘法表,但程序中存在错误。请上机调试,使得此程序运行后,能够输出如下所示的九九乘法表。* 1 2 3 4 5 6 7 8 91 12 2 43 3 6 9 4 4 8 12 165 5 10 15 20 256 6 12 18 24 30 367 7 14 21 28 35 42 498 8 16 24 32 40 48 56 649 9 18 27 36 45 54 63 72 81/test9_1_1.cpp#include#includeusing namespace std;int main() int i,j; cout *; for(i=1;i=9;i+)couti ; coutendl;for(i=1;i=9;i+) couti; for(j=1;j=i;j+) couti*j;return 0;修改后的程序为:#include#includeusing namespace std;int main() int i,j; cout* ; for(i=1;i=9;i+) couti ; coutendl; for(i=1;i=9;i+) couti ; for(j=1;j=i;j+) couti*j ;coutendl; return 0;运行结果:2.下面的程序用于统计文件xyz.txt中的字符个数,请填空完成程序。 /test9_2_1.cpp #include #include using namespace std; int main() char ch;int i=0;ifstream file;file.open(xyz.txt,ios:in);if(!file) coutxyz.txt cannot openendl; abort();while (!file.eof() file.get(ch); i+;cout文件字符个数:iendl; file.close(); return 0;运行结果:3.重载运算符“”,使其能够输入一件商品的信息和输出这件商品的信息。商品的信息由编号、商品名和价格。程序如下:#include#includeusing namespace std;class Merchandise public:Merchandise(int n=0,char na=a,double p=0.0) no=n; name=na; price=p;Merchandise()friend ostream& operator(ostream& out,Merchandise& s) outno:s.noendl; outname:endl; outprice:s.price(istream& in,Merchandise& s) couts.no; ; couts.price; return in; private:int no;char name; double price;int main() Merchandise mer; cout输入商品信息:mer; cout输出商品信息:endl; coutmer; return 0;运行结果:4.编写一个程序,将两个文本文件连接成一个文件,然后将此文件中所有小写字母转换成大写字母,并打印出来。程序如下:#include#include#includeusing namespace std;int main() ofstream fout1(f1.txt,ios:out); if(!fout1) coutf1.txt cannt open.n; exit(1); fout1This is a ; ofstream fout2(f2.txt,ios:out); if(!fout2) coutf2.txt cannt open.n; exit(1); fout2str) fout3str; fin2.close(); fout3.close(); ifstream fin1(f1.txt,ios:in); if(!fin1) cout=a&ch=z) ch=ch-32; coutch; coutendl; fin1.close(); return 0;运行结果:实验九实验名称: 异常处理和命令空间的使用 成绩: 实验目的和要求1 学习异常处理的声明和执行过程。2 学习命名空间的定义和使用方法。实验内容和步骤1.分析并调试下列程序,写出程序的输出结果,并分析输出结果。/test10_1.cpp#includeusing namespace std;namespace NS1 int x=10;namespace NS2 int x=20;void main() using NS1:x; using namespace NS2; coutx=xendl;运行结果:2. 下面是一个用于处理文件打不开的异常处理程序,分析程序并完成相应问题。/test10_2.cpp#include#includeusing namespace std;int main() ifstream source(file9_2.txt); char line128; tryif(!source)throwfile9_2.txt; catch(char*s) couterror opening the file sendl; exit(1); while (!source.eof()source.getline(line,sizeof(line); coutlineendl; source.close(); return 0;请回答以下问题:(1) 若其中没有file9_2.text文件,则输出结果如何? (2)在硬盘上建一个file9_2.text文件,其文件内容自己定义。输出结果如何?3.编写一个程序,求输出数的平方根。设置异常处理,对输入负数的情况给出提示。程序如下:#include#includeusing namespace std;int main() double number; double result; coutnumbe

温馨提示

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

评论

0/150

提交评论