200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > c语言编程函数补充上机题 计算机二级C语言上机操作题及答案(10)

c语言编程函数补充上机题 计算机二级C语言上机操作题及答案(10)

时间:2021-10-24 22:40:20

相关推荐

c语言编程函数补充上机题 计算机二级C语言上机操作题及答案(10)

/********found********/

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

{

inti;

char*p,*r,*a;

strcpy(w,s);

while(*w)

{

p=w;

r=t1;

/********found********/

while(r)

if(*r==*p)

{

r++;

p++;

}

else

{

break;

}

if(*r==’\0’)

{

a=w;

r=t2;

while(*r)

{

*a=*r;

a++;

r++;

}

w+=strlen(t2);

}

else

{

w++;

}

}

}

main()

{

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

printf("\nPleaseenterstrings:");

scanf("%s",s);

printf("\nPleaseentersubstringt1:");

scanf("%s",t1);

printf("\nPleaseentersubstringt2:");

scanf("%s",t2);

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

{

fun(s,t1,t2,w);

printf("\nTheresultis:%s\n",w);

}

else

{

printf("Error:strlen(t1)!=strlen(t2)\n");

}

}

第1处:voidfun(char*s,*t1,*t2,*w)应改为voidfun(char*s,char*t1,char*t2,char*w)

第2处:while(r)应改为while(*r)

编程题

编写函数fun,它的功能是:利用以下所示的简单迭代方法求方程式cos(x)-x=0的一个实根。

迭代步骤如下:

(1)取x1初值为0.0;

(2)x0=x1,把x1的值赋给x0;

(3)x1=cos(x0),求出一个新的x1;

(4)若x0-x1,的绝对值小于0.000001,则执行步骤(5),否则执行步骤(2);

(5)所求x1就是方程cos(x)-x=0的一个实根,作为函数值返回。

程序将输出结果Root=0.739085。

注意:部分源程序给出如下。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

试题程序:#include#include#includefloatfun()

{

}

main()

{

FILE*out;

floatf=fun();

printf("Root=%f\n",f);

out=fopen("out.dat","w");

fprintf(out,"%f",f);

fclose(out);

}

答案是:

floatfun()

{

floatx1=0.0,x0;

do

{

x0=x1;

x1=cos(x0);

}

while(fabs(x0-x1)>=le-6);

returnx1;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。