无法从数组quadraticExpression

时间:2018-01-27 00:51:18

标签: c++ header

因此,在课堂上我们必须为二次表达式编写一个头类。我在大多数情况下都完成了头文件,但是,当我继续使用给定的.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,因此它只是收集垃圾值,但是我在这里难倒。

1 个答案:

答案 0 :(得分:1)

这不符合您的意图。

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

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

您应该从成员变量中为参数指定不同的名称,或者指定this->a = a;

但是,如果您只是从参数初始化成员变量,则根本不需要进行分配,首选初始化列表(请参阅C++: Where to initialize variables in constructor):

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

同样,没有参数的构造函数应该使用初始化列表:

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