1、(1)域宽不够时会自动补足。(2)精度只需一次定义则一直有效。(3)域宽需要每次输出时均进行定义(二)第二题: 编写一个程序,将华氏温度0度212度转化为浮点型摄氏温度,浮点精度为3.转换公式为如下:Celsius=5.0/9.0*(Fahrenheit-32);输出用两个右对齐序列,摄氏温度前面加上正负号。1. 源程序代码: double Celsius,Fahrenheit; cout.precision(3);转换结果为: for(Fahrenheit=0;Fahrenheit=212;Fahrenheit+) cout.unsetf(ios:showpos); Celsius=5.0/
2、9.0*(Fahrenheit-32);华氏度=摄氏 cout.setf(ios:Celsius度n (三)第三题: 编写一个程序,打印出ASC字符集中码值为33126的字符的ASC码表。要求输出十进制值、八进制值、十六进制值以及码值所表示的字符。 int a; char w;字符 八进制 十进制 十六进制n for(a=33;a=126;a+) w=a;wtoctdechex运算符,使其能够直接使用cin语句输入Date类对象。/ 日期类定义date.h#ifndef DATE_H#define DATE_H#include class Date friend ostream &operat
3、or(istream &,Date & int day,month,year; void 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 SetDa
4、te( int mm, int dd ); /日期设置,年默认为系统年份 bool IsLeapYear(int yy) const; / 是否闰年? bool IsEndofMonth() const; / 是否月末? void print_ymd() const; /输出日期yy_mm_dd void print_mdy() const; /输出日期mm_dd_yy const Date &operator+(int days); / 日期增加任意天operator+=(int days); int operator-(const Date& ymd)const; / 两个日期之间的天数
5、void Show(Date& da);#endif/ Date类成员函数定义date.cpptime.h#include date.hconst 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(int m,int d) SetDate(m,d);Date() SystemDate();void Date:SystemDate() /取得系统日期 tm *gm; time_t t=time(NULL);
6、 gm = gmtime(&t); year = 1900 + gm-tm_year; month = gm-tm_mon +1; day = gm-tm_mday;SetDate( int yy, int mm, int dd ) month = ( mm = 1 & mm = 1900 & yy dd = 29 ) ? dd : else= days month ) ?SetDate(int mm, int dd )const Date &operator+( int days ) /重载+ for ( int i = 0; i days; i+ ) IncDay(); return *t
7、his;operator+=( int days ) /重载+=int Date:operator-(const Date& ymd )const /重载- int days; days = DayCalc()-ymd.DayCalc(); return days;bool Date:IsLeapYear( int y ) const if ( y % 400 = 0 | ( y % 100 != 0 & y % 4 = 0 ) ) return true; return false;IsEndofMonth() const return day = 29; /二月需要判断是否闰年 retur
8、n day = days month ;IncDay() /日期递增一天 if ( IsEndofMonth() if (month = 12) / 年末 day = 1; month = 1; year+; else / 月末 month+; else day+;DayCalc() const int dd; int yy = year - 1900; dd = yy*365; if(yy) dd += (yy-1)/4; for(int i=1;i2) dd+; dd += day; return dd;print_ymd() const cout year - month day end
9、l;print_mdy() const char *monthName 12 = January, February, MarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember ; monthName month-1 Show(Date& da) if(day=da.day&month=da.month)Happy Birthday! if(da-Date:Date()0) da.year+;It will be your birthday after da-Date: days!ostream &output,const Date
10、 &d) static char *monthName12= outputmonthNamed.month-1d.dayabc; d.year=a; d.month=b; d.day=c; return in;/main.cpp修改例-9中的程序,重载int main() Date today,Olympicday(2004,8,13);Today (the computers day) is:today/ today += 365;After 365 days, the date is: Date testday(2,28);the test date is:testday Date nex
11、tday = testday + 1;the next date is: today.SystemDate();the Athens Olympic Games openday is:Olympicdaybirthday;输入的是:birthdaystring string s,name,name1;请输入源c+文件的名称(不含有后缀名):name; name1=name+.prn name+=.cpp ifstream read(name.c_str();/读取的文件,用name.c_str()是将string 的char *类型转换成const的类型,不然会出错 fstream write
12、; write.open(name1.c_str(),ios:trunc/*创建文件*/|ios:out/*写文件*/);/输出的文件,创建文件并写文件 int i=0; if(!read)Cannot open input filen return 0; /如果打开输入文件失败write)Cannot open output filen /如果打开输出文件失败 while (getline(read,s)/逐行读取文件中数据到字符串s中 write+is/输出读取文件的信息到目标文件 read.close(); write.close();/关闭文件目标文件生成成功!(和源文件同目录)实验前
13、:实验后:六、第六题:将一个文本文件内容用凯撒尔方法加密,密钥是4。即文本文件中字母用其后第4个字母代替,若是数字则用其后第4个数字代替。例如文本文件原内容为“Red And Black 2008”,加密后文本文件内容为“Vih Erh Fpego 6442”。(提示:先用记事本创建一个文本文件,其内容应该是有意义的英文句子,然后以只读方式打开,加密后的内容写入另一个文本文件中。)char jiami(char ch); ifstream in(test.txt ofstream out(test_1.txtin)Can not open the file. return 1;out) cha
14、r ch; while(in) in.get(ch); ch=jiami(ch); if(in)out=0ch5)ch=ch-6; else ch+=4; else if(chazw)ch=ch+4-+-1;AZW return ch;加密前:加密后:(七)第七题: (必作题)找出100以内的勾股数,输出到文件gouku中。所谓勾股数指找出三个数满足A2+B2=C2,并且ABgougu.txtCan not open the file.n for(int i=0;101; for(int j=i+1;jj+) for(int k=j+1;kk+) if(i*i+j*j=k*k) out.set
15、f(ios:left); outsetw(3)k;t2 2. 实验结果: (八)第八题:仿造实现DOS文件复制命令COPY 源文件名 目标文件名。利用main()函数中的参数int argv,char *argk 识别解析命令。int main(int argc, char*argv) argc; if (argc != 3) cout 请输入两个文件名,及后缀 ifstream fin(argv1, ios:binary | ios:in); if (!fin)无法打开原文件 ofstream fout(argv2, ios:out);fout)无法打开目标文件 while (fin) fin.get(ch); if (fin) fout ch; fin.close(); fout.close();保存为exe格式之后提示“64位不兼容”,由于能力有限不知道如何操作。因而无法得到结果。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1