2019计算机国二操作题,2019年3月计算机二级C++操作练习题及答案(十二)
一、程序改錯題
使用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<
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的2019计算机国二操作题,2019年3月计算机二级C++操作练习题及答案(十二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html5视差滚动效果,视差滚动效果
- 下一篇: typedef函数指针_C语言函数指针之