最终测试试题答案.docx
《最终测试试题答案.docx》由会员分享,可在线阅读,更多相关《最终测试试题答案.docx(21页珍藏版)》请在冰点文库上搜索。
最终测试试题答案
《c语言程序设计》测试试题
姓名:
学号:
一、判断下列语句或程序段的对错。
(“X”表示错,“√”表示对)
(1)charch='55';(X)
(2)inta=2,*p;p=&a;(√)
printf("%d",*&a);
(3)inta[5][5],(*p)[5];(X)
p=&a[0][0];
(4)intn;scanf("%d",&n);(X)
inta[n];
(5)floata=1,*b=&a,*c=&b;(X)
(6)charstr[]={"World"};(√)
printf("%c",*(str+2));
(7)floatx=0.45e+10L(X)
(8)printf('"%f\n",(40,50,,60));(X)
(9)intdata[4]={0,1,2,3,4};(X)
(10)char*p="hust";(X)
gets(p);
(11)#defineN=50;(X)
inta[N];
(12)floata=1,*b=&a,**c=b;(X)
(13)float*p,a[2][3];(√)
p=*a+1;
(14)int_Max_int=65535;(X)
(15)#defineN018
inta=N;(X)
(16)int*p,a[10];
*p=a[0];(X)
(17)char*pstr,str[20];
pstr=str=”HUST”;(X)
(18)while(3)if(getchar()==‘0’)break;(√)
(19)intx=(3>5);(√)
(20)float*p[3],a[2][3];
p=a;(X)
(21) int x=y=z=0; (X)
二、计算下列表达式的值
设intx=3,y=-4,z=4;unsignedinta=7,b=17,c=4,d=2;floatk=3.5,f;
(1)x++-y+++z(12)
(2)y>z||z>2&&x++
(1)
(3)×|z&x^z(7)
(4)x-y+!
z-1&&x+y/2
(1)
(5)y+(x/3*(int)(x+k)/2)%4(-1)
(6)f=b/c(4.0)
(7)a+=b%=a+b(24)
(8)a=2,b=a*++b(36)
(9)f=(d-c)/2(32767.0)
三、改错,根据题意改正下列程序的错误和漏掉的部分
(1)输入球体半径r求其体积.
#include"stdio.h";->#include"stdio.h"
definepi=3.1415926;->#definepi3.1415926
->floatvolume(floatr);
voidmain()
{
floatr,v;
scanf("%d",r);->scanf("%f",&r);
v=volume(r);
printf("%d",v);->printf("%f\n",v);
}
floatvolume(intr);->floatvoulme(floatr)
{->floatv;
v=4/3*pi*r*r*r;->v=4.0/3*pi*r*r*r;
returnv;
}
(2)利用指针数组对字符数组中的5个姓名字符串排序,字符数组内容不变。
#include;->#include
#include
voidmain()
{
charname[5][20];
char*p[5];
inti;->inti,j;
chartemp[20];->char*temp;
for(i=0;i<=5;i++)->for(i=0;i<5;i++)
{
gets(name+i);->gets(*name+i);或gets(name[i]);
p=name[i];->p[i]=name[i];
}
for(i=0;i<5;i++)->for(i=0;i<4;i++)
for(j=0;j<5;j++)->for(j=i+1;j<5;j++)
{
if(p[i]>p[j])->if(strcmp(p[i],p[j])>0)
{
temp=p[i];
p[i]=p[j];
p[j]=p[i];->p[j]=temp;
}
}
for(i=0;i<5;i++)
printf("%c",p[i]);->printf("%s\n",p[i]);
}
(3)统计N个字符串中大写字母和数字字符的个数
#include
#defineN5
->intCount(char*str,int*result);
main()->voidmain()
{
charstring[N][80];
chari;
intCapital_Count,Num_Count;->intCapital_Count=0,Num_Count=0;
for(i=0;i<=N;i++)->for(i=0;iscanf(“%s",&string[i]);->scanf("%s",string[i]);
for(i=0;iCapital_Count+=Count(string[i],&Num_Count);
printf("Capital_count:
=%d,numbercount=%d\n”,Capital_Count,Num_Count);
}
Count(char*str,int*result)
{
inttemp,i;
->temp=0;
for(i=0;i<80;i++)->for(i=0;str[i]!
='\0';i++)
{
if(str[i]>='A'&&str[i]<='Z')
temp++;
if(str[i]>='0'||str[i]<='9')->if(str[i]>='0'&&str[i]<='9')
*result++:
->(*result)++;
}
returntemp;
}
(4) 删除指定字符串中的数字字符,然后显示
#include
->#include
char*DelDigChar(char*str);
void main()
{
char string[80];
scanf(“%s”,&string); ->scanf("%s",string);
puts(DelDigChar(string));
}
DelDigChar(char *str)->char*DelDigChar(char*str)
{
int *temp = str; ->char*temp=str;
while(*str)
{
if(*str ="0"&&*str<="9") ->if(*str=='0'&&*str<='9')
strcpy(str+1,str); ->strcpy(str,str+1);
else
str++;
}
return temp ;
}
四、程序填空
(1)将一个指字符串的正序和反序进行连接,形成一个新串放在另一个字符数组中。
例如:
当字符串为”ABCD”时,则新字符数组的内容应为”ABCDDCBA”。
#include
#include
voidfun(char*s,char*t);
voidmain()
{
charS[100],T[100];
printf("\nPleaseenterstringS:
");
scanf("%s",);->S
fun(S,T),
printf("\nTheresultis:
%s\n",T);
}
voidfun(char*S,char*t)
{
inti,d;
d=;->strlen(S);
for(i=0;ii++
t[i]=s[i];
for(i=0;i->t[d+i]=s[d-i-1];
;->t[2*d]=‘\0’;
}
(2)利用公式
计算sinx的值.
#include
#include
main()
{
floaty,s,x,d,t;
intn,i,j;
scanf(“%d%f,&n,&x”);
s=1.0;
_________________________;->y=x;
for(i=2;i{
d=t=________________;->1
for(j=1;_________________;j++)->j<=2*i-1
{
d=________________;->d*x
t=_________________;->t*j
}
s=(-1)*s;
y+=_____________________;->s*d/t
}
}
(3)用字符指针数组处理多个字符串排序问题,按字典顺序输出
#include
#include
voidsortstr(char*v[],intn);
voidmain()
{
char*proname[]={“pascal”,”basic”,”cobol”,”prolog”,”lisp”};
inti;
sortstr(_____________________);//排序->proname,5
for(i=0;i<5;i++)//输出排序后的字符串
printf(“%s\n”,proname[i]);
}
voidsortstr(char*v[],intn)
{
inti,j;
char*temp;
for(i=0;ifor(j=0;___________;j++)->j{
if(___________)>=0->strcmp(v[j],v[j+1])
{
temp=v[j];
_________________;->v[j]=v[j+1]
_________________;->v[j+1]=temp;
}
}
}
五、写程序输出结果
(1)
#include
inta=1;
voidfunc()
{
staticintx=1;
inty=2;
x=x+1;
a=a+2;
printf(“func:
x=%d,y=%d,a=%d\n",x,y,a);
}
voidmain()
{
staticintx=2;
inty;
y=a;
printf("main:
x=%d,y=%d,a=%d\n",x,y,a);
func();
printf("main:
x=%d,y=%d,a=%d\n",x,y,a);
func();
{
inta;
a=x+y;
printf("main:
x=%d,y=%d,a=%d\n",x,y,a)
}
}
main:
x=2,y=1,a=1
func:
x=2,y=2,a=3
main:
x=2,y=1,a=3
func:
x=3,y=2,a=5
main:
x=2,y=1,a=3
(2)
#includ"stdio.h"
#dfineM10
voidmain()
{
inta[M+1]={10,20,30,40,50,60,70,80,90,100};
Inti.,n,*p,*q;
n=45;
for(p=a,i=0;i<=M;i++)
{
if(n<=*(p+i))
{
p=p+i;
break;
}
}
for(q=a+M-1;q>=p;q--)
{
*(q+1)=*q;
}
*p=n;
for(p=a,i=0;iprintf("%d,",p[i]);
}
10,20,30,40,45,50,60,70,80,90,100,
(3)
#include
structKey
{
char*keyword;
intkeyno;
}
voidmain()
{
structKeykd[3]={{"are",123},{"your",4561},{"my",789}};
structKey*P;
inta;
char*str;
p=kd;
str=p->keyword;
printf("str=%s\n",str);
a=p++->keyno;
printf("a=%d\n",a);
a=p->keyno;
pfintf("a=%d\n",a);
}
str=are
a=123
a=4561
(4)
#include
voidmain()
{
char*pstr[]={"superstar","superstar","superstar","superstar",
"superstar",NULL};
inti;
for(i=0;pstr[i]!
=NULL;i++)
{
printf("%s\n",pstr[i]+i);
}
}
superstar
uperstar
perstar
erstar
rstar
(5)
#include
voidmain()
{
charnn[4][3]={"12","34","56","78"},*pn[4];
intk,s=0;
for(k=0;k<4;k++)
pn[k]=nn[k];
for(k=1;k<4;k+=2)
s=s*10+pn[k][1]-'0';
printf("%d\n",s);
}
48
六、编写程序(编程过程中,不得使用全局变量)
(1)已知五位数a2b3c能被23整除,编程求此五位数。
#include
voidmain()
{
inta,b,c,i;
for(a=1;a<=9;a++)
for(b=0;b<=9;b++)
for(c=0;c<=9;c++)
{
i=a*1000+2*1000+b*100+3*10+c;
if(!
(i%23))
printf("%d\n",i);
}
}
(2)输入一行包含若干单词的字符串,单词之间用空格分开,要求按单词长短从小到大的次序排序后形成新的字符串输出。
(假定字符串中单词个数不超过20个,字符中输入并形成单词序列、单词排序、排序后的单词形成新串并输出,要求用不同的函数实现,编写主函数完成上述函数的调用)
#include
#include
#defineN20
voidinput(char*str[],int*n);
voidsort(char*str[],intn);
voidmerge(char*str[],intn);
voidmain()
{
charstr[N][30];
char*pstr[N];
intn,i;
for(i=0;ipstr[i]=str[i];
input(pstr,&n);
sort(pstr,n);
merge(pstr,n);
}
voidinput(char*str[],int*n)
{
chars[256];
char*p=s,*pstr;
inti=0;
pstr=str[0];
printf("inputstrings\n");
gets(s);
while(*p++=='');
p--;
while(*p)
{
*pstr++=*p++;
if(*p=='')
{
i++;
*pstr=0x00;
pstr=str[i];
while(*p++=='');
p--;
}
}
*pstr=0x00;
*n=i+1;
}
voidsort(char*str[],intn)
{
inti,j;
char*temp;
for(i=0;i{
for(j=i+1;j{
if(strlen(str[i])>strlen(str[j]))
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
}
}
voidmerge(char*str[],intn)
{
chars[256];
char*p=s,*pstr;
inti;
for(i=0;i{
pstr=str[i];
while(*pstr)
*p++=*pstr++;
*p++='';
}
*p=0x00;
puts(s);
}
(3)请编写程序,主函数中输入一行字符串,内有数字字符和非数字字符,调用函数(自己定义及实现的函数)求该字符串中数字子串中最小的数字,并在主函数中显示最小的数字。
限定该字符串中数字子串最多不超过20个。
如字符串“a1236345.6×876.176t”,该字符串中含有数字子串最小的数字是876.176。
#include
#include
#include
floatmindigit(char*str);
voidmain()
{
charstr[256];
gets(str);
printf("Theminiumdigitis%f\n",mindigit(str));
}
floatmindigit(char*str)
{
chars[20];
char*p;
floatmint,t;
intflag=0,i;
while(*str!
='\0')
{
while(*str>='0'&&*str<='9')
{
p=str+1;
s[0]=*str;
i=1;
while((*p>='0'&&*p<='9')||*p=='.')
{
s[i]=*p;
p++;
i++;
}
s[i]=0x00;
if(flag==0)
{
mint=atof(s);
flag=1;
}
else
{
t=atof(s);
if(mint>t)
mint=t;
}
str=p;
}
str++;
}
returnmint;
}
(4)一个公司,有若干名员工,每名员工有姓名,性别,工龄,工资等信息,编程输入并建立员工档案信息和便于发放的各种钞票数(工资为整数,发放的工资各种钞票限定为100元,50元,20元,10元,5元,1元,发放的钞票数张数要求最少),要求输出工龄大于20年,工资高于5000元的所有男员工信息和工资发放的各种钞票数。
(要求输入和输出功能用不同的函数实现,编写主函数完成上述函数的调用)
#include
#defineN10
structEmployee
{
charname[20];
charsex;
intyear;
intwage;
intmoney[6];
};
voidinput(structEmployee*person,int*n,int*deno);
voidoutput(structEmployee*person,intn,int*deno);
voidmain()
{
structEmployeeperson[N];
intn;
intdeno[6]={100,50,20,10,5,1};
input(person,&n,deno);
output(person,n,deno);
}
voidinput(structEmployee*person,int*n,int*deno)
{
inti,j,w;
printf("Eneterthenumberofperson:
\n");
scanf("%d",n);
for(i=0;i<*n;i++,person++)
{
printf("namesexyear