#23373: using classes and overriding operators ( 2ms , AC )


jameslee55566@gmail.com (李俊翰)

學校 : 不指定學校
編號 : 131420
來源 : [1.170.103.68]
最後登入時間 :
2024-06-29 17:02:57
b538. 分數運算-2 -- 分數四則 | From: [36.234.222.100] | 發表日期 : 2020-11-11 21:37

#include <iostream>

#include <cmath>

#include <string>

using namespace std;

 

class fraction

{

public:

fraction();

fraction(int, int);

fraction add(const fraction&)const;

fraction subtract(const fraction&)const;

fraction multiply(const fraction&)const;

fraction devide(const fraction&)const;

int getNumerator()const;

int getDenominator()const;

double getValue()const;

void printFraction()const;

fraction operator+(const int&)const;

fraction operator-(const int&)const;

fraction operator*(const int&)const;

fraction operator/(const int&)const;

fraction operator+(const fraction&)const;

fraction operator-(const fraction&)const;

fraction operator*(const fraction&)const;

fraction operator/(const fraction&)const;

private:

int up, down;

static int GCD(int, int);

};

 

fraction::fraction()

{

    up = 0;

    down = 1;

}

 

fraction::fraction(int up, int down)

{

    int gcd = GCD(abs(up), abs(down));

    up /= gcd;

    down /= gcd;

    this->up = (down < 0) ? up * -1 : up;

this->down = abs(down);

}

 

fraction fraction::add(const fraction& fraction_2)const

{

    fraction ans(up * fraction_2.getDenominator() + fraction_2.getNumerator() * down, down * fraction_2.getDenominator());

    return ans;

}

 

fraction fraction::subtract(const fraction& fraction_2)const

{

    fraction ans(up * fraction_2.getDenominator() - fraction_2.getNumerator() * down, down * fraction_2.getDenominator());

    return ans;

}

 

fraction fraction::multiply(const fraction& fraction_2)const

{

    fraction ans(up * fraction_2.getNumerator(), down * fraction_2.getDenominator());

    return ans;

}

 

fraction fraction::devide(const fraction& fraction_2)const

{

    fraction ans(up * fraction_2.getDenominator(), down * fraction_2.getNumerator());

    return ans;

}

 

int fraction::getNumerator()const

{

    return up;

}

 

int fraction::getDenominator()const

{

    return down;

}

 

double fraction::getValue()const

{

    return up / down;

}

 

void fraction::printFraction()const

{

    if (down == 1)

        cout << up;

    else

    cout << up << "/" << down;

}

 

fraction fraction::operator+(const int& num)const

{

    return fraction(up + down * num, down);

}

 

fraction fraction::operator-(const int& num)const

{

    return fraction(up - down * num, down);

}

 

fraction fraction::operator*(const int& num)const

{

    return fraction(up * num, down);

}

 

fraction fraction::operator/(const int& num)const

{

    return fraction(up, down * num);

}

 

fraction fraction::operator+(const fraction& f2)const

{

    return add(f2);

}

 

fraction fraction::operator-(const fraction& f2)const

{

    return subtract(f2);

}

 

fraction fraction::operator*(const fraction& f2)const

{

    return multiply(f2);

}

 

fraction fraction::operator/(const fraction& f2)const

{

    return devide(f2);

}

 

int fraction::GCD(int m, int n)

{

    while (n != 0)

    {

        int r = m % n;

        m = n;

        n = r;

    }

    return m;

}

 

int main()

{   

    int a, b, d, e;

char c;

while(cin>>a>>b>>d>>e>>c)

{

fraction m(a, b);

fraction n(d, e);

fraction ans;

switch (c)

{

case '+':

ans = m + n;

break;

case '-':

ans = m - n;

break;

case '*':

ans = m * n;

break;

case '/':

ans = m / n;

break;

}

 

ans.printFraction();

cout<<endl;

}

return 0;

}

 
ZeroJudge Forum