资源描述
诚信应考,考试作弊将带来严重后果!
期末考试
《高级语言程序设计I》试卷(A )□|p
倒考前须知:1.考前请将密封线内填写清楚;
题号
■
四
五
总分
得分
评卷人
2.所有答案写在答题纸上;
3.试卷和答题纸同时提交;4.考试形式:闭卷;
5.本试卷共五大题,总分值100分,考试时间120分钟。
一、单项选择题。(每题2分,共20分)1.以下选项中,合法的常量表示是()。
(A) program1 (B) 183AF(C) -618e3(D) 1.0e-5.32.执行以下语句后,i, j,k的值为()o int i=l, j=l,k=l; (i++, —j) && ++k;
(A) 2,0, 1(B) 2, 0,2(C) 1, 1, 1(D) 1,0,23.以下有关C++的说法中,正确的陈述是( )o
(A) const只能约束普通内存变量的的写操作,不能约束指针变量的写操作。 (B)静态变量和全局变量的作用域都是文件作用域。
(C) 一维数组定义中数组的长度表达式可以使用赋初值的变量。
(D) inline函数没有普通函数调用的时空开销。
4.有如下代码段,不正确的函数调用形式()。
• • •typedef int (*pType)(int, int); int max (int a, int b){ return a>b?a, b; }
*匚■E
□|p
料
pType pf = max;
(A) pf(l,2);(B) (*pf)(l,2);(C) max (1,2);(D) (&pf) (1,2);.以下程序段中,循环次数是()
for(int i=10;i>0&&i%2;){ i=i-2; }
(A) 0(B) 4(C) 5(D) 6.int iArray[2][2] = {0,l,2,3},以下表达式的值为2的是()。
(A) iArray[2] [1](B) iArray [1] [1]
(C) *(*(iArray+l))(D) *(* (iArray)+1). 假设 char *a口={“fortran",“basic","c++”,“java”};
那么语句cout〈〈a+3;的输出结果是()□
(A) c++java (B)一个地址值(C)c++(D) java.设有两个字符指针char *sl和char*s2分别指向两个字符串,判断字符串si和s2是 否相等的表达式是()。
(A) sl=s2 (B) sl=s2 (C) strcmp(sl, s2)=0(D) strcpy (si, s2)=0.以下运算符,优先级最高的是( )o
(A)-(B) =(C)->(D) □
using namespace std;
void main()
{ double x[5], t; int i, j, k;for(i=0; i<5; i++) cin»x[i];
for(i=0; i<4; i++){ k=i;
for( j=i+l 【1】 ;j〈5;j++)if(x[j]<x[k]) k=j;
=x[kl ;x[k]=t;【2】 〃将选择好的下标元素和下标为i的元素交换
)for(i=0; i<5; i++) cout«x[i]«n n;
}2、以下函数的功能是:用递归法将一个整数m转换成字符串。例如:输入整数 1472,应输出字符串“1472”。m的位数不确定,可以是任意位数的整数。请填空。
void print( int m )
{if(m< 10)putchar(m+48) 【3】 〃递归出口else
{print(m/10)【41/递归体putchar(m% 10+48);
3、以下程序的功能是:执行该程序后运行结果如下:
5.1 3.2/请填空。
#include<iostream>
using namespace std;
void dl( int&a、intb【51)
{a=a+3;b=b+3;
)
void main()
(double x=2.1, y=3.2;
dl(x, y);cout«x«n n«y«endl;
4、以下程序的功能是:定义一个结构体类型,内含学生学号和一门课的成绩,并开辟动态内存存放一个学生的数据:学号3001,成绩95。执行该程序后运行 结果如下:
3001,95/请填空。
#include<iostream>
using namespace std;
struct S1
{ int m; 〃学号double x; 〃成绩
);
void main()
(SI *p;
p=new S1;cin〉〉p->m:>:>p->x;【6 ] 〃赋值
coutvvp->mvv- Jvvp-〉xv〈endl; 17 1 〃输出 delete p;
)5、以下函数的功能是:判断一个数是否为素数。请填空。
bool prime(int m)
(int i;
bool prime 1;prime l=true;
for(i=2; i<=sqrt(m); i++)if( m%i==018 1) prime l=false;
return prime 1;
}WIN7用户需要将PCSX2VU.exe设置成管理员权限启动才可以运行6、以下程序的功能是:输出二维数组各元素值。请填空。
#include<iostream>
using namespace std;
void main()
{ void output(int (*)⑵);intb[3][2]={6,5,4,3,2,l};
output( b[ 9 ]);
)
void output(int (*p)[2])
(inti, j;
for(i=0;i<3;i++)for(j=0;j<2;j++)
coutvv *(*(p+i)+j)【10】«M H; cout«endl;)
)三、阅读程序,写出运行结果。(每题3分,共24分)
答案栏:
1、3、
5、7、
2、4、
6、8、
1、
#include <iostream>
using namespace std;
void main()
(int a—1, b;
if(a<2)if(a<-2)b=a+l;
else if(a>0)b=a+2;else b=a+4;
else b=a+5;a=-l,b=3
a=-l,b=3
cout«na=n«a«n,b=H«b;
)
#include<iostream>
using namespace std;
void main()
(int n=2,m=6,a,b;
a=++m+n-;9b=m+++--n;7
cout«a«l '«b«, ,«m«, («n«endl;
)
97 8 0 3、
#include <iostream>
using namespace std;
void q(float f)
(cout«nfloatn«f;
)
void q(double d)
(cout«ndoubleH«d;
)
void q(int i)
(cout«nintn«i;
}
void main()
(q(3);
q('B');q(3.1);
} int3int66double3.14、
#include <iostream>
using namespace std;
void main()
{ int m=3, n=2;while(m<5)
s witch(m++” m=4(
case 4: n—;n-=4;break;
case 3: n++;)
cout«n;
)
-2#include <iostream>
using namespace std;
int x, y;
void fun(int a)
{ inty, b;b=a+3; x=x+a; y=x-b;
cout«x«H n«y«H n«a«n H«b«endl;
int main()
{ int a=l, b=3;x=5, y=4;
fun(b);
cout«x«n H«y«H n«a«H n«b«endl; return 0;
}8 2 3 6
84 13#include <iostream> using namespace std; int f(int a)
{ int b=l;static int c=2;
b++; c++;return (a+b+c);
)
int main(){ for (int i=0; i<3; i++) cout«f(i)«H H ; return 0;
)5 7 9
5. #include <iostream> using namespace std; int main()
{ char ch[『PROGRAM” ;
cout«ch«endl«ch[3]«endl«ch+3«endl; return 0;
)
PROGRAM
G
GRAM#include <iostream> using namespace std; int main()
{ int a=78, b=21, *p, *q, *r;p=&a; q=&b;
if (*p!=*q) { r=p; p=q; q=r; } cout«*p«n H«*q«endl;*p=*p+*q;
cout«a«n n«b«endl;return 0;
)2178
78 99四、简答题(每题4分,共12分)
1 .试分析字符串常量Habc\\e\tfg\101\102H由哪几个字符构成?该字符串占用多少个字 节?假设用cout«Habc\\e\tfg\101\102H ;会看到的输出结果是什么? abc\e\101 \102abc\e fgAB
2 .假设有如下定义:
struct stype
{ float y;short int m;
char code[3];
);
stype sdat;
试画出变量sdat的内存构造图(示意图),并指出该变量占用内存的字节数。
ym
code[3].下面程序有错误:
#include <iostream>
using namespace std;
void fun(int b[]){ for (int i=0; i<7; i++) cout«*(b++)«n ”;//(I)
cout«endl;
}
int main()
{ int b[7]={ 10,20,30,40,50,60,70);for (int i=0; i<7; i++)
cout«*(b++)«n ”;// (2)cout«endl;
fun(b);return 0;
)源程序中(1)和(2)之处写法一样,但编译时(2)处出错而(1)处正确,试指出原因, 并将(2)处改正。
b在(1)处是变量而在(2)是常变量不能用于b++ int *a;a=b;
coutv<*(a++)<< “ ";五、程序设计题(每题8分,共24分)
1 .设计程序输出数列{A"的前36个数,输出时要求每行输出6个数。数列有 以下规律:
A[=1.0, A2= 2.0, An+i
= 4+1
4(4-1+D
(〃>2)
#include<iostream> using namespace std; double a[36];
int main() {double A(int); int i;int j;a[ll]=A(12);
for(i=0;i<=35;i++)
{for(j=0;j<=5;j++) cout«a[i+j]«,\tJ ;
i=i+6; cout<<endl;}
return 0;} double A(int i) {double m;
if (i==l) m=l;else if(i=2)
m=2;else
{m=(A(i-l)+l)/(A(i-l)*(A(i-2)+l));}
return m;}2.逆置是指将数组中的值按逆序重新存放。例如:数组原值是{4,7,3』,5,6},逆 置后数组变成{6,5』,3,7,4}。试设计一个将数组逆置的函数reverse,其形式参 数有2个:数组(该数组存放一组待逆置的数据)、数据个数。在主函数中输 入一组数据存放在数组中,然后调用函数reverse将数组逆置,最后在主函数 中显示出逆置后的数组。
#include<iostream>using namespace std;
int main(){void reverse(double *a, int n);
int n, i;double *p;
cout<<〃请?输°?入-?个?数°y〃《endl;cin>>n;
cout<<〃请?输°?入-?各1A个?数。y值| lzz«endl;p=new double[n];
for (i=0;i<n;i++)cin>>*(p+i);
reverse (p, n);for (i=0;i<n;i++)
cout<<* (p+i) <<,\V ;delete []p;
return 0;}void reverse (double *a, int n) {int i;
float t;for(i=0;i<n/2;i++) {t=*(a+n-l-i);
*(a+n-l-i)=* (a+i);*(a+i)=t;}}
3.需要求2个不同的圆柱体的体积,设计一个面向对象的程序。数据成员包括: radius(半径),height(高);还可根据需要定义其它数据成员。要求分别用成 员函数实现以下功能:
(1)由键盘输入圆柱体的半径、高;(2)计算圆柱体的体积;
(3)输出圆柱体的体积。
请编写程序。
#include<iostream>using namespace std;
const float pi=3. 1415;struct cylinder
{float radius;
float height;
float volume;
void volumel () ;};
void cylinder::volumel()
{volume=pi*radius*radius*height;}int main ()
{cylinder cl,c2;cout* 〃输°?入•.?半元?径?, 6?高?度・・・・ 〃《endl;
cin>>cl. radius»cl. height;cin>>c2. radius»c2. height;
cl. volumel ();c2. volumel ();
cout<<cl. volume<<endl;cout<<c2. volume;
return 0;}
10. 30*('F'-'C')/5-2*3 表达式的值是( )o
(A) 6(B) 12(C)48(D)18sizeof(char)+6*(6>5)/(2>3?2:3)表达式的值是()。
(A) 3(B) 4(C) 1(D)2二、简答题。(共20分)
1 .写出两个表达变量X和y的值都不等于零的逻辑表达式。x!=0&&y!=0 x&&y.有以下语句,循环体执行次数是多少?结束后x的值是什么?10, T
int x=10; while(x-) cout<<x<<endl;.有说明语句: int a; double x; int *p=new int[100];
分析以下表达式值的类型。
a+x a=a+x p+1 double int int*.设有函数:
void funl (int a){ a++; };
void fun2(int & a){ a++; };
有以下调用: int b=5;
funl (b) ;//b的值是什么?
fun2 (b) ;〃1)的值又是什么?
两次调用函数后变量b的值有变化吗?为什么?传值参数,引用参数.设有函数:
int function(int a)
{ static int k=0; return a+k++; }并有调用: int t=l;
t=function(t)+function(t)+function(t);有人说t的结果值等于3,对吗?为什么?6, k是静态变量
2 .设有语句:
int *ap=new int [10];请写出两个动态数组最后一个元素的表示形式。
3 .请解释以下说明语句中标识符www的含义。 double * www(double);ww是函数名,有一个double值参,返回值类型为double*
4 .设有语句:
char *s二〃South China University of Technology”;请写输出子串 ^University of Technologyn 的语句。cout<<s+12«endl;
5 .有说明语句:
int ary[100]; int max;赋值语句调用函数MaxAry求数组的最大元素值: max=MaxAry(ary, 10);
请写出 MaxAry 的函数原型。int MaxAry (const int *, int);或 int MaxAry(const int [], int);
6 .有语句:
struct link{ int code; link *next; }; link *head;
//……
push (head, 256);head为单链表的头结点,函数调用语句push在表头插入一个数据,请写出对应的函数 原型。 void push (link *&, int);
三、阅读程序写输出结果(共20分)1.〃循环
ttinclude <iostream. h>
void main()
{ int i=0, s=0;
while (i++<=10)
{ if(i%2) continue;s=s+i;
cout«s<<*\t';
)
)
2612 20 302 .〃数组,指针
#include<iostream. h>void main()
{ int num[5];
int *p=num, i;
for(i=l;i<=5;i++) num[i-l]=i;
for(i=0; i<5; i++)
cou t «num [ i ] + (*p++) «,\t,;
cout<<endl;)
2. 46810〃递归
#include<iostream. h>void print(char ch)
{ int i=0;
if(ch=='D,)
return;
else
{ print(ch+l);while (i++<-ch-W)
cout<<ch;cout«endl;
))
void main(){ print ('A');
cccBB
A〃函数指针参数 #include <iostream. h> void fun(int *x, int *y) { cout«/,*x=,/<<*x«'\t,;
cout<<,,*y=,/<<*y«'\t,;
*x = 3 ;
*y 二 4 ;)
void main(){ int x = 1, y = 2 ;
fun(&y, &x);
cout<<,/x=//<<x<<,\t,;
cout<<,,y=/z<<y<<,\t,«endl;)
*x=2 *y=l x=4 y=3四、程序填空题(每空2分,共20分)
1 .假设90分以上为A等,80分到89分为B等,70分到79分为C等,60分到69分为D等, 60分以下为E等。下面是输入一个分数,输出相应的五级制成绩的程序。
#include<iostream. h>void main()
{double score;
cout<<,zscore="z;
if( score > 100 else switch(
void main()
{double score;
cout<<,zscore="z;
if( score > 100 else switch(
cin»score;
score < 0 ) cout <<〃Input Error!,z;
// (1) (int) score/10
{case
9:
case
10:
cout«z,A\nzz;
break;
case
8:
cout〈〈〃B\n〃;
break;
case
7:
cout〈〈〃C\n〃;
break;
case
6:
cout<<〃D\n〃;
break;
default:
(2)
•
)
(1)
// (2) cout<</,E\n,/;
2.下面是显示如以下图案的程序。
2222222
33333
444
#include<iostream. h>
void main()
{ int i , j, k ;
for( i = 1; i <= 5; i++ )
{ for( k = 1;(3)
cout<<ends ;
for( j =1 ;(4) cout << (5J ;
cout<<endl;
)
)
k++ )// k<=i-l
j++ )//( 2*(5-i)+l)
// i
3 .下面的程序的输出结果是:
432 1 10 9 8 7 6 5
ttinclude <iostream. h>
void fun( ^6) , int n , int m ) ( int i , j , t ;
1 = n ; j = m ;
while ( i<j )
{ t=s[i]; s[i]=s[j]; s[j]=t; i++; j—; )
)
//int * s 或 int s[]
void main()
{ int a[10] = {1,2, 3, 4, 5, 6, 7,8, 9,10 };
fun( a , 0 , 3 );
fun( (7) );
for ( int i = 0; i<10; i++ ) cout « a[i] << ends;
// a, 4, 9
cout << endl;
4 .下面的程序运行时屏幕显示Please input 运行结果如图1所示:
i(rio):键盘输入3后,屏幕显示程序
K "G:\L\Debug\08-09( 1)08级计埴空蔻.exe
lease input i < 1~10 > : 3DEFGHIJ
Press any key to continue图1程序运行结果
ttinclude <iostream. h>int i ;
// char * s
// (s + i - 1)
〃 *( s + i )
void prints( ^8) , int i ){ cout<< ^9) << endl;
cout<< (10) << endl;);
void main (){ int i ;
cout << "Please input i ( P10 ): 〃;
cin >> i;
prints ( 〃ABCDEFGHIJ〃, i );五、编程题(20分)
1、设计程序,输入一个正整数i (〈256),求另一个正整数j,使i和j在用8位二进制表示 时互为逆序。例如,输入i=3 (00000011),应求得j=192 (11000000)o
#include<iostream.h>
void main()
{ int ij=O,k;cout«Hi=";
cin»i;fbr(k=0;k<8;k++)
{j=j*2+i%2;i/=2;
)cout«"j="«j«endl;
2、以下程序由随机数生成一个整数序列,放在数组a中,然后按奇数在前,偶数在后的顺 序重新排放。程序运行效果如图2所示。请编写函数RandAry和函数PutAry。
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void RandAry(int *a, int n, int m);
void PutAry(int *a, int n);
void main()
{ int *a, i, n;cout«nn=H; cin»n;
a=new int[n];RandAry(a, n, 100); 〃用小于100的随机数对数组赋值
for(i=0; i<n; i++) cout«a[i]«n〃输出原始序列cout« endl;
PutAry(a,n);〃整理数组,奇数放在前,偶数在后〃输出整理后序列
〃输出整理后序列
for(i=0; i<n; i++) cout«a[i]«" cout« endl;
| c \ "C:\Documents and Settings\Administrator\^S\Debug\Cpp3.exe"
—
-1□! x|
n=15 5484
51
813247
8073995116
98
93
92
24刍
5181
47
739951
9384325416
98
80
92
24
Press any
key
to continue
储
_|
图2整理数据
void RandAry(int *a, int n, int m){ srand( time( 0 )); 〃调用种子函数
for( int i = 0; i<n; i++ )
a[i] = rand() % m;〃用随机函数初始化数组)
void PutAry(int *a, int n){ int i,t,k=O;
for( i = 0; i<n; i++ )
if(a[i]%2)
{ t=a[i];a[i]=a[k];
a[k++]=t;3、设有说明语句:
struct list{ int data; list * next; };
list *head;head是有序单向链表的头指针。请编写函数:
void Count(list * head);计算并输出链表中数据相同值的结点及个数。例如,数据序列为:
233345566667899那么输出为:
datanumber
33
5 2
6 4
9 2
void Count(list * head)
{ list *p,*q;int n=l,t=O;//t表示是否发现了相同的值,n为相临结点具有相同值的结点的个数。
q=head; p=head->next;cout«"data\tn«nnumber\nu;
while(p)
{ if(q->data==p->data)
{n++; t=l;} else t=0;{cout«q->data«,\t'«n«endl;
n=l;)
q=p; p=p->next;
)if(n>l)〃考虑最后一组相邻节点的情况 cout«q->data«,\t'«n«endl;
《C++程序设计》试卷单项选择题:(每题2分,共20分)
1 .以下表达中错误的选项是()oA)用户所定义的C++标识符允许使用关键字
B)用户所定义的C++标识符应尽量做到“见名知意”C)用户所定义的C++标识符必须以字符或下划线开头
D)用户所定义的C++标识符中,大、小写字符代表不同标识。
2 .用C++语言编制的源程序要变为目标程序必须经过()oA)解释B)汇编C)编辑D)编译
那么表达式x+a%3*(int) (x+y)%2/4的
3 .设有定义语句:int a=7; float x=2. 5, y=4. 7;
值是()oA) 2. 5B) 2. 75C) 3.5D) 0.0
4 .以下所列的C++语言常量中,错误的选项是()oD) '\72'
D) '\72'
A) OxFFB) 1.2e0.5 C) 2L5.设有:int a=7, b=5, c=3, d=l;
A) 7B) 5
5.设有:int a=7, b=5, c=3, d=l;
A) 7B) 5
那么条件表达式a〈b?a:c>d?c:d的值为(
C) 3D) 1
)o
6 .以下程序的运行结果是()o
int main () { short b=一1; unsigned short a; a=b;cout<<a;
return 0;A) -32768B) 32767C) 65535D) -1
7 .设有定义语句:int a[ ] = {0, 2, 4, 6, 8, 10), *p=a;,值不等于0的表达式是()。
A) *p++B) *(p++) C ) (*p)++D ) *++p
8 .以下对C++字符数组的描述错误的选项是( )oA)字符数组可以存放字符串;
B)字符数组的字符串可以整体输入和输出;C)可以在赋值语句中通过赋值运算符“二”对字符数组整体赋值;
D)不可以用关系运算符对字符数组中的字符串进行比拟;
9 .以下带有默认参数的函数原型声明中,正确的选项是( )ovoid f(int a=5 , int b=5, int c);
A) void f (int a, int b, int c=5);void f (int a , int b=5 , int c );
B) float fun (int a=5 , int b, int c=5);;.设有以下定义语句:struct node {int x; int y;} n[3] = {{1, 2}, {3, 4}, {5, 6}};值 等于4的正确表达式是( )o
A) n[l] [2] B) node [1] [2] C) n[l].y D) n[l]->y
答案栏:
1、A
2、_D 3、_A_4> _B_5、_C 6、_c_7、_D_8、_C_9. _B_ 10、二、程序填空题(每空2分,共20分)
• • • ,,, • • ,9
答案栏:
1、 2、3、 4、
5、6、7、 8、
9、 101、以下程序的功能是:用选择法对5个实数排序(按从小到大顺序)。请填空。
#include <iostream>
展开阅读全文