1、二级 C+分类模拟 135 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 prog1 下的工程 prog1。此工程中包含程序文件 main.cpp,其中有类 Score(“成绩”)和主函数 main 的定义。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 学号:12345678 课程:英语 总评成绩:85 注意:只修改每个“/ ERROR *found*”下的一行,不要改动程序中的其他内容。 #include iostream usin
2、g namespace std; class Score public: Score (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 * getCou
3、rse() const return course; /返回课程名称 / ERROR *found* const char * getID() const return /返回学号 int getNormal() const return normal; /返回平时成绩 int getMidterm() const return midterm; /返回期中考试成绩 int getEndOfTerm() const return end_of_term; /返回期考试成绩 int getFinal() const; /返回总评成绩 private: const char * course; /
4、课程名称 char student_id12; /学号 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_terms 0.5 + 0.5; int main() char English = “英语“; Score score (English, “1234
5、5678“,68,83,92); cout “学号:“ score.getID() “ “; cout “课程:“ score.getCourse() “ “; cout “总评成绩:“ score.getFinal() endl; return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 prog2 下的工程 prog2,此工程中包含一个程序文件 main.cpp,其中有“班级”类 Class 和“学生”类 Student 的定义,还有主函数 main 的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序展
6、示,当该班级换教室后,这两个人的教室也同时得到改变。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: 改换教室前: 学号:0789 姓名:张三 班级:062113 教室:521 学号:0513 姓名:李四 班级:062113 教室:521 改换教室后: 学号:0789 姓名:张三 班级:062113 教室:311 学号:0513 姓名:李四 班级:062113 教室:311 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。 #include iostream using namespace std; class Class /“班级”类 publ
7、ic: Class(const char * id, const char * room) strcpy(class_id, id); / *found* _ const char * getClassID() const return class_id; /返回班号 / *found* const char * getClassroom() const _ /返回所在教室房号 void changeRoomTo(const char * new_room) /改换到另一个指定房号的教室 strcpy (classroom, new_room); private: char class_id2
8、0; /班号 char classroom20; /所在教室房号 ; class Student /“学生”类 char my_id10; /学号 char my_name20; /姓名 Class /所在教室 public: / *found* Student (const char * the_id, const char * the_name, Class strcpy (my_name, the_name); const char * getID() const return my_id; const char * getName() const return my_name; Cla
9、ss getClass() const return my_class; ; void showStudent (Student * stu) cout “学号:“ stu - getID() “ “; cout “姓名:“ stu - getName() “ “; cout “班级:“ stu - getClass().getClassID() “ “; cout “教室:“ stu - getClass().getClassroom() endl; int main() Class cla(“062113“,“521“); Student Zhang (“0789“, “张三“, cla)
10、, Li(“0513“, “李四“, cla); cout “改换教室前:“ endl; showStudent ( showStudent ( /062113 班的教室由 521 改换到 311 / *found* cout “改换教室后:“ endl; showStudent ( showStudent ( return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 prog3 下的工程 prog3,其中包含了类 Polynomial(“多项式”)的定义。 形如 5x 4 +3.4x 2 -7x+2 的代数式称为多项式
11、,其中的 5 为 4 次项系数,3.4 为 2 次项系数,-7 为 1 次项系数,2 为 0 次项(常数项)系数。此例缺 3 次项,意味着 3 次项系数为 0,即省略了 0x 3 。在Polynomial 中,多项式的各个系数存储在一个名为 coef 的数组中。例如,对于上面的多项式,保存在coef0、coef1.coef4中的系数依次为:2.0、-7.0、3.4、0.0、5.0,也即对于 i 次项,其系数就保存在 coefi中。成员函数 getValue 计算多项式的值,多项式中 x 的值是由参数指定的。 请补充完成文件:Polynomial.cpp 中成员函数 getValue 的定义。此
12、程序的正确输出结果应为: Value of p1 when x=2.0:59.8 Value of p2 when x=3.0:226.8 注意:只在函数 getValue 的“/ *333*”和“/ *666*”之间填入若干语句,不要改动程序中的其他内容。 /Polynomiac.h #include iostream using namespace std; class Polynomial /“多项式”类 public: Polynomial (double coef, int hum):coef (new doublenum), num_of_terms(num) for (int i
13、 = 0; i num_of_terms; i +) this - coefi = coefi; Polynomial() delete coef; /返回指定次数项的系数 double getCoefficient (int power) const return coefpower; /返回在 x 等于指定值时多项式的值 double getValue (double x) const; private: /系数数组,coef0为 0 次项(常数项)系数,coef1为 1 次项系数,coef2为 2 次项(平方项)系数,余类推。 double * coef; int num_of_term
14、s; ; void writeToFile(const char * path); / Polymomial.cpp #include “Polynomial.h“ double Polynomial:getValue (double x) const /多项式的值 value 为各次项的累加和 double value = coef0; / *333* / *666* return value; /main.cpp #include “Polynomial.h“ int main() double p1 = 5.0, 3.4, -4.0, 8.0, p2 = 0.0, -5.4, 0.0,
15、3.0, 2.0; Polynomial poly1 (p1, sizeof(p1)/sizeof (double), poly2 (p2,sizeof(p2)/sizeof (double); cout “Value of p1 when x = 2.0:“ poly1.getValue(2.0) endl; cout “Value of p2 when x = 3.0:“ poly2.getValue(3.0) endl; writeToFile(“ “); return 0; (分数:40.00)_二级 C+分类模拟 135 答案解析(总分:100.00,做题时间:90 分钟)一、基本操
16、作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 prog1 下的工程 prog1。此工程中包含程序文件 main.cpp,其中有类 Score(“成绩”)和主函数 main 的定义。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 学号:12345678 课程:英语 总评成绩:85 注意:只修改每个“/ ERROR *found*”下的一行,不要改动程序中的其他内容。 #include iostream using namespace std; class Score public: Score (co
17、nst 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() const return course; /返回课程名称 / ERROR *fo
18、und* const char * getID() const return /返回学号 int getNormal() const return normal; /返回平时成绩 int getMidterm() const return midterm; /返回期中考试成绩 int getEndOfTerm() const return end_of_term; /返回期考试成绩 int getFinal() const; /返回总评成绩 private: const char * course; /课程名称 char student_id12; /学号 int normal; /平时成绩
19、int midterm; /期中考试成绩 int end_of_term; /期考试成绩 ; /总评成绩中平时成绩占 20%,期中考试占 30%,期考试占 50%,最后结果四舍五入为一个整数 / ERROR *found* int getFinal() const return normal * 0.2 + midterm * 0.3 + end_of_terms 0.5 + 0.5; int main() char English = “英语“; Score score (English, “12345678“,68,83,92); cout “学号:“ score.getID() “ “;
20、 cout “课程:“ score.getCourse() “ “; cout “总评成绩:“ score.getFinal() endl; return 0; (分数:30.00)_正确答案:()解析:(1)strcpy(student_id,the_id); (2)const char*getID() const return student_id; (3)int Score:getFinal() const 答案考生文件夹 考点 本题考查的是 Score 类,其中涉及动态数组、构造函数、strcpy 函数、const 函数和成员函数。strcpy()函数和 strlen()函数等经常会考
21、到,要注意它们的参数要求。类的成员函数在类外定义时需要加上类名的作用域符。 解析 (1)主要考查考生对 strcpy()函数的掌握情况,strcpy(参数一,参数二)函数的功能是将参数二的字符串复制给参数一,因此在这里 student_id 应该位于参数一的位置,即strcpy(student_id,the_id);。 (2)主要考查考生对函数返回值的掌握情况,根据注释:返回学号可知学号应该由一个字符串组成。再看函数要返回的类型:const char*,可知要返回一个 char 型指针,也就是一个 char 型数组,而class Class /“班级”类 public: Class(const
22、 char * id, const char * room) strcpy(class_id, id); / *found* _ const char * getClassID() const return class_id; /返回班号 / *found* const char * getClassroom() const _ /返回所在教室房号 void changeRoomTo(const char * new_room) /改换到另一个指定房号的教室 strcpy (classroom, new_room); private: char class_id20; /班号 char cla
23、ssroom20; /所在教室房号 ; class Student /“学生”类 char my_id10; /学号 char my_name20; /姓名 Class /所在教室 public: / *found* Student (const char * the_id, const char * the_name, Class strcpy (my_name, the_name); const char * getID() const return my_id; const char * getName() const return my_name; Class getClass() c
24、onst return my_class; ; void showStudent (Student * stu) cout “学号:“ stu - getID() “ “; cout “姓名:“ stu - getName() “ “; cout “班级:“ stu - getClass().getClassID() “ “; cout “教室:“ stu - getClass().getClassroom() endl; int main() Class cla(“062113“,“521“); Student Zhang (“0789“, “张三“, cla), Li(“0513“, “李
25、四“, cla); cout “改换教室前:“ endl; showStudent ( showStudent ( /062113 班的教室由 521 改换到 311 / *found* cout “改换教室后:“ endl; showStudent ( showStudent ( return 0; (分数:30.00)_正确答案:()解析:(1)strcpy(classroom,room); (2)return classroom; (3)my_class(the_class) (4)cla.changeRoomTo(“311“); 答案考生文件夹 考点 本题考查的是 Class 类和 S
26、tudent 类,其中涉及构造函数、const 函数和 strcpy函数。在 Student 类中使用 Class 类,在 C+中很常见,在调用的时候要注意类的调用格式及构造函数。 解析 (1)主要考查考生对 strcpy 函数的掌握情况,根据上一条语句:strcpy(class_id,id);可知,这条语句要复制字符串 room,因此使用 strcpy 函数复制,即 strcpy(classroom,room);。 (2)主要考查考生对函数返回值的掌握情况,根据函数要求:返回所在教室房号及函数要求返回的类型为const char*,可以得出这里直接使用 return 语句返回 classro
27、om 即可。 (3)主要考查考生对构造函数的掌握情况,先看函数体中: strcpy(my_id,the_id); strcpy(my_name,the_name); 可知只有参数 Class class Polynomial /“多项式”类 public: Polynomial (double coef, int hum):coef (new doublenum), num_of_terms(num) for (int i = 0; i num_of_terms; i +) this - coefi = coefi; Polynomial() delete coef; /返回指定次数项的系数
28、double getCoefficient (int power) const return 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“ doub
29、le Polynomial:getValue (double x) const /多项式的值 value 为各次项的累加和 double value = coef0; / *333* / *666* return value; /main.cpp #include “Polynomial.h“ int main() double p1 = 5.0, 3.4, -4.0, 8.0, p2 = 0.0, -5.4, 0.0, 3.0, 2.0; Polynomial poly1 (p1, sizeof(p1)/sizeof (double), poly2 (p2,sizeof(p2)/sizeof
30、 (double); cout “Value of p1 when x = 2.0:“ poly1.getValue(2.0) endl; cout “Value of p2 when x = 3.0:“ poly2.getValue(3.0) endl; writeToFile(“ “); return 0; (分数:40.00)_正确答案:()解析:for (int i = 1; i num_of_terms; i+) /从 i=1 开始遍历数组 coef 的所有项 int j = i - 1; /把 i-1 赋值给 j,保证从零次方开始 double x_value = x; /把 x
31、赋给 x_value while(j 0) /当 j 大于零时,做相乘操作,即完成该项的乘方动作 x_value * = x; j -; value += coefi * x_value; /把 i 项的乘方结果乘以该项系数后加进 value 中 答案考生文件夹 考点 本题考查 Polynomial 类,其中涉及构造函数、动态数组、析构函数和 const 函数。 解析 题目要求成员函数 getValue 计算多项式的值,多项式中 x 的值由参数指定,多项式的值 value 为各次项的累加和。由类的定义可知数组 coef 中存储的是各次项的系数,这里使用 for 循环来完成题目要求,当次项为 0 时,value=coef0。当次项为 1 时,value=coef1*x+coef0。依次类推直到 x 的最高次数。