【问题标题】:Add fractions using operator overloading使用运算符重载添加分数
【发布时间】:2016-03-27 12:56:22
【问题描述】:

在一次采访中,我被要求创建两个类。第一个抽象类称为 Number,它支持一个操作“+”。另一部分实现了“数字”抽象类。

进一步:对于分数一旦添加,它需要以原始形式显示。也就是说,2/4 必须显示为“2/4”,而不是“1/2”或“0.5”。

没有向我提供其他详细信息。

以下是我尝试过的(不完整)。

我的 main.cpp

#include <iostream>
#include "Fraction.h"
using namespace std;
int main()
{
    Fraction sumFraction;
    Fraction n11(1,2);
    Fraction n21(1,2);
    cout << n11.getValuenum() << "/";
    cout << n11.getValueden() << endl;
    cout << n21.getValuenum() << "/";
    cout << n21.getValueden() << endl;
    sumFraction = n11 + n21;
    cout << sumFraction.getValuenum() << endl;
    cout << sumFraction.getValueden() << endl;
    return 0;
}

My Numbers.h // 抽象类

  #pragma once
    template<class T>
    class Number
    {
        virtual T& operator= (const T &) = 0; // first parameter is implicitly passed
        virtual const T operator+ (const T &) = 0;
        virtual void display() = 0;
    };

我的分数.cpp

#include "Fraction.h"

int Fraction::getValuenum()
{
    return this->a1;
}

int Fraction::getValueden()
{
    return this->a2;
}

Fraction::Fraction()
{
    a1 = 0;
    a2 = 0;
}
Fraction::Fraction(int num, int den)
{
    a1 = num;
    a2 = den;
}

void Fraction::display()
{
    // will display the number in its original form
}

Fraction& Fraction::operator=(const Fraction &num)
{
    a1 = num.a1;
    a2 = num.a2;
    return *this;
}

const Fraction Fraction::operator+(const Fraction &numberTwo)
{
    Fraction n1;
    n1.a1 = this->a1*numberTwo.a2 + this->a2*numberTwo.a1;
n1.a2 = this->a2*numberTwo.a2;
    return n1;
}

我的分数.h

#pragma once
#include "Number.h"
class Fraction : public Number<Fraction>
{
private:
    int a1;
    int a2;
public:
    void display();
    Fraction();
    Fraction(int num, int den);
    int getValuenum();
    int getValueden();
    Fraction& operator= (const Fraction &); // first parameter is implicitly passed
    const Fraction operator+ (const Fraction &); // first parameter is implicitly passed

};

以下是我的问题:

  1. 对于每个分数,我真的需要将分子和分母与我的 Main 函数分开传递吗?目前,我将它单独传递以跟踪分子和分母,这在以分数形式添加和返回结果时可能会有所帮助。

  2. 使用我的运算符 + 逻辑,如果我添加 1/4+1/4,我会得到 8/16,如果我们正常添加,我猜我猜我们会得到 2/4。那么如何使用分子和分母相加,并以这样的方式保持分数,这样如果输出是 2/4 那么是 2/4 而不是 1/2 或 0.5。

请帮帮我。

【问题讨论】:

  • Khan Academy 可以帮助添加逻辑。
  • 我认为抽象模板类Number 完全没用。重新思考设计。
  • 我想我会简单地添加分子,而对分母不做任何事情,只要添加的两个分数具有相同的分母。这样 1/4+1/4 将是 2/4
  • 这是面试问题,所以我不得不使用抽象类来实现。可能他们计划在“Fraction”类之外添加另一个“Integer”类,该类也实现了 Number 类。
  • @Logicrat : 如果分母不同,它不起作用,

标签: c++ operator-overloading


【解决方案1】:

一些备注:

  • 您不应允许分母为 0,因为它给出了一个不存在的数字(无穷大或未定)
  • 出于同样的原因,您绝对应该将分母初始化为 0(1 似乎是一个更合理的值)
  • 分数的正确(数学)加法是(*):

    a/b + c/d = (ad +bc)/bd
    

我建议你写一个ostream&amp; operator &lt;&lt; (ostream&amp;, const Fraction&amp;)重载而不是(或除了)显示方法。这将允许你只写你的 main

std::cout << n11 << " + " << n21 << " = " << sumFraction << std::endl;

我并没有真正理解你的第一个问题,但我会添加一个来自 int 的转换:

Fraction(int n): a1(n), a2(1) {};

允许直接写Fraction(1, 2) + 1Fraction(1) + Fraction(1/2)(加法的第一个元素必须是分数)

(*) 这是简单通用的方法。您还可以使用最小公倍数来获得更清晰的结果:

den = lcm(b,d)
a/b + c/d = (a * den/b) + c * den/d) / den

这样你会得到 1/4 + 2/4 = 3/4 而不是 12/16

但是computing the LCM 远远超出这个答案...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2010-12-13
    • 2019-02-13
    相关资源
    最近更新 更多