void降低();成员函数减少分数
void reduce(); MEMBER FUNCTION REDUCES FRACTIONS
这是我需要编写的成员函数。该功能旨在减少分数。void降低((;
这就是我拥有的
void reduce()
{
num /= gcd();
den /= gcd();
}
提供了GCD函数,因此不正确。
它不允许我在班级定义的其余部分启动我的代码,因为它太长
我的问题是;为什么我会遇到RELAD((
的错误我也尝试了
void reduce()
// reduce this fraction to simplest form. For instance,
// 2/4 will be reduced to 1/2
{
int a = gcd();
den /= a;
num /= a;
}
我的代码正在测试以下:
// Test reduce
f1.reduce(); // f1 is -4/5
f2.reduce(); // f2 is 2/3
if(f1.get_numerator() != -4 || f1.get_denominator() != 5 || f2.
get_numerator() != 2 || f2.get_denominator() != 3)
{
cout <<"The reduce function was wrong.n";
result -= 0.5;
}
假设gcd()
返回的值取决于num
和den
,因此gcd()
在第一行中返回的值将与第二行中gcd()
返回的值不同。
将gcd()
的返回值存储在本地变量中并使用它。
void reduce()
{
auto g = gcd();
num /= g;
den /= g;
}
向您的程序添加一些调试输出:
if ( f1.get_numerator() != -4 ||
f1.get_denominator() != 5 ||
f2.get_numerator() != 2 ||
f2.get_denominator() != 3)
{
cout <<"The reduce function was wrong.n";
if ( f1.get_numerator() != -4 )
{
cout << "Expected value of f1.get_numerator(): " << -4 << "n";
cout << "Actual value of f1.get_numerator(): " << f1.get_numerator() << "n";
}
else
{
cout << "Got the expected value of f1.get_numerator()n";
}
if ( f1.get_denominator() != 5 )
{
cout << "Expected value of f1.get_denominator(): " << 5 << "n";
cout << "Actual value of f1.get_denominator(): " << f1.get_denominator() << "n";
}
else
{
cout << "Got the expected value of f1.get_denominator()n";
}
if ( f2.get_numerator() != 2 )
{
cout << "Expected value of f2.get_numerator(): " << 2 << "n";
cout << "Actual value of f2.get_numerator(): " << f2.get_numerator() << "n";
else
{
cout << "Got the expected value of f2.get_numerator()n";
}
if ( f2.get_denominator() != 3 )
{
cout << "Expected value of f2.get_denominator(): " << 3 << "n";
cout << "Actual value of f2.get_denominator(): " << f2.get_denominator() << "n";
}
else
{
cout << "Got the expected value of f2.get_denominator()n";
}
result -= 0.5;
}
相关文章:
- 如何使用指针传递给函数的数组中对象的函数成员
- c++构造函数成员初始化:传递参数
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 如何在C++通过公共函数访问私有函数成员?
- 解释了构造函数成员初始化列表
- 调用std::函数成员时内存损坏
- 是否可以为模板类的模板函数成员设置别名?
- 捕获 lambda 函数C++成员变量
- 构造函数成员初始值设定项跨成员列出,安全吗?
- 获取与在模板参数中传递的函数成员类型相同的类
- 如何从公共函数成员访问地图私有成员
- C 构造函数成员分配优化
- 使用命名空间进行函数成员定义
- 函数成员作为 CUDA 内核的参数
- 模板基类函数成员的别名
- 函数成员中用于void和继承的enable_if
- 头文件中是否定义了一个很长的Class函数成员
- 类内/构造函数成员初始化
- 使用指向部分专用函数成员的指针自动填充向量
- 指向函数成员的指针