1、国家二级 C+机试(操作题)模拟试卷 406 及答案解析(总分:6.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:2.00)1.使用 VC6 打开考生文件夹下的源程序文件 modilcpp。本程序要求输入一字符串,然后将其中的小写字母改变成大写字母,字符可能输入多行,当输入字符“?”时,默认字符输入结束,最后把结果输出到界面。 注意:不要改动 main 函数,不能增行或删行,也不能更改程序的结构,错误的语句在*error*的下面。#includeiostreamhvoid main()( int i=0; char str120; cout”Enter a string end
2、with?“end1; *error* cingetline(str,120); *error* while(Str) *error* if(stri=a) Stri =Stri -a+A; i+; ) coutstrend1; return;(分数:2.00)_二、简单应用题(总题数:1,分数:2.00)2.使用 VC6 打开考生文件夹下的源程序文件 modi2cpp。完成函数 fun(char*str,char*s)空出部分。函数 fun(char*str,char* s)的功能是:将在字符串 str 中下标为偶数位置上的字符,紧随其后重复出现一次,放在一个新串 s 中,s 中字符按原字符
3、串中字符的顺序排列。(注意 0 为偶数) 例如:当 str 中的字符串为:“abcdef“时,s 中的字符串应为:“aacccc“。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 #include void fun(char* str,char* s) void main() char str100,S100; cout=a&stri=a)”语句是判断字符 stri是否为小写字符,而现有的语句只是判断字符是否大于a,但是小写字符应该是a到z,所以修改为“if(stri=a&stri=z)”。二、简单应用题(总题数:1,分数:2.00)2.使用 VC6 打开考生文件夹
4、下的源程序文件 modi2cpp。完成函数 fun(char*str,char*s)空出部分。函数 fun(char*str,char* s)的功能是:将在字符串 str 中下标为偶数位置上的字符,紧随其后重复出现一次,放在一个新串 s 中,s 中字符按原字符串中字符的顺序排列。(注意 0 为偶数) 例如:当 str 中的字符串为:“abcdef“时,s 中的字符串应为:“aacccc“。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 #include void fun(char* str,char* s) void main() char str100,S100;
5、 cout“Please enter strinq String:“The result is: “_正确答案:(正确答案:int 1; for(i=0;stri!=0;i+) 循环 判断每一个字符 if(i2=0) 判断字符串 中下标为偶数位置 si=stri; si+1=stri; si+2=0; 新串添加尾符 )解析:解析:(1)判断一个字符串中的某一字符下标 i 是否为偶数,可用“i2=0”来判断。 (2)对串str 中各个字符串逐个进行考查,满足条件的字符就顺序放在新串 S 中,否则就不存放。 (3)所有字符处理完后,记得在新串 S 的末尾加上结束符“0”。三、综合应用题(总题数:1
6、,分数:2.00)3.使用 VC6 打开考生文件夹下的源程序文件 modi3cpp,其中定义了用于表示日期的类 Date,但类 Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有成员变量year、month、day,分别表示年、月、 日,类型为 int。请在注释*1*后添加适当的语句。 (2)完成构造函数,分别给 year、month、day 赋值,请在注释*2*后添加适当的语句。 (3)完成重载符号“+=”的定义,请在注释*3*后添加适当的语句。 (4)完成 print()打印函数,如 2008 年 8 月 8 日到屏幕和文件 modi3txt 格式相同,请在注
7、释*4*后添加适当的语句。 注意:仅在函数指定位置添加语句,请勿改动主函数 main 与其他函数中的任何内容。#includeiostreamh#includefstream#includeiomanip#includecmathusing namespace std;void WriteFile(int c) ofstream out1; out1open(“modi3txt“,ios_base:app); out1c ; out1Close();void WriteFile(char* str) Ofstream out1; out1open(“modi3txt“,ios_base:app
8、); out1Str;out1close();void ClearFile() ofstream out1; out1open(“modi3txt“);out1Close();Class Datepublic: Date(int y,int m,int d) *2* void print()const; *3* month+=m; int i=month12; int j=month12; if(j=0) year+=(i-1); month=12; elase year+=i; month=j; return *this; private: *1*;void Date:print()cons
9、t *4* WriteFile(year); WriteFile(“年“); WriteFile(month); WriteFile(“月“); WriteFile(day); WriteFile(“日“);int main() ClearFile(); Date Oly day(2008,8,8); Oly_day+=3; Oly_dayprint(); return 0;(分数:2.00)_正确答案:(正确答案:(1)添加语句:int year,month,day; (2)添加语句:year=y;month=m;day=d; (3)添加语句:Date&operator+=(int m) (
10、4)添加语句:coutyear“年“month“月“day“日“end1;)解析:解析:(1)题目 1 要求“定义私有成员变量 year、month、day”。在 C+程序的 private 区域中添加变量 year、month、day 的定义,即在第 1 个标识下添加“int year,month,day;”。 (2)题目 2 要求“完成构造函数,分别给 year、month、day 赋值”。在程序中“Date(int y,int m,int d)”的构造函数中添加给 year、month、day 赋值的语句,即在第 2 个标识下添加“year=y;month=m;day=d;”。 (3)题目 3 要求“完成重载符号“+=”的定义”。在 C+中,运算符重载的定义方法是定义一个重载运算符的函数,格式为函数 operator+重载运算符号+,所以这里补全“Date&operator+=(int m)”。 (4)题目 4 要求“完成函数 print()打印函数”。在 C+中的 print()函数中补全其打印功能,即在第四个标识下添加“coutyear“年“month“月“day“日“end1;”。