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

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

1、国家二级 C+机试(操作题)模拟试卷 300及答案与解析 一、基本操作题 1 请使用 VC6或使用【答题】菜单打开考生文件夹 prog1下的工程 prog1。此工程中包含程序文件 main cpp,其中有类 Score(“成绩 ”)和主函数 main的定义。程序中位于每个 “ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 学号: 12345678课程:英语总评成绩: 85 注意:只修改每个 “ ERROR *found*”下的一行,不要改动 程序中的其他内容。 #include ios tream USing namespace std; C1ass

2、 Score public: Score f const char * the_ course, const char * the_id, int the_normal, int the_midterm, int the_end_of_term) : course(the_course), normal(the_normal), midterm(the_midterm),end_of_term(the_end_of_term) ERROR *found* strcpy(the_id, student_id); const char * getCourse()constreturn course

3、; 返回课程名称 ERROR *found* const char * getID()constreturn&student id; ) 返回学号 int getNormal()constreturnnormal; 返回平时成绩 int getMidterm()const return midterm; 返回期中考试成绩 int getEndOfTerm()constreturn end of term; ) 返回期末考试成绩 int getFinal()const;返回总评成绩 private: const char * course; 课程名称 char student id12; 学号

4、int normal; 平时成绩 int midterm; 期中考试成绩 int end of term; 期末考试成绩 ; 总评成绩中平时成绩占 20,期中考试占 30,期末考试占 50,最后结果四舍五入为一个整数 ERROR *found* int getFinal()const return normal * 0 2 midterm*0 3 end_of_term * 0 5 0 5; int main() char English “英语 “; Score score(English, “12345678“, 68, 83, 92); cout “学号: “ score getID()

5、 “; cout “课程: “ score getCourse() “; cout “总评成绩: “ score getFinal() end1 return 0; 二、简单应用题 2 请使用 VC6或使用【答题】菜单打开考生文件夹 prog2下的工程 prog2,此工程中包含一个程序文件 main cpp,其中有 “班级 ”类 Class和 “学生 ”类 Student的 定义,还有主函数 main的定义。在主函数中定义了两个 “学生 ”对象,他们属于同一班级。程序展示,当该班级换教室后,这两个人的教室也同时得到改变。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出

6、结果应为: 改换教室前: 学号: 0789姓名:张三班级: 062113教室: 521 学号: 0513姓名:李四班级: 062113教室: 521 改换教室后: 学号: 0789姓名:张三班级: 062113教室: 311 学号: 0513姓名:李四班级: 062113教室: 311 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。 #include lostream using namespace std; class class “班级 ”类 public: C1ass(const char * id, const char * room) strcpy(class_id,

7、id); *found* const char * getClasSID() const return class_id; 返回班号 *found* const char * getClass room() const_ 返回所在教室房号 void changeRoomTo(const char * new_room) 改换到另一个指定房号的教室 strcpy(classroom, new_room); private: char class_id20;班号 char classroom20; 所在教室房号 ; class Student “学生 ”类 char my_id10;学号 char

8、 my_name20;姓名 Class &my_class; 所在教室 public: *found* Student(const char * the_id, const char * the_name, Class&the_class): _ strcpy(my_id, the_id); strcpy(my_name, the_ame); const char * getID()constreturn my_id; const char *getName()constreturn my_name; Class getClass()constreturn my_class; ; void s

9、howStudent (Student * stu) cout “学号: “ stu getID() “; cout “姓名: “ stu getName() “; cout “班级: “ stu getClass() getClassID() “; cout “教室: “ stu getClass() getClassroom() end1; int main() Class cla(“062113“, “521“); Student Zhang(“0789“, “张三 “, cla), Li(“0513“, “李四 “, cla); cout “改换教室前: “ end1; showStu

10、dent(&Zhang); showStudent(&Li); 0 62113班的教室由 521改换到 311 *found* cout “改换教室后 “ end1; showStudent(&Zhang); showStudent(&Li); return 0; 三、综合应用题 3 请使用 VC6或使用【答题】菜单打开考生文件夹 prog3下的工程 prog3,其中包含了类 Polynomial(“多项式 ”)的定义。 形如 54 3 42 7 2的代数式称为多项式,其中的 5为 4次项系数, 3 4为 2次项系数, 7为 1次项系数, 2为 0次项(常数项 )系数。此例缺 3次项,意味着

11、3次项系数为 0,即省略了 03。在Polynomial中,多项式的各个系数存储在一个名为 coef的数组中。例如,对于上面的多项式,保存在 coef0、 coef1coef4 中的系数依次为: 2 0、 7 0、3 4、 0 0、 5 0,也即对于 i次项,其系数就保存在 coefi中。成员函数getValue计算多项式的值,多项式中 的值是由参数指定的。 请补 充完成文件Polynomial cpp中成员函数 getValue的定义。此程序的正确输出结果应为: Value of p1 when x 2 0: 59 8 Value of p2 when x 3 0: 226 8 注意:只在函

12、数getVahe的 “*333*”和 “*666*”之间填入若干语句,不要改动程序中的其他内容。 Polynomiac h #include iostream using namespace std; class Polynomial “多项式 ”类 public: Polynomial(double coef, int num): coef(new doublenum), num of terms(num) for(int i 0; inum_of_terms; i ) this coefi coef i, Polynomial()deletecoef; 返回指定次数项的系数 double

13、getCoefficient (int power)constreturn coefpower; 返回在 x等于指定值时多项式的值 double getValue(double x) const; private: 系数数组, coef0为 0次项 (常数项 )系数, coef1为 1次项系数, coef2为 2次项 (平方项 )系数,余类推。 double * coef; int num_of_terms; ; void writeToFile(const char * path); Polymomial cpp #include“Polynomial h” double Polynomia

14、l getValue (double x)const 多项式的值 value为各次项的累加和 double value coef0; *333* *666* return value; main cpp #include“Polynomial h“ int main()f double pl5 0, 3 4, 4 0, 8 0), p2 0 0, 5 4, 0 0, 3 0, 2 0); Polynomial polyl(p1, sizeof(p1) sizeof(double), poly2(p2, sizeof(p2)sizeof(double); cout “Value of pl wh

15、en x 2 0: “ poly1 getValue(2 0) end1; cout “Value of p2 when x 3 0: “ poly2 getValue(3 0)end1; writeToFile(“); return 0; 国家二级 C+机试(操作题)模拟试卷 300答案与解析 一、基本操作题 1 【正确答 案】 (1)strcpy(student_id, the_id); (2)const char * getID()constreturn student_id; (3)int Score getFinal()eonst 【试题解析】 (1)主要考查考生对 strcpy()

16、函数的掌握情况, strcpy(参数一,参数二 )函数的功能是将参数二的字符串复制给参数一,因此在这里 student_id应该位于参数一的位置,即 strcpy(sIudent_id, the_id);。 (2)主要 考查考生对函数返回值的掌握情况,根据注释:返回学号可知学号应该由一个字符串组成。再看函数要返回的类型: const char *,可知要返回一个 char型指针,也就是一个 char型数组,而 &student_id是一个 char型数组指针,因此直接写 student_jd即可。 (3)主要考查考生对类的成员函数的掌握情况,因为 getFinal函数是 Score类的成员函数

17、,所以在定义时要加上类名和作用域符,即 Score 。 二、简单应用题 2 【正确答案】 (1)strcpy(classroom, room); (2)retum classroom; (3)my_class(the_class) (4)cla changeRoomTo(“311“); 【试题解析】 (1)主要考查考生对 strcpy函数的掌握情况,根据上一条语句:strcpy(class_id, id);可知,这条语句要复制字符串 room,因此使用 strcpy函数复制,即 strcpy(classroom, room);。 (2)主要考查考生对函数返回值的掌握情况,根据函数要求:返回所在

18、教室房号及函数要求返回的 类型为 const char *,可以得出这里直接使用 return语句返回classroom即可。 (3)主要考查考生对构造函数的掌握情况,先看函数体中: strcpy(my_id, the_id); strcpy(my_name, the_name); 可知只有参数 Class &the_class未使用,因此在这里使用成员列表初始化给my_class赋初始值。 (4)主要考查考生对成员函数调用的掌握,程序要求 062113班的教室由 521改换到 311。在类 Class中已经定 义了函数: void changeRoomTo(const char * new_

19、room),因此直接调用函数 changeRoomTo即可。 三、综合应用题 3 【正确答案】 for(int i 1; i num_of_terms; i ) 从 i 1开始遍历数组coef的所有项 int j i 1; 把 i 1赋值给 j,保证从零次方开始 double x_value X; 把 x赋给 x_value while(j 0) 当 j大于零时,做相乘操作,即完成该项的乘方动 作 x_value * x; j; value coefi*x_value; 把 i项的乘方结果乘以该项系数后加进 value中 【试题解析】 题目要求成员函数 getValue计算多项式的值,多项式中 X的值由参数指定,多项式的值 value为各次项的累加和。由类的定义可知数组 coef中存储的是各次项的系数,这里使用 for循环来完成题目要求,当次项为 0时, value coef0。当次项为 1时, value coef1 *cx coef0。依次类推直到 x的最高次数。

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

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

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