提问者:小点点

使用二次公式的简单二次方程计算器


我想知道是否有人能帮我解决这个(初学者)问题。 我正在创建一个非常基本的二次方程计算器。 我在下面列出了我的代码,还有注释(代码段顶部的注释解释了我必须做的事情)。 我在网上看过多种解决方案,也尝试过自己,但似乎我一直得到不正确的x1和x2值。 如果有人能指引我,我会非常高兴。 干杯。

/* 
create program to calculate x for quadratic equation. (a, b and c)
1) create function which prints out roots of quad equation.
2) throw exception if b2 - 4ac is less than 0.
3) call the function from main.
*/

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

double roots() { //return type, function name, parameters and body of function.
    double a = 0, b = 0, c = 0, x1, x2;
    double discriminant = (b * b) - 4 * a * c;
    cout << "Enter roots of quad equation (in order: a, b, c) " << endl;
    cin >> a >> b >> c;

        if (discriminant >= 0) {

            cout << "Your quadratic equation is: " << a << "x squared + " << b << "x + " << c << " = 0 " << endl;
            x1 = -b + sqrt(discriminant) / (2 * a);
            x2 = -b - sqrt(discriminant) / (2 * a);
            cout << "x1 = " << x1 << endl;
            cout << "x2 = " << x2 << endl;
        } 
        else {
            cout << "Negative value returned from (b2 - 4ac), please try again! " << endl;
            exit(1);
        }   
}

int main() {

    cout << "Quadratic Equation Calculator " << endl;
    roots();




    return 0;
}`enter code here`

共3个答案

匿名用户

这是一个简单的错误。 您只是把代码行的顺序放错了。 这应该可以解决这个问题:

/* 
create program to calculate x for quadratic equation. (a, b and c)
1) create function which prints out roots of quad equation.
2) throw exception if b2 - 4ac is less than 0.
3) call the function from main.
*/

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

double roots() { //return type, function name, parameters and body of function.
    double a = 0, b = 0, c = 0, x1, x2;
    cout << "Enter roots of quad equation (in order: a, b, c) " << endl;
    cin >> a >> b >> c;
    double discriminant = (b * b) - 4 * a * c; // restructed this line

        if (discriminant >= 0) {

            cout << "Your quadratic equation is: " << a << "x squared + " << b << "x + " << c << " = 0 " << endl;
            x1 = -b + sqrt(discriminant) / (2 * a);
            x2 = -b - sqrt(discriminant) / (2 * a);
            cout << "x1 = " << x1 << endl;
            cout << "x2 = " << x2 << endl;
        } 
        else {
            cout << "Negative value returned from (b2 - 4ac), please try again! " << endl;
            exit(1);
        }   
}

int main() {

    cout << "Quadratic Equation Calculator " << endl;
    roots();




    return 0;
}

匿名用户

判别式的问题可能会教你写一个函数来计算判别式,比如:

double f_discriminant(double a,b,c){
  return b*b-4*a*c;
}

这样,您的代码段就变成了(基于PranavaGande的改进):

...
double roots() { //return type, function name, parameters and body of function.
    double a = 0, b = 0, c = 0, x1, x2;
    cout << "Enter roots of quad equation (in order: a, b, c) " << endl;
    cin >> a >> b >> c;
    double discriminant = f_discriminant(a, b, c); // restructed this line
...

这里的优点是,如果你需要再次计算判别式,你可以使用你为它编写的函数。 (对于本例,这是显而易见的,但您会遇到更难开发的函数)

匿名用户

问题是计算判别式的直线:

double discriminant = (b * b) - 4 * a * c;

这一行出现在您将非零值赋给a,b和C之前。 在执行时,所有这些变量都等于零,所以判别式也是零。 要使用来自用户的值计算判别式,那一行需要在这一行之后:

cin >> a >> b >> c;

原因是算术计算只执行一次,就在它所写的行上执行; 之后,=运算符(赋值)将结果放入变量中。 之后,变量就不知道值是如何到达那里的,也没有办法使用旧表达式中的新值来“刷新”自己。 你可以通过使判别式成为a,b和c的函数来近似它。