【问题标题】:Simple Quadratic Equation Calculator using Quadratic Formula使用二次公式的简单二次方程计算器
【发布时间】:2020-11-28 14:45:18
【问题描述】:

我想知道是否有人可以帮助我解决这个(初学者)问题。 我正在创建一个非常基本的二次方程计算器。我在下面列出了我的代码以及 cmets(代码顶部的那些 sn-p 解释了我必须做什么)。我在网上查看了多种解决方案并尝试了自己,但似乎我一直得到不正确的 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;
}

【问题讨论】:

  • 您在获取 a、b、c 的输入之前计算判别式

标签: c++ calculator


【解决方案1】:

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

/* 
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;
}

【讨论】:

  • 非常感谢.. 我也意识到,一旦我改变了这个,公式就缺少括号,这似乎解决了这个问题!
  • 太好了!很高兴看到我提供帮助。
  • @matiboy212121 -- 计算discriminant 的代码没有缺少括号;它有额外的括号。 b * b + 4 * a * c(b * b) + 4 * a * c 做的事情完全相同。 *+ 更紧密地分组,因此您不需要在 b * b4 * a * c 周围加上括号(方括号)。
  • @PeteBecker 我指的是程序的 x1 和 x2 部分,我将括号放在“-b”之前和“判别)”部分之后。那是对的吗?我这样做是为了让 c++ 更清楚(我认为)知道要除以 (2*a) 的内容。
  • 听起来你做对了。 (-b + sqrt(discriminant) / (2 * a)。或者您可以使用/ 2 / a,但大多数人会觉得这很奇怪。
【解决方案2】:

您遇到的判别式问题可能会教您编写一个函数来计算判别式,例如:

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
...

这里的好处是,如果你需要再次计算判别式,你可以只使用你为它编写的函数。 (这个例子很明显,但是你会遇到更难开发的功能)

【讨论】:

  • 感谢您解决这个问题!我想知道为什么我目前正在阅读的书告诉我在 main 之外创建函数(我认为这毫无意义),但这允许我在程序中重用该函数!欣赏它。
【解决方案3】:

问题在于计算判别式的那一行:

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

这一行出现在您为 a、b 和 c 分配非零值之前。在执行此操作时,所有这些变量都等于零,因此判别式也为零。要使用用户的值计算判别式,该行需要在该行之后:

cin >> a >> b >> c;

原因是算术计算只执行一次,就在它写入的那一行;之后, = 运算符(赋值)将该结果放入变量中。之后,该变量不记得该值是如何到达那里的,也无法使用旧表达式中的新值“刷新”自身。您可以通过将判别式设为 a、b 和 c 的函数来近似。

【讨论】:

  • 我明白了!因此,根据您所说,只需重新排列代码(就像上面的人解释的那样)并可能专门为判别式创建一个函数(以简化程序)?
猜你喜欢
  • 1970-01-01
  • 2019-08-28
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多