计算机等级考试二级C++上机模拟试题11套汇总及答案解释(三)
来源:互联网 发布:java视频教程哪个好 编辑:程序博客网 时间:2024/04/30 14:05
练习7
一、改错题
使用VC6打开考生文件夹下的工程kt12_1,此工程包含一个源程序文件kt12_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为:
100
源程序文件kt12_1.cpp清单如下:
#include<iostream.h>
template<class T>
class pair
{
T value1,value2;
public:
pair(T first,T second)
{value1=first;value2=second;}
/*****************found*****************/
char getmax();
};
/*****************found*****************/
T pair<T>::getmax()
{
T retval;
/*****************found*****************/
retval=value1>value2??value1:value2;
return retval;
}
void main()
{
pair<int>myobject(100,75);
cout<<myobject.getmax()<<endl;
}
【说明】题目里的#include<iostream.h>
如果改为#include<iostream>
using namespace std;
会导致该题目中的pair与标准库的pair重名,而报错。
如果要改用标准库,则该题目pair的名字需要修改例如改为pair1
【参考答案】
(1)将char getmax ();改为:T getmax ();
(2)缺少模板的声明,前面需要加上:template <class T>
(3)将retval = value1>value2?? value1 : value2;
改为:retval = value1>value2? value1 : value2;
【试题解析】
(1)主要考查对模板使用的理解,该函数属于模板类定义的一部分,对于返回值类型,应该使用模板类名称T,这样编译的时候才能被接受;
(2)主要考查是模板的使用,前面的模板类已经声明完成了,在类的外面定义类的成员函数时仍然需要使用模板的声明,这样在后面的函数定义体中才能使用模板类;
(3)主要考查对“表达式1?表达式2 : 表达式3”语句的掌握,这个语句是一个复合语句,先计算第一个表达式,如果为真则整个式子值为表达式2的值,否则为表达式3的值,题目中错误的使用了两个问号。
#include<iostream>
using namespace std;
template<class T>
class pair1
{
T value1,value2;
public:
pair1(T first,T second)
{value1=first;value2=second;}
/*****************found*****************/
T getmax();//char getmax();
};
/*****************found*****************/
template <class T> T pair1<T>::getmax()//T pair1<T>::getmax()
{
T retval;
/*****************found*****************/
retval=value1>value2?value1:value2;//retval=value1>value2??value1:value2;
return retval;
}
void main()
{
pair1<int>myobject(100,75);
cout<<myobject.getmax()<<endl;
}
二、简单应用题
请编写函数fun(),其功能是将s所指字符串中除了下标为奇数、同时ASCII值也为奇数的字符之外,其余的所有字符都删除。字符串中剩余的字符所形成的一个新的字符串放在t所指的数组中。
例如:s所指字符串中的内容为ABCDEFG12345,其中字符A的ASCII码值虽为奇数,但元素所在的下标为偶数,因此必需删除;字符1的ASCII码值为奇数,所在数组中的下标也为奇数,不删除,最后t所指的数组中的内容应是135。
请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。
文件kt12_2.cpp的内容如下:
#include<conio.h>
#include<cstdio>//#include<stdio.h>
#include<iostream>
#include<cstring>//#include<string.h>
using namespace std;
void fun(char*s,char t[])
{
}
void main()
{
char s[100],t[100];
cout<<"Please enter string S:"<<endl;
gets(s);
fun(s,t);
puts(t);
}
【参考答案】
void fun(char *s,char t[ ])
{ int i,j=0,n;
n=strlen(s);
for(i=0;i<n;i++)
if(i%2!=0&&s[i]%2!=0)
{ t[j]=s[i];j++;}
t[j]='\0'; }
【试题解析】
本体的解题思路是要先搞清楚在字符参与数值运算时,用的是其ASCII码值来进行计算。其次是判断某数是奇数的方法,即判断该数与2的余数是否为0。
【调试过程】
#include<conio.h>
#include<cstdio>//#include<stdio.h>
#include<iostream>
#include<cstring>//#include<string.h>
using namespace std;
void fun(char*s,char t[])
{
int len,j=0,m;
len=strlen(s);
// cout<<len<<endl;//已经检查输出的s长度正确
// t[0]=len;//已经检查可以这样赋值
// t[0]='a';
// t[3]='c';
for(int i=1;i<len;i=i+2)
{
//检测ASCII是否为奇数,如果是奇数就赋值给t[j]
if(s[i]%2==1){t[j]=s[i];j++;}//if(((int)s[i])%2==1){t[j]=s[i];j++;}
//检测字符串s是否结束,如果是就将t[j]除了有内容以外的后面的都赋值为空NULL
}
// cout<<int('A')<<endl;//输出A的ASCII为65
//cout<<s[0]<<endl;//这样会直接输出字符
/* int m;
m=s[0]%2;
cout<<m<<endl; //测试这样可以正确地输出奇偶
*/
/* int ascii;
char a = 'c';
ascii = (int)a;
cout<<ascii<<endl; //测试这样可以正确地获取ASCII
*/
/* int ascii;
char a = s[0];
ascii = (int)a;
cout<<ascii<<endl;
*/
m=j;
for(int k=m;k<100;k++)//之前k=len先只尝试将比s长的后面都赋值为空NULL
{
t[j]=NULL;
j++;
}
}
void main()
{
char s[100],t[100];
cout<<"Please enter string S:"<<endl;
gets(s);
fun(s,t);
puts(t);
}
【优化答案】
三、综合应用题
使用VC6打开考生文件夹下的工程kt12_3。此工程包含一个kt12_3.cpp,其中定义了类ARRAY,但类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)完成类ARRAY的带一个参数的构造函数,参数i为int型,如果i不是正数则输出错误信息并退出,否则申请int型的大小为i的空间,然后把i赋值给类的数据成员num。请在注释“//**1**”之后添加适当的语句。
(2)完成类ARRAY的拷贝初始化构造函数,注意解决重复删除的问题,请在注释“//**2**”之后添加适当的语句。
(3)完成类ARRAY的重载的运算符函数[],参数i为int型,如果i超界则输出错误信息并退出,否则把下标为i的元素返回,请在注释“//**3**”之后添加适当的语句。
(4)完成类ARRAY的重载的运算符函数=,同样需要注意解决重复删除的问题,不能只是简单的赋值,请在注释“//**4**”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件kt12_3.cpp清单如下:
#include<iostream>
#include<cstdlib>
class ARRAY
{
private:
int *p,num;
public:
ARRAY(){p=new int[10],num=10;}
ARRAY(int i)
{ //**1**
{ cout<<"错误!数组长度应为正。\n";
exit(0);
}
p=new int[i];
num=i;
}
ARRAY(const ARRAY &a);
int &operator[](int i);
~ARRAY(){delete p;}
ARRAY &operator=(const ARRAY &a);
friend ARRAY operator+(ARRAY &a,ARRAY &b);
friend ostream& operator<<(ostream &os,ARRAY &a);
};
ARRAY::ARRAY(const ARRAY &a)
{
//**2**
for(int i=0;i<num;i++)
p[i]=a.p[i]; }
int &ARRAY::operator[](int i)
{ //**3**
{
cout<<"越界访问!";
exit(0);
}
return p[i];
}
ARRAY &ARRAY::operator=(const ARRAY &a)
{
num=a.num;
p=new int[num];
for(int i=0;i<num;i++)
p[i]=a.p[i];
//**4**
}
ARRAY operator+(ARRAY &a,ARRAY &b)
{
if(a.num!=b.num)
{
cout<<"数组长度不相同!"<<endl;
exit(0);
}
ARRAY t(a.num);
for(int i=0;i<a.num;i++)
t.p[i]=a.p[i]+b.p[i];
return t;
}
ostream &operator<<(ostream &os,ARRAY &a)
{
int i=0;
for(;i<a.num;i++)
{
cout<<a.p[i]<<' ';
if(!((i+1)%10))cout<<endl; }
return os; }
void main()
{ ARRAY a(3);
a[0]=a[1]=a[2]=3;
cout<<'a'<<a<<endl;
ARRAY b(a);
cout<<'b'<<b<<endl;
ARRAY c(2);
c=a+b+b;
cout<<'c'<<c<<endl;
c=((b=(a+b))+c);
cout<<'a'<<a<<'b'<<b<<'c'<<c<<endl;
a[7]=3;
cout<<a;
}
【参考答案】
(1)if(i<=0)
(2)num=a.num;
p=new int[num];
(3)if(i>=num||i<0)
(4)return *this;
【试题解析】
主要考查对一个特殊的类--安全数组的掌握,其中涉及了友元函数、重载函数等,其中(2)中必需申请新的空间,这样可以使得两个对象分别占用不同的两个空间,在自动调用析构函数时不会遇到重复删除的问题,这种方法要掌握。
这里头文件如果改为:#include<iostream.h>
#include<stdlib.h>会报错。fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
error C2433:'ostream' : 'friend' not permitted on data declarations
#include<iostream.h>
#include<stdlib.h>
class ARRAY
{
private:
int *p,num;
public:
ARRAY(){p=new int[10],num=10;}
ARRAY(int i)
{ //**1**
if(i<=0)
{ cout<<"错误!数组长度应为正。\n";
exit(0);
}
p=new int[i];
num=i;
}
ARRAY(const ARRAY &a);
int &operator[](int i);
~ARRAY(){delete p;}
ARRAY &operator=(const ARRAY &a);
friend ARRAY operator+(ARRAY &a,ARRAY &b);
friend ostream& operator<<(ostream& os,ARRAY& a);
};
ARRAY::ARRAY(const ARRAY &a)
{
//**2**
num=a.num;
p=new int[num];
for(int i=0;i<num;i++)
p[i]=a.p[i]; }
int &ARRAY::operator[](int i)
{ //**3**
if (i>=10||i<0)
{
cout<<"越界访问!";
exit(0);
}
return p[i];
}
ARRAY &ARRAY::operator=(const ARRAY &a)
{
num=a.num;
p=new int[num];
for(int i=0;i<num;i++)
p[i]=a.p[i];
//**4**
return *this;
}
ARRAY operator+(ARRAY &a,ARRAY &b)
{
if(a.num!=b.num)
{
cout<<"数组长度不相同!"<<endl;
exit(0);
}
ARRAY t(a.num);
for(int i=0;i<a.num;i++)
t.p[i]=a.p[i]+b.p[i];
return t;
}
ostream &operator<<(ostream &os,ARRAY &a)
{
int i=0;
for(;i<a.num;i++)
{
cout<<a.p[i]<<' ';
if(!((i+1)%10))cout<<endl; }
return os; }
void main()
{ ARRAY a(3);
a[0]=a[1]=a[2]=3;
cout<<'a'<<a<<endl;
ARRAY b(a);
cout<<'b'<<b<<endl;
ARRAY c(2);
c=a+b+b;
cout<<'c'<<c<<endl;
c=((b=(a+b))+c);
cout<<'a'<<a<<'b'<<b<<'c'<<c<<endl;
a[7]=3;
cout<<a;
}
计算机等级考试二级C++模拟题及解析8上机部分
一、改错题
使用VC6打开考生文件夹下的工程kt13_1,此工程包含一个源程序文件kt13_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果为:
5
5
源程序文件kt13_1.cpp清单如下:
#include<iostream>
using namespace std;
class A{
public:
/*****************found*****************/
static int n=1;
A(){n++;};
~A(){n--;};
};
/*****************found*****************/
int n=0;
int main(){
A a;
A b[3];
A *c=new A;
c=&a;
/*****************found*****************/
cout<<c.n<<endl;
cout<<A::n<<endl;
return 0;
}
【参考答案】
(1)将static int n=1;改为:static int n;
(2)将int n=0;改为:int A::n=0;
(3)将cout<<c.n<<endl;改为:cout<<c->n<<endl;
【试题解析】
(1)在类的声明中只能对静态数据成员进行引用性声明,不能进行静态数据成员初始化;
(2)由于静态数据成员是类的成员,所以在初始化时必须限定它所属的类;
(3)c为类的对象指针,访问类成员函数时使用符号“->”,而不能用“.”。
#include<iostream>
using namespace std;
class A{
public:
/*****************found*****************/
static int n;//static int n=1;
A(){n++;};
~A(){n--;};
};
/*****************found*****************/
int A::n=0;//int n=0;
int main(){
A a;
A b[3];
A *c=new A;
c=&a;
/*****************found*****************/
cout<<c->n<<endl;//cout<<c.n<<endl;
cout<<A::n<<endl;
return 0;
}
二、简单应用题
请编写一个函数sortnum(intnum),参数num是一个三位的整数,该函数将num的百位、十位和个位的数字进行重排,并返回由上述的三个数字组成的最大的三位数。
注意:部分源程序已存在文件kt13_2.cpp中。
如输入456后,输出结果如下:
654
请勿修改主函数main和其他函数中的内容,仅在函数sortnum的花括号中填写若干语句。
文件kt13_2.cpp的内容如下:
#include<iostream>
using namespace std;
int sortnum(int num)
{
}
void main()
{ int num;
int result=0;
cout<<"请输入一个三位数";
cin>>num;
cout<<sortnum(num)<<endl;
}
【参考答案】
int sortnum(int num)
{
int number[3],i,j,temp;
for(i=0;i<3;i++)
{ number[i]=num%10;
num=num/10; }
for (i=0;i<2;i++)
{ for (j=i+1;j<3;j++)
if (number[j]<=number[i])
{ temp=number[j];
number[j]=number[i];
number[i]=temp; } }
num=number[2]*100+number[1]*10+number[0];
return num;
}
【试题解析】
本题主要考查对基本控制结构和数组的访问,以及根据实际需要建立模型,对数字进行排序的能力,答案中采用的是冒泡排序法。请注意程序中数组下标的范围和两层for循环嵌套中循环变量的取值情况。
【参考答案2】
【调试过程】
#include<iostream>
using namespace std;
int sortnum(int num)
{
if(num>999||num<=99)
{ cout<<"num越界,输入的数字要求是非零开头的三位整数!";
exit(0);
}
int a,b,c;
c=num%10;//获得个位
b=((num-c)/10)%10;
a=(num-b*10-c)/100%10;//获得百位
/* int a,b,c;
c=num%10;//获得个位
cout<<c<<endl;
b=((num-c)/10)%10;
cout<<b<<endl;
a=(num-b*10-c)/100%10;//获得百位
cout<<a<<endl;
*/ //测试个位十位百位是否正确获取
if(a>b)//使得a<b
{
int z;
z=b;
b=a;
a=z;
}
if(b>c)//使得b<c
{
int z;
z=c;
c=b;
b=z;
}
if(a>b)
{
int z;
z=b;
b=a;
a=z;
}
return c*100+b*10+a;
//return 1;//本该返回重新排序的3位数,暂时先假定返回1.
}
void main()
{ int num;
int result=0;
cout<<"请输入一个三位数";
cin>>num;
cout<<sortnum(num)<<endl;
}
【答案优化】
#include<iostream>
using namespace std;
int sortnum(int num)
{
if(num>999||num<=99)
{ cout<<"num越界,输入的数字要求是非零开头的三位整数!";
exit(0); }
int a,b,c;
c=num%10;//获得个位
b=((num-c)/10)%10;
a=(num-b*10-c)/100%10;//获得百位
if(a>b)//使得a<b
{
int z;
z=b;
b=a;
a=z;
}
if(b>c)//使得b<c
{
int z;
z=c;
c=b;
b=z;
}
if(a>b)
{
int z;
z=b;
b=a;
a=z;
}
return c*100+b*10+a;
}
void main()
{ int num;
int result=0;
cout<<"请输入一个三位数";
cin>>num;
cout<<sortnum(num)<<endl;
}
参考答案1的缺点是当输入一个4位或者更多位的数字时,该程序会自动按照最后3位数字进行比较,而不提示输入数位错误。
三、综合应用题
使用VC6打开考生文件夹下的工程kt13_3。此工程包含一个kt13_3.cpp,其中定义了类Person,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)定义类Person的私有数据成员forename、surname和money,forename和surname都是char型的指针数据,money是double型的数据。请在注释“//**1**”之后添加适当的语句。
(2)完成类Person的带三个参数的构造函数Person(char*f,char*s,doublem),分别为forename和surname申请新的空间来存储参数f和s指针指向的内容,注意空间的大小,最后把参数m的值赋给money,请在注释“//**2**”之后添加适当的语句。
(3)完成类Person的析构函数的定义,把forename和surname指向的空间释放,请在注释“//**3**”之后添加适当的语句。
(4)完成类Person的成员函数display的定义,使其以格式"forname surname has money"的形式输出内容,请在注释“//**4**”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
输出结果如下:
RichardBerkhas1000.56
Pressanykeytocontinue
源程序文件kt13_3.cpp清单如下:
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
//**1**
double money;
public:
Person(char *f,char *s,double m);
~Person();
void display();
};
Person::Person(char *f,char *s,double m)
{
//**2**
strcpy(forename,f);
surname=new char[strlen(s)+1];
strcpy(surname,s);
money=m;
}
Person::~Person()
{
//**3**
}
void Person::display()
{
//**4**
}
void main()
{
Person p("Richard","Berk",1000.56);
p.display();
}
【参考答案】
(1)char *forename;
char *surname;
(2)forename = new char[strlen(f)+1];
(3)delete []forename;
delete []surname;
(4)cout<<forename<<" "<<surname<<" has "<<money<<endl;
【试题解析】
主要考查类的定义和字符指针的动态空间申请和释放。(2)中new关键字是动态申请空间,里面使用了函数strlen,该函数是返回参数字符串的长度,之所以加一是要加入字符串结尾标志,这一点考生切记。(3)中delete是释放动态内存的关键字,释放一位数组占用的内存格式是先写[],之后是指针名,括号内不写数组的大小。
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
//**1**
char *forename,*surname;
double money;
public:
Person(char *f,char *s,double m);
~Person();
void display();
};
Person::Person(char *f,char *s,double m)
{
//**2**
forename=new char[strlen(f)+1];
strcpy(forename,f);
surname=new char[strlen(s)+1];
strcpy(surname,s);
money=m;
}
Person::~Person()
{
//**3**
delete []forename;
delete []surname;
}
void Person::display()
{
//**4**
cout<<forename<<" "<<surname<<" "<<"has"<<" "<<money<<endl;
//即cout<<forename<<" "<<surname<<" has "<<money<<endl;
}
void main()
{
Person p("Richard","Berk",1000.56);
p.display();
}
练习9
9
上机部分
一、改错题
使用VC6打开考生文件夹下的工程kt14_1,此工程包含一个源程序文件kt14_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果为:
A:noparameters
B:intparameter
A:noparameters
B:intparameter
C:intparameter
源程序文件kt14_1.cpp清单如下:
#include<iostream>
using namespace std;
class A
{
/*****************found*****************/
private:
A(){cout<<"A:noparameters\n";}
A(int a){cout<<"A:intparameter\n";}
};
class B:public A
{
public:
B(int a){cout<<"B:intparameter\n";}
};
/*****************found*****************/
class C:public B,public A
{
public:
/*****************found*****************/
C(int a)::B(a){cout<<"C:intparameter\n";}
};
void main()
{
B b(1);
C c(2);
}
【参考答案】
(1)将private:改为:public:
(2)将class C:public B,public A改为:class C:public B
(3)将C(int a)::B(a){cout<<"C: int parameter\n"; }
改为:C(int a):B(a){cout<<"C: int parameter\n";}
【试题解析】
(1)一个类的构造函数和析构函数可以由系统自动生成,也可以由用户提供,但构造函数和析构函数都必须是该类的公有成员函数,否则编译时将出现错误,不能被调用;
(2)A已经是B的基类,C公有继承B,A也就成为了C的基类,根据程序的运行结果可知,C是要公有继承B;
(3)派生类的构造函数,初始化基类的参数,调用基类的构造函数时,使用符号“:”,而不是“::”;
#include<iostream>
using namespace std;
class A
{
/*****************found*****************/
public://private:
A(){cout<<"A:noparameters\n";}
A(int a){cout<<"A:intparameter\n";}
};
class B:public A
{
public:
B(int a){cout<<"B:intparameter\n";}
};
/*****************found*****************/
class C:public B//class C:public B,public A
{
public:
/*****************found*****************/
C(int a):B(a){cout<<"C:intparameter\n";} // C(int a)::B(a){cout<<"C:intparameter\n";}
};
void main()
{
B b(1);
C c(2);
}
二、简单应用题
编写函数fun(),它的功能是求n以内(不包括n)同时能被3与7整除的所有自然数之和的平方根s,并做为函数值返回。
例如:n为1000时,函数值应为s=153.909064。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include<conio.h>
#include<math.h>
#include<stdio.h>
double fun(int n)
{
}
void main()
{ clrscr();
printf("s=%f\n",fun(1000)); }
【参考答案】
double fun(int n)
{ double s=0.0;
int i;
for(i=0; i<n;i++)
/*从0~n中找到既能被3整除同时又能被7整除的数,并将这些数求和*/
if(i%3==0&&i%7==0)
s=s+i;
s=sqrt(s); /*对s求平方根*/
return s; }
【试题解析】
本题的解题思路是:逐个取得从0~n之间的所有数,对每次取得的数进行条件判断,条件是既能被3整除同时又能被7整除,注意:这两个条件要求同时成立,因此用到了“&&”运算符。满足条件,该数就被累加到s中去,所有符合条件的数找完后,用sqrt()函数对s求平方根。
【参考答案2】
【调试过程】
#include<windows.h>//#include<conio.h>
#include<math.h>
#include<stdio.h>
double fun(int n)
{
int sum=0;
double a;
for(int i=1;i<n;i++)
{
if((i%3==0)&&(i%7==0))
{sum=sum+i;}
}
// return sum;//先直接返回sum,检查是否正确153.909063*153.909063=23688
a=sqrt(sum); /*对sum求平方根*/
return a;
}
void main()
{ system("cls");//clrscr();其头文件是#include<conio.h>,但clrscr函数是turboc专有的,vc中不能用
printf("s=%f\n",fun(1000)); }
【优化后】
#include<windows.h>
#include<math.h>
#include<stdio.h>
double fun(int n)
{
int sum=0;
double a;
for(int i=1;i<n;i++)
{
if((i%3==0)&&(i%7==0))
{sum=sum+i;}
}
a=sqrt(sum); /*对sum求平方根*/
return a;
}
void main()
{ system("cls");
printf("s=%f\n",fun(1000)); }
三、综合应用题
使用VC6打开考生文件夹下的工程kt14_3。此工程包含一个kt14_3.cpp,其中定义了二维坐标类Coordinate和三维坐标类ThreeDCoord,其中ThreeDCoord类由Coordinate类public派生,但两个类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)定义类Coordinate的保护数据成员x和y,它们都是int型的数据,代表二维坐标的横纵坐标值。请在注释“//**1**”之后添加适当的语句。
(2)根据类Coordinate定义后的成员函数Display的实现,补充该函数在类Coordinate定义体内的声明,Display为二维坐标类的虚函数。请在注释“//**2**”之后添加适当的语句。
(3)完成二维坐标类Coordinate的构造函数,将参数a和b分别赋值给数据成员x和y。请在注释“//**3**”之后添加适当的语句。
(4)根据ThreeDCoord类构造函数的声明,补充ThreeDCoord构造函数的实现,参数a和b通过调用基类的构造函数来初始化基类的数据成员x和y,c赋值给数据成员z。请在注释“//**4**”之后添加适当的语句。
输出结果如下:
[1,2]
[3,4,5]
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件kt14_3.cpp清单如下:
#include<iostream>
using namespace std;
class Coordinate
{
protected:
//**1**
public:
Coordinate(int a=0,int b=0);
//**2** };
Coordinate::Coordinate(int a,int b)
{ //**3** }
void Coordinate::Display()const
{ cout<<'['<<x<<","<<y<<']'<<endl; }
class ThreeDCoord:public Coordinate
{ int z;
public:
ThreeDCoord(int a=0,int b=0,int c=0);
virtual void Display()const; };
//**4**
void ThreeDCoord::Display()const
{ cout<<'['<<x<<","<<y<<","<<z<<']'<<endl; }
void main()
{ Coordinate c(1,2);
ThreeDCoord t(3,4,5);
c.Display();
t.Display(); }
【参考答案】
(1)protected:
int x;
int y;
(2)virtual void Display() const;
(3)x=a; y=b;
(4)ThreeDCoord::ThreeDCoord(int a, int b, int c) : Coordinate(a, b)
{ z=c;}
【试题解析】
本题主要考查对类数据成员的定义、构造函数、派生类及虚函数的理解,是C++类知识的一个综合考核。对虚函数的定义格式及继承类构造函数的定义格式请多加注意。
#include<iostream>
using namespace std;
class Coordinate
{
protected:
//**1**
int x,y;
public:
Coordinate(int a=0,int b=0);
//**2**
virtual void Display()const;};
Coordinate::Coordinate(int a,int b)
{ //**3**
x=a;
y=b;
}
void Coordinate::Display()const
{ cout<<'['<<x<<","<<y<<']'<<endl; }
class ThreeDCoord:public Coordinate
{ int z;
public:
ThreeDCoord(int a=0,int b=0,int c=0);
virtual void Display()const; };
//**4**
ThreeDCoord::ThreeDCoord(int a,int b,int c):Coordinate(a,b)
{
z=c;
}
void ThreeDCoord::Display()const
{ cout<<'['<<x<<","<<y<<","<<z<<']'<<endl; }
void main()
{ Coordinate c(1,2);
ThreeDCoord t(3,4,5);
c.Display();
t.Display(); }
练习10
10
上机部分
一、改错题
使用VC6打开考生文件夹下的工程kt15_1,此工程包含一个源程序文件kt15_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果为:
Previous=9,Next=11
源程序文件kt15_1.cpp清单如下:
#include<iostream>
using namespace std;
/*****************found*****************/
int main()
{ int x=10,y,z;
prevnext(x,y,z);
cout<<"Previous="<<y<<",Next="<<z<<endl;
return 0; }
/*****************found*****************/
void prevnext(int x,int prev,int next)
{ /*****************found*****************/
prev=x--;
next=++x; }
【参考答案】
(1)在main()函数前缺少prevnext函数声明
应添加:void prevnext (int,int &,int &);
(2)将void prevnext (int x, int prev,int next)
改为:void prevnext (int x, int &prev,int &next)
(3)将prev=x--;改为:prev=x-1;
【试题解析】
(1)函数在使用前必须已经被定义,main()中调用prevnext函数,而该函数的实现在main()之后,所以在main()之前必须添加该函数的声明;
(2)由运行结果可知,通过函数prevnext调动要改变main()中的实参值,所以prev和next都应为引用型参数;
(3)由运行结果prev=x-1,而源程序的prev=x,x=x-1,这里涉及运算符的优先级问题。
#include<iostream>
using namespace std;
/*****************found*****************/
void prevnext (int,int &,int &);
int main()
{ int x=10,y,z;
prevnext(x,y,z);
cout<<"Previous="<<y<<",Next="<<z<<endl;
return 0; }
/*****************found*****************/
void prevnext (int x, int &prev,int &next)//void prevnext(int x,int prev,int next)
{ /*****************found*****************/
prev=x-1;//prev=x--;
next=++x; }
二、简单应用题
请编写一个函数fun(int x,int n),该函数返回x的n次幂的值,其中x和n都是非负整数。x的n次幂的计算方法是1与x相乘n次,如x的20次幂的计算为1与x相乘20次。
注意:部分源程序已存在文件kt15_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。
如输入3和4,输出结果如下:
34
81
文件kt15_2.cpp清单如下:
#include<iostream>
using namespace std;
double fun(int x,int n)
{
}
void main()
{ int x,n;
cin>>x>>n;
cout<<fun(x,n)<<endl; }
【参考答案】
if(x==0) return 0;
if(n==0) return 1;
int y=1;
for(int i=0;i<n;i++) y*=x;
return y;
【试题解析】
本题主要考查用基本控制结构与函数知识解决实际问题的能力。求x的n次幂是数学中常用的运算,编程时在x=0与n=0时需特殊处理。
【参考答案2】
#include<iostream>
using namespace std;
double fun(int x,int n)
{
if(x<0||n<0)
{ cout<<"输入错误,x和n都是非负整数!\n";
exit(0);
}
int sum=1;
for(int i=0;i<n;i++)
{sum=sum*x;}
return sum;
}
void main()
{ int x,n;
cin>>x>>n;
cout<<fun(x,n)<<endl; }
三、综合应用题
使用VC6打开考生文件夹下的工程kt15_3。此工程包含一个kt15_3.cpp,其中定义了类A、A1和A2,其中A1类由A类公有派生,A2类由A1类公有派生。上述三个类的定义并不完整,请按要求完成下列操作,将程序补充完整。
(1)定义类A的构造函数,该构造函数有一个整型的参数x,在构造函数中请将x赋值给数据成员a。请在注释“//**1**”之后添加适当的语句。
(2)定义类A1的构造函数,该构造函数有两个整型参数x和y,在构造函数中请将x赋值给数据成员b,将y作为基类A构造函数的参数值传入。请在注释“//**2**”后添加适当的语句。
(3)定义类A2的构造函数,该构造函数有三个整型的参数x,y和z,在构造函数中请将x赋值给数据成员c,将y和z分别赋值给基类A1构造函数的参数x和y。请在注释“//**3**”之后添加适当的语句。
(4)完成类A2的成员函数show的定义,该函数调用基类成员函数,输出基类数据成员a和b及类A2自身的数据成员c的值,上述三个值在输出时以空格隔开。请在注释“//**4**”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
程序输出结果如下:
9
6
9 6 2
源程序文件kt15_3.cpp清单如下:
#include<iostream>
using namespace std;
class A
{ int a;
public:
//**1**
int geta(){return a;} };
class A1:public A
{ int b;
public:
//**2**
int getb(){return b;} };
class A2:public A1
{ int c;
public:
//**3**
void show()
{ //**4**
}
};
void main()
{ A2 a(2,6,9);
cout<<a.geta()<<endl;
cout<<a.getb()<<endl;
a.show(); }
【参考答案】
(1)A(int x) {a=x;}
(2)A1(int x,int y) : A(y){b=x;}
(3)A2(int x,int y,int z):A1(y,z){c=x;}
(4)cout<<geta()<<" "<<getb()<<" ";
cout<<c<<"\n";
【试题解析】
本题主要考查对继承和派生的掌握程度,涉及派生类构造函数的定义、基类成员函数的访问等内容。派生类的构造函数的格式请牢记,另外派生类构造函数及析构函数的执行顺序也是需要掌握的。
#include<iostream>
using namespace std;
class A
{ int a;
public:
//**1**
A(int x){a=x;};
int geta(){return a;} };
class A1:public A
{ int b;
public:
//**2**
A1(int x,int y):A(y){b=x;};
int getb(){return b;} };
class A2:public A1
{ int c;
public:
//**3**
A2(int x,int y,int z):A1(y,z){c=x;};
void show()
{ //**4**
cout<<this->geta()<<" ";
cout<<this->getb()<<" ";
cout<<c<<endl;
}
};
void main()
{ A2 a(2,6,9);
cout<<a.geta()<<endl;
cout<<a.getb()<<endl;
a.show(); }
计算机等级考试二级C++模拟题及解析11上机部分
一、改错题
使用VC6打开考生文件夹下的工程kt17_1,此工程包含一个源程序文件kt17_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为:
0 1 4 9 16 25 36 49 64 81
源程序文件kt17_1.cpp清单如下:
#include<iostream>
using namespace std;
template<class T,int N=100>class Vector
{ T vec[N];
public:
void set(int pos,T val);
T get(int pos);
/*****************found*****************/ }
template<class T,int N>void Vector<T,N>::set(int pos,T val)
{ vec[pos]=val; }
/*****************found*****************/
template<class T,int N>Vector<T,N>::get(int pos)
{ return vec[pos]; }
int main()
{ Vector<double,10> v;
int i=0;
double d=0.0;
for(i=0;i<10;i++)
v.set(i,double(i*i));
for(i=0;i<10;i++)
cout<<v.get(i)<<" ";
cout<<endl;
/*****************found*****************/ }
【参考答案】
(1)在“}”后添加分号。
(2)将template <class T, int N> Vector<T, N>::get(int pos)
改为:template <class T, int N> T Vector<T, N>::get(int pos)
(3)将缺少返回值
改为:加入return 0;
【试题解析】
(1)主要考查对于类定义的理解,即使使用了类模板,在类定义的结尾仍然需要使用分号,这是C++的规定;
(2)主要考查是模板类的定义,template是关键字,在<>中间是类型的定义,题目中Vector是一个类的名称,前面应该有该模板的名称,即T,这样才是完整的定义;
(3)主要考查对函数返回值的掌握,任何返回值类型不为int型的函数最后都必须使用returen语句返回对应类型的值,就算是main函数也不例外。
#include<iostream>
using namespace std;
template<class T,int N=100>class Vector
{ T vec[N];
public:
void set(int pos,T val);
T get(int pos);
/*****************found*****************/ };
template<class T,int N>void Vector<T,N>::set(int pos,T val)
{ vec[pos]=val; }
/*****************found*****************/
template<class T,int N>T Vector<T,N>::get(int pos)//template<class T,int N>Vector<T,N>::get(int pos)
{ return vec[pos]; }
int main()
{ Vector<double,10> v;
int i=0;
double d=0.0;
for(i=0;i<10;i++)
v.set(i,double(i*i));
for(i=0;i<10;i++)
cout<<v.get(i)<<" ";
cout<<endl;
/*****************found*****************/
return 0;}
二、简单应用题
请编写函数fun(),其功能是计算并输出下列多项式值
Sn=1+1/1!+1/2!+1/3!+1/4!+…+1/n!
例如:从键盘输入15,则输出为s=2.718282。
注意:部分源程序以存在文件kt17_2.cpp中。
请勿改动主函数main和其他函数中的内容,仅在函数fun的花括号中填入所编写的若干语句。
文件kt17_2.cpp的内容如下:
#include<cstdio>
#include<iostream>
using namespace std;
double fun(int n)
{
}
void main()
{ int n;
double s;
cout<<"In put n:"<<endl;
cin>>n;
s=fun(n);
cout<<"s="<<s<<endl; }
【解释】n!即1*2*3*……*(n-1)*n
【参考答案】
double fun(int n)
{ double t,sn=1.0;
int i,j;
for(i=1;i<=n;i++)
{ t=1.0;
for(j=1;j<=i;j++)
t*=j;
sn+=1.0/t; }
return sn; }
【试题解析】
本题解法是通过观察所给的多项式,可以找出该多项式相应的数学规律。本题中,多项式是一个累加求和的多项式,并且每一项总是比前一项多乘一个整数,所乘整数恰好等于每一项的编号,这样利用循环语句进行阶乘、累加就可以实现功能。由于函数中出现了"/"的运算,所以最终的函数值要考虑到用double类型。
【参考答案2】
【调试】
#include<cstdio>
#include<iostream>
using namespace std;
double fun(int n)
{
if(n<0){cout<<"n必须为非负整数"<<endl;exit(0);}
double sum=1.00;
if(n==0)sum=1;
double k=1.00;
if(n>0)
{
for(int i=1;i<=n;i++)
{
k=k*i;
sum=sum+1/k;}
}
return sum;
}
void main()
{ int n;
double s;
cout<<"In put n:"<<endl;
cin>>n;
s=fun(n);
cout<<"s="<<s<<endl; }
【优化】
#include<cstdio>
#include<iostream>
using namespace std;
double fun(int n)
{
if(n<0){cout<<"n必须为非负整数"<<endl;exit(0);}
double sum=1.00;
if(n==0)return 1;
double k=1.00;
for(int i=1;i<=n;i++)
{
k=k*i;
sum=sum+1/k;}
return sum;
}
void main()
{ int n;
double s;
cout<<"In put n:"<<endl;
cin>>n;
s=fun(n);
cout<<"s="<<s<<endl; }
三、综合应用题
使用VC6打开考生文件夹下的工程kt17_3。此工程包含一个kt17_3.cpp,其中定义了类Letter和Number以及Grid,其中Grid类由Letter类和Number类public派生,但三个类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)添加类Letter的带一个参数c的构造函数,c是char型的数据,默认值为“A”,该函数把参数c的值赋给类的保护成员ch,请在注释“//**1**”之后添加适当的语句。
(2)添加类Number的带一个参数n的构造函数,n是int型的数据,默认值为0,该函数把参数n的值赋给类的保护成员num,请在注释“//**2**”之后添加适当的语句。
(3)添加派生类Grid构造函数的定义,传入的参数为char型的c和int型的n,并将其分别赋值给基类的ch和num,请在注释“//**3**”之后添加适当的语句。
(4)完成派生类Grid的友元函数--"<<"运算符重载的定义,使其以格式"[g.ch,g.num]"输出,请在注释“//**4**”之后添加适当的语句。
源程序文件kt17_3.cpp清单如下:
#include<iostream.h>
class Letter
{
protected:
char ch;
public:
//**1**
};
class Number
{
protected:
int num;
public:
//**2**
};
class Grid:public Letter,public Number
{
public:
//**3**
friend ostream&operator<<(ostream &o,Grid &g); };
//**4**
{ o<<"["<<g.ch<<","<<g.num<<"]";
return o; }
int main()
{ Grid g('C',3);
cout<<"Gridreference:"<<g<<endl;
return 0; }
【参考答案】
(1)Letter(char c = 'A') {ch = c;}
(2)Number(int n = 0) {num = n;}
(3)Grid(char c = 'A', int n = 0) : Letter(c), Number(n){}
(4)ostream &operator<<(ostream &o,Grid &g)
【试题解析】
主要考查对类和派生类的构造函数的定义,以及重载为友元的运算符函数的定义的掌握,其中(3)使用了参数列表进行变量赋值,这是派生类构造函数中经常使用的,(4)对于友元函数在类体外的定义不需要使用作用域符,而ostream类的对象引用可直接使用原来意义的符号“<<”进行输出。
#include<iostream.h>//本题如果改为用#include<iostream>和using namespace std;会报错
class Letter
{
protected:
char ch;
public:
//**1**
Letter(char c='A'){ch=c;};
};
class Number
{
protected:
int num;
public:
//**2**
Number(int n=0){num=n;};
};
class Grid:public Letter,public Number
{
public:
//**3**
Grid(char c='A',int n=0):Letter(c),Number(n){};
//写成Grid(char c,int n):Letter(c),Number(n){};亦可以
friend ostream &operator<<(ostream &o,Grid &g); };
//**4**
ostream &operator<<(ostream &o,Grid &g)
{ o<<"["<<g.ch<<","<<g.num<<"]";
return o; }
int main()
{ Grid g('C',3);
cout<<"Gridreference:"<<g<<endl;
return 0; }
参考资料:计
网上有很多关于这套模
网上流传的大多都是错漏比较多的,本人对其进行了完善并给出了参考答案的思考调试过程仅供参考。
- 计算机等级考试二级C++上机模拟试题11套汇总及答案解释(三)
- 计算机等级考试二级C++上机模拟试题11套汇总及答案解释(一)
- 计算机等级考试二级C++上机模拟试题11套汇总及答案解释(二)
- 计算机等级考试二级C语言模拟试题(1)及答案
- 2011年3月全国计算机等级考试二级c考试试题及答案
- 计算机等级考试上机模拟题(二级C语言)_等级考试模拟题,计算机等级考试,计算机考试
- 二级C语言上机模拟题及答案[1]_等级考试模拟题
- 2004年9月二级C笔试试题及答案
- 2004年4月二级C笔试试题及答案
- 2003年9月二级C笔试试题及答案
- 2003年4月二级C笔试试题及答案
- 2002年9月二级C笔试试题及答案
- 2002年4月二级C笔试试题及答案
- 2001年9月二级C笔试试题及答案
- 2001年4月二级C笔试试题及答案
- 计算机等级考试二级C语言(三)搞定C指针
- 计算机等级考试模拟试题下载
- 全国计算机等级考试二级Java笔试基础答案
- C++ 纯虚函数
- opencart文件目录介绍
- 数据、工具、人员和决心,重新定义大数据行业的机会
- POJ - 2823 Sliding Window(单调队列RMQ)
- 一个swift应用方案
- 计算机等级考试二级C++上机模拟试题11套汇总及答案解释(三)
- CITRIX打印问题全集
- LINUX 性能查看
- 如何理解指向指针的指针?
- 手机照片被误删是否能恢复
- 值得看的书随时记录
- 博弈游戏(2)-c#求解-英雄会在线编程题目
- Azure学习笔记:Service Bus(2)
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed