2004级信息学院《C语言设计》考试试题
一、判断下列语句或程序的对错。 10分 √ 1 int x=y=z=’0’; (×) 2 #include
3 printf(“%s\\n”,”c language”); (×) 4 float a[100];
int *p=a; (×) 5 char str[20];
6 int data[4]={0,1,2,3,4}; (×) 7 float x=1.45e+310L; (×) 8 int xyz-1=2; (×) 9 int x=’\\xae’ ; (√) 10 int *p,a[2][3] ;
p=a ; (×)
二 计算下列表达式的值 10分 设 unsigned int a=10,b=17,c=5,d=3; float f ;
(1)f=b/c ( ) (2)!(a+b)+c-1&&b+c/2 ( ) (3)(a^b)+(c>>1+d) ( ) (4)a+=b%=a=b ( ) (5)a=2,b=a*++b ( ) 三 程序改错 10分 (1) 求两个浮点数的平方和及平方差 #include
float a,b;
float add_reasult, sub_result; scanf (“%f,%f”,a,b);
add_result=calculate(a,b,&sub_result);
printf( “a*a+b*b=%d,a*a-b*b=%d\\n”,add_result,sub_result); }
calculate (float x,float y,float *sub) {
float *temp; sub=a*a-b*b ; return *timp; }
(2) 统计N 个字符中大写字母和数字字符的个数 #include
#define N 5 main () {
char string[N][80]; char i;
int Capital_Count,Num_Count
for(i=0;i scanf( “%s”,&string[i]) ; for(I=0;I Capital_Count+=Count(string[I].&Num_Count); Printf(“Capital count :=%d,numbercount=%d\\n” ,Capital_Count,Num_Count) ; } Count(char *str,int *result) { int temp,I ; for(I=0;I<80;I++) { If(str[I]>=’A’&& str[I]<=’Z’) Temp++; If(str[I]>’0’||str[I]<’9’) *result++; } return temp; } 四 程序填空 10分 3 (1)利用公式 sin x=x-x /3!+x x=0.5,n=20 #include float y,s,x,d,t; int n,I,j; scanf(“%d%f”,&n,&x); s=1.0; ____________________________; for(I=2;I d=t=__________________________; for(j=1;_______________;j++) { d=________________; t=________________; } s=(-1)*s; y+=_____________________; } (2)利用库函数char *strstr(char *sl,char *s2)在给定字符串中查找子串最后(最右)一次出现的位置。如果S2并没有出现在S1的任何地方,函数返回一个NULL 指针。如果第二个参数是一个空字符串,函数就返回S1; 注:库函数char strstr(char*s1,char*s2),这个函数在S1中查找子字符 串S2第一次出现的起始位置,并返回一个指向该位置的指针。如果S2并没有出现在S1的任何地方,函数返回一个NULL指针。如果第二个参数是一个空字符串,函数返回S1; #include char str[80]=”ABCdabcdfgabc”; char *p; p=my_strrstr(str,”abc”); printf(“%s |n”,p); p=my_strrstr(str,” “); printf(“%s|n”,p); } char *my_strrstr(char *s1,char*s2) { char *last; char *current; _________________________; if(________________________) { last=current=_____________; While(______); { last=current; current=_______; } } return last; } 五.写输出结果(20分) (1) #iinclude int a[]={5,6,7,8},I; fun(a,4); for(I=0;I<4;I++) printf(“%d\\n”,a[I]) } void fun(int *b,int n) { int I; for(I=0;I #include int I,j,max; int row=0,column=0; int a[3][3]={{1,2,3},{2,-3,4},{9,4,7}}; max=a[0][0]; for(I=0;I<3;I++0 for(j=0;j<3;;j++) { if(a[I][j]>max) { max=a[I][j]; row=I+1; column=j+1; } } printf(“max=%d,row=%d,column=%d\\n”,man\\x,row,column); } (3) #iinclude int n=1; void func(_); void main() { static int x=5; int y; y=n; printf(“main:x=%d,y=%d,n=%d\\n”,x,y,n,); func(); printf(“main:x=%d,y=%d,n=%d\\n”,x,y,n); } void func() { static int x=4; int y=10; x=x+2; n=n+2; y=y+n; printf(“func:x=%d,y=%d,n=%d|n”,x,y,n); } (4) #include char name[20]; int count; }; Void main() { Struct person leader[3]={{“li”,0},{“zhang”,0},{“wang”},0}; Char name[20],m,n; For(m=1;m<8;m++) { Scanf(“%s”,name); for(n=0;n<3;n++) if (!strcmp(name,leader[n].name)) { Leader[n].count++; Break; } } Printf(“|n”); For(m=0;m <3;m++)