【问题标题】:void reduce(); MEMBER FUNCTION REDUCES FRACTIONS无效减少();会员功能减少分数
【发布时间】:2018-02-20 06:31:09
【问题描述】:

这是我需要编写的成员函数。该功能旨在减少分数。 无效减少();

这就是我所拥有的

void reduce()

{
    num /= gcd();
    den /= gcd();
}

提供了gcd函数,所以没有错。

它不允许我为类定义的其余部分包含我的代码,因为它太长了

我的问题是; 为什么我收到 reduce() 错误

我也试过了

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;
}

【问题讨论】:

  • How do I ask a question?。什么错误?什么不工作。这与运算符重载有什么关系。我们甚至不知道 num/def 的类型或 gcd 的返回类型......如果我们不知道这一切,我们应该如何提供帮助?
  • 什么错误?想象这不是你自己的问题。想象一下,你要回答别人写的问题。你甚至能理解这篇文章所要求的内容吗?请访问help center 并阅读How to Ask 好问题。特别关注解释如何创建minimal reproducible examples 的部分。
  • 如果您不提供足够的信息,您是否认为互联网上的人们有超能力神奇地知道您为什么会出错?
  • 调试时有用(并且总是):不要只打印结果错误,还要打印结果是什么以及应该是什么。一次测试一件事,这样你就可以知道哪件事是错的。 “这两件事中至少有一件出了问题”没有“这应该是 -4/5,但它是 29/73”那么有用。

标签: c++ class operator-overloading


【解决方案1】:

假设gcd()返回的值依赖于numden,那么第一行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;
}

【讨论】:

  • 太好了,这就是我最终做的,但仍然无法工作 D:我绑定添加其余代码,但它说代码太多,细节不够
  • @KatPantle 您至少可以用您尝试过但不起作用的代码更新您的问题吗?
  • @KatPantle,你可以(1)尝试创建一个minimal reproducible example并发布它,(2)学习how to debug small programs
  • 和我一起呆一会儿。谢谢你的建议。我添加的内容有用吗?
  • @KatPantle,请参阅更新后的答案。这是解决代码问题的一种方法。
猜你喜欢
  • 2020-11-01
  • 2020-03-25
  • 2018-10-29
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多