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

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

1、国家二级 C+机试(操作题)模拟试卷 138及答案与解析 一、基本操作题 1 请使用 VC6或使用【答题】菜单打开考生文件夹 pmjl下的工程 pmjl,该工程中包含程序文件 main cpp,其中有类 Foo和主函数 main的定义。程序中位于每个“ ERROR木 *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: X=a Y=42 注意:只修改每个 “ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include using namespace std; class Foo public: Foo(char x)x =x; ) char

2、getX()consLreturn x一; ) public: stat; iC int V; private: char x; ; ERROR*found* int Foo.Y一 =42; int main(int argo, char*argv) ERROR*found* Foo f; ERROR*found* couL #include using rlaltlespace st: d; 平面坐标中的点 本题坐标系统中, x轴的正方向水平向右, y轴的正方向竖直向下。 ClaSs Point: public: Point: (doubl e x=0 0, double y=0 0): X

3、一 (x), y一 (y) double getX()constreturn xj double geY()constreturn y一; ) void set; X(double X)x =x; ) void setY(double y)y一。 y; ) private: double x; x坐标 double y; y坐标 ; 矩形 class Rectangl e public: Rectangle(Point P, int W, int h) : point(p), width(w), height(h) dopble area()const矩形面积 return width*hei

4、ght; Point topLeft()const左上角顶点 return point; Point bottomRight()const 右下角颂点 (注: y轴正方向竖直向下 ), *found* return Point(_); private: Point point;左上角顶点 double width;水平边长度 double height;垂直边长度 ; 圆形 class Circle public: Circle(Point P, double r): center (p), radius(r) Rectangle boundingB0x()const, 外切矩形 double

5、 area()const圆形面积 * found* return PI*_; public: static const double PI;圆周率 private: Point center; 圆心 double radius; 半径 ; const double Circle: PI=3 14159; RectangleCircle: boundingBox ()const *found* Point pt(_); int W, h; *found* W=h=_; return Rectangle(pt, W, h); int main() Point P(3, 2); Circle C(p

6、, 1); cout #include using namespace std; class MyStrincj public: MyString(const char*S) sir=new charstrlen(S)+1; strcpy(str, s); 一 NyString()clelete, st: r; void EeveEse(); friend ostream&operator void NyString: Eeverse() *333* *666* int main() char inname128, pathname80; strcpy(pathname, “”); sprin

7、tf(inname, “in dat”, pathname); cout“读取输入文件 n n”; ifstream infile(inname); if(infile fail() ceEr“打开输入文件失败 !”; exit(1); char bur4096; infile getline(buf, 4096); NyString strl(“ABCDEF”), str2(“ABCDEFG”), str3(bur); cout“_反转前 n”; tout“STR1=”str1end1; tout“STR2=”Str2end1 end1; str1 reveEse(); str2 Eever

8、se(); str3 reverse(); tout“-一 反转后一一一 n”; cout“STR1=”Strlendl; cout“STR2 =” str2 erldl end1; writeToFile(pathname, str3); return 0; 国家二级 C+机试(操作题)模拟试卷 138答案与解析 一、基本操作题 1 【正确答案】 (1)int Foo: y一 =42; (2)F00 f(a); (3)cout“X=”f getX()endl; 【试题解析】 本 题考查的是 Foo类,其中涉及构造函数、 const函数和静态成员。给类的静态成员赋值时要加上类名和作用域符号,与

9、类的成员函数一样,类的私有成员不能被类外函数调用。 【解题思路】 (1)主要考查考生对静态成员的掌握,因为静态整型变量 y一是 Foo类的公有成员,所以给 y一赋值时要加上 “Foo: ”,即 int Foo: y一 =42;。 (2)主要考查考生对构造函数的掌握,题目要求程序输出: X=a Y=42 可以知道,在给 Foo类的 f声明时要同时初始化为字符 a,即语句 Foo f(a);。 (3)主要考查考生对成员 函数的掌握,因为 x是类 Foo的私有成员,所以不能在main函数中直接调用,要通过公有成员函数 getX()调用。 【解题宝典】 类的静态成员和类的成员函数一样,赋值时要加上类名

10、和作用域符号,要注意通过观察题目对程序输出结果的要求,来给类赋初始值。 二、简单应用题 2 【正确答案】 (1)loint getX()+width, point getY()+height (2)radius*: radius (3一 )center getX()一 radius, center getY。 ()一 radius (4)2*radius 【试题解析】 本题考查 Point类、 Rectangle类和 Circle类,其中涉及构造函数、 const函数和静态成员。 【解题思路】 (1)主要考查考生对成员函数的掌握,程序要求返回右下角顶点,该点的 x坐标为左上角顶点的 x坐标加上

11、 width,该点的 y坐标为左上角顶点 y坐标加上 height,即 retum Point(point getX()+width, point getY()+height);。 (2)主要考查考生对成员函数的掌握,程序要求计算圆形面积,也就是返回圆面积,即 return P1*radius*radius;。 (3)主要考查考生对成员函数的掌握,首先看函数声明: Rectangle Circle:boundingBox()const,可知该函数要返回的是一个 Rectangle类型,即要返回的是圆的外切矩形。再看 Rectangle类的构造函数 Rectangle(Point p, int

12、w, int h),由此可知,空格处要定义的点 pt为左上角点,即 Point pt(cen-ter getX()一radius, center getY()一 radius);。 (4)由函数声明和 Rectangle类的构造函数可知, w和 h应该为直径,即w=h=2*radius;。 三、综合应用题 3 【正确答案】 Int ength=str en(str); 把字符串 str的长度赋值给 lenth for(int i=0, j =length一 1; ij; i+, j一一 ) 从 i=0, j=length一 1, ij为条件开始遍历,并把 stri和 strj交换 char t

13、emp =stri; 给定义的临时变量 temp赋值为 stri stri =strj; 给 stri赋值 strj strj=temp; 给 strj赋值为 temp 【试题解析】 主要考查 MyString类,其中涉及动态数组、构造函数、析构函数、成员函数和友元函数。本题要把动态数组中的字符串反转过来,用两个变量 i和 j来代表要调换的元素的下标,再通过中间变量 temp进行交换即可。 【解题思路】主要考查考生对动态数组的掌握,先看题目要求:成员函数 reverse的功能是将字符串进行 “反转 ”。再由类的定义可知,字符串存放在动态数组 str中,由 strlen函数得出字符串的长度, 最后一个字符的下标为 length一 1,第一个字符的下标为 0,将这两个字符交换,然后 j依次减 1同时 i依次加 1,继续交换,直到 i大于 j时停止循环即可。

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

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

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