无法从数组二次表达式中检索数据值

Cannot retrieve data values from array quadraticExpression

本文关键字:表达式 二次 检索 数据 数组      更新时间:2023-10-16

所以,在工作类中,我们必须为二次表达式编写一个标头类。我已经完成了大部分头文件,但是,当我继续使用给定的.cpp文件运行以测试头文件时,它似乎没有读取 cpp 文件中数组中给定的值。当我调试时,它只是为值输入垃圾值。对我来说,我认为这是有道理的,看不出有什么问题。除非我错过了什么?

我构建了以下头文件...

#pragma once
#include <cmath>
enum roots {
    NO_ROOTS = 0,
    ONE_ROOT = 1,
    TWO_ROOTS = 2,
    INFINITE_ROOTS = 3
};
class quadraticExpression
{
private:
    double a, b, c;
public:
    double evaluate(double x) const;
    int getNumberOfRoots() const;
    double getFirstRoot() const;
    double getSecondRoot() const;
    double getACoefficient() const;
    double getBCoefficient() const;
    double getCCoefficient() const;
    quadraticExpression();
    quadraticExpression(double a,
                        double b,
                        double c);
};
quadraticExpression::quadraticExpression()
{
    a = 0;
    b = 0;
    c = 0;
}
inline quadraticExpression::quadraticExpression(double a, double b, double 
                                                c)
{
    a = a;
    b = b;
    c = c;
}
;
double quadraticExpression::evaluate(double x) const
{
    double y;
    y = (a*(x * x)) + (b * x) + c;
    return y;
}
int quadraticExpression::getNumberOfRoots() const
{
    //return value from enum
    double eins;
    double zwei;
    eins = getFirstRoot();
    zwei = getSecondRoot();

    if (eins == 0 && zwei == 0)
    {
        return TWO_ROOTS;
    }
    else if (eins == 0 || zwei == 0)
    {
        return ONE_ROOT;
    }
    else if (eins != 0 && zwei != 0)
    {
        return NO_ROOTS;
    }
}
double quadraticExpression::getFirstRoot() const
{
    //return one x value where y is 0
    double root1 = (b * b);
    double root2 = (4 * a*c);
    double solutionOne;
    double zUno;
    zUno = (abs(b) + sqrt(root1 - root2)) / (2 * a);
    solutionOne = (a*(zUno * zUno)) + (b * zUno) + c;
    return solutionOne;
}
double quadraticExpression::getSecondRoot() const
{
    //return another x value where y is 0
    double root1 = (b * b);
    double root2 = (4 * a*c);
    double solutionTwo;
    double zDos;
    zDos = (abs(b) - sqrt(root1 - root2)) / (2 * a);
    solutionTwo = (a*(zDos *zDos)) + (b *zDos) + c;
    return solutionTwo;
}
double quadraticExpression::getACoefficient() const
{
    return a;
}
double quadraticExpression::getBCoefficient() const
{
    return b;
}
double quadraticExpression::getCCoefficient() const
{
    return c;
}

这是.cpp测试器文件

#include <iostream>
#include "QuadraticExpression.h"
using namespace std;
void evaluateExpression(const quadraticExpression &);
int main()
{
    quadraticExpression q[6] = { quadraticExpression(2.1, 3, -7),
                                 quadraticExpression(1.4, 3.9, +7),
                                 quadraticExpression(-.75, 0, 0),
                                 quadraticExpression(0, .3, -7),
                                 quadraticExpression(0, 0, 4),
                                 quadraticExpression() };
    for (int i = 0; i<6; i++)
        evaluateExpression(q[i]);
    return EXIT_SUCCESS;
}
void evaluateExpression(const quadraticExpression &q)
{
    int errorsHandled = 0;
    cout << q.getACoefficient() << " A " << endl;
    cout << q.getBCoefficient() << " B " << endl;
    cout << q.getCCoefficient() << " C " << endl;
    cout << "f(-5) = " << q.evaluate(-5) << endl;
    cout << "f(0)  = " << q.evaluate(0) << endl;
    cout << "f(5)  = " << q.evaluate(5) << endl;
    if (q.getNumberOfRoots() == INFINITE_ROOTS)
        cout << "The Expression has Infinite Roots" << endl;
    else if (q.getNumberOfRoots() == ONE_ROOT)
        cout << "The Expression has One Root at x = " << q.getFirstRoot() << 
            endl;
    else if (q.getNumberOfRoots() == TWO_ROOTS)
    {
        cout << "The Expression has First Root at x  = " << q.getFirstRoot() << 
            endl;
        cout << "The Expression has Second Root at x = " << q.getSecondRoot() << 
            endl;
    }
    else
        cout << "The Expression has No Roots" << endl;
    try {
        q.getFirstRoot();
    }
    catch (domain_error e) {
        errorsHandled++;
    }
    try {
        q.getSecondRoot();
    }
    catch (domain_error e) {
        errorsHandled++;
    }
    cout << "Errors Handled: " << errorsHandled << endl;
    cout << endl;
    cout << endl;
}

明白我可能没有从 cpp 文件中给出的数组中正确获取数据值 a、b 和 c,因此它只是收集垃圾值,但我在这里被难住了。

这不会按您的预期工作。

inline quadraticExpression::quadraticExpression(double a, double b, double c)
{
    a = a;
    b = b;
    c = c;
}

您只是将参数变量分配给它们自己,而不是分配给类的成员变量。 在函数中声明的变量优先于具有相同名称的成员变量。

您应该为参数指定与成员变量不同的名称,或者像this->a = a;一样分配。

但是,如果您只是从参数初始化成员变量,则根本不需要执行赋值,首选初始值设定项列表(请参阅C++:在构造函数中初始化变量的位置(:

quadraticExpression::quadraticExpression(double a, double b, double c) : a(a), b(b), c(c) 
{}

同样,没有参数的构造函数应使用初始值设定项列表:

quadraticExpression::quadraticExpression() : a(0), b(0), c(0) 
{}