2016年计算机二级C语言上机考试试题及答案
- 计算机二级
- 关注:4.65K次
一、程序改错题
使用VC++6.0打开考生文件夹下的源程序文件,该程序运行时有错,请改正程序中的错误,使程序输出的结果为
150
200
注意:错误的语句在//******error******的下面,修改该语句即可。
提示:定义Numl为一个整型数据位长的变量。
试题程序:
#include
//******error******
struct
{
//******error******
intNuml:1;
intNum2;
}
MyStruct;
voidmain()
{
MyStructmstr;
1=150;
2=15;
//******error******
int*ptr=&;
cout<<*ptr< *ptr=200;
cout<<*ptr< } (1)“typedefstruct”。
(2)“intNuml:32;”或“intNuml;”。
(3)“int*ptr=(int*)&mstr;”。
【解析】第1处是定义结构体,结构体定义的'一般形式为
struct(结构体名>
{
<成员列表>
}
(变量名列表);
程序当中定义结构体名放在后面,MyStruct为结构体变量类型,相当于typedef为已有的类型定义一个新的类型,第1处应改为“typedefstruct”。第2处是定义Numl为一个整型数据变量,整型数据位数为32(4字节),因此第2处应改成“intNuml:32;”或“intNuml;”。ptr为整型数据指针,mstr为MyStruct类型结构变量,两者类型不一致应该做强制类型转换,第3处应改成“int*ptr=(int*)&mstr;”,表示将mstr的地址给ptr,ptr指向MyStruct的第一个域Numl,Numl也是整型,类型一致,故可通过ptr指针来改变Numl的值。
二、综合应用题
使用VC++6.0打开考生文件夹下的源程序文件,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int。请在注释1后添加适当的语句。
(2)完成构造函数,分别给year、month、day赋值,请在注释2后添加适当的语句。
(3)完成重载符号“+=”的定义,请在注释3后添加适当的语句。
(4)完成函数print打印函数,如2005年1月5日到屏幕和文件格式相同,请在注释4后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
试题程序:
#include
#include
#include
#include
usingnamespacestd;
voidWriteFile(intc)
{
ofstreamout1;
("",ios_base::app);out1< e();
}
voidWriteFile(char*str)
{
ofstreamout1;
("",ios_base::app);out1< e();
}
voidClearFile()
{
ofstreamout1;
(""):
e();
}
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_t();
return0;
} (1)应添加“year=y;month=m;day=d;”。
(2)应添加“Date&operator+=(intm)”。
(3)应添加“intyear,month,day;”。
(4)应添加“cout<
【解析】本题要求私有成员变量year、month、day为整形表示方法,所以注释3应添加“intyear,month,day;”;注释1要求分别给year、month、day赋值,根据上下文提意,所以应添加“year=y;month=m;day=d;”;注释2是对“+=”运算符的重栽,Date&operator4+=(intm);第4处打印输出年、月、日,C++语言中,输出使用cout。
- 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/jsj/erji/mwq343.html