gpt4 book ai didi

c++ - 无法从数组 quadraticExpression 中检索数据值

转载 作者:行者123 更新时间:2023-11-28 04:49:50 24 4
gpt4 key购买 nike

因此,在工作类中,我们必须为二次表达式编写一个头类。我已经完成了大部分头文件,但是,当我继续运行给定的 .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++: 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) 
{}

关于c++ - 无法从数组 quadraticExpression 中检索数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48471548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com