华中科技大学级信息学院《C语言程序设计》考试试题及答案DOC.docx
《华中科技大学级信息学院《C语言程序设计》考试试题及答案DOC.docx》由会员分享,可在线阅读,更多相关《华中科技大学级信息学院《C语言程序设计》考试试题及答案DOC.docx(58页珍藏版)》请在冰豆网上搜索。
华中科技大学级信息学院《C语言程序设计》考试试题及答案DOC
2003级信息学院《C语言程序设计》考试试题
系班级
姓名学号成绩一.判断下列语句或程序的对错。
(“×”表示错,“√”表示对)(10分)
1intx=y=z=20;(×)y,z需要在前定义
2#defineM50;(×)不能有分号
3printf(“%\t”,(40,50,60));(√)
4floata[100],*p;
p=a++;(×)数组名是地址常量,不能做++操作
5charstr[20];
str[20]=0;(×)str[20]单元不可用
6intdata[4]={0,1,2,3,4};(×)0,1,2,3,4是5个元素,与data[4]不匹配
7intx=’\014’;(√)
8intxyz.1=2;(×)xyz.1不是合法的标识符(变量名)
9char*p=”c:
\test\prg.c”;(×)修改为c:
\\test\\prg.c
10int*p;
*p=20;(×)指针不能用常量进行赋值
二.计算下列表达式的值(10分)
设unsignedinta=15,b=21,c=5,d=1;
floatf;
(1)f=b/c(4.0)b/c是整数除,先得到4,然后赋值给f
(2)!
(a+b)+c-1&&b+c/2
(1)
(3)a^b+b&0x55+c<<3|015(0x000f)
(4)d*=c--%b++(5)
(5)a%=(c%=2)(0)
三.程序改错(10分)
1从键盘上输入三个字符串,然后按字典(从小到大)顺序进行输出
#include
#include
voidswap(char*,char*);//函数声明需要分号结束
main()
{
chara[80],b[80],c[80];
scanf(“%s%s%s”,&a,&b,&c);//去掉&号,数组名即表示地址
if(a>b)swap(a,b);strcmp(a,b)>0a,b比较需要用字符串比较函数
if(b>c)swap(b,c);strcmp(b,c)>0b,c比较需要用字符串比较函数
printf(“%s\n%s\n%s\n”,a,b,c);
}
voidswap(char*pstr1,char*pstr2)
{
char*p;//charp[80];
p=pstr1;//strcpy(p,pstr1);字符串赋值需要用拷贝函数
pstr1=pstr2;//strcpy(pstr1,pstr2);
pstr2=p;//strcpy(pstr2,p);
}
(2)求某班30个学生数学成绩的最高分和平均分.
#include
flaotprocess(float*p1,intn,int*p2);添加函数声明
main()
{
floata[30],aver;
int;
for(m=0;m<30;m++)
scanf(“%f”,&a[m]);
max=process(a,30,&aver);
printf(“max=%f,ave=%f\n),max,aver);
}
floatprocess(float*p1,intn,int*p2)
{
charx;
inttemp;//floattemp与函数返回类型匹配
for(x=0;x<=n;x++)//for(x=0;x{
if(p1[x]>temp)
temp=p1[x];
*p2+=p1[x];
}
p2=p2/n;//*p2=*p2/n;
returntemp;
}
四.程序填空
(1)有一分数系列:
2/1,3/2,/5/3,8/5,13/8,21/13……求出这数列的前50项和
#include
main()
{
intm;
float__________;//t,s=0,a=2,b=1
for(m=1;__________;m++)//m<=50
{
s=s+a/b;
t=a;
a=_a+b_____;
b=_t_;
}
printf(“sum=%f\n:
s);
}
(2)按如图所示格式打印杨辉三角形的前10行
1
11
121
1331
14641
………
main()
{
intm,n,a[10][10];
for(m=0;m<10;m++)
{
a[m][0]=1;
__________________;a[m][m]=1;
}
for(__m=2___;m<10;m++)
for(___n=1____;__na[m][n]=a[m-1][n-1]+a[m-1][n];
for(m=0;m<10;m++)
{
for(n=0;__n<=m_____________;n++)
printf(“%6d”,a[m][n]);
_printf(“%5d”,a[m][n]);
}
}
五.写输出结果
(1)
#include
voidmain()
{
intm,n,p,s;
s=0;
for(m=1;m<=4;m++)
{
for(n=1;n<=m;n++)
{p=1;
for(n=1;n<=m;n++)
p=p*n;
}
s=s+p;
}
printf(“s=%d\n”,s);
}
S=33;//1!
+2!
+3!
+4!
;
(2)
#include
voidauto_static();
intg_val;
main()
{
intm;
for(m=0;m<5;m++)
auto_static();
}
voidauto_static()
{
intau=0;
staticintst=0;
printf(“au=%d,st=%d,g_val=%d\n”,au++,++st,g_val++);
}
au=0,st=1,g_val=0
au=0,st=2,g_val=1
au=0,st=3,g_val=2
au=0,st=4,g_val=3
au=0,st=5,g_val=4
(3)
#include
main()
{
inti,j,a[][3]={2,4,6,1,2,3};
int*p[2]={a[0],a[1]};
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf(“%3d\t”,*(p[i]+j));
printf(“\n”);
}
}
246
123
(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[j].name))
{
leader[j].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
写出程序的输出结果。
基本上是书中例题8.3p238-239
li:
2
zhang:
1
wang:
2
(5)
#include
#defineNULL0
voidmain()
{
char**pp;
char*name[]={“fortran”,”basic”,”pascal”,””};
intm;
pp=name;
for(m=0;**pp!
=NULL;m++,pp++)
printf(“%s\n”,*pp+m);
}
fortran
asic
scal
六.编写程序(35分)
(1)一个程序实现这样的功能:
商店卖西瓜,10斤以上(包括10斤,以下同)的每斤0.8元,8斤以上10斤以下的每斤0.6元,8斤以下的每斤0.4元。
输入西瓜的重量和顾客所付的金额,输出应付货款和应找钱数。
(9分)
#include
voidmain()
{
floatweight,a,b,c;
printf("请输入重量和所付金额:
");
scanf("%f%f",&weight,&a);
if(weight<8.0)
{
b=weight*0.4;
c=a-b;
}
elseif(weight<10)
{
b=8*0.4+(weight-8)*0.6;
c=a-b;
}
else
{
b=8.0*0.4+2*0.6+(weight-10)*0.8;
c=a-b;
}
printf("应付货款:
%f应找钱数:
%f\n",b,c);
}
(2)编写一个函数,求两个在整数的最大公约数;主函数完成两个整数的输入,调用所编函数和输出所求的最大公约数。
(9分)
(3)编写一个函数,删除给定字符串中的指定字符,如给定字符串“abcdfrc”,删除指定的字符‘c’后,字符串变成“abdfr”;主函数完成给定字符串和指定字符的输入,调用所编函数,输出处理后的字符串。
注意:
不得使用全局变量,注意程序结构。
(9分)
(4)编程处理某班30个学生4门课的成绩,它们是数学,物理,英语和计算机,按学号依次输入学生的学号,姓名,性别(用1表示男生,0表示女生)和4门课成绩。
要求以清晰的格式从高分到低分的顺序打印平均分高于全班总平均分的男生的成绩单。
(8分)
要求:
输入.输出,计算和排序分别用程序实现,主函数只是调用这些函数。
不得使用全局变量,注意程序结构。
#include
#defineNUM5
structStudent
{
intnumber;
charname[20];
intsex;
floatmaths;
floatphys;
floateng;
floatcomp;
floataver;
};
Input(structStudent*stu)
{
inti;
for(i=0;i{
printf("pleaseentername:
");
gets(stu[i].name);
printf("pleaseenternumsexmathsphysenglishcomputer:
");
scanf("%d%d%f%f%f%f",
&stu[i].number,
&stu[i].sex,
&stu[i].maths,
&stu[i].phys,
&stu[i].eng,
&stu[i].comp);
getchar();
}
}
Aver(structStudent*stu,float*total_aver)
{
inti;
floattotal=0;
floatsum=0;
for(i=0;i{
total=stu[i].maths+stu[i].phys+stu[i].eng+stu[i].comp;
stu[i].aver=total/4;
sum+=total;
}
*total_aver=sum/(4*NUM);
}
Sort(structStudent*stu)
{
inti,j;
structStudenttemp;
for(i=0;i{
for(j=i+1;jif(stu[i].aver>stu[j].aver)
{
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
}
Output(structStudent*stu,floattotal_aver)
{
inti;
printf("total_aver:
%f\n",total_aver);
printf("numnamesexmathphysenglishcomputeraver\n");
for(i=0;i{
if(stu[i].aver>=total_aver)
printf("%d%s%d%f%f%f%f%f\n",
stu[i].number,
stu[i].name,
stu[i].sex,
stu[i].maths,
stu[i].phys,
stu[i].eng,
stu[i].comp,
stu[i].aver);
}
}
voidmain()
{
structStudentstu[NUM];
floattotal_aver;
Input(stu);
Aver(stu,&total_aver);
Sort(stu);
Output(stu,total_aver);
}
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(36)
三程序改错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;iscanf(“%s”,&string[i]);去掉&符
for(I=0;ICapital_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;ib[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;