优题选
题目:电大《C++语言程序设计》形成性考核册作业及(3)

一、填空题

1.假定p所指对象的值为28,p+1所指对象的值为62,则* p + +的值为 28 。

2.假定p所指对象的值为28,p+1所指对象的值为62,则* + + p的值为 62 。

3.假定p所指对象的值为25,p+1所指对象的值为50,则执行“(*p)+ +;”语句后,p所指对象的值为 26 。

4.假定p所指对象的值为25,p+1所指对象的值为50,则执行“*(p+ +);”语句后,p所指对象的值为 50 。

5.假定a是一个指针数组,则a+i所指对象的地址比a地址大 未知 字节。

6.假定a是一个一维数组,则a[i]的指针访问方式为 *(a+i) 。

7.假定a是一个二维数组,则a[i] [j]的指针访问方式为 *(*(a+i)+j) 。[h1]

8.假定a是一个一维数组,则a[i]对应的存储地址(以字节为单位)为 (char *)a+i*sizeof(a[0]) 。

9.假定一个二维数组为a[M] [N],则a[i] [j]对应的存储地址(以字节为单位)为 (char *)a+(i*N+j)*sizeof(a[0][0]) 。

10.假定一个二维数组a[M] [N],则a[i]的地址值(以字节为单位)为 (char *)a+i*N*sizeof(a[0][0]) 。

11.假定p是一个指向float型数据的指针,则p+1所指数据的地址比p所指数据的地址大 4 字节。

12.假定a为一个字符数组名,则元素a[8]的字节地址为 8 。

13.假定a为一个整型数组名,则元素a[4]的字节地址为 16 。

14.假定一个结构类型的定义为“struct A{int a,b;short c;A*d;};”,则该类型的大小为 14 字节。

15.假定一个结构类型的定义为“struct B{int a[8];char* b;};”,则该类型的大小为 36 字节。

16.假定一个结构类型的定义为“struct D{int a;union{int b;double c;};D*d[3];};”,则该类型的大小为 24 字节。

17.假定要动态分配一个类型为Worker的具有n个元素的数组,并由r指向这个动态数组,则使用的语句为 r=new Worker[n]; 。

18.假定要访问一个结构x中的由a指针成员所指向的对象,则表示方法为 *(x.a) 。

19.假定要访问一个结构指针p所指对象中的b指针成员所指的对象,则表示方法为 *(p->b) 。

二、给出下列程序运行后的输出结果

以下结果中空格以’ˉ’表示

1.#include<iomanip.h>

void main(){

int a[8]={7,9,11,13,3,8,15,17};

int *p = a;

for(int i =0;i<8;i + +){

cout<<setw(5)<< * p + +;

if((i +1)%4 = =0)cout<<endl;

}

}

ˉˉˉˉ7ˉˉˉˉ9ˉˉˉ11ˉˉˉ13

ˉˉˉˉ3ˉˉˉˉ8ˉˉˉ15ˉˉˉ17

2.#include<iomanip.h>

void main(){

int a[5]={3,6,15,7,20};

int *p = a;

for(int i = 0;i<5;i + +)

cout<<setw(5)<< * p + +;

cout<<endl;

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

cout<<setw(5)<< * - -p;

cout<<endl;

}

ˉˉˉˉ3ˉˉˉˉ6ˉˉˉ15ˉˉˉˉ7ˉˉˉ20

ˉˉˉ20ˉˉˉˉ7ˉˉˉ15ˉˉˉˉ6ˉˉˉˉ3

3.#include<iomanip.h>

void main(){

int a[8] ={4,8,12,16,20,24,28,32};

int *p = a;

do{

cout<< *p << ’ ’;

p + =3;

}while(p<a+8);

cout<<endl;

}

4 16 28

4.#include<iomanip.h>

void main(){

int x =20,y =40, * p;

p =&x;cout<< * p<< ’ ’;

* p= x +10;

p =&y;cout<< * p<<endl;

* p = y +20;cout<< x << ’ ’ << y <<endl;

}

20 40

30 60

5.#include<iomanip.h>

int LA(int * a,int n){

int s = 0;

for(int i =0;i<n;i + +)

s + = a[i];

return s;

}

void main(){

int a[ ]={5,10,15,20,25,30};

int b =LA(a,5);

int c =LA(a+3,2);

cout<< b << ’ ’ << c << ’ ’ << b +2 * c<<endl;

}

75 45 165

6.#include<iomanip.h>

void LC(int a,int b){

int x = a;

a = b;b = x;

cout<< a << ’ ’ << b <<endl;

}

void main(){

int x =15,y =36;

LC(x,y);cout<< x << ’ ’ << y <<endl;

}

36 15

15 36

7.#include<iomanip.h>

void LF(int & x, int y){

x = x + y;

y = x + y;

cout<<”x =”<< x <<”,y =”<< y <<endl;

}

void main(){

int x =5,y =8;

cout<<”x =”<< x <<”,y =”<< y <<endl;

LF(x,y);

cout<<”x =”<< x <<”,y =”<< y <<endl;

}

x=5,y=8

x=13,y=21

x=13,y=8

8.#include<iomanip.h>

void LG(int * & a, int & m){

a = new int[m];

int * p = a;

for(int i = 0;i<m;i + +)

* p + + =2 * i +1;

}

void main(){

int * p, n =5;

LG(p,n);

for(int i = 0;i<n;i + +)

cout<< p[i]<< ’ ’;

cout<<endl;

delete[ ]p;

}

1 3 5 7 9

9.#include<iomanip.h>

void LH(int * a, int n){

int * p = a + n-1;

whlie(a<p){

int x = * a;

* a = * p;

* p = x;

a + +;p- -;

}

}

void main(){

int * d = new int[5];

int i;

for(i = 0;i<5;i + +){

d[i]=2 * i +3;

cout<<setw(5)<<d[i]<< ’ ’;

}

cout<<endl;

LH(d,5);

for(i = 0;i<5;i + +){

cout<<setw(5)<<d[i]<< ’ ’;

}

cout<<endl;

delete[ ]d;

}

ˉˉˉˉ3ˉˉˉˉ5ˉˉˉˉ7ˉˉˉˉ9ˉˉˉ11

ˉˉˉ11ˉˉˉˉ9ˉˉˉˉ7ˉˉˉˉ5ˉˉˉˉ3

10.#include<iostream.h>

struct Worker{

char name[15];/ /姓名

int age;/ /年龄

float pay;/ /工资

};

void main(){

Worker x ={”weirong”,55,640};

Worker y, * p;

y = x;p =&x;

cout<< y. name<< ’ ’ <<y. age<< ’ ’ <<y. pay<<endl;

cout<< p->name<< ’ ’ << p->age+5<< ’ ’ << p->pay-10<<endl;

}

weirong 55 640

weirong 60 630

11.#include<iostream.h>

#include<string.h>

struct Worker{

char name[15];/ /姓名

int age;/ /年龄

float pay;/ /工资

};

void main(){

Worker x;

char * t =”liouting”;

int d =46;float f =725;

strcpy(x. name, t);

x. age = d;x. pay = f;

cout<< x. name<< ’ ’ <<x. age<< ’ ’ <<x. pay<<endl;

}

liouting 46 725

三、写出下列每个函数的功能

1.#include<iostream.h>

void LI(int n){

int * a = new int[n], * p = a + n;

for(int i =0;i<n;i + +)cin>> a[i];

for(i = n-1;i> =0;i- -)cout<< *(- -p)<< ’ ’;

cout<< ’/ n’;

delete [ ]a;

}

输入n个数并以相反的顺序显示出来。

2.#include<iostream.h>

void LK(int a[ ], int n, int * & b, int& m){

float s =0;int i;

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

s + = a[i];

s/= n;

m = 0;

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

if(a[i]> = s)m + +;

b = new int[m];

int * p = b;

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

if(a[i]> = s)* p + + = a[i];

}

将数组a中大于平均数的元素存放到动态申请的数组b中,数组b的大小由m返回。

3./ /struct Worker{

/ / char name[15];/ /姓名

/ / int age;/ /年龄

/ / float pay;/ /工资

/ /};

istream & operator>>(istream& istr,Worker& x){

cout<<”请输入一个职工记录:姓名、年龄、工资”<<endl;

istr>> x. name>> x.. age>> x.. pay;

return istr;

}

重载istream的>>操作符以输入Worker结构对象。

4./ / struct StrNode{

/ / char name[15];/ /字符串域

/ / StrNode * next;/ /指针域

/ /};

void QB(StrNode * & f, int n){

if(n = = 0){f =NULL;return;}

f =new StrNode;

cin>>f->name;

StrNode * p = f;

whlie(- -n){

p = p->next= new StrNode;

cin>>p->name;

}

p->next=NULL;

}

创建有n个结点的StrNode类型的链表,并从键盘输入每个结点的name值。

5./ / struct StrNode{char name[15];StrNode * next;};

void QC(StrNode * f){

whlie(f){

cout<< f->name<< ’ ’;

f = f->next;

}

}

遍历链表并输出所有结点的name数据成员


[h1]可能不正确

关键字:
返回顶部