资源描述
. . . . .
2014年秋省等级考试C++试卷
〔笔试题,共60分〕
第一局部局部 计算机信息根底 〔20道选择题,共20分,略〕
第二局部 C++语言程序设计〔共430分〕
一、 选择题〔用答题卡答题,答案依次填在21~30答题号〕
21. 以下不符合C++语法规那么的数值常量是d。
A.034B.2.1E3 C.0Xab23D.2E1.4
22.表达式:3.6-5/2+1.2+5%2的值是c。
A.4.3B.4.8C.3.8D.3.3
23.以下关于虚函数的表达中,正确的选项是c。
A.虚函数可以是一个static类型的成员函数
B.基类中用virtual说明一个虚函数后,派生类中定义一样原型的函数时可不必加virtual说明
C. 派生类中的虚函数与基类中一样原型的虚函数具有不同的参数个数或类型
D.虚函数可以是非成员函数
24.以下关于缺省构造函数的表达中,不正确的选项是a。
A.每个类至少有一个缺省构造函数
B.缺省构造函数没有参数或每一个参数都有缺省值
C.缺省构造函数是唯一的
D.每个类至少有两个构造函数
25.在循环体中,使用break和continue语句的作用分别是d。
A.两语句都完毕该层循环 B.前者完毕该层的本次循环,后者完毕该层循环
C.两语句都完毕该层的本次循环 D.前者完毕该层循环,后者完毕该层的本次循环
26.以下表达中,不正确的选项是d。
A.派生类可以继承多个基类 B.可以有多个派生类继承同一个基类
C.派生类可以有多个虚基类 D.抽象类必须是虚基类
27.设由类的成员函数和友元函数分别实现一样的运算符重载功能,以下表达中正确的选项是b。
A.两种函数的参数个数一样 B.友元函数比成员函数多一个参数
C.成员函数比友元函数多一个参数 D.两种函数都带有this指针
28. 以下对函数重载的表达中,正确的选项是c。
A.函数名不同,但函数实现的功能一样
B.函数名一样,函数的参数个数一样但参数的类型不同
C.函数名一样,但函数的参数个数不同或参数的类型不同
D.函数名一样,但函数的参数类型不同或函数的返回值的类型不同
29. 以下关于类成员的表达中, 不正确的选项是a。
A.类的析构函数可以重载 B.类的构造函数可以重载
C.类中的成员都有明确的访问权限 D.可将成员函数定义为静态的
30. 以下的表达中,不正确的选项是c。
A.在不同函数中可以使用一样名字的变量
B.函数的形式参数是局部变量
C.在函数的复合语句中定义的变量在本函数围有效
D.在函数定义的变量只在本函数围有效
二、 填空题〔请将答案填写在答题纸的相应答题号,每个答案只占一行〕
●根本概念题,共5分
1. 在C++中,函数的参数传递方式有三种:第一种是值传递,第二种是指针传递,第三种是引用传递。
2.符号“&〞作为单目运算符时的功能是取地址;作为双目运算符时,其功能是按位与。
3.设有以下语句:
#define S(x) x*x
int k=3,y;
y= S(k+k);
执行赋值语句:“y= S(k+k);〞,那么y的值为15。
4.如果派生类没有重载基类的纯虚函数时,那么该派生类也是抽象类。
●阅读程序题,共13分
5. [程序] 〔2分〕
#include <iostream.h>
void f1(void)
{ int x=10;
static int y=10;
x+=y;y+=x;
cout<<x<<’\t’<<y<<endl;
}
void main(void)
{ f1( );f1 ( ); f1 ( );}
执行程序,输出的第二行是40,第三行是70。
6.[程序] 〔2分〕
#include <iostream.h>
int x=100;
void main(void)
{ int x=30;
x+=::x++;
{int x=60; ::x+=x; }
cout<<++x<<'\n';
cout<<::x<<'\n';
}
执行程序,输出的第一行是131,第二行是161。
7. [程序] 〔3分〕
#include <iostream.h>
int fact(int n)
{ if (n>1)
return n*fact(n-1);
return 1;
}
int f (int a[ ], int n)
{ if (n>=2) return f(a,n-1) + a[n-1] ;
return a[0] ;
}
void exchange(int a[], int n)
{ int i, temp ;
for (i=0; i<n/2; i++){
temp=a[i]; a[i]=a[n-i-1]; a[n-i-1]=temp;
}
}
void main(void)
{ int aa[5] = {1, 2, 3, 4};
cout<< f(aa,4) <<endl;
cout<<fact(aa[3])<<endl;
exchange (aa, 4);
for (int i=0; i<4; i++)
cout<<aa[i]<<'\t';
cout<<endl;
}
执行程序,输出的第一行是10,第二行是24,第三行是4 3 2 1 。
8. [程序] 〔3分〕
#include <iostream.h>
class A{
int x;
public:
A(int i) { x=i; cout<<"A: x="<<x<<endl; }
void print( ) { cout<<"x="<<x<< " "; }
};
class B: public A{
int y;
A a;
public:
B(int i, int j): A(i+j), a(j+10)
{y=i*j; cout<<"B: y="<<y<<endl; }
void print( )
{ A::print( ); a.print( );cout<<"y="<<y<<endl; }
};
void main(void)
{B b(5,10);
b.print( );
}
执行程序,输出的第一行是A:x=15,第三行是B:y=50,第四行是x=15,x=20,y=50。
9. [程序] 〔3分〕
#include <iostream.h>
const int SIZE=100;
class Stack{
char stck[SIZE];
int top;
public:
Stack( ) { top=0; }
virtual void push(char ch)
{ if (top== SIZE) return;
if('a'<=ch && ch<='z') {
stck[top]=ch; top++;
}
}
virtual char pop( )
{ if(top<0) return NULL;
top--; return stck[top];
}
void print( )
{ for (int i=top-1; i>=0;i--)
cout<<stck[i];
cout<<endl;
}
};
class Stack2: public Stack
{char stck[SIZE];
int top;
public:
Stack2( ) { top=0; }
void push(char ch)
{ if (top== SIZE) return;
if('0'<=ch && ch<='9') { stck[top]=ch; top++; }
}
char pop( )
{ if(top<0) return NULL;
top--; return stck[top];
}
void print( )
{ for (int i=0; i<top;i++)cout<<stck[i];cout<<endl;}
};
void main(void)
{char ch,*ptr;
Stack s1,*p;
Stack2 s2;
char *str1 ="a1b2c3D4",*str2= "nangjin5678";
ptr=str1; p=&s1;
while(*ptr) p->push(*ptr++);
ptr=str2; p=&s2;
while(*ptr) p->push(*ptr++);
s1.print( ); s2.print( );
while(ch=p->pop())cout<<ch;
cout<<endl;
}
执行程序,输出的第一行是cba,第二行是5678,第三行是8765。
●完善程序题,共12分
10. 以下函数sortdel(char *s)的功能是:首先将s所指向的字符串中的字符按照字符ASII码值的大小按升序排序,然后在排序后的字符串中删除重复的字符。在主函数中输入一个字符串,调用函数sortdel( ),输出排序后的字符串。
[程序] 〔4分〕
#include <iostream.h>
void sortdel(char *s)
{ char *p,*q,*r,c;
for(p=s; *p; p++) { // 选择法排序
for(r=p,q=p+1; *q; q++) if(*r>*q)r=q;
if(r!=p){ c=*r; *r=*p;*p==c; }
}
for(p=s; *p; p++){ // 删除重复的字符
r=p+1;
while(*p==*r) r++;
if(p != r )
for(q=p+1; *r; q++){ *q=*r;r++;}
*q='\0';
}
}
void main(void)
{ char str[200];
cin.getline(str,199); cout<<str<<endl;
sortdel(str); cout<<str<<endl;
}
11. 以下程序实现一个由数组构成的线性表类,动态产生线性表,表中存放假设干实数。为了简化,本程序仅给出此线性表类的主要成员函数。程序中,重载赋值运算完成线性表对象间的赋值;重载插入运算符“>>〞完成数组的输入;成员函数add( )实现将一个实数参加线性表。
[程序] 〔4分〕
#include <iostream.h>
class List{
public:
List(int=1);
~List( ){ delete []list; }
List& operator =(const List&);
void print( )
{ for(int i=0;i<num;i++){
cout<<list[i]<<','; if((i+1)%5==0)cout<<'\n';
}
}
friend istream& operator >>(istream&, List&);
void add(double x)
{ if( num==size){
double *list1= new double[size+1];
for(int i=0;i<size;i++)list1[i]=list[i];
delete [ ] list;
list=list 1;
size++;
}
list[num]=x; num++;
}
private:
int size,num;
double *list;
};
List::List(int sz)
{ size=sz;
list= new double[size] ; num=0;
}
List& List::operator =(const List& v)
{ if(&v!=this){
delete []list; size=v.size;
list=new double[v.size]或double[size];
for(int i=0;i<num; i++) list[i]=v.list[i];
}
return *this;
}
Iostream&operator>>(istream& is, List& v)
{ for(int i=0;i<v.size;i++){
cout<<i<<":"; is>>v.list[i] ;
}
v.num=v.size; return is;
}
void main(void)
{ List a(10),b(14);
cout<<"Input List b:\n";
cin>>b; b.print( );
a=b; a.add(37.25);
a.print( );
}
12. 以下程序的功能是:先创建两条带有空头结点〔链表的第一个结点不存储数据,只是为了方便处理〕的有序单向链表〔结点元素值按升序排列〕,假设每条链表中的元素均不一样。然后将两条具有一样次序的单链表归并成一条有序单链表,并且规定一样元素只归并一次。函数Create( )利用给定的参数数组〔数组中元素不要求有序〕创建一条具有空头结点的有序单链表,函数Merge( )把两条有序链表归并成一条有序链表,原先的两条链表保持不变。
[程序] 〔4分〕
#include <iostream.h>
struct Node {
int data;
Node *next;
};
void Create(Node *&head,int *a,int n)
{ //根据数组a中n个数据产生一条有序链表
Node *L, *t, *p, *q; //p指向待处理结点,q指向p的前驱结点
head=new Node;
L=head->next=NULL; //L总是指向第一个结点(头结点的下一个结点)
for (int i=0;i<n;i++) {
t=new(Node); t->data=a[i]; t->next=NULL;
if (L==NULL || a[i]<L->data){ //假设链表为空或a[i]小于第一个结点的值
t->next=L; head->next=t;
L=t;
}
else{
q=L;p=q->next;
while (p!=NULL && a[i]>p->data){ //找出新结点t的插入点
q=p;
p=p->next;
}
t->next=p;q->next=t;
}
}
}
void Print(Node *h)
{ h=h->next;
while (h!=NULL) { cout<<h->data<<" ";h=h->next; }
cout<<endl;
}
Node *Merge(Node *h1,Node *h2)
{Node *head,*p,*q,*t,*s;
head=new Node;head->next=NULL;
t=head;p=h1->next;q=h2->next;
while (p!=NULL && q!=NULL) {
s=new Node;
if (p->data<q->data){
s->data=p->data;p=p->next;
}
else if (p->data>q->data){
s->data=q->data;q=q->next;
}
else{
s->data=p->data;p=p->next; q=q->next;
}
t->next=s;t=s;
}
if (q!=NULL) p=q;
while (p!=NULL){
s=new Node;
s->data=p->data;p=p->next;t->next=s;t=s;
}
t->next=NULL;
return head;
}
void main(void)
{ int a[]={12,1,8,2,5,11,3,6,9,7,10,16,4};
int b[]={0,12,8,2,5,18,13,16,9,17,3,6};
int m=sizeof(a)/sizeof(int), n=sizeof(b)/sizeof(int);
Node *h1,*h2,*h3;
Create(h1,a,m);Create(h2,b,n);
cout<<"有序表h1为:"; Print(h1);
cout<<"有序表h2为:"; Print(h2);
h3=Merge(h1,h2);
cout<<"两表归并后:"; Print(h3);
}
参考答案
一、选择题
21.D 22.C 23.B 24.A 25. D 26. D 27. B 28. C 29. A 30.C
二、填空题
1. 指针(地址)传递
2. 引用传递 注:1和2可交换
3. 按位与
4. 15
5. 抽象〔类〕
6. 40 70
7. 80 150
8. 131
9. 161
10. 10
11. 24
12. 4 3 2 1
13. A: x=15
14. B: y=50
15. x=15 x=20 y=50
16. cba
17. 5678
18. 8765
19. r=q
20. *p=c
21.*p == *r
22. *q=*r
23. list =list1
24. list=new double[v.size]或 double[size]
25. istream& operator >>
26. return is
27. L=t
28. p=p->next
29. s->data=q->data
30. p=p->next
21 / 18
展开阅读全文