C语言笔试程序改错题Word下载.docx
《C语言笔试程序改错题Word下载.docx》由会员分享,可在线阅读,更多相关《C语言笔试程序改错题Word下载.docx(35页珍藏版)》请在冰豆网上搜索。
if(alph(str[i]))
if(flag)
flag=0;
point=i;
len++;
else/*Find2*/
/////////////flag=1;
if(len>
length)
{length=len;
place=point;
len=0;
return(place);
main()
inti;
charline[100];
printf("
Enterstring\n"
);
gets(line);
thelongestis:
"
for(i=longest(line);
alph(line[i]);
%c"
line[i]);
\n"
2下列程序是建立一个包含学生有关数据的单向链表。
但该程序有2个错误,错误出现在
每个注释行附近,请调试改正。
调试改正中,不得改变程序结构,也不得增删语句。
#defineNULL0
structstud
longnum;
charname[10];
floatscore;
structstud*next;
};
/*……………comment……………*/
intn;
structstudcreate()////////structstud*create(void)
structstud*head,*p1,*p2;
n=0;
p1=p2=(structstud*)malloc(sizeof(structstud));
scanf("
%ld,%s,%f"
&
p1->
num,p1->
name,&
score);
/*……………comment……………*/
head=NULL;
while(p1!
=0)//////////while(p1->
num!
=0)
n=n+1;
if(n==1)head=p1;
elsep2->
next=p1;
p1=(structstud*)malloc(size(structstud));
p2->
next=NULL;
return(head);
}
gc56.c程序用于将字符串中的小写字母转换为大写字母,程序中有两处错误,请调试改正之,但不得增删语句.
#include<
stdio.h>
voidmain(void)
{
chars[255];
%s"
s);
for(i=0;
s[i];
if(s[i]>
b'
&
s[i]<
)//////s[i]>
s[i]=s[i]+'
+'
;
//////s[i]=s[i]-'
gc57.c的功能是互换给定数组中的最大数和最小数。
如将下面8个数:
5,3,1,4,2,8,9,6
变成:
5,3,9,4,2,8,1,6
程序中,最大数与最小数的互换操作通过函数调用来实现,让指针max和min分别指向最大数和最小数。
程序有二处错误,请改正。
注意:
不得增行和删行,也不得修改程序的结构。
main()
staticinta[8]={5,3,1,4,2,8,9,6};
voidjhmaxmin();
Originalarray:
\n"
i<
8;
i++)
%5d"
a[i]);
jhmaxmin(a,8);
Arrayafterswapingmaxandmin:
voidjhmaxmin(p,n)
intp,n;
////////int*p,n;
intt,*max,*min,*end,*q;
end=p+n;
max=min=p;
for(q=p+1;
q<
end;
q++)
{if(*q>
*max)max=q;
if(*q<
max)min=q;
////////*q<
*min
t=*max;
*max=*min;
*min=t;
gc101.c的功能为:
输入8个整数,使用指针以选择法对其进行排序(从小到大).程序中有两处错误代码,请改正.注意,不得改变程序结构。
#include<
inta[8],*p=a;
inti,j;
inttempmin,tempcode;
\ninputthenumbers:
%d"
p+i);
tempmin=*p;
7;
for(j=i;
j<
j++)
if(j==i||*(p+j)<
tempmin)
{tempmin=*(p+i);
//////////tempmin=*(p+j);
tempcode=j;
if(tempcode!
=i)
{*(p+tempcode)=*(p+i);
*(p+j)=tempcode;
/////////////*(p+i)=tempmin;
%d,"
*(p+i));
gc137.c中的sub函数的功能是:
将s所指字符串的反序和正序进行连接形成一个新串放在t所指的数组中。
例如,当s所指字符串的内容为“ABCD”时,t所指数组中的内容为“DCBAABCD”。
请改正函数sub中的两个错误,使它能得出正确的结果。
不要改动main函数,不得增行或删行,也不得修改程序的结构。
#include"
stdio.h"
conio.h"
string.h"
voidsub(char*s,char*t)
inti,d;
d=strlen(s);
for(i=1;
d;
i++)///////for(i=0;
d;
t[i]=s[d-1-i];
for(i=0;
i++)
t[d+i]=s[i];
t[2*d]='
/0'
;
/////////t[2*d]='
\0'
chars[100],t[100];
clrscr();
printf("
\nPleaseenterstringS:
"
);
s);
sub(s,t);
\nTheresultis:
%s\n"
t);
gc155.c的功能是从键盘输入一行字符,分别统计其中的字母、数字、空格和其它字符的个数。
但该程序中有2处错误,请调试并改正。
不允许改变程序结构和增删语句。
{intc;
intletter=0,digit=0,space=0,other=0;
Pleaseinputastring:
while(c=getchar()!
\n'
)//////while((c=getchar())!
)
if(c>
||c>
letter++;
elseif(c>
0'
9'
digit++;
elseif(c='
'
)//////////elseif(c=='
space++;
other++;
letter=%d,"
letter);
digit=%d,"
digit);
space=%d,"
space);
other=%d\n"
other);
gc163.c的功能是实现求1000以内的自然数中的完数及其个数(所谓完数,指它恰好等于除它本身之外的因子之和,如:
6=1+2+3,28=1+2+4+7+14),并显示结果。
inti,k,m,n,s,p;
//////////////////p=0
inta[100];
\n10000以内的自然数中的完数有:
for(i=2;
=10000;
{s=0;
m=0;
for(k=1;
k<
=i/2;
k++)
{if(i%k==0)
{s=s+k;
a[m]=k;
m=m+1;
if(s=i)////////if(s==i)
{p=p+1;
\n%d=%d"
s,a[0]);
n=1;
while(n<
m)
{printf("
+%d"
a[n]);
\n总个数为:
%d\n"
p);
gc165.c程序中的函数find_str()用来返回字符串s2在字符串s1中第一次出现的首地址;
如果字符串s2不是s1的子串,则该函数返回空指针NULL。
现在程序中发现有两个错误,错误就出现在那些注释行的附近,请你找出错误并将它们改正过来,要求在调试过程中不允许改变程序结构,更不得增删语句。
include<
string.h>
char*find_str(char*s1,char*s2)
inti,j,ls2;
ls2=strlen(s2);
=strlen(s1)-ls2;