当前位置:文档之家› 同济大学c++C卷答案

同济大学c++C卷答案

同济大学c++C卷答案
同济大学c++C卷答案

同济大学课程考核试卷

课号:课名:C++语言考试考查:考查

此卷选为:期中考试()、期终考试(V)、重考()试卷

年级____________ 专业___________________ 学号_____________ 姓名_______________ 得分____________ 、选择填空(30分)

1)按照标识符的要求, 组成部分的是(D A、大小写字母

C、下划线F列符号中不属于标识符

)°

B数字

D ~ ! @ # $ % A& *

9)有下列定义:int c[10]; int *p;

以下执行语句中不能使数组c中元素c[1]的值为1

的是(C )°

A、*(c+1)=1;

C、* ++c =1;

B、p=c; *(p+1)=1;

D、p=c; * ++p =1;

2) 若有定义:char c;int x;float y;double z;

则表达式x+c-(int)z/y 值的类型为( C ) °

A、char B 、int C 、float D 、double

3)有下列定义:char s[10],*p,s1[]={

“xyz ” };

以下语句中正确的是( B )°

A、s = “ abcdefgh ”;

B、p = “ abcdefgh ”;

C、s = s1; D p=&s; cin >> p;

4)以下数组定义中正确的是(C )°

A、int n=5;int a[n];B 、int b[3][]={1,2,3};

C、int c[3][4]={0}; D 、int d[][]={2,4,6,8};

5)已知int a(5),b(3);

当逻辑表达式语句!a&&b++;b||a--||a+b;

执行完毕后,a和b的值分别为( D ) °

A、4,4 B 、4,3 C、5,4 D、5,3 10)当被调函数的形参是数组,调用函数的实参用数组名相对应,实现函数调用时,下列描述中,

(B )是错误的:

A、实参数组把地址值传递给形参数组,使得两者指向内存的同一片存储域;

B、函数调用时系统将实参数组元素拷贝一个副本给形参数组;

C、若实参是一维数组名,其对应的一维形参数组可以不必说明数组大小,只需在数组名后跟一对[] 说明即可,但两者类型应保持一致;

D、在被调函数中对形参数组的任何改变,会影响实参数组所指内存存储单兀中的内容。

11)在下列关键字中, 用以说明类中公有成员的是(A ):

A、public

B、private

C、protected

D、friend

6)以下常量中不是字符型常量的是(B )°

A、‘01 '

B、"a"

C、+'

D、\h '

7)对于int *p(); 的描述,(B )是正确的。

A、定义一个指向某int型函数的指针变量p°

B、函数p的声明,该函数的返回值是一个指向整

型数据的指针值。

C、定义函数p,函数p的返回值为int型数据。

D、定义一个int型指针变量p,指向int型数据。12)以下关于构造函数特征描述中,(D )是不正确的:

A、构造函数的函数名与类名相同;

B、构造函数允许重载;

C、构造函数可以设置缺省参数;

D、构造函数必须指定其函数类型说明。

8)下列变量定义中,不正确的是( D )°

A、int a(0),b=1 ; B int a,&b=a ;

C、int a,*b=&a ; D int a=b=1 ;13)下面关于对象概念的叙述中,(A )是错

误的。

A、对象就是结构体变量;

B、对象代表正在创建的系统中的一个实体;

C、对象是一个状态和操作(或方法)的封装体;

D、对象之间的信息传递是通过发送消息进行的。14)在私有继承中,基类的公有成员将成为其派生类的(C )成员。

A、公有B 、保护C、私有D、friend 15) 设Date 是一个日期类,date1 是该类的一个对象,p 是指向date1 的Date 类指针,GetDate() 是Date 类的一个公有成员函数,则以下不正确的表达式是( A )。

A、Date.GetDate()

B、date1.GetDate()

C、(*p).Date::GetDate()

D、p->GetDate()

二、写出下列各程序运行时的输出结果(50 分) 1.

#include

void main()

{ int i=10,j;

float a;

j = 1/(10/3); a = i/3.0;

cout << j << endl;

cout << a << endl;

} 答:0

3.33333

2.

#include void main( ) { int i,j;

i=j=2; if(i=1)

if(j==2)

cout << (i,i+j) << endl; else

cout << (i=i-j) << endl;

cout << i << “” << j << endl;

} 答:3

1 2

3.

#include void swap(int &x,int y) { int t=x;

x=y;

y=t; } void main( ) { int a=10,b=15;

swap(a,b);

cout<< “ a=” <

答:a=15 b=15

4.

#include

int x=1;

void fun(int *p)

{ static int x=2;

*p *=x-1;

x += 2; }

void main( )

{ int i;

for(i=1;i<4;i++)

{ fun(&x);

cout << x << endl; }

} 答:1

3

5. 15 #include void f(char *p,int n)

{ int i;

for(i=0;i

}

void main( )

{ char s[]={ “ 0123456789ABCDE”F };

f(s+8,5);

} 答:89ABC 6. #include

void main( )

{ int x(5);

do{ switch(x%2)

{ case 1: x--;break;

case 2: x++;break;

}

x--;

cout << x << endl;

}while(x>0); 答:3

} 1 -1 7.

#include

void f(int *p,int n)

{ for(int i=0;i

void main( )

{ int a[3][3],i,j;

f(a[0],9);

for(i=0;i<3;i++)

{ for(j=0;j<3;j++)cout<

cout << endl; } 答:1 2 3

} 4 5 6 7 8 9 8.

#include

void main( )

{ int a[3][4]={ {1,2,3,4},{5,6,7,8},

{9,10,11,12}};

int *p1[3]={a[0],a[1],a[2]};

int (*p2)[4]=a;

int *p=&a[0][0];

cout << *(p+1) << endl;

cout << *(p1[1]+2) << endl; cout << *(*(p2+2)+3) << endl;

} 答:2

7

12

9.

#include class TPoint

{ public:

TPoint( ){x=0;y=0;}

TPoint(int i,int j){x=i;y=j;} void Display( );

private:

int x,y;

};

void TPoint::Display( )

{ cout << x << “ , ” << y << endl; }

void main()

{

TPoint p1(4,5),p2,p3(-1,8);

p1.Display( );

p2.Display( );

p3.Display( );

}答:4, 5

0 ,0

-1 ,8

10.

else if(wage < 5000) p=0.1; else p=0.15;

tax = p * wage; wAfterTax = wage - tax; }

void main( )

{

int i;

Staff st[3],*p;

st[0].SetData("Zhang",1800);

st[1].SetData("Wang",4000);

st[2].SetData("Zhao",6000);

p=st;

for(i=0;i<3;i++)

(p+i)->TaxCalc();

for(i=0;i<3;i++)

(p+i)->Show();

}

答:Zhang

1800 90 1710

Wang

4000 400 3600 Zhao

6000 900 5100

#include

#include

class Staff

{

private:

char name[32];

float wage,tax,wAfterTax;

public:

void SetData(char *s,float w)

{ strcpy(name,s); wage = w; }

void Show()

{ cout << name << endl; cout <<

wage << " "; cout << tax << " ";

cout << wAfterTax << endl;} void

TaxCalc();

};

void Staff::TaxCalc()

{ float p;

if(wage < 1000.0) p=0.0; else

if(wage < 2000) p=0.05;

三、编程(20分)

1)某班有30个学生,5门功课。试根据sort函

数的原型声明,写出按学生课程总分由高到低进行排序的sort函数程序。

#in elude

#i nclude

struct stude nt

{ int num;

char n ame[32];

float score[5];

float total; // 总分

};

void sort(stude nt *p,i nt n);

//sort函数的原型声明,其中:n为学生数

void mai n()

{ stude nt stu[30];

int i,j;

float sum;

for(i=0;i<30;i++)

{ cin >> stu[i].num;

gets(stu[i]. name);

sum = 0.0;

for(j=0;j<5;j++)

{ cin >> stu[i].score[j];

sum += stu[i].score[j]; }

stu[i].total = sum;

}

sort(stu,30);

for(i=0;i<30;i++)

{ cout << stu[i]. num << en dl;

cout << stu[i]. name << en dl;

for(j=0;j<5;j++)

cout<

cout << stu[i].total << en dl;

}

} ...............................................

// sort 函数程序写在下面空白处

void sort(stude nt *p,i nt n)

{

int i,j,k;

stude nt temp;

for(i=0;i

{ k=i;

for(j=i+1;j

if((p+k)->total<(p+j)->total) k=j;

if(i!=k)

{ temp=*(p+i);

*(p+i)=*(p+k);

*(p+k)=temp; }

2)根据MyStr类中成员函数Replace函数的原型声

明,写出将MyStr类对象s中所有出现字符c1的地方均替换为字符c2的Replace函数。

#in clude

#in clude

class MyStr { private:

char str[80];

public:

MyStr(char s[]){strcpy(str,s);}

void Prin t(){cout << str << en dl;}

void Replace(char c1,char c2);

};

void mai n()

{ ” MyStr s( “ this is a c++ program. ” ) s.Replace( t' s.Replace( ‘ c' s.Pri nt();

}

// MyStr::Replace void MyStr::Replace(char c1,char c2) {

int i;

for(i=0;*(str+i)!= ' \0 ' ;i++)

{

if(*(str+i)==c1) *(str+i)=c2;

}

}

'T');

'C');

函数程序写在下面空白处

相关主题
文本预览
相关文档 最新文档