c++ - 使用运算符重载添加分数

标签 c++ operator-overloading

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

进一步:对于添加的Fraction,需要以其原始形式显示。也就是说,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。

请帮帮我。

最佳答案

一些说明:

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

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

我建议您写一个 ostream& operator << (ostream&, const Fraction&) 来代替(或补充)显示方法重载。那将允许你只写在你的 main

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

我不是很理解你的第一个问题,但我会添加一个从 int 的转换:

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

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

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

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远远超出这个答案...

关于c++ - 使用运算符重载添加分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36247587/

相关文章:

c++ - const std::shared_ptr&const正确性

c++ - 这被认为是动态内存分配吗?

c# - 为 null 重载 == 运算符

c++ - 两个以上类的对象之间重载运算符=

c# - 在 C# 中是否可以通过以下方式重载泛型转换运算符?

c++ - 返回值优化 : ho can I avoid copy construction of huge STL containers.

c++ - 通过远程桌面运行 qt creator

c++ - LNK1120 和 LNK2019 错误

c++ - 重载运算符 '>>' 的使用不明确(操作数类型为 'istream'(又名 'basic_istream<char>')和 'MyIncreEx')

c++ - C++ 中类型定义的重载运算符