资源描述
诚信应考,考试作弊将带来严重后果!
期末考试
《高级程序设计语言C++》试卷
考前须知:1.考前请将试卷和答题纸密封线内各项填写清楚;.所有答案写在答题纸上,否那么无效;
2 .试卷和答题纸同时提交;.考试形式:闭卷;
3 .本试卷共五大题,总分值100分,考试时间120分钟。
题号
—*
=
四
五
总分
得分
评卷人
一、单项选择题(每题2分,共26分)
1 .由C++目标文件连接而成的可执行文件的缺省扩展名为(C )。
(A) cpp(B) obj(C) exc(D) dll.以下选项中全部都是C++关键字的选项为(B )。
-E
(A)
cpp
int
break
(B)
int
char
new
(C)
break
CASE
sizeof
(D)
switch
float
integer
2 .以下四个选项中,不能交换变量a和b的值是(B )。
(A) a=a-b, b=a+b, a=b-a;t=a, a=b, t=b, b=a;
(B) l=a, a=b, b=t;t=b, a=b, b=a;
4 .字符串"Hello,\nHi\041”包含的有效字符个数为(D )。
(A) 14(B) 13(C) 12(D) 10.以下代码执行的结果是(B )。
n]r>
料
int x = 3510; double y = x/100* 100;cout «y;
(A) 3510(B) 350()(C) 3000(D) 1.ini i=3;下面语句执行的循环次数是(B )。
do{ i—; cout«i«endl; } while( i != 1 );
(D)无限次
(A) 1(B) 2(C) 3.假设int a=l, b=2, &ra=a. &rb=b;以下正确的语句是(B )。
2 1
3 1
4 2
5 、
^include <iostream. h>
void func(int, int, int *);
void mainO
{ int x, y, z ;
func(l, 2, &x);
func(3, x, &y);
func (x, y, &z);
cout«x«endl«y«endl«z«endl ;
)
void func(int a, int b, int *c)
{ b-=a ; *c=b-a; }
0
-6
-6
6 、
it include <iostream. h>
int fl(int a, int b) {return a%b*5:}
int f2(int a, int b) {return a*b;}
int f3(int(*t) (int, int), int a, int b) { return (*t) (a, b) :} void mainO
{ int (*p) (int, int);
p=fl ; cout<<f3(p, 5, 6)«endl ;
p=f2 ; cout«f3(p, 7, 8)«cndl ;
}
25
56
7 、
#include〈iostream. h>
#include<iomanip. h>
void fNum (int w)
{ int i;if(w>0)
{ for (i=l: i<=w; i++) cout«setw(3) «w;cout«endl;
fNum(w-l);)
)void mainO { fNum(4);}
4 4 4 4
3 3 3
2 2
1四、程序填空题(每空2分,共20分)
1、下面程序的功能是:输入三角形的三条边存放在变量a, b和c中,判别它们能否构成三角 形,假设能,那么判断是等边、等腰、还是其它三角形,在横线上填上适当内容。
^include〈iostream. h> void main()
{ float a, b, c ;
coutX<"a, b, c="; cin»a»b»c; if ( a+b>c && b+c>a && c+a>b ) ( if (11]) a==b && b==ccom。”等边三角形! \n”;
else if (12J) a==b| |a=c| |b=c cout<<”等腰三角形! \n”;else cout<<”其它三角形! \n”;
)
else cout«”不能构成三角形! \n”;
)
2、以下程序功能是输出1000以内个位数为6且能被3整除的所有数。请填空。 ^include <iostream. h> void main () { int i, j ;
for ( i=0 ; Lil ; i++ )i <100
{ j = i * 10 + 6 ;
if ( [4] ) continue ; j % 3 cout « j « * ”;
)
)
3、求n (n26)内的所有偶数表示为两个素数之和,图1为输入12的运行结果。补充完整以 下程序。
[提示:一个偶数 n (n26)可以表示为 l+(n-l), 2+(n-2), 3+(n-3),...] #include<iostream. h>
图I
#include<math. h>#include<iomanip. h> int isprime(int); void main() { int num;
coui«”请输入一个偶数 N (N>=6) :\n*; cin»num:
for(int n=6: n<=num: n+=2) for(int i=3;i<=n/2;i+=2)if(15J) isprime(i) && isprime(n-i) (cout«setw(3) <<n«*=*«setw (3) «i«A +*«setw (3) «(n-i)«endl;
break ; }
}
int isprime(int n){ int i, sqrtm=(int)sqrt(n); for(i=2; i<=sqrtm; i++)
if( 16】 ) return 0 ;n%i==0【7】 : return 1
}
4、以下程序是创立一个动态数组,数组长度由程序运行时输入数据决定。调用随机函数对动态数 组赋初值,并输出动态数组各元素值。请填空。
#include< iostream. h>
#include<stdlib. h>
#include<time. h> void mainO { int n, *p= [8j ;NULL
cout « ”Please input n:\n"; cin»n;p= [9] new int[n] if(p==NULL)
{ cout«*Allocation faiure \n"; return;} srand(time(0));for( int i=0; i<n; i++ ) { p[i]=rand()%100; }
for( 【10] ; a<p+n; a++ ) int *a=p { cout«*a«, \tf ; } cout«encll;
delete []p; )
五、编程题(20分)(6分)编写程序,打印正整数的平方和立方值。程序运行后显示相应的提示信息, 要求输入2个正整数,然后显示这个范围的数据的平方和立方值。例如,分别输入整 数2和9,执行效果如图2所示。
CT -E:\c+ + \unc++更 \07级春 \Debug\平方立方,
请卷入塞1个整数< >=0 >:2
请输入奈2个整数 < ”第1个整数
N率方立方
248
392?
41664
525125
636216
749343
864512
981729
Press any key to continue
▲
二J
1
图2显示数制对照表
#include<iostream. h>
#include<iomanip. h>
void mainO
{ int a, b;cout«"请输入第1个整数(>=0 )
cin»a;cout<<"请输入第2个整数( >=第1个整数):";
cin»b;cout<<setw(12)«"N"<<setw(12)<<"平方"<<setw(12)<<"立方"<<endl;
for(int i=a; i<=b; i++)cout«setw(12) «i«setw(12) «i*i«setw(12) «i*i*i«endl;
)1口| x
1口| x
c「E:\C++\CUC++蔻 \0 出卷'Debug 也成,
1、 (6分)以下程序用随机函数生成两位整数,取M个各不相等的数据,按生成顺序存 放在数组a中。图3是生成20个数据的显示效果。请依题意编写函数insert及填写 函数原型。
54 79 二 64 20
生成数组:
Press any key to continueii
int insert (int *ap, int k, int n);〃函数原型图 3 生成数组
或:
int insert (int ap 口,int k, int n);
void mainO
{ const int M=20;int n, i=0;
int a[M] = {0};srand(time(0));
while (i<M){ do{n=rand()%100;}whilc(n<10); 〃生成数据
if (insert (a, i, n))〃把不相同数据插入数组ai++;
(高级语言程序设计I》试卷(A) 第13页共15页
coul<<"生成数组:"<<endl;
for(i=l; i<=M;i++){ cout«a[i]«* *; if(i%10==0)cout<<endl; } cout«endl;
)
int insert(int *ap, int k, int n)
{ for(int j=0; j<k; j++) 〃漉去相同数if (ap[j]=n) break;
if (j==k) {ap[j]=n; return 1;) 〃添加数据return 0;
)
3、(8分)本程序功能是把一个用拼音输入的名字自动生成6位数字串的密码。生成规 那么是把字母串的最后6位逆序,取每个字母小写的ASCII码值,其除以10的余数为该位的 密码值。当输入名字的字母串缺乏6位,生成时以字母“z”补足。图4是程序的运行效果。 请填写change函数的函数原型并编写函数。
#include<iostream. h>
c< wE:\C++\<^JC++S\07Q(^\Deb.
#include<ctype. h>
请输入名字拼音,以标结束:
struct link {char s; link * next;};
ZhongFuwalt 生成密码为: 797230ft
Press any key to continue
void inputNamedink *& h);
void outLinkdink *h);
//change的函数原型
图4牛.成密码
void mainO
{ link *name=NULL, *code=NULL;
cout<<”请输入名字拼音,以#结束八n";
inputName(name): change(code, name);
cout<<"生成密码为:\n";
outl.ink (code);
}
void inputNamed ink *& h)〃逆序存放字符串
{ 1 ink *p:
p=new link; cin»(p->s);
while((p->s>=, a* &&p->s<=,z \ \ p->s>=* K &&p->s<=, Z' )&&p->s!=, #')
{ p->next=h; h=p;p=new link: cin»(p->s);
)
)
void change(link *&hCode, link *h)
{ char d;《高级语言程序设计I》试卷(A) 第14页共15页 link *p=NULL;
hCode=new link;hCode->next=NULL;
p=hCode;d=h->s;
for(int i=0;i<6;i++)
{ p->s=int(tolower(d))%10+, 0* ;p->next=new link;
p=p->next;p->s=,;
p->next=NULL;if(h->next)
{h=h->next; d=h->s;} else d=, z';
)
)
void outLinkdink *h){ while(h) (cout«(h->s); h=h->next;} cout«endl;
)《高级语言程序设计I》试卷(A) 第15页共15页
(A) &ra=a;
(B) rb = ra;
(C) &rb=&ra;
(D) *rb=*ra;
.在下面的函数声明中,存在着语法错误的选项是(D )。
(A) void f( int a, int)(B) void f( in( a, int b )
(C) void f(int a, int b=5)(D) void f( int a; int b ).假设有定义语句:inta[2][3],*p⑶;那么以下语句中正确的选项是(C )。
(A) p=a;(B) p[0]=a;(C) p[0]=&a[l] [2];(D) p[l]= &a;.以下代码输出结果为(D )
int* arr = new int[10j; cout «arr[OJ;
(A)编译时将产生错误(B)编译正确,运行时将产生错误
(C)输出零(D)输出值不确定.己知 char*sl="123", *s2="34";那么strcmp(sl, s2)的值为以下哪个(B )。
(A) 1(B)-l(C)" 12334"(D) ,,34".假设有以下声明和定义,
struct worker那么以下错误的引用是(D )。
{ int no ; char name[ 20 ] ; } w, *p=&w ;
(A) w.no=3011;(B) p->no=3011;(C) (*p).no=30l 1;(D) *p.no=3011;.要求翻开文件“D:\file.dat",可写入数据,正确的语句是(D )。
(A) ifstream infile( "D:\file.dat”, ios::in );
(B) ifstream infile( ttD:\\file.daf,, ios::in);
(C) ofstream infilc( "D:\file.dat",ios::out);
(D) (stream infile( <4D:\\file.daf,, ios::in|ios::out);二、简答题(共12分)
1、(3分)有以下语句:
int a = 63; double b = 3.456789 ;
cout«setfill('#')«selw(6)«hex«a«'\n'«setprecision(5)« b«endl;
输出显示什么?
3.45682、(3分)有说明int A13][5J;写出两个不同形式的表示元素值的表达式。
A[il[31*(A[l]+3)*(*(A+l)+3)3. (3 分)设有说明 double a[6]= { 0 }, *b = new double [6]; sizeof (a), sizeof (b) 的值各是多少?并分析结果原因。
4844、(3分)设有函数调用语句Count (A , N, right, negative);功能是由参数right, negative返回统计数组A的N个元素中正整数和负整数的个数。对应的函数原型是什么?
void Count( int * , int, int &, int &);三、读程序写结果(共6题,每题4〜5分,共28分)
1 > (4 分):
#include<iostrcam. h>const int N=5;
void main (){ int a[N] = { 8, 6, 5, 4, 1 }, i, temp;
for( i=0; i<N/2; i++ )
{ temp = a[i]; a[i] = a[N-i-l]; a[N-i-l] = temp; }for( i=0; i<N; i++ ) cout<<a[i]<<,';
)答案:1 4 5 6 8
2、(4 分)。
#include<iostream. h>void fun()
{ static int a=0; int b=0;
a+=2; ++b;
cout«a<<,\t* «b<<endl;)
void main (){ for(int i=l; i<4; i++) fun(); }
答案:
2141
613、(4 分)。
#include<iostream. h>void main ()
(int i;char c;
char s 口=“abccda”;
for(i=l; (c=s[i]) != ' \0' ; i++)
{ switch(c){ case 'a' :cout<<,%> ;continue;
case ' b' :cout<<,%) ;break;case ' c' :cout<<,** ;break;
case ' d' : continue;}
cout<〈' <<endl;
)}
答案:
%n4、(5 分)
#include<iostream. h> void func( char *p ) { if ( *p==' \0' ) return ; func( p+1 );
cout«*p;}
void main (){ func (,,hello,/) ; } 答案: olleh
5、(5 分)。
ttinclude <iostream. h>void func(int, int&, int *);
void main (){ int x=l1, y=22, z=33 ;
func (1,x,&z);cout<<x<<*'<<"<') «z<<endl;
func (2,z,&y);cout<〈x<〈‘'J «z«endl;) void func(int a, int &b, int *c) { b+=a ; *c=b-a; } 答案: 12 22 11 12 11 13 6、(5 分)
#include<iostream. h> #include<iomanip. h> void func(int *p) { int t;
for(int i=0;i<3;i++)
for (int j=i;j<3;j++){ t=*(p+3*i+j);
* (p+3*i+j)=*(p+3*j+i);
*(p+3*j+i)=t; } } void main (){ int a[3] [3] = {1, 2, 3,4, 5, 6, 7, 8,9}, *p, i, j;
P=&a[0][0];
func(p);
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)cout«setw(5) «a[i] [j];
cout«endl;
)
答案:
1 47
2 58
3 69
四、读程序填空(每空2分,共20分).以下程序的功能是:将一组数据从大到小排列后输出,并显示每个元素及它们在原数组 中的下标。请将程序补充完整。
#include <iostream.h>
void main()
{ int a[]= { 38, 6, 29, 1, 25, 20, 6, 32, 78, 10 );
int index[ 10];
int i,j,temp;
for( i=0; i<10; i++); index[i] = i
for( i=0; i<= 8; i++ )
for( j = i+1 ;j<=(2); j++ ) sizeof(a)/ sizeof(int)-l 或 9if( (3)) a[i]<a|jl
{ temp = afi]; a[i] = a[j]; afj] = temp;temp = index[i]; index[i] = index|j]; index|j] = temp;
}
for( i=0; i<10; i++)
cout « a[i] « ,\t,« index[i] « endl;
)
2.下面程序的功能是:用字符指针作为函数的参数,实现字符串拷贝。请将程序补充完 整。
#include <iostream.h>
void get_copy( char *to , char *from )
{ inti = 0;whilc( from[i] != '\0')
{ lo[i]=⑷;from[i]i++;
)(5); to[i] =、0'
)
void main()
{ char str[20];
get_copy( (6) “I am a sludent."); str cout«str«endl;
)
3.下面程序的功能是:将两个递增有序的数组a和b合并成一个数组c,并仍保持递增次 序。请将程序补充完整。
#includc <iostrcam.h> void main()
{ int a[6] = { 1,3,5,7,9,11}, b[5]= {2,4,6,8,10}, c[ll];
int i, j, k;
i=j=k=O;
while(i<6 &&j<5)
if(a[i]<bU]) c[k++]=a[i++];
else clk++]=b[j++];
wh ile((7))i<6
c[k++]=ali++J;
while(j<5)(8);c[k++]=b[j++]
for(k=0;k<ll;k++) cout« c[k]«ends;
)4.下面程序的功能是:建立一个文本文件。请将程序补充完整。 tfinclude <fstream. h> void main ()
{(9); ofstream outstuf
outstuf. open(:\\test. dat ", ios::out);
outstuf << "This is a test.\n〃 ; outstuf. (10); } close()五、编程题(共14分)
1. (6)编写程序其功能是输出1000以内个位数为6且能被3整除的所有正整数。要求输出 格式为:每一行显示10个数,每个数的宽度为5。图1是输出效果。
^include <iostream. h>^include <iomanip. h> void main () { cout« ”1000以内个位数为6 且能被3整除的所有正整数: \n\n";
- ♦ WSrt-ZOOBl r c 1 •09(2)C + +l\08-09(
,1□! X
1800以内个粒数为6且能被3整除的所有正整数:
6366696126156186216246276
306336366396426456486516546576
606636666696726756786816846876
906936966996
Press any key to continue
J
Li12J
for (int i=0, j=0 , k=0 ; i <100 ; i++ )
{ j = i * 10 + 6 ;
if ( j % 3 ) continue;
cout << setw(5)«j;
if ( (++ k %10 )== 0 ) cout«endl;
)
cout<<endl;2、(8分)以下程序用随机函数生成两位整数,取U个各不相等的数据,按生成顺序存放在 数组a中。图2是生成20个数据的显示效果。请按照题意编写insert函数及填写函数原型。
#includc<iostream. h>图2生成的数组
图2生成的数组
#include<iomanip. h>#include<stdlib.h>
#include<time. h>;//insert 函数原型 int insert ( int *ap, int k, int n );
void main()
{ const int M=20;
int n, i=0;
int a[M]={ 0 };
srand(timc(0));
while (i<M)
{ do{ n=rand()%100;}while(n<10); //生成数据if( insert ( a, i, n ) ) i++;〃把不相同数据插入数组a
}
cout<X〃生成的数组:〃<<endl;
for( i=l; i<=M;i++ )
{ cout«a[i]<<,z "•、if(i%10==0)cout<<endl; }cout«endl;
int insert(int *ap, int k, int n)
{ for(int j=0; j<k; j++)〃滤去相同数if( ap[j]== n ) break;
if (j==k) { ap[j] = n; return 1; } 〃添加数据 return 0;一、单项选择题。(每题2分,共20分)
1、以下合法的变量名是(C )。
(A) 8d(B) l_2h (C) _int(D) file. cpp2、有说明语句:int a=0; double x=5. 16;,那么以下语句中,(C )属于编译错误。
(A) x=a/x;(B) x=x/a; (C) a=a%x;(D) x=x*a;3、设有:int a=7,b=5,c=3, d=l;,那么条件表达式 a<b?a:c>d?c:d 的值为(C )。
(A) 7(B) 5(C)3(【))14、执行以下语句后,x的值是(D ), y的值是(C )。 int x, y ;
x = y = 1; ++x||++y ;
(A)不确定 (B) 0(C) 1(D) 25、以下for语句循环的次数是(B ) o
for ( int i=0, x=0; !x && i<=3; i++ )
(A) 3(B) 4(C) 0(D)无限6、有函数原型void fun( int * );下面选项中,正确的调用是(C )。
(A ) double x = 0. 12; fun( &x );( B ) int a = 1 ; fun( a*3. 14 );
(C ) int b = 10; fun( &b );( D ) fun( 56 );7、关于函数定义和调用的说法正确的选项是(A )。
(A)函数能嵌套调用,但不能嵌套定义
(B)函数能嵌套调用,也能嵌套定义
(C)函数不能嵌套调用,也不能嵌套定义
(D)函数不能嵌套调用,但能嵌套定义8、有定义一维数组语句:int a[5],*p;,那么以下表达式错误的选项是(B )。
(A) p=p+1(B) a=a+l ( C ) p-a( D ) a+ 29、假定有语句:int b[][3] = {{l}, { 1,2), {1,2,3), (0});
那么b[2][2]的值是(D )。
(A) 0(B) 1(C ) 2( D ) 31()、假设用数组名作为调用函数的实参,那么传递给形参的是(A )。
(A)数组存贮首地址(B)数组的第一个元素值
(C)数组中全部元素的值(D)数组元素的个数二、简答题。(共20分)
1、有以下循环语句无法正常结束循环,请找出原因。(2分) int i=100, j=0, m=0;
while(l)
{ m+=j; j++; if(j=i) break; }2、一程序要求统计未退休(男性年龄<60,女性<55)职工中『3月份出生的人数。请写出 职工记录中结构的最小定义形式,并写出用于判断的C++逻辑表达式。(4分) struct Employee{ char name[20]; char sex; int Byear; int Bmonth; int Bday;}; Employee em;
〃设男性,女性日 f,
(eni.sex==,m,&&2007-em.Byear<60||
em.sex==,f,&&20()7-em.Byear<55)&&(em.Bmonth<=3&&em.Bmonth>=l)3、设有说明int a[4*5];请写出两个表示数组a最后一个元素地址值的表达式。(3分) &a[19]a+19
4、设有说明 double x(10]= { 0 }, * y = new double [10]:问 sizeof(x), sizeof(y)的值各是 多少?并分析结果原因。(4分) sizeof (y)的值为4。y是指针变量。
5^设有函数调用语句CountXa , n, right, negative);功能是由参数right, negative 返回统计数组a的n个元素中正整数和负整数的个数。对应的函数原型是什么?(2分) void Count (int *a, int , int&, int&);
void Count(int a口, int , int&, int&):
6、以下语句不能正确输出单向链表的数据元素值,请找出原因。(2分) struct link{int data; link * next; };
1 i nk *head, *p;
p=head;
while(p) {cout«p. data; p++; }
链表非连续存储,P++不能访问后续结点。
7、设有以下说明语句,请写出3个调用函数function的语句。(3分) typedef void funType (int ,double);
funType function, *fp;
设:
int a; double x;
fp=function;
那么可有:
function(a, x);
fp (a, x);
(*fp) (a, x);三、阅读程序写输出结果(每题4分,共20分)
t?include <iostream. h>
void mainO
{ int i, s = 0:
for( i=4; i<6; i++ ) { switch( i )
{ case 3: s
case 4: s
case 5: sdefault: s cout « "s=" «
+= i*i; break;+= i*i; break;
+= i*i; break;+= 2; }
s «endl;
) ) S=16
S=412、
ttinclude <iostream. h>
void main()
{ int i,j;
for( i=l; i<=3; i++ )
{ j=l;
while (j<i)
{ cout « i«' \t' «j«endl;」++;}
展开阅读全文