无法从数组二次表达式中检索数据值
Cannot retrieve data values from array quadraticExpression
所以,在工作类中,我们必须为二次表达式编写一个标头类。我已经完成了大部分头文件,但是,当我继续使用给定的.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)
{}
相关文章:
- 输出 0 和 -0 的二次公式,与给定的项无关
- 处理所有二次公式结果
- 两个嵌套循环的运行时间复杂性:二次型还是线性
- Cgal二次规划目标函数
- 二次代数建议数组等返回功能
- 无法从数组二次表达式中检索数据值
- 创建二次公式求解器-范围中未声明的变量
- C++:你如何确定一个解是微不足道的还是不存在的二次函数?
- 如何将二次贝塞尔曲线代码转换为三次贝塞尔曲线
- 为什么使用STL排序()进行二次时间排序
- omp并行用于二次筛的无优化
- 分段故障:SET C++11中的二次排序
- C++识别以像素为单位的二次和三次显示
- 在 Vulkan 中加载非二次幂纹理
- C++二次码错误
- sqrt() 使用 <math.h) 在二次公式中的意外结果>
- 如何顺时针交换二次矩阵的四分之一(从左上角开始)
- 如何在C++中进行二维二次拟合
- 求值表达式二叉树c++
- 编写二次公式程序时出现编译错误