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

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

1、国家二级 C+机试(操作题)模拟试卷 506及答案与解析 一、基本操作题 1 请打开考生文件夹下的解决方案文件 proj1,其中在编辑窗口内显示的主程序文件中定义有 Xabe类和主函数 main。在程序文本中位于每行 ”ERROR*found*下面的一行有错误,请加以更正。 更正后程序的输出为: 57 注意:只允许修改每个 ” ERROR*found*下面的一行语句,不允许改动程序中的其他任何内容。 #include iostream using namespace std; class Xabc int*a; int n; public: xabc(int aa , int nn): n(n

2、n) a=new intn; for(int i=0; i n; i+) ERROR*found* aai=ai; int GetA(int i)constreturn ai; int SumA(int n); Xabc( )delete a; ; int Xabc: SumA(int n) int s=0: for(int j=0; j n; j+) s+=aj; return s; int main( ) int a6=2, 5, 8, 3, 6, 9; Xabc x(a, 6); a3=19; int d=0; for(int i=0; i 6; i+) ERROR*found* d+=

3、x ai; ERROR*found* int f=SumA(5); cout d+f endl; return0; 二、 简单应用题 2 请打开考生文件夹下的解决方案文件 proj2,该工程中包含一个程序文件main cpp,其中有类 Quadritic、类 Root及主函数 main的定义。一个 Quadritic对象表示一个 ax2+bx+c的一元二次多项式。一个 Root对象用于表示方程 ax2+bx+c=0的一组根,它的数据成员 num_of_roots有 3种可能的值,即 0、 1和 2,分别表示根的 3种情况:无实根、有两个相同的实根和有两个不同的实根。请在程序中的横线处填写适当的

4、代码并删除横线,以实现上述类定义。此程序的正确输出结果应为(注:输出中的 X 2表示 x2): 3X 2+4X+5=0 0无实根 4 5X 2+6X+2=0 0有两个相同的实根: -0 666667和 -0 666667 1 5X 2+2X-3=0 0有两个不同的实根: 0 896805和 -2 23014 注意:只能在横线处填写适当的代码,不要改动 程序中的其他内容,也不要删除或移动 “ *found*”。 #include iostream #include iomanip #include cmath using namespace std; class Root一元二次方程的根 pub

5、lic: const double x1;第一个根 const double x2;第二个根 const int num_of_roots;不同根的数量: 0、 1或 2 创建一个 “无实根 ”的Root对象 Root( ): x1(0 0), x2(0 0), num_of_rootS(0) 创建一个 “有两个相同的实根 ”的 Root对象 Root(double root) *found* : _ 创建一个 “有两 个不同的实根 ”的 Root对象 Root(double root1, doubleroot2):x1(root1), x2(root2), mum_of_roots(2) v

6、oid show( )const显示根的信息 cout “ t t“; switch(num_of_roots) case0: *found* case1: cout “有两个相同的实根: “ x1 “和 “ x2; break; default: cout “有两个不同的实根: “ x1 “和 “ x2; break; , class Quadratic二次多项式 public: const double a, b, c;分别表示二次项、一次项和常数项等 3个系数 Quadratic(double a, double b, double c) 构造函数 *found* : _ Quadrat

7、ic(Quadratic&x)复制构造函数 :a(x a), b(x b), c(x c) Quadratic add(Quadratic x)const 求两个多项式的和 return Quadratic(a+x a, b+x b, c+x c); Quadratic sub(Quadratic x)const 求两个多项式的差 *found* _ double value(double x)const 求二次多项式的值 returna*x*x+b*x+c; Root root( )const求一元二次方程的根 double delta=b*b-4*a*c; 计算判别式 if(delta 0

8、 0)return Root ; if(deita=0 0) return Root(-b (2*a); double sq=sqrt(delta); return Root(-b+sq) (2*a), (-b-sq) (2*a); void show( )const显示多项式 cout endl a “X 2“ showpos b “X“ Cnoshowpos; void showFunction( ) 显示一元二次方程 show( ); cout “=0 0“; ; int main( )( Quadratic q1(3 0, 4 0, 5 0), q2(4 5, 6 0,2 0), q3

9、q2 sub(q1); q1 showFunction( ); q1 root( ) show( ); q2 showFunction( ); q2 root( ) show( ); q3 showFunction( ); q3 root( ) show( ); cout endl; return0; 三、综合应用题 3 请打开考生文件夹下的解决方案文件 proj3,此工程中包含一个源程序文件proj3 cpp,补充编制 C+程序 proj3 cpp,其功能是读取文本文件 in dat中的全部内容,将文本存放到 doc类的对象 myDoc中。然后将 myDoc中的字符序列反转,并输出到文件

10、out dat中。文件 in dat的长度不大于 1000字节。 要求: 补充编制的内容写在 “ *333*”与 “*666*”两行之间。实现将 myDoc中的字符序列反转,并将 反转后的序列在屏幕上输出。不得修改程序的其他部分。 注意:程序最后已将结果输出到文件 out dat中,输出函数 writeToFile已经给出并且调用。 proj3 cpp #include iostream #include fstream #include cstring using namespace std; Class doC private: char*str;文本字符串首地址 int length;文

11、本字符个数 public: 构造函数,读取文件内容,用于初始化新对象, filename是文件名字符串首地址 doc(char*filename); void reverse( );将字符序列反转 doc( ); void writeToFile(char*filename); ; doc: doc(char*filename) ifstream myFile(filename); int len=1001, tmp; str=new charlen; length=0; while(tmp=myFile get( )!=EOF) strlength+=tmp; strlength= 0; m

12、yFiie close( ); void doc: reverse( ) 将数组 str中的 length个字符中的第一个字符与最后一个字符交换,第二个字符与倒数第二个 字符交换 *333* *666* doc: doc( ) delete str; void doc: writeToFile(char*filename) ofstream outFile(filename); outFile str; outFile close( ); void main( ) doc myDoc(“in dat“); myDoc reverse( ); myDoc writeToFile(“out dat

13、); 国家二级 C+机试(操作题)模拟试卷 506答案与解析 一、基本操作题 1 【正确答案】 (1)ai=aai; (2)d+=x GetA(j); (3)int f=x SumA(5); 【试题解析 】 (1)Xabc的构造函数需要使用形参 aa为成员指针 a赋值,函数体中,首先动态分配 n个连续的整型变量的内存空间,并将内存空间的首地址赋给a,然后通过 for循环,使用数组 aa为 a的 n个元素的赋值 (2)x是类 Xabc的对象,成员 a在定义时没有指定访问标号,默认为 private成员,所以这里不能通过 x对象直接访问 a,应该通过公有成员函数 GetA( )来访问 (3)Su

14、mA( )函数是类 Xabc的公有成员函数,需要通过对象来引用,而不能直接引用 二、简单应用题 2 【正确答案】 (1)x1(root), x2(root), num_of_roots(1) (2)cout “无实根 “; break; (3)a(a), b(b), c(C) (4)return Quadratic(a-x a, b-x b, c-x c); 【试题解析】 (1)主要考查考生对构造函数的掌握,题目要求创建一个 “有两个相同的实根 ”的 Root对象。说明两个根 x1和 x2相等,根的数量为 1,因此可以得出语句: x1(root), x2(root), num_of_roots

15、1)。 (2)主要考查考生对 switch语句的掌握,在语句 switch(num_of_roots)中,num_of_roots代表根的数量,当为 0时,表示没有根,因此输出无实根,注意要在句尾加 break。 (3)主要考查考生对构造函数的掌握,本题使用成员初始化列表来构造函数。 (4)主要考查考生对成员函数的掌握,题目要求求两个多项式的差。两个多项式的差就是各个次方的系数相减,因此得出语句: return Quadratic(a-x a, b-x b,c-x c);。 三、综合应用题 3 【正确答案】 int i, j;定义两个整数临时变量 i j for(i=0, j=length-

16、1; i j; i+, j-) char temp=stri;把 Stri中的值保存在临时变量 temp stri=strj; 把 Strj值赋给 Stri,实现字符前后替换 strj=temp; 把保存在临时变量 temp中的值再赋值给 Strj 【试题解析】 题目要求将 myDoc中的字符序列反转,在 main函数中我们看到mvDoc是 doc类,根据 doc类的定义可以知道它是把读取文件的字符串存到 str动态数组中。 reverse函数实现将数组 str中的 length个字符中的第一个字符与最后一个字符交换,第二个字符与倒数第二个字符交换,依次类推。使用变量 i和 j,分别表示第一个字符和最后一个字符的下标,定义 temp作为中间变量进行交换。

展开阅读全文
相关资源
猜你喜欢
  • BS PD ISO TR 4191-2014 Plastics piping systems for water supply Unplasticized poly(vinyl chloride)(PVC-U) and oriented PVC-U (PVC-O) Guidance for installation《供水用塑料管系统 未增塑聚氯乙烯(PVC-U)和定向PVC-U (PVC-.pdf BS PD ISO TR 4191-2014 Plastics piping systems for water supply Unplasticized poly(vinyl chloride)(PVC-U) and oriented PVC-U (PVC-O) Guidance for installation《供水用塑料管系统 未增塑聚氯乙烯(PVC-U)和定向PVC-U (PVC-.pdf
  • BS PD ISO TS 13399-305-2017 Cutting tool data representation and exchange Creation and exchange of 3D models Modular tooling systems with adjustable cartridges for boring《切削工具数据表示和交换 三维模型的创造和交流.pdf BS PD ISO TS 13399-305-2017 Cutting tool data representation and exchange Creation and exchange of 3D models Modular tooling systems with adjustable cartridges for boring《切削工具数据表示和交换 三维模型的创造和交流.pdf
  • BS PD ISO TS 19337-2016 Nanotechnologies Characteristics of working suspensions of nano-objects for $ii$in $iv$ii$it$ir$io assays to evaluate inherent nano-object toxi city《纳米技术 评估纳米物体固有毒性的体外试验所.pdf BS PD ISO TS 19337-2016 Nanotechnologies Characteristics of working suspensions of nano-objects for $ii$in $iv$ii$it$ir$io assays to evaluate inherent nano-object toxi city《纳米技术 评估纳米物体固有毒性的体外试验所.pdf
  • BS PD ISO TS 21219-2-2014 Intelligent transport systems Traffic and travel information (TTI) via transport protocol experts group generation 2 (TPEG2) UML modelling rules《智能运输系统 利用第二代传输协议专家组 (TPEG.pdf BS PD ISO TS 21219-2-2014 Intelligent transport systems Traffic and travel information (TTI) via transport protocol experts group generation 2 (TPEG2) UML modelling rules《智能运输系统 利用第二代传输协议专家组 (TPEG.pdf
  • BS PD ISO TS 28560-4-2014 Information and documentation RFID in libraries Encoding of data elements based on rules from ISO IEC 15962 in an RFID tag with partitioned memory《信息与文献 图书馆中应用的无线射频识别技术.pdf BS PD ISO TS 28560-4-2014 Information and documentation RFID in libraries Encoding of data elements based on rules from ISO IEC 15962 in an RFID tag with partitioned memory《信息与文献 图书馆中应用的无线射频识别技术.pdf
  • BS S 150-1975 Specification for chromium-molybdenum-vanadium-niobium heat-resisting steel billets bars forgings and parts (930-1080 MPa) (Cr 10 5 Mo 0 6 V 0 2 Nb 0 3)《铬-钼-钒-铌-耐热钢坯、棒材、锻件及零件规范(930-.pdf BS S 150-1975 Specification for chromium-molybdenum-vanadium-niobium heat-resisting steel billets bars forgings and parts (930-1080 MPa) (Cr 10 5 Mo 0 6 V 0 2 Nb 0 3)《铬-钼-钒-铌-耐热钢坯、棒材、锻件及零件规范(930-.pdf
  • BS PD ISO IEC TR 20000-12-2016 Information technology Service management Guidance on the relationship between ISO IEC 20000-1 2011 and service management frameworks CMMI-SVC 《信息技术 .pdf BS PD ISO IEC TR 20000-12-2016 Information technology Service management Guidance on the relationship between ISO IEC 20000-1 2011 and service management frameworks CMMI-SVC 《信息技术 .pdf
  • BS PD ISO IEC TR 20007-2014 Information technology Cultural and linguistic interoperability Definitions and relationship between symbols icons animated icons pictograms characters .pdf BS PD ISO IEC TR 20007-2014 Information technology Cultural and linguistic interoperability Definitions and relationship between symbols icons animated icons pictograms characters .pdf
  • BS PD ISO IEC TR 29110-5-1-3-2017 Systems and software engineering Lifecycle profiles for Very Small Entities (VSEs) Software engineering Management and engineering guide Generic p.pdf BS PD ISO IEC TR 29110-5-1-3-2017 Systems and software engineering Lifecycle profiles for Very Small Entities (VSEs) Software engineering Management and engineering guide Generic p.pdf
  • 相关搜索

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

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