一、程序改错题
使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错,请改正其中的错误,使程序正确运行,其输出的结果为
30
130
注意:错误的语句在//******error******的下面,修改该语句即可。
试题程序:
#include
inta=10;
classTC
{
public:
TC()
{
a=b=0:
}
voiddisplay()
{
//******error******
cout<<a<<b<endl:< p="">
}
voidfunc(inta)
{
//******error******
a+=a:
}
voidfunc2()
{
//******error******
a+=a:
}
private:
inta,b;
};
voidmain()
{
TCobj;
obj.func(3);
obj.display();
obj.func2();
obj.display();
}
答案:(1)应改为“cout<<a<<b<<endl;”。< p="">
(2)应改为“this->a+=a;”。
(3)应改为“a+=::a;”。
二、简单应用题
使用VC++6.0打开考生文件夹下的源程序文件2.cpp。阅读下列函数说明和代码。函数num(ehar*str)用于返回字符串中非数字的个数。
例如:abcl23abc45
返回值为:6
将函数num补充完整。
注意:请勿改动主函数main。
试题程序:
#include
intnum(char*str)
{
}
intmain()
{
charstr[1024];
cout<<"pleaseinputastring:"<<endl;< p="">
cin.getline(str,1024);
cout<<"charnumberis"<<num(str)<<endl:< p="">
return0;
}
答案:intnumber=0;
for(inti=0;sir[i]!=0;i++)
if(str[i]>'9'‖str[i]<'0')//非数字字符
number++:
returnnumber;
三、综合应用题
使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int。请在注释1后添加适当的语句。
(2)完成构造函数,分别给year、month、day赋值,请在注释2后添加适当的语句。
(3)完成重载符号“+=”的定义,请在注释3后添加适当的语句。
(4)完成函数print打印函数,如2005年1月5日到屏幕和文件out3.txt格式相同,请在注释4后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
试题程序:
#include
#include
#include
#include
usingnamespacestd;
voidWriteFile(intc)
{
ofstreamout1;
out1.open("out3.txt",ios_base::app);out1<<c<<'';< p="">
out1.close();
}
voidWriteFile(char*str)
{
ofstreamout1;
out1.open("out3.txt",ios_base::app);out1<<str;< p="">
out1.close();
}
voidClearFile()
{
ofstreamout1;
out1.open("out3.txt"):
outl.close();
}
classDate
{
public:
Date(inty,intm,intd)
{
//********1*********}
voidprint();
//********2********
{
month+=m:
inti=month/12;
intj=month%12;
if(j==0)
{
year+=(i-1):
month=12;
}
else
{
year+=i:
month=j;
}
return*this:
}
private:
//********3********
};
voidDate::print()
{
//********4********
WriteFile(year);
WriteFile("年");
WriteFile(month);
WriteFile("月");
WriteFile(day);
WriteFile("日");
}
intmain()
{
ClearFile();
DateNationa1_day(2004,10,5);
National_day+=3;
National_day.print();
return0;
}
答案:(1)应添加“year=y;month=m;day=d;”。
(2)应添加“Date&operator+=(intm)”。
(3)应添加“intyear,month,day;”。
(4)应添加“cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;”。< p="">


热门课程培训
热门培训机构
