西安交大C++程序设计第十一章作业教学提纲Word下载.docx
《西安交大C++程序设计第十一章作业教学提纲Word下载.docx》由会员分享,可在线阅读,更多相关《西安交大C++程序设计第十一章作业教学提纲Word下载.docx(26页珍藏版)》请在冰豆网上搜索。
(1)域宽不够时会自动补足。
(2)精度只需一次定义则一直有效。
(3)域宽需要每次输出时均进行定义
(二)第二题:
编写一个程序,将华氏温度0度~212度转化为浮点型摄氏温度,浮点精度为3.转换公式为如下:
Celsius=5.0/9.0*(Fahrenheit-32);
输出用两个右对齐序列,摄氏温度前面加上正负号。
1.源程序代码:
doubleCelsius,Fahrenheit;
cout.precision(3);
转换结果为:
for(Fahrenheit=0;
Fahrenheit<
=212;
Fahrenheit++)
cout.unsetf(ios:
showpos);
Celsius=5.0/9.0*(Fahrenheit-32);
华氏"
度=摄氏"
cout.setf(ios:
Celsius<
度\n"
(三)第三题:
编写一个程序,打印出ASCⅡ字符集中码值为33~126的字符的ASCⅡ码表。
要求输出十进制值、八进制值、十六进制值以及码值所表示的字符。
inta;
charw;
字符八进制十进制十六进制\n"
for(a=33;
a<
=126;
a++)
w=a;
w<
\t"
oct<
dec<
hex<
四、第四题:
修改例11-2中的程序,重载>
>
运算符,使其能够直接使用cin语句输入Date类对象。
//日期类定义date.h
#ifndefDATE_H
#defineDATE_H
#include<
classDate
friendostream&
operator<
(ostream&
constDate&
);
friendistream&
operator>
(istream&
Date&
intday,month,year;
voidIncDay();
//日期增加一天
intDayCalc()const;
//距基准日期的天数
staticconstintdays[];
//每月的天数
public:
Date(inty,intm,intd);
//构造函数
Date(intm,intd);
//构造函数,年默认为系统当前年份
Date();
//构造函数,默认为系统日期
voidSystemDate();
voidSetDate(intyy,intmm,intdd);
//日期设置
voidSetDate(intmm,intdd);
//日期设置,年默认为系统年份
boolIsLeapYear(intyy)const;
//是否闰年?
boolIsEndofMonth()const;
//是否月末?
voidprint_ymd()const;
//输出日期yy_mm_dd
voidprint_mdy()const;
//输出日期mm_dd_yy
constDate&
operator+(intdays);
//日期增加任意天
operator+=(intdays);
intoperator-(constDate&
ymd)const;
//两个日期之间的天数
voidShow(Date&
da);
};
#endif
//Date类成员函数定义date.cpp
time.h>
#include"
date.h"
constintDate:
days[]={0,31,28,31,30,31,30,
31,31,30,31,30,31};
//构造函数
Date:
Date(inty,intm,intd){SetDate(y,m,d);
Date(intm,intd){SetDate(m,d);
Date(){SystemDate();
voidDate:
SystemDate()
{//取得系统日期
tm*gm;
time_tt=time(NULL);
gm=gmtime(&
t);
year=1900+gm->
tm_year;
month=gm->
tm_mon+1;
day=gm->
tm_mday;
SetDate(intyy,intmm,intdd)
month=(mm>
=1&
&
mm<
=12)?
mm:
1;
year=(yy>
=1900&
yy<
=2100)?
yy:
1900;
if(month==2&
IsLeapYear(year))
day=(dd>
dd<
=29)?
dd:
else
=days[month])?
SetDate(intmm,intdd)
constDate&
operator+(intdays)
{//重载+
for(inti=0;
i<
days;
i++)
IncDay();
return*this;
operator+=(intdays)
{//重载+=
intDate:
operator-(constDate&
ymd)const
{//重载-
intdays;
days=DayCalc()-ymd.DayCalc();
returndays;
boolDate:
IsLeapYear(inty)const
if(y%400==0||(y%100!
=0&
y%4==0))
returntrue;
returnfalse;
IsEndofMonth()const
returnday==29;
//二月需要判断是否闰年
returnday==days[month];
IncDay()
{//日期递增一天
if(IsEndofMonth())
if(month==12){//年末
day=1;
month=1;
year++;
}
else{//月末
month++;
elseday++;
DayCalc()const
intdd;
intyy=year-1900;
dd=yy*365;
if(yy)dd+=(yy-1)/4;
for(inti=1;
i<
month;
i++)
dd+=days[i];
if(IsLeapYear(year)&
(month>
2))
dd++;
dd+=day;
returndd;
print_ymd()const
cout<
year<
"
-"
<
month<
day<
endl;
print_mdy()const
char*monthName[12]={"
January"
"
February"
"
March"
April"
May"
June"
July"
August"
September"
October"
November"
December"
};
monthName[month-1]<
'
<
Show(Date&
da)
if(day==da.day&
month==da.month)
HappyBirthday!
if(da-Date:
Date()<
0)
da.year++;
Itwillbeyourbirthdayafter"
da-Date:
days!
ostream&
output,constDate&
d)
staticchar*monthName[12]=
output<
monthName[d.month-1]<
'
d.day<
"
d.year;
returnoutput;
istream&
in,Date&
d)//
inta,b,c;
in>
a>
b>
c;
d.year=a;
d.month=b;
d.day=c;
returnin;
//main.cpp修改例-9中的程序,重载>
intmain()
Datetoday,Olympicday(2004,8,13);
Today(thecomputer'
sday)is:
today<
//
today+=365;
After365days,thedateis:
Datetestday(2,28);
thetestdateis:
testday<
Datenextday=testday+1;
thenextdateis:
today.SystemDate();
theAthensOlympicGamesopendayis:
Olympicday<
Andafter"
Olympicday-today
days,theAthensOlympicGameswillopen."
Datebirthday;
输入出生年月日:
cin>
birthday;
输入的是:
birthday<
return0;
五、第五题:
编写一个程序,可以读入一个C++语言的源文件,每一行加上行号后保存到另一个后缀为.prn的同名文件中。
1.源程序代码:
fstream>
string>
strings,name,name1;
请输入源c++文件的名称(不含有后缀名):
name;
name1=name+"
.prn"
name+="
.cpp"
ifstreamread(name.c_str());
//读取的文件,用name.c_str()是将string的char*类型转换成const的类型,不然会出错
fstreamwrite;
write.open(name1.c_str(),ios:
trunc/*创建文件*/|ios:
out/*写文件*/);
//输出的文件,创建文件并写文件
inti=0;
if(!
read)
Cannotopeninputfile\n"
return0;
}//如果打开输入文件失败
write)
Cannotopenoutputfile\n"
}//如果打开输出文件失败
while(getline(read,s))//逐行读取文件中数据到字符串s中
write<
++i<
s<
//输出读取文件的信息到目标文件
read.close();
write.close();
//关闭文件
目标文件生成成功!
(和源文件同目录)"
实验前:
实验后:
六、第六题:
将一个文本文件内容用凯撒尔方法加密,密钥是4。
即文本文件中字母用其后第4个字母代替,若是数字则用其后第4个数字代替。
例如文本文件原内容为“RedAndBlack2008”,加密后文本文件内容为“VihErhFpego6442”。
(提示:
先用记事本创建一个文本文件,其内容应该是有意义的英文句子,然后以只读方式打开,加密后的内容写入另一个文本文件中。
)
charjiami(charch);
ifstreamin("
test.txt"
ofstreamout("
test_1.txt"
in)
Cannotopenthefile."
return1;
out)
charch;
while(in)
in.get(ch);
ch=jiami(ch);
if(in)out<
ch;
in.close();
out.close();
charjiami(charch)
if(ch>
='
0'
ch<
9'
if(ch>
5'
)ch=ch-6;
elsech+=4;
elseif(ch>
a'
z'
w'
)ch=ch+4-'
+'
-1;
A'
Z'
W'
returnch;
加密前:
加密后:
(七)第七题:
(必作题)找出100以内的勾股数,输出到文件gouku中。
所谓勾股数指找出三个数满足A2+B2=C2,并且A<
B<
C。
要求将三个勾股数的计算公式A^2+B^2=C^2输出到文件中的每一行,例如3^2+4^2=5^29+16=25。
iomanip>
gougu.txt"
Cannotopenthefile.\n"
for(inti=0;
101;
for(intj=i+1;
j<
j++)
for(intk=j+1;
k<
k++)
{
if(i*i+j*j==k*k)
{
out.setf(ios:
left);
out<
setw(3)<
k;
\t'
^'
2<
}
}
2.实验结果:
(八)第八题:
仿造实现DOS文件复制命令COPY源文件名目标文件名。
利用main()函数中的参数intargv,char*argk[]识别解析命令。
intmain(intargc,char*argv[])
argc;
if(argc!
=3)
cout<
请输入两个文件名,及后缀"
ifstreamfin(argv[1],ios:
binary|ios:
in);
if(!
fin)
无法打开原文件"
ofstreamfout(argv[2],ios:
out);
fout)
无法打开目标文件"
while(fin)
fin.get(ch);
if(fin)
{
fout<
ch;
fin.close();
fout.close();
保存为exe格式之后提示“64位不兼容”,由于能力有限不知道如何操作。
因而无法得到结果。