gpt4 book ai didi

C语言程序无法结束循环

转载 作者:行者123 更新时间:2023-11-30 19:36:19 24 4
gpt4 key购买 nike

我正在编写一段代码,用户输入起始值、最大值和步长值。我计算的结果是正确的,所有的公式都是正确的,但是有一个问题。我无法停止循环以便正确显示我的答案。当我运行时,它只是启动循环,然后我在屏幕上收到大量垃圾答案。

#include <stdio.h>
#include <math.h>

float funqcia(float x, float y, float k);

int main()
{
float y;
float A;
float B;
float H;
float x;
float t;
int e=0;
int N=15;
int k = 0;
while(e==0)
{
printf("enter starting value here: ");
scanf("%fl",&A);
printf("enter maximum value: ");
scanf("%fl", &B);
printf("enter step: ");
scanf("%fl", &H);
if(B > A)
{
e=1;
}
else
{
printf("Sorry, stopping value should be higher than starting value.");
}
}
while(e==1)
{
printf("%s","_________________________________________________");
printf("%s","\n");
printf("%s","| |");
printf("%s","\n");
printf("%s","| Results are shown below |");
printf("%s","\n");
printf("%s","| |");
printf("%s","\n");
printf("%s","--------------------------------------------------");
printf("%s","\n");
t=0;
/*for(int i=0;i<N;i++)
{
if(i>0)
{
if (B<=y)
{
break;
}
t=pow(2,i)*pow(H,i-1);
x=x+t;
printf("%s","| ");
printf("%fl",x);
printf("%s"," | ");
funqcia(x,y);
printf("%s","\n");
}
else
{
if (B<=y)
{
break;
}
x=A;
printf("%s","| ");
printf("%fl",x);
printf("%s"," | ");
funqcia(x,y);
printf("%s","\n");
}
}*/
for (int i=0;i<N;i++)
{
t=pow(2,i)*pow(H,i-1);
x=x+t;
if (i == 0)
{
x=A;
}
y=funqcia(x,y,k);
if (B<=k)
{
break;
}
else
{
printf("x=%fl\ty=%fl", x,y);
//}
}
}
}
}

float funqcia(float x, float y, float k)
{
y=((3+exp(x-1))/(1+pow(x,2)*(3-tan(x))));
if(isinf(y))
{
printf("this is infinity");
}
else
{
printf("y: %fl",y);
}
k=y;
return k;
}

最佳答案

您似乎在程序开始时将变量 e 初始化为 0。你可以检查 while(e==0),当 B>A 条件为 true 时,e 现在变为 1。接下来你有 while(e==1) 和一些代码来显示计算和结果,这里 e 的值永远不会从 1 变为其他值。由于它始终为 1,因此 while 条件会发出绿色信号以继续循环直到无穷大,并以某种方式弄乱屏幕上的结果。

关于C语言程序无法结束循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41066102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com