c - 二元一次方程不能正常工作

标签 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;
}

最佳答案

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

    root(a, b, c);

此外,如上所述修复 printf() 调用。

关于c - 二元一次方程不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34937596/

相关文章:

c - 打印宏功能

c++ - C++ 中的 Win32 显示中文...我做错了什么?

c++ - 我已经创建了一个模板函数,但在检查输入是奇数还是偶数时出现错误 C++

python - 我的工作簿在哪里弹出?

c++ - 错误 "expected primary-expression before int"

function - 自定义函数中存储的值

javascript - 在 javascript 中,window.function(){} 和 var variable = function 有什么区别?

C数组索引

python - 我可以用 C 重写昂贵的 pygame 函数吗?

c++ - typedef 类型和相同类型的指针的正确方法是什么?