二次方程式无法正常工作

时间:2016-01-22 01:38:38

标签: c function

我是这个网站和C编程的新手,我试图制作一个二次公式,但我无法找到工作的根源。我相信我不是在调用函数,也可能是其他错误。任何帮助将不胜感激,谢谢!

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

float userinput(char prompt[]); //Function Prototype
float root(float a, float b, float c);

int main()
{

    float a,b,c;

    a=userinput("Enter the value for a:"); //Function Call
    b=userinput("Enter the value for b:");
    c=userinput("Enter the value for c:");

    printf("The Equation you entered is |n%fx^2%+fx%+f=0", a, b, c);
    return 0;
}

float root(float a, float b, float c)
{
    float D,x,x1,x2,x3,x4;
    D = b*b - 4*a*c;
    if(D>0)
        {
            printf("There are two real roots, the roots are: ");
            x1 = ((-b+(sqrt(D)))/(2*a));
            x2 = ((-b-(sqrt(D)))/(2*a));
            printf("%.2f and %.2f,x1 , x2");
        }

    if(D==0)
        {
            printf("There is one real root, the root is: ");
            x = ((-b)/(2*a));
            printf("%.2f,x");
        }

    if(D<0)
        {
            printf("There are two imaginary roots. The roots are: ");
            x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
            printf("%.2f,x3i and");
            x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
            printf("%.2f,x4i");
        }
}

float userinput(char prompt[]) //Function definition
{
    float answer;
    int status;
    do
        {
            printf("%s",prompt);

            status=scanf("%f", &answer);
            if(status!=1)
                {
                    fflush(stdin);
                    printf("INPUT ERROR!\n");
                }
        }
    while(status!=1);

    return answer;
}

3 个答案:

答案 0 :(得分:0)

您永远不会调用函数root(),因此永远不会执行它。将函数调用放在return 0; main()之前的某个地方:

    root(a, b, c);

另外,修复上述printf()来电。

答案 1 :(得分:0)

我不确定这是否会解决所有问题,但在您的代码中,您的打印语句格式不正确。例如,这一行:

printf("%.2f and %.2f,x1 , x2");

应为printf("%.2f and %.2f", x1, x2);

您尝试使用的值的变量应该在引号之外。希望这会有所帮助。

答案 2 :(得分:0)

你有几个错误。

  1. 您永远不会致电root()
  2. 声明
  3. root返回float,但它不会返回任何内容,只会打印根。应该声明void
  4. 您的许多printf()来电都有错误。当您显示等式时,您有|n而不是\n,而%+f代替+%f。当您显示根时,您需要在格式字符串中打印变量,而不是作为printf的单独参数。
  5. 这是更正后的代码。

    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    float userinput(char prompt[]); //Function Prototype
    void root(float a, float b, float c);
    
    int main()
    {
    
        float a,b,c;
    
        a=userinput("Enter the value for a:"); //Function Call
        b=userinput("Enter the value for b:");
        c=userinput("Enter the value for c:");
    
        printf("The Equation you entered is \n%fx^2+%fx+%f=0\n", a, b, c);
        root(a, b, c);
        return 0;
    }
    
    void root(float a, float b, float c)
    {
        float D,x,x1,x2,x3,x4;
        D = b*b - 4*a*c;
        if(D>0)
            {
                printf("There are two real roots, the roots are: ");
                x1 = ((-b+(sqrt(D)))/(2*a));
                x2 = ((-b-(sqrt(D)))/(2*a));
                printf("%.2f and %.2f" ,x1 , x2);
            }
    
        if(D==0)
            {
                printf("There is one real root, the root is: ");
                x = ((-b)/(2*a));
                printf("%.2f",x);
            }
    
        if(D<0)
            {
                printf("There are two imaginary roots. The roots are: ");
                x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
                printf("%.2fi and ", x3);
                x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
                printf("%.2fi", x4);
            }
    }
    
    float userinput(char prompt[]) //Function definition
    {
        float answer;
        int status;
        do
            {
                printf("%s",prompt);
    
                status=scanf("%f", &answer);
                if(status!=1)
                    {
                        fflush(stdin);
                        printf("INPUT ERROR!\n");
                    }
            }
        while(status!=1);
    
        return answer;
    }
    

    DEMO