第1部分 程序改错题Word格式文档下载.docx
- 文档编号:17592145
- 上传时间:2022-12-07
- 格式:DOCX
- 页数:33
- 大小:31.78KB
第1部分 程序改错题Word格式文档下载.docx
《第1部分 程序改错题Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《第1部分 程序改错题Word格式文档下载.docx(33页珍藏版)》请在冰豆网上搜索。
do{
scanf("
%d"
&
x[i]);
}while(x[i++]!
=0);
%f\n"
fun(x));
第2题
学生花名册可以用结构体型链表存放。
结构中包含成员学号(intxh)、姓名(charxm[8])、性别(charsex)、班级号(intbj)、成绩(intcj),sex='
m'
表示男学生,'
w'
为女学生,班级序号bj为1至9。
该结构已用typedef定义为数据类型STUDENT。
给定程序FILL0.C就建立了这样一个链表,函数ftotal()的功能是:
统计各班的平均成绩。
请改正函数ftotal中的错误,使它能计算出正确的结果。
alloc.h>
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;
if(b[i])a[i]=a[i]/b[i];
elsea[i]=0;
voidfinput(structSTUDENT*head)
printf("
\nPleasenputthescoreofclass%dthestudent%s:
p->
bj,p->
xm);
p->
cj);
structSTUDENT*fsetup()
structSTUDENT*h,*p,*p1;
h=NULL;
p1=NULL;
while
(1)
p=(structSTUDENT*)malloc(sizeof(structSTUDENT));
%ld%s%c%d"
xh,p->
xm,&
sex,&
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;
returnh;
print(structSTUDENT*head)
%08ld,%-8s,%c,%1d,%3d\n"
xm,p->
sex,p->
\n"
structSTUDENT*h;
inta[9],i;
h=fsetup();
print(h);
finput(h);
ftotal(h,a);
for(i=0;
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中的语法错误,使它能计算出正确的结果。
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;
intn=15;
Thevalueis%ld\n"
fun(n));
NONO();
NONO()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件*/
FILE*rf,*wf;
inti,n;
rf=fopen("
gc01.in"
"
r"
wf=fopen("
gc01.out"
w"
10;
fscanf(rf,"
%d,"
n);
fprintf(wf,"
%ld\n"
fclose(rf);
fclose(wf);
第4题
给定程序FILL1.C中函数scmp的功能是比较两个给出的C字符串。
按比较结果,函数将返回如下函数值:
若:
s1=s2返回0
s1<
s2返回负值
s1>
s2返回正值
请改正函数scmp中的语法和逻辑错误,使它能计算出正确的结果。
#include<
stdlib.h>
#defineN40
scmp(char*s1,char*s2)
while(*s1)
if(s1!
=s2)
return(*s1-*s2);
else{
(*s1)++;
s2++;
return(*s1);
charstr1[]="
china"
str2[]="
chiness"
str3[]="
str4[]="
chinaren"
;
theresultforcompare%sand%sis%d\n"
str1,str2,scmp(str1,str2));
str2,str3,scmp(str2,str3));
str1,str3,scmp(str1,str3));
str3,str4,scmp(str3,str4));
charstr1[N],str2[N];
tk01.in"
tk01.out"
3;
%s%s"
str1,&
str2);
%d\n"
scmp(str1,str2));
第5题
给定程序MODI2.C中函数fun的功能是:
通过某种程度方式实现两个变量的值交换的操作,例如变量a中的值原为8,b中的值原为3,程序运行后变量a中的值为3,b中的值原为8。
请改正函数fun中的错误,使它能计算出正确的结果。
intfun(int*x,inty)
intt;
t=x;
x=y;
return(y);
inta=3,b=8;
%d%d\n"
a,b);
b=fun(&
a,b);
第6题
给定程序FILL2.C中函数printd(intn)的功能是以十进制数形式打印出任int型数。
请改正函数printd中的语法和逻辑错误,使它能计算出正确的结果。
下面函数的功能是以十进制数形式打印出任int型数。
请填入适当语句完善之。
#include"
stdio.h"
printd(intn)
inti;
if(n<
0){
n=-n;
+"
if(i=n/10!
=0)
printd(i);
putchar(n+'
0'
);
printd(12345);
printd(-32091);
printd(0);
printd(-2);
第7题
给定程序MODI3.C中函数fun的功能是:
计算并输出k以内最大的10个能被13或17整除的自然数之和。
k的值由主函数传入,若k的值为500,则函数值为4622。
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;
%d\n"
fun(500));
第8题
给定程序FILL3.C的程序建立一个链表,函数finsert(structnode*head,intx)的功能是:
在链表头部插入一个结点,数据域的值为x,并返回新链表的头指针主函数。
该函数的第一个参数是链表的首指针。
请改正函数finsert中的错误,使它能计算出正确的结果。
[程序]
structnode{
intdata;
structnode*next;
structnode*finsert(structnode*head,intx)
structnode*p;
p=malloc(sizeof(structnode*));
p->
data=x;
next=head;
returnhead;
print(structnode*head)
data);
structnode*h=NULL,*p,*p1;
inta,i;
a=rand()%1000;
h=finsert(h,a);
第9题
给定程序MODI4.C中函数fun的功能是:
读入一个整数m,计算如下公式的值。
t=1-
例如:
若输入5,则应输出-0.283333。
请改正函数fun中的错误,使它能计算出正确的结果。
注意:
doublefun(intm)
doublet=1.0;
for(i=2;
=m;
t=1.0-1/i;
t;
intm;
\nPleaseenter1integernumbers:
scanf("
m);
\ntheresultis%lf\n"
fun(m));
intj,n;
gc04.in"
gc04.out"
for(j=1;
j<
=10;
j++)
fprintf(wf,"
%lf\n"
第10题
给定程序FILL3.C的程序首先建立一个链表,函数fmax()的功能是:
求出链表所有结点中,数据域值最大的结点的值,并由参数s返回给主函数。
请改正函数fmax中的错误,使它能计算出正确的结果。
voidfmax(structnode*head,structnode**s)
*s=p;
if(p){
return;
while(p==NULL){
if(p->
data>
s->
data)s=p;
structnode*h=0,*p,*p1;
p=(structnode*)malloc(sizeof(structnode));
data=a;
if(h==0){h=p;
else{p1->
next=0;
fmax(h,&
p);
if(p)printf("
Maxdatais:
第11题
给定程序MODI5.C中函数fun的功能是:
用于计算S=f((f(-1.0)+f(5.0)))的值。
当程序正确时,函数值应为:
2488.00000。
设
doublefun(doublex)
doubley;
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
f(f(-1.0)+f(5.0)));
第12题
给定程序FILL3.C的程序首先建立一个链表,函数fdelete()的功能是:
删除链表中数据域值为x的结点,并将链表的首指针返回给主函数。
structnode*fdelete(structnode*head,intx)
structnode*p,*q;
q=p;
while(p!
=NULL){
data=x)
if(p==head)
{
head;
p;
q=p;
}else{
q;
p=p->
}
}else{
q;
p;
p=(structnode*)malloc(sizeof(structnode));
returnp;
structnode*h=NULL;
Pleaseinputthedeletenode:
scanf("
a);
h=fdelete(h,a);
afterdeletethenodeis:
inti,a;
structnode*h=NULL,*p;
gc05.in"
if(rf==NULL)
数据文件gc05.in不存在!
p=h;
fprintf(wf,"
fscanf(rf,"
gc05.out"
第13题
给定程序MODI6.C中函数fun的功能是:
读入一个整数m,计算如下公式的值.
s=1-
例如:
若输入5,则应输出0.783333。
inti,j=1;
j;
t=1.0/i;
returnt;
\nPleaseenter1integernumber:
\nTheresultis%lf\n"
{/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第1部分 程序改错题 部分 程序 改错
![提示](https://static.bdocx.com/images/bang_tan.gif)