华南理工大学期末考试
《 高级程序设计语言C++(1)》试卷A
注意事项:1. 考前请将试卷和答题纸密封线内各项填写清楚;
2. 所有答案写在答题纸上,否则无效;
3.试卷和答题纸同时提交;
4.考试形式:闭卷;
5. 本试卷共五大题,满分100分,考试时间120分钟。
一、 单项选择题(每小题2分,共22 分
1. 表达式65/10*2.0+100%3的结果为(D )。
(A) 14 (B) 12 (C) 1 (D) 13
2. 设变量定义int x =10, y, z; 执行语句 y = z = x --; y = x = = z; 后变量y的值是( A )。
(A) 0 (B) 1 (C) 9 (D) 10
3. 下面for语句执行时循环次数为(B )。
int i,j;
for ( i=0, j=3; i=j; )
{
cout << i << j << endl;
i++; j--;
}
(A) 0 (B) 3 (C) 10 (D) 无限
4. 下列不合法的标识符名称是( B )。
(A) word (B) continue (C) cpp (D) _ab
5. 假定有以下说明语句,则下面引用形式错误的是( B )。
struct Student
{
char sno[20] ;
double score ;
};
Student stu[3]={{“201011111”,80}, {“201011112”,75}, {“201011113”,91}}, *p=stu ;
(A) p->sno; (B) (p++).sno; (C) (p++)->sno; (D) (*p).sno ;
6. 已知int i = 1 ; int *Ptr1 , *Ptr2 ;下面哪条语句不合法( C )。
(A)Ptr1 = &i; (B) Ptr1 = Ptr2; (C) Ptr1 = 0x1234; (D) Ptr1 = NULL;
7. 已知char* s1= "123", *s2="34";cout<
(A) true (B) false (C) 1 (D) -1
8. 设已定义浮点型变量data,以二进制方式把data的值写入输出文件流对象outfile中去,正确的语句是( C )。
(A) outfile.write((double *) &data, sizeof(double)) ;
(B) outfile.write((double *) &data, data) ;
(C) outfile.write((char *) &data, sizeof(double)) ;
(D) outfile.write((char *) &data, data) ;
9. 已知 int A = 50; int * const iA = &A; const int B = 80; const int* iB = &B;下面哪条语句不合法( B )。
(A) *iA = 200; (B) iA = &B; (C) iB = &A; (D) iB = iA;
10. 若有定义语句: int a[2][3],*p[3]; 则以下语句中正确的是( C )。
(A) p=a; (B) p[0]=a; (C) p[0]=&a[1][2]; (D) p[1]= &a;
11. 有函数原型void fun(int *); 已知int a =10; int *x = &a;
下面选项中,正确的调用是( A )。
(A) fun(x); (B) fun(*x); (C) fun(&x); (D) fun(*a);
二、简答题 (共15分)
1、(3分) 设有说明int a[6]= { 0 }, *b; 这段代码运行时总共需要分配给数据多少内存空间?请分析并说明。(假定运行在32位的windows系统环境下)
28字节
2、(3分) 已知char *a[] ={"you", "are", "a", "student", "right"};则cout <<*(a+3);的显示结果是?
student
3. (3分) 语句 cout<
%%3456
4、(3分) 有如下代码
int main()
{
char str[10];
cin>>str;
cout<
}
如果输入的字符串为“south china”,那么输出结果是什么,请解释这种结果的原因。
south
5、(3分) 下面代码存在什么问题,请指出并改正。
int main()
{ int * p = NULL ;
p = new int (50) ;
if ( p == NULL )
{ cout << "allocation faiure\n"; return 1; }
cout << *p ;
}
new动态生成的对象没有释放会造成内存泄露问题,需要在最后加上delete p;
三、读程序写结果(共6题,每题 4~5分,共28分)
1、(4分) 。
#include
using namespace std ;
int main()
{
int m =75, n = 35 , a , b , r ;
if ( m > n) { a = m ; b = n ; }
else { a = n ; b = m ; }
r = b ;
while ( r != 0 )
{ r = a % b ;
a = b ;
b = r ;
}
cout << a << endl ;
}
5
2、(4分)。
#include
using namespace std;
int main()
{
int a[9];
int *p = a , sum = 0 ;
for(int i=0;i<9;i++)
{
a[i] = i+4;
}
if( *p % 2 == 0 ) sum += *p ;
cout << "sum = " << sum << endl ;
}
sum = 40
3、(5分)。
#include
using namespace std;
void func(int, int&, int *) ;
void main()
{
int x=10, y=20, z=30 ;
func(1, x, &z) ;
cout<
func(2, z, &y) ;
cout<
}
void func(int a, int &b, int *c)
{
b+=a ; *c=b-a;
}
4、(5分)。
#include
using namespace std;
void print(int w)
{
int i;
if( w )
{ print( w-1 );
for( i=1; i<=w; i++ )
cout << w << " ";
cout << endl;
}
}
void main()
{
print( 3 );
}
5、(5分)。
# include
using namespace std ;
int f ( char p[] )
{
int s = 0,n;
n = sizeof(p)/sizeof(char);
for(int i=0;i
{
s += (p[i]-'A');
}
return s;
}
int main()
{
char ap[]="ABCDEF";
int sum = f(ap);
cout<
}
6
6、(5分)。
#include
using namespace std;
struct employee
{ char name[20];
char sex;
};
void fun(employee *p)
{ if ((*p).sex=='f')
cout<
int main()
{ employee emp[5]={"liming",'m',"wangxiaoping",'f',
"luwei",'m',"lixiaohong",'f' } ;
int i;
for(i=0;i<5;i++)
fun(emp+i);}
四、读程序填空(每空2分,共20 分)
1.下面程序的功能是:用选择法从大到小排序。请将程序补充完整。
#include
using namespace std ;
void sort ( int [] , int ) ;
int main()
{
int i, a [10]={41,72,18,9,7,46,80,34,25,13} ;
(1) ; //调用排序函数 //sort(a,10)
for ( i = 0 ; i < 10 ; i ++ ) cout << a[ i ] << " " ;
cout << endl ;
}
void sort ( int *x , int n )
{
int max , t ;
for ( int i = 0 ; i < n-1 ; i ++ )
{
t = i ;
for ( int j = i + 1 ; j < n ; j ++ )
if ( x [ j ] > x [ t ] ) (2) ; //t=j
if ( t != i )
{
(3) ; //max=x[i]
x [ i ] = x [ t ] ;
x [ t ] = max ;
}
}
return ;
}
2. 下面程序的功能是:把文件 d:\test 复制到文件 d:\testnew 中。请将程序补充完整。
#include
#include
using namespace std;
int main ( )
{
char ch ;
(4) ;// ifstream f1(“d:\\test”);
if( !f1 ){ cout << "cannot open 'test' for input." ; return 0; }
ofstream f2 ( "d:\\testnew" ) ;
if ( !f2 ){ cout << "cannot open testnew for ouput." ; return 0; }
while ( ! f1.eof() && f1.get(ch) ) f2.put( ch ) ;
(5) ;// f1.close ()
f2.close () ;
cout << "It is over !\n" ;
}
3. 以下程序的功能是:判断一个字符串是否是回文,是输出“yes”,否则输出“no”.(回文是字符串正读反读一样,如abcba)
# include
#include
using namespace std ;
int main ( )
{
int i,j,length;
char a[100];
cin>>a;
(6) ;//length=strlen(a)
for(i=0,j=length-1;i
if(a[i]!=a[j])
break;
if(i>=j)
cout<<"yes";
else
cout<<"no";
}
4. 下面程序的功能是:输入一个整数,输出该整数的所有素数因子。例如,输入120,输出为2、2、2、3和5。
#include
using namespace std;
int main()
{ int m,i = 2;
cout << "please input m:";
cin >> m;
while( i<=m )
if( (9) ) //!(m%i)
{ cout << i << ",";
(10) ;// m=m/i
}
else i++;
}
五、编程题(共15分)
1.( 7分 ) 设计程序,输出所有各位数字之和等于7的三位数,每行显示10个数。运行效果如下图所示。
#include
using namespace std;
int main()
{ int i,n=0;
cout<<"各位数之和等于的位整数:\n";
for( i=100; i<1000; i++ )
if( i%10 + i/10%10+i/100== 7 )
{ cout<
n++;
if(n%10==0)cout<
}
cout<
}
2. (8分)补全下面程序,使程序的运行结果如下图所示。任务:(1)给出函数原型;(2)写出 stringcopy函数的实现代码,以实现字符串拷贝功能.要求:在实现stringcopy函数过程中不能调用任何其他函数。
#include
using namespace std;
void stringcopy(char *,char *); //函数原型
void main( )
{ char str[20];
stringcopy( str , "I am a student.");
cout<
}
void stringcopy(char * s1,char * s2)
{
int i = 0;
while( s2[i] != '\0' )
{ s1[i] = s2[i] ;
i++;
}
s1[i] = '\0' ;
}
¥29.8
¥9.9
¥59.8