一、程序改错题
使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,输出的结果为
Constructor,i=0,
Destructor
注意:错误的语句在//******error******的下面,修改该语句即可。
试题程序:
#include(iostream.h)
classTC
{
inti;
public:
TC();
voiddisplay();
~TC();
};
//******error******
TC:TC()
{
cout<
i=0;
)
//******error******
TC:display()
{
tout<
}
//******error******
TC:TC()
{
COUI<
}
voidmain()
{
TCa;
a.display();
}
答案:(1)应改为“TC::TC()”。
(2)应改为“voidTC::display()”。
(3)应改为“TC::~TC()”。
二、简单应用题
使用VC++6.0打开考生文件夹下的源程序文件2.cpp。请完成函数fun(char*str,charch)的定义,本函数采用二分法,在已按字母次序从小到大排序的字符数组str中,查找字符ch,若ch在数组中,函数返回字符ch在数组中的下标,否则返回1。
二分法查找的思想是初始查找区间的下界为0,上界为len-1,查找区间的中部后,k=(下界+上界)/2;若list[k]等于ch,查找成功;若list[k]>ch,则新的查找区间的下界不变,上界改为k-1;否则新的查找区间的下界改为k+1,上界不变。在新区间内继续用二分法查找。
注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include(iostream.h>
intfun(char*str,charch)
{
}
voidmain()
{
charstr[]={'a','b','C','d','e','f','9','h','i,'
j','k');
charch;
cout<
cin>>ch;
cout<
return;
}
答案:intlow=0;//初始查找区间的下界
inthigh;
intk;
for(high=0;str[high]!=0;high++)//求字符串长度
while(10w
{
k=(low+high)/2;
if(str[k]==ch)
returnk;
elseif(str[k]>ch)
high=k-l:
elselow=k+1:
}
if(str[low]==ch)
returnlow;
return-1:
三、综合应用题
使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有成员变量year、month、day。分别表示年、月、日,类型为int。请在注释1后添加适当的语句。
(2)完成构造函数,分别给year、month、day赋值,请在注释2后添加适当的语句。
(3)完成重载符号“十=”的定义,请在注释3后添加适当的语句。
(4)完成print打印函数,输出到屏幕和文件的格式相同,请在注释4后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
程序正常运行,输出的结果为2008年11月813。
试题程序:
#include(iostream.h)
#include(fstream)
#include(iomanip)
#include
usingnamespacestd;
voidWriteFile(intc)
{
ofstreamout1;
out1.open("3.txt",ios_base::app);
out1<
out1.close();
}
voidWriteFile(char*str)
{
ofstreamoutl;
out1.open("3.txt",ios_base::app);
out1<
out1.close();
}
voidClearFile()
{
ofstreamout1;
out1.open("3.txt");
out1.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();
DateOly_day(2008,8,8);
Olyday+=3:
Oly_day.print();
return0;
}
答案:(1)应添加“year=y;month=m;day=d;”。
(2)应添加“Date&operator+=(intm)”。
(3)应添加“intyear,month,day;”。
(4)应添加“cout<