西安交大C++程序设计第十一章作业_第1页
西安交大C++程序设计第十一章作业_第2页
西安交大C++程序设计第十一章作业_第3页
西安交大C++程序设计第十一章作业_第4页
西安交大C++程序设计第十一章作业_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

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

文档简介

1、西安交通大学实验报告课程 计算机程序设计 实验名称_标准库和输入输出流_第 1页 共25页系 别_ _ 实 验 日 期 2014 年 6 月 7 日专业班级_ _组别_ _ 实 验 报 告 日 期 2014 年 6 月 7 日姓 名_ _学号_ _ 报 告 退 发 ( 订正 、 重做 )同 组 人_ 教 师 审 批 签 字 一、 实验目的掌握C+语言输入和输出操作的方法、流和流类库的使用方法。二、实验内容 (一)第一题: 编写一个程序,分别用不同的域宽(010)打印出整数12345和浮点数1.2345。观察当域宽小于数值的实际需要域宽时会发生什么状况。1.源程序代码:#include<i

2、ostream>using namespace std;void main()int WIDTH;for(WIDTH=10;WIDTH>0;WIDTH-)cout<<"域宽、精度为"<<WIDTH<<":n"cout.precision(WIDTH);/全局作用cout.width(WIDTH);/只起一次作用cout<<12345<<"n"<<1.2345<<endl;2.实验结果:3.结论:(1)域宽不够时会自动补足。(2)精度只需一次

3、定义则一直有效。(3)域宽需要每次输出时均进行定义(二)第二题: 编写一个程序,将华氏温度0度212度转化为浮点型摄氏温度,浮点精度为3.转换公式为如下:Celsius=5.0/9.0*(Fahrenheit-32);输出用两个右对齐序列,摄氏温度前面加上正负号。1. 源程序代码: #include<iostream>using namespace std;void main()double Celsius,Fahrenheit;cout.precision(3);cout<<"转换结果为:n"for(Fahrenheit=0;Fahrenheit&

4、lt;=212;Fahrenheit+)cout.unsetf(ios:showpos);Celsius=5.0/9.0*(Fahrenheit-32);cout<<"华氏"<<Fahrenheit<<"度=摄氏"cout.setf(ios:showpos);cout<<Celsius<<"度n"2.实验结果: (三)第三题: 编写一个程序,打印出ASC字符集中码值为33126的字符的ASC码表。要求输出十进制值、八进制值、十六进制值以及码值所表示的字符。1.源程序代码:#i

5、nclude<iostream>using namespace std;void main()int a;char w;cout<<"字符 八进制 十进制 十六进制n"for(a=33;a<=126;a+)w=a;cout<<w<<"t"<<oct<<a<<"t"<<dec<<a<<"t"<<hex<<a<<endl;2.实验结果:四、第四题:修改例11-

6、2中的程序,重载>>运算符,使其能够直接使用cin语句输入Date类对象。1.源程序代码:/ 日期类定义date.h#ifndef DATE_H#define DATE_H#include <iostream>using namespace std;class Date friend ostream &operator<<(ostream &,const Date &);friend istream &operator>>(istream &,Date &);int day,month,year;v

7、oid IncDay();/日期增加一天int DayCalc() const;/距基准日期的天数static const int days;/每月的天数public:Date( int y, int m, int d);/构造函数Date( int m, int d);/构造函数,年默认为系统当前年份Date();/构造函数,默认为系统日期void SystemDate();void SetDate( int yy, int mm, int dd );/日期设置void SetDate( int mm, int dd );/日期设置,年默认为系统年份bool IsLeapYear(int y

8、y) const;/ 是否闰年?bool IsEndofMonth() const;/ 是否月末?void print_ymd() const;/输出日期yy_mm_ddvoid print_mdy() const;/输出日期mm_dd_yyconst Date &operator+(int days); / 日期增加任意天const Date &operator+=(int days); / 日期增加任意天int operator-(const Date& ymd)const; / 两个日期之间的天数void Show(Date& da);#endif/ Da

9、te类成员函数定义date.cpp#include <iostream>#include <time.h>#include "date.h"using namespace std;const int Date:days = 0, 31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31 ;/构造函数Date:Date(int y,int m,int d) SetDate(y,m,d); Date:Date(int m,int d) SetDate(m,d); Date:Date() SystemDate();void

10、 Date:SystemDate()/取得系统日期tm *gm;time_t t=time(NULL);gm = gmtime(&t);year = 1900 + gm->tm_year;month = gm->tm_mon +1;day = gm->tm_mday;void Date:SetDate( int yy, int mm, int dd )month = ( mm >= 1 && mm <= 12 ) ? mm : 1;year = ( yy >= 1900 && yy <= 2100 ) ? yy

11、: 1900;if ( month = 2 && IsLeapYear( year ) )day = ( dd >= 1 && dd <= 29 ) ? dd : 1;elseday = ( dd >= 1 && dd <= days month ) ? dd : 1;void Date:SetDate(int mm, int dd )tm *gm;time_t t=time(NULL);gm = gmtime(&t);month = ( mm >= 1 && mm <= 12 ) ?

12、mm : 1;year = 1900 + gm->tm_year;if ( month = 2 && IsLeapYear( year ) )day = ( dd >= 1 && dd <= 29 ) ? dd : 1;elseday = ( dd >= 1 && dd <= days month ) ? dd : 1;const Date &Date:operator+( int days )/重载+for ( int i = 0; i < days; i+ )IncDay();return *thi

13、s;const Date &Date:operator+=( int days )/重载+=for ( int i = 0; i < days; i+ )IncDay();return *this;int Date:operator-(const Date& ymd )const/重载-int days;days = DayCalc()-ymd.DayCalc();return days;bool Date:IsLeapYear( int y ) constif ( y % 400 = 0 | ( y % 100 != 0 && y % 4 = 0 ) )

14、return true;return false;bool Date:IsEndofMonth() constif ( month = 2 && IsLeapYear( year ) )return day = 29; /二月需要判断是否闰年elsereturn day = days month ;void Date:IncDay()/日期递增一天if ( IsEndofMonth()if (month = 12) / 年末day = 1;month = 1;year+;else / 月末day = 1;month+;else day+;int Date:DayCalc() c

15、onstint dd;int yy = year - 1900;dd = yy*365;if(yy) dd += (yy-1)/4;for(int i=1;i<month;i+)dd += daysi;if(IsLeapYear(year)&&(month>2)dd+;dd += day;return dd;void Date:print_ymd() constcout << year << "-" << month << "-" << day << en

16、dl;void Date:print_mdy() constchar *monthName 12 = "January","February", "March", "April", "May", "June","July", "August", "September", "October","November", "December" ;cout <&

17、lt; monthName month-1 << ' '<< day << ", " << year << endl;void Date:Show(Date& da)if(day=da.day&&month=da.month)cout<<"Happy Birthday!"elseif(da-Date:Date()<0)da.year+;cout<<"It will be your birthday after &qu

18、ot;<<da-Date:Date()<<" days!"ostream &operator<<(ostream &output,const Date &d)static char *monthName12="January","February","March","April","May","June","July","August","Sept

19、ember","October","November","December"output<<monthNamed.month-1<<' '<<d.day<<","<<d.year;return output;istream &operator>>(istream &in,Date &d)/int a,b,c;in>>a>>b>>c;d.year=a;d.mon

20、th=b;d.day=c;return in;/main.cpp修改例-9中的程序,重载>>运算符,使其能够直接使用cin语句输入Date类对象。#include <iostream>#include "date.h"using namespace std;int main()Date today,Olympicday(2004,8,13);cout << "Today (the computer's day) is: "<<today<<endl;/today += 365;/cout

21、 << "After 365 days, the date is: "<<today<<endl;Date testday(2,28);cout << "the test date is: "<<testday<<endl;/Date nextday = testday + 1;cout << "the next date is: "<<testday<<endl;/today.SystemDate();cout <<

22、; "the Athens Olympic Games openday is: "<<Olympicday<<endl;cout << "And after " << Olympicday-today<< " days, the Athens Olympic Games will open." <<endl;Date birthday;cout<<"输入出生年月日:"cin>>birthday;cout<<&

23、quot;输入的是:"<<birthday<<endl;return 0;2.实验结果:五、第五题:编写一个程序,可以读入一个C+语言的源文件,每一行加上行号后保存到另一个后缀为.prn的同名文件中。 1.源程序代码:#include <fstream>#include <string>#include<iostream>using namespace std;int main()string s,name,name1;cout<<"请输入源c+文件的名称(不含有后缀名):"cin>&g

24、t;name;name1=name+".prn"name+=".cpp"ifstream read(name.c_str();/读取的文件,用name.c_str()是将string 的char *类型转换成const的类型,不然会出错fstream write;write.open(name1.c_str(),ios:trunc/*创建文件*/|ios:out/*写文件*/);/输出的文件,创建文件并写文件int i=0;if(!read)cout<<"Cannot open input filen"return 0;/

25、如果打开输入文件失败if(!write)cout<<"Cannot open output filen"return 0;/如果打开输出文件失败while (getline(read,s)/逐行读取文件中数据到字符串s中write<<+i<<s<<endl;/输出读取文件的信息到目标文件read.close();write.close();/关闭文件cout<<"目标文件生成成功!(和源文件同目录)"<<endl;return 0;2.实验结果:实验前:实验后:六、第六题:将一个文本

26、文件内容用凯撒尔方法加密,密钥是4。即文本文件中字母用其后第4个字母代替,若是数字则用其后第4个数字代替。例如文本文件原内容为“Red And Black 2008”,加密后文本文件内容为“Vih Erh Fpego 6442”。(提示:先用记事本创建一个文本文件,其内容应该是有意义的英文句子,然后以只读方式打开,加密后的内容写入另一个文本文件中。)1.源程序代码:#include<iostream>#include<fstream>using namespace std;char jiami(char ch);int main()ifstream in("t

27、est.txt");ofstream out("test_1.txt");if(!in)cout<<"Can not open the file."<<endl;return 1;if(!out)cout<<"Can not open the file."<<endl;return 1;char ch;while(in)in.get(ch);ch=jiami(ch);if(in)out<<ch;in.close();out.close();return 0;char

28、 jiami(char ch)if(ch>='0'&&ch<='9')if(ch>'5')ch=ch-6;else ch+=4;else if(ch>='a'&&ch<='z')if(ch>='w')ch=ch+4-'z'+'a'-1;else ch+=4;else if(ch>='A'&&ch<='Z')if(ch>='W&#

29、39;)ch=ch+4-'Z'+'A'-1;else ch+=4;return ch;2.实验结果:加密前:加密后:(七)第七题: (必作题)找出100以内的勾股数,输出到文件gouku中。所谓勾股数指找出三个数满足A2+B2=C2,并且A<B<C。要求将三个勾股数的计算公式A2+B2=C2输出到文件中的每一行,例如32+42=52 9+16=25。1.源程序代码:#include<iostream>#include<fstream>#include<iomanip>using namespace std;int main()ofstream out("gougu.txt");if(!out)cout<<"Can not open the file.n"return 1;for(int i=0;i<101;i+)for(int j=i+1;j<101;j+)for(int

温馨提示

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

最新文档

评论

0/150

提交评论