C编程不能结束循环

时间:2016-12-09 17:48:19

标签: c loops math codeblocks

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

#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;
}

1 个答案:

答案 0 :(得分:0)

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