[计算机类试卷]国家二级C++机试(操作题)模拟试卷72及答案与解析.doc

上传人:赵齐羽 文档编号:497632 上传时间:2018-11-28 格式:DOC 页数:8 大小:34KB
下载 相关 举报
[计算机类试卷]国家二级C++机试(操作题)模拟试卷72及答案与解析.doc_第1页
第1页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷72及答案与解析.doc_第2页
第2页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷72及答案与解析.doc_第3页
第3页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷72及答案与解析.doc_第4页
第4页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷72及答案与解析.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、国家二级 C+机试(操作题)模拟试卷 72及答案与解析 一、基本操作题 1 使用 VC6打开考生文件夹下的源程序文件 modil cpp,但该程序运行时有错,请改正程序中的错误,使该程序的输出结果为: 20, 15 15, 20 注意:错误的语句在 *error*的下面,修改该语句即可。 #include *error* void Exchangel(int m, int n) int t=m; m=n; n=t; *error* void Exchange2(int m, int n) int t=*m; *m =*n; *n =t; void main() int b=2 0 ; int

2、a=15 ; Exchangel(a, b); cout void fun(char*s, char*s1,char *s2) void main() char s=“ABCABC”; fun(s, “AC”, “ZX”); Std: cout #include #include #include US ing namespace std; void WriteFile(int C) ofstream outl; outl open(”modi3 txt”, los base: app); outlC ; outl C10se(); void WriteFile(char*Str) ofstr

3、eam outl; outl open(“modi3 txt”, ios base: app); outlStr: outl close(); void C1earFile() ofstream outl ; outl open(“modi3 txt”); outl Close(); C1asS Date public: Date(int y, int m, intD *2* void print()const; *3* month+=m; int i=month 12; int j=month f2 if(j=0) year+=(i一 1); month: 12 ; else year+=1

4、; month=j; return*this; private: *1* ); VOid Date: print()const *4* WriteFile(year); WriteFile(“年 ”); WriteFile(month); WriteFile(“月 ”); WriteFile(day); WriteFile(“日 ”); int main() ClearFile(); Date Oly_day(2008, 8, 8); Oly_day+=3; Oly_day print(); return 0; 国家二级 C+机试(操作题)模拟试卷 72答 案与解析 一、基本操作题 1 【正确

5、答案】 (1)void Exchangel(int for。 (; *s; s+) 在串 s中找符合条件的字符,然后替书 for(p: s1, q=s2; *p; p+, q+) 让指针 p, q分别指向 s1和 s2 if(*p=*s) 如果 s中存在 s1中 p指向的字符 *s=*q; 那么用 q指向的 s2中的字符替换 【试题解析】 (1)该程序功能是将已知字符串 s中所有在字符串 s1中也存在的字符,都用字符串 s2中的对应位置的字符代替。它的解题思路:首先是找到符合替换条件的字符,然后进行替换。 (2)从已给部分源程序的 main主函数开始入手,核心函数 void fun(char*

6、s,cha,*s1, char*s2)中的 s1, s2参数为要替换的字符串, s为要被替换的字符串。 解题思路 (1)让指针 P,q分别指向 s1和 s2。 (2)然后利用 for循环对 s中的每一个当前字符与 p指向的字符进行比较。 (3)如果 (2)中的比较结果相等,那么用 q指向 s2中对应的字符替换。 三、综合应用题 3 【正确答案】 (1)添加语句: int year, month, day; (2)添加语句: year=y; month=m; day=d; (3)添加语句: Date&operator+=(int m) (4)添加语句: coutyear“年 ”month”月 ”

7、day“日 ”endl ; 【试题解析】 在 VC环境下打开程序,根据题干给出的几条功能要求,对程序中给出注释下的内容逐个补全或修改。从已给定源程序的 main主函数开始入手,可以看出程序通过调用类 Date和函数 print实现各种输出操作。 解题思路 (1)题目 1要求 “定义私有成员变量 year、 month、 day”。在 C+程序的 private区域中添加变量 year、 month、 day的定义,即在第 1个标识下添加 “int year,month,day; ”。 (2)题目 2要求 “完成构造函数,分别给 year、 month、 day赋值 ”。 在程序中“Date(i

8、nt y,int m,intD”的构造函数中添加给 year、 month、 day赋值的语句,即在第 2个标识下添加 “year=y; month=m; day=d; ”。 (3)题目 3要求 “完成重载符号 “+=”的定义 ”。在 C+中,运算符重载的定义方法是定义一个重载运算符的函数,格式为函数 operator+重载运算符号 +,所以这里补全 “Date &operator+=(int m)”。 (4)题目 4要求 “完成函数 print()打印函数 ”。在 C+中的 print(1函数中补全其打印功能,即在第四 个标识下添加 “coutyear“年 ”month”月 ”day”日 ”“endl; ”。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1