第1部分 程序改错题.docx
《第1部分 程序改错题.docx》由会员分享,可在线阅读,更多相关《第1部分 程序改错题.docx(33页珍藏版)》请在冰豆网上搜索。
第1部分程序改错题
第1部分程序改错题
第1题
在主函数中从键盘输入若干个数放入数组中,输入0结束输入并放在最后一个元素中。
给定程序MODI0.C中函数fun的功能是:
计算数组中值为正数的平均值(不包括0)。
例如:
数组中元素的值依次为:
39,-47,21,2,-8,15,0,则程序的运行结果为:
19.25。
请改正函数fun中的错误,使它能输出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
doublefun(intx[])
{
intsum=0.0;
intc=0,i=0;
while(x[i]!
=0)
{
if(x[i]>0)
{
sum+=x[i];
c++
}
i++;
}
sum\=c;
returnsum;
}
main()
{
intx[1000];inti=0;
clrscr();
printf("\nPleaseentersomedata(endwith0):
");
do{
scanf("%d",&x[i]);
}while(x[i++]!
=0);
printf("%f\n",fun(x));
}
第2题
学生花名册可以用结构体型链表存放。
结构中包含成员学号(intxh)、姓名(charxm[8])、性别(charsex)、班级号(intbj)、成绩(intcj),sex='m'表示男学生,'w'为女学生,班级序号bj为1至9。
该结构已用typedef定义为数据类型STUDENT。
给定程序FILL0.C就建立了这样一个链表,函数ftotal()的功能是:
统计各班的平均成绩。
请改正函数ftotal中的错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
typedefstructSTUDENT{
longxh;
charxm[8];
charsex;
intbj;
intcj;
structSTUDENT*next;
};
voidftotal(structSTUDENT*head,inta[])
{
structSTUDENT*p;
intb[9],i;
p=head;
for(i=0;i<9;i++)
{
a[i]=0;
b[i]=0;
}
while(p)
{
a[p->bj-1]=p->cj;
b[p->bj-1]=1;
p=p->next;
}
for(i=0;i<9;i++)
if(b[i])a[i]=a[i]/b[i];
elsea[i]=0;
}
voidfinput(structSTUDENT*head)
{
structSTUDENT*p;
p=head;
while(p)
{
printf("\nPleasenputthescoreofclass%dthestudent%s:
",p->bj,p->xm);
scanf("%d",&p->cj);
p=p->next;
}
}
structSTUDENT*fsetup()
{
structSTUDENT*h,*p,*p1;
h=NULL;
p1=NULL;
while
(1)
{
p=(structSTUDENT*)malloc(sizeof(structSTUDENT));
scanf("%ld%s%c%d",&p->xh,p->xm,&p->sex,&p->bj);
p->cj=0;
if(p->xh==0)
{
free(p);
if(p1)p1->next=NULL;
break;
}else{
if(h==0){h=p;p1=p;}
else{p1->next=p;p1=p;}
}
}
returnh;
}
print(structSTUDENT*head)
{
structSTUDENT*p;
p=head;
while(p)
{
printf("%08ld,%-8s,%c,%1d,%3d\n",p->xh,p->xm,p->sex,p->bj,p->cj);
p=p->next;
}
printf("\n");
}
main()
{
structSTUDENT*h;
inta[9],i;
h=fsetup();
print(h);
finput(h);
print(h);
ftotal(h,a);
for(i=0;i<9;i++)
if(a[i])printf("Thescoreofclass%dis%d\n",i+1,a[i]);
第3题
给定程序MODI1.C中函数fun的功能是:
求广义菲玻那级数的第n项。
1,1,1,3,5,9,17,31,.....项值通过函数值返回main()函数。
例如:
若n=15,则应输出:
2209。
请改正函数fun中的语法错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
longfun(intn)
{
longa=1,b=1,c=1,d=1,k;
For(k=4;k<=n;k++)
{
d=a+b+c;
a=b;b=c;c=d
}
returnd;
}
main()
{
intn=15;
clrscr();
printf("Thevalueis%ld\n",fun(n));
NONO();
}
NONO()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件*/
FILE*rf,*wf;
inti,n;
rf=fopen("gc01.in","r");
wf=fopen("gc01.out","w");
for(i=0;i<10;i++)
{
fscanf(rf,"%d,",&n);
fprintf(wf,"%ld\n",fun(n));
}
fclose(rf);
fclose(wf);
}
第4题
给定程序FILL1.C中函数scmp的功能是比较两个给出的C字符串。
按比较结果,函数将返回如下函数值:
若:
s1=s2返回0
s1s1>s2返回正值
请改正函数scmp中的语法和逻辑错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
#include
#defineN40
scmp(char*s1,char*s2)
{
while(*s1)
if(s1!
=s2)
return(*s1-*s2);
else{
(*s1)++;
s2++;
}
return(*s1);
}
main()
{
charstr1[]="china",str2[]="chiness",str3[]="china",str4[]="chinaren";
printf("theresultforcompare%sand%sis%d\n",str1,str2,scmp(str1,str2));
printf("theresultforcompare%sand%sis%d\n",str2,str3,scmp(str2,str3));
printf("theresultforcompare%sand%sis%d\n",str1,str3,scmp(str1,str3));
printf("theresultforcompare%sand%sis%d\n",str3,str4,scmp(str3,str4));
NONO();
}
NONO()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件*/
FILE*rf,*wf;
inti,n;
charstr1[N],str2[N];
rf=fopen("tk01.in","r");
wf=fopen("tk01.out","w");
for(i=0;i<3;i++)
{
fscanf(rf,"%s%s",&str1,&str2);
fprintf(wf,"%d\n",scmp(str1,str2));
}
fclose(rf);
fclose(wf);
}
第5题
给定程序MODI2.C中函数fun的功能是:
通过某种程度方式实现两个变量的值交换的操作,例如变量a中的值原为8,b中的值原为3,程序运行后变量a中的值为3,b中的值原为8。
请改正函数fun中的错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
intfun(int*x,inty)
{
intt;
t=x;x=y;
return(y);
}
main()
{
inta=3,b=8;
clrscr();
printf("%d%d\n",a,b);
b=fun(&a,b);
printf("%d%d\n",a,b);
}
第6题
给定程序FILL2.C中函数printd(intn)的功能是以十进制数形式打印出任int型数。
请改正函数printd中的语法和逻辑错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
下面函数的功能是以十进制数形式打印出任int型数。
请填入适当语句完善之。
#include"stdio.h"
printd(intn)
{
inti;
if(n<0){
n=-n;
printf("+");
}
if(i=n/10!
=0)
printd(i);
putchar(n+'0');
}
main()
{
printd(12345);
printf("\n");
printd(-32091);
printf("\n");
printd(0);
printf("\n");
printd(-2);
printf("\n");
}
第7题
给定程序MODI3.C中函数fun的功能是:
计算并输出k以内最大的10个能被13或17整除的自然数之和。
k的值由主函数传入,若k的值为500,则函数值为4622。
请改正函数fun中的错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
intfun(intk)
{
intm=0,mc=0,j,n;
while((k>=2)&&mc<10))
{
if((k%13=0)||(k%17=0))
{m=m+k;mc++;}
k--;
}
returnm;
main()
{
clrscr();
printf("%d\n",fun(500));
}
第8题
给定程序FILL3.C的程序建立一个链表,函数finsert(structnode*head,intx)的功能是:
在链表头部插入一个结点,数据域的值为x,并返回新链表的头指针主函数。
该函数的第一个参数是链表的首指针。
请改正函数finsert中的错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
[程序]
#include
#include
structnode{
intdata;
structnode*next;
};
structnode*finsert(structnode*head,intx)
{
structnode*p;
p=malloc(sizeof(structnode*));
p->data=x;
p->next=head;
returnhead;
}
print(structnode*head)
{
structnode*p;
p=head;
while(p)
{
printf("%d\n",p->data);
p=p->next;
}
printf("\n");
}
main()
{
structnode*h=NULL,*p,*p1;
inta,i;
for(i=0;i<10;i++)
{
a=rand()%1000;
h=finsert(h,a);
}
print(h);
}
第9题
给定程序MODI4.C中函数fun的功能是:
读入一个整数m,计算如下公式的值。
t=1-
例如:
若输入5,则应输出-0.283333。
请改正函数fun中的错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
doublefun(intm)
{
doublet=1.0;
inti;
for(i=2;i<=m;i++)
t=1.0-1/i;
t;
}
main()
{
intm;
clrscr();
printf("\nPleaseenter1integernumbers:
\n");
scanf("%d",&m);
printf("\ntheresultis%lf\n",fun(m));
NONO();
}
NONO()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件*/
FILE*rf,*wf;
intj,n;
rf=fopen("gc04.in","r");
wf=fopen("gc04.out","w");
for(j=1;j<=10;j++)
{
fscanf(rf,"%d",&n);
fprintf(wf,"%lf\n",fun(n));
}
fclose(rf);
fclose(wf);
}
第10题
给定程序FILL3.C的程序首先建立一个链表,函数fmax()的功能是:
求出链表所有结点中,数据域值最大的结点的值,并由参数s返回给主函数。
该函数的第一个参数是链表的首指针。
请改正函数fmax中的错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
structnode{
intdata;
structnode*next;
};
voidfmax(structnode*head,structnode**s)
{
structnode*p;
p=head;*s=p;
if(p){
return;
}
while(p==NULL){
if(p->data>s->data)s=p;
p=p->next;
}
printf("\n");
}
print(structnode*head)
{
structnode*p;
p=head;
while(p)
{
printf("%d,",p->data);
p=p->next;
}
printf("\n");
}
main()
{
structnode*h=0,*p,*p1;
inta,i;
for(i=0;i<10;i++)
{
a=rand()%1000;
p=(structnode*)malloc(sizeof(structnode));
p->data=a;
if(h==0){h=p;p1=p;}
else{p1->next=p;p1=p;}
}
p->next=0;
print(h);
fmax(h,&p);
print(h);
if(p)printf("Maxdatais:
%d\n",p->data);
}
第11题
给定程序MODI5.C中函数fun的功能是:
用于计算S=f((f(-1.0)+f(5.0)))的值。
当程序正确时,函数值应为:
2488.00000。
设
请改正函数fun中的错误,使它能输出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
doublefun(doublex)
{
doubley;
inti;
If(x<=2.0)
y=2.0*x*x*+3.0*x+4.0;
else
y=-2.0*x*x+3.0*x-4.0;
returny
}
main()
{
clrscr();
printf("%f\n",f(f(-1.0)+f(5.0)));
}
第12题
给定程序FILL3.C的程序首先建立一个链表,函数fdelete()的功能是:
删除链表中数据域值为x的结点,并将链表的首指针返回给主函数。
请改正函数fmax中的错误,使它能计算出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
structnode{
intdata;
structnode*next;
};
structnode*fdelete(structnode*head,intx)
{
structnode*p,*q;
p=head;q=p;
while(p!
=NULL){
if(p->data=x)
{
if(p==head)
{
head;
p;
q=p;
}else{
q;
p=p->next;
}
}else{
q;
p;
}
}
returnhead;
}
structnode*finsert(structnode*head,intx)
{
structnode*p;
p=(structnode*)malloc(sizeof(structnode));
p->data=x;
p->next=head;
returnp;
}
print(structnode*head)
{
structnode*p;
p=head;
while(p)
{
printf("%d,",p->data);
p=p->next;
}
printf("\n");
}
main()
{
structnode*h=NULL;
inta,i;
for(i=0;i<10;i++)
{
a=rand()%1000;
h=finsert(h,a);
}
print(h);
printf("Pleaseinputthedeletenode:
\n");
scanf("%d",&a);
h=fdelete(h,a);
printf("afterdeletethenodeis:
\n");
print(h);
NONO();
}
NONO()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件*/
FILE*rf,*wf;
inti,a;
structnode*h=NULL,*p;
rf=fopen("gc05.in","r");
if(rf==NULL)
{
printf("数据文件gc05.in不存在!
");
return;
}
for(i=0;i<10;i++)
{
fscanf(rf,"%d",&a);
h=finsert(h,a);
}
p=h;
while(p)
{
fprintf(wf,"%d,",p->data);
p=p->next;
}
fprintf(wf,"\n");
fscanf(rf,"%d",&a);
h=fdelete(h,a);
fscanf(rf,"%d",&a);
h=fdelete(h,a);
fscanf(rf,"%d",&a);
h=fdelete(h,a);
fclose(rf);
wf=fopen("gc05.out","w");
p=h;
while(p)
{
fprintf(wf,"%d,",p->data);
p=p->next;
}
fprintf(wf,"\n");
fclose(wf);
}
第13题
给定程序MODI6.C中函数fun的功能是:
读入一个整数m,计算如下公式的值.
s=1-
例如:
若输入5,则应输出0.783333。
请改正函数fun中的错误,使它能输出正确的结果。
注意:
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
doublefun(intm)
{
doublet=1.0;
inti,j=1;
for(i=2;i<=m;i++)
{
j;
t=1.0/i;
}
returnt;
}
main()
{
intm;
clrscr();
printf("\nPleaseenter1integernumber:
");
scanf("%d",&m);
printf("\nTheresultis%lf\n",fun(m));
NONO();
}
NONO()
{/