【问题标题】:Can't figure out why this isn't working无法弄清楚为什么这不起作用
【发布时间】:2014-11-09 22:15:17
【问题描述】:

我是一名学习 c++ 的学生,这周我必须制作一个二次公式求解器。我应该只使用主代码块中的函数来获取输出。 这是我目前所拥有的:

#include <iostream>
#include <cmath>
using namespace std;
double a, b, c, x1, x2;
char choice, response, mychar;
double disc = (b*b - 4 * a*c);

void GetCoefficients(double a, double b, double c)
{
    cout << "Enter the coefficients of your quadratic equation (a, b, c): ";
    cin >> a, b, c;
}

bool ComputeRoots(double a, double b, double c, double x1, double x2)
{
    
    if (disc > 0)
    {
        x1 = (-b + sqrt((b*b) - 4 * a*c)) / 2 * a;
        x2 = (-b - sqrt((b*b) - 4 * a*c)) / 2 * a;
        return true;
    }
    if(disc == 0)
    {
        x1 = x2 = (-1 * b) / (2 * a);
        return true;
    }
    else 
    {
        cout << "'a' cannot be zero. That is not a quadratic equation.";
        return false;
    }
}

char PromptToContinue()
{
    char mychar;
    cout << "Would you like to solve another quadratic equation (Y, N): ";
    cin >> mychar;
    return mychar;
}

void PrintRoots(double x1, double x2)
{
    if (disc > 0)
    {
        cout << "The roots are: " << x1 << ", " << x2;
    }
    
    if (disc == 0)
    {
        cout << "The single root is: " << x1;
    }
}

void main()
{

    do
    {
        GetCoefficients(a, b, c);
        if (ComputeRoots(a, b, c, x1, x2))
        {
            PrintRoots(x1, x2);
        }
        choice = PromptToContinue();
    } while (choice != 'n' || 'N');
    system("pause");
}

我确定我的代码存在多个问题,但我已经盯着这个看了好几个小时,不知道为什么它不起作用。任何见解都会很棒。

我应该得到的示例输出是这样的(冒号后面的值是用户输入):

输入二次方程 (a, b, c) 的系数:2 3 4
根源很复杂。

你想解另一个二次方程(Y,N):y
输入二次方程 (a, b, c) 的系数:1 2 1

单根是:-1

你想解另一个二次方程(Y,N):y
输入二次方程 (a, b, c) 的系数:0 5 6

'a' 不能为零。这不是一个二次方程。

你想解另一个二次方程(Y,N):y
输入二次方程 (a, b, c) 的系数:5 25 5

根是:-0.208712, -4.79129

你想解另一个二次方程(Y,N):n
按任意键继续 。 . .

我得到的输出是这样的:

Enter the coefficients of your quadratic equation (a, b, c): 2 3 4
The single root is: 0Would you like to solve another quadratic equation (Y, N):
Enter the coefficients of your quadratic equation (a, b, c): The single root is:
 0Would you like to solve another quadratic equation (Y, N):

【问题讨论】:

  • 你的程序输出的是什么?
  • 什么不起作用?你的问题需要更具体。您应该发布输出/错误。另外,您的问题标题基本上没用。如果您提出正确的问题,您可能会自己解决问题。
  • 创建一个返回双精度并计算光盘的函数。

标签: c++


【解决方案1】:

一个明显的问题是void GetCoefficients(double, double, double) 的参数是按值传递的,而您显然希望它们按引用传递。照原样,您将它们复制到您的函数中,将用户输入读取到这些副本中,然后在返回时将它们全部删除。

编辑void main() 不正确,main 的类型应为 int main(int, char**)int main()

while (choice != 'n' || 'N'); 不符合您的预期:它检查choice 是否与'n' 不同,然后使用文字'N' 进行布尔。由于'N' 不为零,因此条件始终为真。正确的语法是(choice != 'n' &amp;&amp; choice != 'N')

您的程序总体上也很奇怪,但我想它会变得更好。例如,您只使用全局变量,应尽可能避免使用。 void PrintRoots() 很有趣,它通过参数列表获取一半参数,并从全局变量中获取另一半。

【讨论】:

  • 所以我把GetCoefficients中的cin改成了正确的格式,改了主类型,把disc变成了一个全局函数,修复了choice的问题。我仍然对返回 0 的根的值有问题。这是因为磁盘是全局的,还是根的值没有从ComputeRoots 转移到PrintRoots
  • @Marcus Bingo :再一次,ComputeRoots 按值获取 x1x2,当您希望它们通过引用传递时。我建议你研究一下 C++ 中的参数传递,否则你永远不会摆脱这些问题:)
  • @LightnessRacesinOrbit 谢谢。 C 的任意参数语法潜伏在它周围,看起来确实有点前卫。编辑:第二次编辑:p
【解决方案2】:
cin >> a, b, c;

是错误的并且没有做你认为的(阅读comma operator)。

你可能想要

cin >> a >> b >> c;

至少

编译所有警告和调试信息 (g++ -Wall -Wextra -g)。然后使用调试器 (gdb) 例如一步一步地运行你的程序。

更一般地,阅读更多关于您正在使用的函数和运算符的文档(关于 C++)(例如 this

【讨论】:

    【解决方案3】:

    这段代码:

    double a, b, c, x1, x2;
    double disc = (b*b - 4 * a*c);
    

    disc 设置为零。在 C++ 中,表达式是使用遇到表达式时的变量值来计算的。此行设置了一个公式,该公式将在您稍后在程序中使用disc 时用于计算disc

    (注意:由于double a, b, c, x1, x2; 是全局的,这些值被初始化为0.0,因此disc 的计算也以0.0 结束。

    例如,你继续做:

    bool ComputeRoots(double a, double b, double c, double x1, double x2)
    {
        if (disc > 0)
    

    但是disc 仍然为零,因为您在程序开始时将其设置为零,如前所述,并且您没有更改它。

    要设置一个值作为其他输入值的结果计算的情况,您需要编写一个函数,例如:

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

    【讨论】:

    • 注意。您需要修改存储根的方式,以便PrintRoots 知道要打印哪种情况;因为它无法从另一个函数访问disc() 的结果。
    • 您可能应该提到 为什么 这些变量设置为零,因为这有点极端,而且大多数其他上下文都会让您使用未初始化的变量,UB 位于顶部: )
    【解决方案4】:

    您可能对使用局部变量和全局变量感到困惑。在这里您已将所有变量指定为全局变量,因此您无需在函数参数中传递变量(请记住,使用许多全局变量是一种糟糕的编程练习)。你的代码有很多语义问题

    #include <iostream>
    #include <cmath>
    using namespace std;
    double a, b, c, x1, x2;
    char choice, response, mychar;
    double disc = (b*b - 4 * a*c); // here only declare disc like `double disc;`  create a function for calculating disc like
    /* void caldisc()
    {
    disc = (b*b - 4 * a*c);
    } call this caldisc() in main after GetCoefficients()*/
    
        void GetCoefficients(double a, double b, double c) // passing parameters double a, double b, double c means your creating local copy of a b and c and getting input in it not in global a b c
        {
            cout << "Enter the coefficients of your quadratic equation (a, b, c): ";
            cin >> a, b, c;   // use cin>>a>>b>>c;
        }
    
        bool ComputeRoots(double a, double b, double c, double x1, double x2) // dont pass parameters here and use directly it in main() like ComputeRoots();
        {
    
            if (disc > 0)
            {
                x1 = (-b + sqrt((b*b) - 4 * a*c)) / 2 * a;
                x2 = (-b - sqrt((b*b) - 4 * a*c)) / 2 * a;
                return true;
            }
            if(disc == 0)
            {
                x1 = x2 = (-1 * b) / (2 * a);
                return true;
            }
            else 
            {
                cout << "'a' cannot be zero. That is not a quadratic equation.";  // this block will get executed when disc<0 regardless of a. ZeroCheck of a should be done in upper blocks.
                return false;
            }
        }
    
        char PromptToContinue()
        {
            char mychar;       // here you're creating local copy of mychar and returning it in last line.Thus local copy of mychar will return garbage. remove this line
            cout << "Would you like to solve another quadratic equation (Y, N): ";
            cin >> mychar;
            return mychar; // remove this change function return type as void
        }
    
        void PrintRoots(double x1, double x2) // no need to pass x1 and x2 change function call in main too.
        {
            if (disc > 0)
            {
                cout << "The roots are: " << x1 << ", " << x2;
            }
    
            if (disc == 0)
            {
                cout << "The single root is: " << x1;
            }
        }
    

    现在主要不要使用选择。使用 mychar !='N' 检查 do-while 条件 // 意味着如果你传递了 N 以外的任何内容,则循环将被执行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2014-12-08
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多