大一c语言期末试题及参考答案.docx
- 文档编号:1565529
- 上传时间:2022-10-23
- 格式:DOCX
- 页数:16
- 大小:20.04KB
大一c语言期末试题及参考答案.docx
《大一c语言期末试题及参考答案.docx》由会员分享,可在线阅读,更多相关《大一c语言期末试题及参考答案.docx(16页珍藏版)》请在冰豆网上搜索。
大一c语言期末试题及参考答案
2004级信息学院《C语言设计》考试试题
一、判断下列语句或程序的对错。
10分√
1intx=y=z=’0’;(×)y,z没有定义
2#include
3printf(“%s\n”,”clanguage”);(√)
4floata[100];
int*p=a;(×)数据类型不匹配
5charstr[20];
6intdata[4]={0,1,2,3,4};(×)五个元素,但是只有四个单元
7floatx=1.45e+310L;(×)数值越界
8intxyz-1=2;(×)
9intx=‘\xae’;(√)
10int*p,a[2][3];
p=a;(×)数据类型不匹配
二计算下列表达式的值10分
设unsignedinta=10,b=17,c=5,d=3;
floatf;
(1)f=b/c(3.0)
(2)!
(a+b)+c-1&&b+c/2
(1)
(3)(a^b)+(c>>1+d)(0x1b)
(4)a+=b%=a=b(17)
(5)a=2,b=a*++b
(2)
三程序改错10分
(1)求两个浮点数的平方和及平方差
#include
floatcalculate(floatx,floaty,float*sub);添加函数原型声明
main()
{
floata,b;
floatadd_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);
}
floatcalculate(floatx,floaty,float*sub)添加函数类型
{
float*temp;应该直接定义为变量floattemp;
sub=a*a-b*b;*sub=a*a-b*b;
temp=a*a+b*b;
return*temp;returntemp
}
(2)统计N个字符中大写字母和数字字符的个数
#include
#defineN5
Count(char*str,int*result);添加函数声明
main()
{
charstring[N][80];
chari;
intCapital_Count=0,Num_Count=0;需要初始化为0
for(i=0;i scanf(“%s”,&string[i]);去掉&符 for(I=0;I Capital_Count+=Count(string[I],&Num_Count); Printf(“Capitalcount: =%d,numbercount=%d\n” Capital_Count,Num_Count); } Count(char*str,int*result) { inttemp,I;inttemp=0,i;temp应该初始化为0 for(I=0;I<80;I++) { If(str[I]>=’A’&&str[I]<=’Z’) Temp++; If(str[I]>’0’||str[I]<’9’) *result++; } returntemp; } 四程序填空10分 (答案参考书中p85~86) (1)利用公式sinx=x-x/3! +x x=0.5,n=20 #include main() { floaty,s,x,d,t; intn,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; 注: 库函数charstrstr(char*s1,char*s2),这个函数在S1中查找子字符串S2第一次出现的起始位置,并返回一个指向该位置的指针。 如果S2并没有出现在S1的任何地方,函数返回一个NULL指针。 如果第二个参数是一个空字符串,函数返回S1; (答案见书中p196~197) #include #include voidmain(void) { charstr[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=_______; } } returnlast; } 五.写输出结果(20分) (1) #include voidfun(int*,int); voidmain() { inta[]={5,6,7,8},i; fun(a,4); for(i=0;i<4;i++) printf("%d\n",a[i]); } voidfun(int*b,intn) { inti; for(i=0;i b[i]=i*2; return; } 0 2 4 6 (2) #include voidmain() { inti,j,max; introw=0,column=0; inta[3][3]={{1,2,3},{2,-3,4},{9,4,7}}; max=a[0][0]; for(i=0;i<3;i++) 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",max,row,column); } (书中例题5.5,p123) max=9,row=3,column=1 (3) #include intn=1; voidfunc(); voidmain() { staticintx=5; inty; 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); } voidfunc() { staticintx=4; inty=10; x=x+2; n=n+2; y=y+n; printf("func: x=%d,y=%d,n=%d\n",x,y,n); } main: x=5,y=1,n=1 func: x=6,y=13,n=3 main: x=5,y=1,n=3 (4) #include #include structperson { charname[20]; intcount; }; voidmain() { structpersonleader[3]={{"li",0},{"zhang",0},{"wang",0}}; charname[20],m,n; for(m=1;m<7;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++) printf("%s: %d\n",leader[m].name,leader[m].count); } 当程序运行时,键入情况如下: Li Wang Lei Li Wang Zhang 写出程序的输出结果。 (基本上是书中例题) Li: 2 Zhang: 1 Wang: 2 (5) #include #include voidmain() { char*name[]={"capital","index","large","small"}; inta,b,n=4; char*temp; for(a=0;a for(b=a+1;b { if(strcmp(name[a],name[b])>0) { temp=name[a]; name[a]=name[b]; name[b]=temp; } }在此之前是书中的例题7.19 for(a=0;a printf("%s\n",name[a]+a);输出时应该能够识别指针及偏移情况 } capital ndex rge ll 六、编写程序(35分) (1)求一元二次方程ax+bx+c=0的根,实系数a,b,c从终端输入,只考虑两个不同实根和两个相同的实根(9分) (书中例题3.4,p66`67) #include #include voidmain() { floata,b,c; floatx1,x2; floatx3,x4; floatm; printf("inputthenumbers: a,b,c"); scanf("%f%f%f",&a,&b,&c); if(a==0) { printf("theinputiserror! \n"); return; } m=b*b-4*a*c; if(m>0) { x1=(-b+sqrt(m
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 大一 语言 期末 试题 参考答案
![提示](https://static.bdocx.com/images/bang_tan.gif)