程序改错最终修订版Word文档格式.docx

上传人:b****5 文档编号:21719168 上传时间:2023-01-31 格式:DOCX 页数:97 大小:39.31KB
下载 相关 举报
程序改错最终修订版Word文档格式.docx_第1页
第1页 / 共97页
程序改错最终修订版Word文档格式.docx_第2页
第2页 / 共97页
程序改错最终修订版Word文档格式.docx_第3页
第3页 / 共97页
程序改错最终修订版Word文档格式.docx_第4页
第4页 / 共97页
程序改错最终修订版Word文档格式.docx_第5页
第5页 / 共97页
点击查看更多>>
下载资源
资源描述

程序改错最终修订版Word文档格式.docx

《程序改错最终修订版Word文档格式.docx》由会员分享,可在线阅读,更多相关《程序改错最终修订版Word文档格式.docx(97页珍藏版)》请在冰豆网上搜索。

程序改错最终修订版Word文档格式.docx

输出"

FACED"

#include<

string.h>

voidfun(chartt[])

{inti;

for(i=0;

tt[i];

i++)

/***********ERROR***********/

if((tt[i]>

)||(tt[i]<

z'

))

tt[i]+=32;

chartt[81];

printf("

\nPleaseenterastring:

"

gets(tt);

fun(tt[81]);

输出变化后的字符串:

puts(tt);

}

参考答案if((tt[i]>

)&

(tt[i]<

tt[i]-=32;

fun(tt);

【程序改错】功能:

读入一行英文文本,将其中每个单词的最后一个字母改成

大写,然后输出此文本行(这里的“单词”是指由空格

隔开的字符串)。

#include"

conio.h"

stdio.h"

ctype.h"

string.h"

up1st(char*p)

integerk=0;

for(;

*p;

p++)

if(k)

if(*p='

'

k=0;

*(p-1)=toupper(*(p-1);

elseif(*p!

='

)k=1;

*(p-1)=toupper(*(p-1));

main()

charchrstr[81];

printf("

"

);

gets(chrstr);

\n\nBeforechanging:

\n%s"

chrstr);

up1st(chrstr);

\nAfterchanging:

【1】intk=0;

【2】if(*p=='

【3】*(p-1)=toupper(*(p-1));

输入一行英文文本,将每一个单词的第一个字母变成大

写。

conio.h>

fun(charp)

intk=0;

dowhile(*p=='

\0'

if(k==0&

*p!

*p=toupper(*p);

k=0;

elseif(*p!

k=1;

else

*p+;

charstr[81];

printf("

pleaseinputaEnglishtextline:

gets(str);

Theoriginaltextlineis:

puts(str);

fun(str);

Thenewtextlineis:

【参考答案】fun(char*p)

while(*p)

k=1;

p++;

任意输入一个字符,如果是小写字母以大写字母输出,

是大写字母以小写字母输出;

否则,按原样输出。

程序中共有4条错误语句,请改正错误。

charch;

Enterch:

ch=getchar(r);

if(ch>

=a&

ch<

=z)

putchar(ch-32);

/**********ERROR**********/

elseif(ch>

=A&

=Z)

putchar(ch+32);

elseputchar();

【参考答案】ch=getchar();

if(ch>

elseif(ch>

=65&

=90)

elseputchar(ch);

计算正整数num的各位上的数字之积。

输入252,则输出应该是20。

longfun(longnum)

longk;

do

k*=num%10;

num\=10;

while(num);

returnk;

longn;

\nPleaseenteranumber:

scanf("

%ld"

n);

\n%ld\n"

fun(longn));

【1】longk=1;

【2】num/=10;

【3】scanf("

&

n);

【4】printf("

fun(n));

求1到10的阶乘的和。

inti;

floats=0;

floatfac(intn);

/**********ERROR**********/

for(i=1;

i<

10;

s=fac(i);

%f\n"

s);

floatfac(intn)

inty=1;

for(i=1;

=n;

y=y*i;

return;

答案for(i=1;

=10;

s=s+fac(i);

floaty=1.0;

returny;

求1到20的阶乘之和。

fun()

{

intn,j;

floats=0.0,t=1.0;

for(n=1;

n<

=20;

n++)

{

s=1;

for(j=1;

j<

j++)

t=t*n;

s+t=s;

jiecheng=%d\n"

fun();

答案:

t=1;

t=t*j;

s=s+t;

jiecheng=%f\n"

实现两个字符串的连接。

#include"

chars1[80],s2[80];

voidscat(chars1[],chars2[]);

gets(s1);

gets(s2);

scat(s1,s2);

puts(s1);

voidscat(chars1[],chars2[])

inti=0,j=0;

while(s1[i]=='

)i++;

while(s2[j]=='

s2[j]=s1[i];

i++;

j++;

s2[j]='

【1】while(s1[i]!

)i++;

【2】while(s2[j])

【3】s1[i]=s2[j];

【4】s1[i]='

比较两个字符串的长度,输出长度大的字符串。

chara[80],b[80];

inti,j;

给字符串a赋值:

\n"

gets(a);

给字符串b赋值:

gets(b);

输出原字符串a:

puts(a);

输出原字符串b:

puts(b);

a[i]!

for(j=0;

b[j]!

%d,%d"

i,j);

if(i<

j)

puts(a);

else

puts(b);

答案for(i=0;

0\'

i++);

j++);

if(i>

j)

生成一个周边元素为5,其它元素为1的3*3的二维数组。

voidfun(intarr[][3])

inti,j

3;

i++)

for(j=0;

if(i==0&

j==0||i==2||j==2)

arr[i][j]=5;

else

arr[i][j]=1;

inta[3][3],i,j;

fun(a);

for(i=0;

%d"

a[i][j]);

【1】inti,j;

【2】for(i=0;

【3】if(i==0||j==0||i==2||j==2)

用下面的和式求圆周率的近似值。

直到最后一项的绝对值

小于等于0.0001。

stdlib.h>

inti=1;

ints=0,t=1,p=1;

while(fabs(t)<

=1e-4)

p=-p;

i=i+2;

t=p/i;

pi=%d\n"

s*4);

答案#include<

math.h>

floats=0,t=1,p=1;

while(fabs(t)>

1e-4)

pi=%f\n"

s*4);

在一个一维整型数组中找出其中最大的数及其下标。

#defineVSIZE20

intvector[VSIZE]

intfun(intlist[],intsize)

intmax,i,j=0;

max=list[i];

size;

if(max>

list[i]){max=list[i];

j=i;

returnj;

VSIZE;

{vector[i]=rand();

if(i%5==0)printf("

%6d,"

vector[i]);

i=intfun(vector,VSIZE);

\nMininum:

Vector[%d]=%6d\n"

i,vector[i]);

【1】intvector[VSIZE];

【2】max=list[0];

【3】if(max<

【4】i=fun(vector,VSIZE);

找出一个大于给定整数m且紧随m的素数,并作为函数值返回。

intfun(intm)

k;

for(i=m+1;

for(k=2;

k<

i;

k++)

if(i%k!

=0)

break;

if(k=i)

return(i);

intn;

\nPleaseentern:

%d"

%d\n"

答案inti,k;

if(i%k==0)

if(k==i)

找出大于m的最小素数,并将其作为函数值返回。

math.h"

intfun(intm)

inti,k;

for(k=1;

=0)break;

if(k<

i)

voidmain()

从键盘输入十个学生的成绩,统计最高分,最低分和平均分。

max代表最高分,min代表最低分,avg代表平均分。

/***********ERROR***********/

floata[8],min,max,avg;

input10score:

=9;

%f"

a);

}

max=min=avg=a[1];

if(min<

a[i])

min=a[i];

if(max<

a[i])

max=a[i];

avg=avg+a[i];

avg=avg/10;

max:

%f\nmin:

%f\navg:

max,min,avg);

答案floata[10],min,max,avg;

a[i]);

max=min=avg=a[0];

if(min>

给定n个实数,输出平均值,并统计在平均值以下(含平

均值)的实数个数。

n=6时,输入23.5,45.67,12.1,6.4,58.9,98.4所得

平均值为40.,在平均值以下的实数个数应为3。

intfun(doublex[],intn)

intj,c=0;

doublej=0;

xa+=x[j];

xa=xa/n;

ave=%f\n"

xa);

if(x[j]<

=xa)c++;

returnxa;

doublex[]={23.5,45.67,12.1,6.4,58.9,98.4};

fun(x,6));

答案floatxa=0;

n;

returnc;

【程序改错】

intfun(char*s,char*t1,char*t2,char*w)

char*p,*r,*a;

strcpy(w,s);

while(*w)

p=w;

r=t1;

while(*r)

if(*r==*p)

r++;

if(*r=='

a=w;

r=t2;

while(*r)

*a=*r;

a++;

w+=strlen(t2);

w++;

chars[100],t1[100],t2[100],w[100];

\nPleaseenterstringS:

%s"

s);

\nPleaseentersubstringt1:

t1);

\nPleaseentersubstringt2:

t2);

if(strlen(t1)==strlen(t2))

fun(s,t1,t2,w);

\nTheresultis:

%s\n"

w);

Error:

strlen(t1)!

=strlen(t2)\n"

if(i%k==0)

if(k==i)

%d\

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 工学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1