c++ - 如何使用运算符重载来简化两个分数的加法?

标签 c++

我需要使用运算符重载来简化两个分数的加法。我想获得极简主义的结果​​。我使用了两次欧几里得算法,第一次,我得到了分母的倍数。第二次,我想简化分数。 像这两个分数

1 10
1 10

结果:

1 5

两个数相加并化简

主要代码片段:

Fraction operator+(const Fraction &a1, const Fraction &a2) {

    int max, min, temp_1 , temp_2, n, m, sum, max_1, min_1, temp_3 ;
    if (a1.numerator < a2.numerator) {
        max = a2.numerator;
        min = a1.numerator;
    } else {
        max = a1.numerator;
        min = a2.numerator;
    }
    //euclidean algorithm 
    while (max % min != 0) {
        temp_1 = max % min;
        max = min;
        min = temp_1;
    }
    //Least common multiple
    temp_2 = max * min / temp_1;

    n = temp_2 / a1.numerator * a1.denominator;
    m = temp_2 / a2.numerator * a2.denominator;
    sum = n + m;
    if (sum > temp_2) {
        max_1 = sum;
        min_1 = temp_2;
    } else {
        max_1 = temp_2;
        min_1 = sum;
    }
    //euclidean algorithm 
    while (max_1 % min_1 != 0) {
        temp_3 = max_1 % min_1;
        max_1 = min_1;
        min_1 = temp_3;
    }
    sum = sum / temp_3;
    temp_2 = temp_2 / temp_3;
    return Fraction(sum, temp_2);
}

完整代码:

#include <iostream>

using namespace std;

class Fraction {
private:

    int numerator, denominator;
public:
    Fraction(int numerator1=0, int denominator1=0) : numerator(numerator1), denominator(denominator1) {}
    void show() const; //Output all data
    friend Fraction operator+(const Fraction &a1, const Fraction &a2);
};
void Fraction::show() const {
    cout << "x/y= " << numerator << " / " << denominator << endl;
}
Fraction operator+(const Fraction &a1, const Fraction &a2) {

    int max, min, temp_1 , temp_2, n, m, sum, max_1, min_1, temp_3 ;
    if (a1.numerator < a2.numerator) {
        max = a2.numerator;
        min = a1.numerator;
    } else {
        max = a1.numerator;
        min = a2.numerator;
    }
    //euclidean algorithm 
    while (max % min != 0) {
        temp_1 = max % min;
        max = min;
        min = temp_1;
    }
    //Least common multiple
    temp_2 = max * min / temp_1;

    n = temp_2 / a1.numerator * a1.denominator;
    m = temp_2 / a2.numerator * a2.denominator;
    sum = n + m;
    if (sum > temp_2) {
        max_1 = sum;
        min_1 = temp_2;
    } else {
        max_1 = temp_2;
        min_1 = sum;
    }
    //euclidean algorithm 
    while (max_1 % min_1 != 0) {
        temp_3 = max_1 % min_1;
        max_1 = min_1;
        min_1 = temp_3;
    }
    sum = sum / temp_3;
    temp_2 = temp_2 / temp_3;
    return Fraction(sum, temp_2);
}
int main() {
    Fraction a1(1 ,5);
    Fraction a2(3, 5);
    Fraction a;
    cout << "a1: ";
    a1.show();
    cout << "a2: ";
    a2.show();
    cout << "a: " ;
    a = a1 + a2;
    a.show();

}

最佳答案

这个加法太复杂了。
(几乎不可能说出应该是一个非常简单的算术运算的问题是什么,这是一个强有力的指标。)

从将 Euclides 提取到函数中开始:

int gcd(int a, int b)
{
    if (a < b)
        return gcd(b, a);
    while (b != 0) {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

(或者使用 std::gcd,如果你是 C++17-modern。)

然后重写构造函数来做简化(你不想强制你的类的用户担心这个):

Fraction(int numerator1=0, int denominator1=1) 
{
    int divisor = gcd(numerator1, denominator1);
    numerator = numerator1 / divisor;
    denominator = denominator1 / divisor;   
}

您还应该让默认分母为 1,因为除以零是未定义的。
默认情况下无效的分数是个坏主意。

有了这个,加法变得几乎微不足道:

Fraction operator+(const Fraction &a1, const Fraction &a2) {
    int numerator = a1.numerator * a2.denominator + a2.numerator * a1.denominator;
    int denominator = a1.denominator * a2.denominator;
    return Fraction(numerator, denominator);
}

关于c++ - 如何使用运算符重载来简化两个分数的加法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55769514/

相关文章:

c++ - 我可以在类定义中实例化当前类的新对象吗

C++ String 和 char * c stype 字符串

c++ - 在 C++14 的 [class.copy]/12 中加粗文本的目的是什么?

c++ - SFML 2.1 RenderWindow 链接错误

c++ - 变量中重复两次的数字

c++ - 在 C++11 中定义只移动对象是否有意义?

c++ - 如何在 C++ 项目中使用 C gnutls API

c++ - 为什么虚拟关键字 "persistent"贯穿 C++ 的整个层次结构?

c++ - Qt,运行缓慢时不卡住GUI输入元素

c++ - 我不明白如何在 C++ 中执行 remove_if