1022 TDA Rational(Play with oop)

URI 1022 TDA Rational:

It is so easy problem. so after see solution try your self.

It can be solve various way.But i am use OOP for practicing OOP.And i am use some extra thing.

If you solve the problem.But it is not accepted than use uDebug to find out the BUG.
Here is the problem uDebug Link.

C++ code: 

#include<iostream>
#include<math.h>
using namespace std;
int GCD(int num1,int num2){
    if(num1%num2==0){
        return num2;
    }else{
        return GCD(num2,num1%num2);
    }
}
class rational{
private:
    int numerator{0};
    int denominator{0};
public:
    void setData(int numerator,int denominator){
        this->numerator = numerator;
        this->denominator = denominator;
    }friend ostream& operator<<(ostream& out,rational &ob);
    rational operator+(rational ro){
        rational temp;
        temp.setData((numerator*ro.denominator+ro.numerator*denominator),(denominator*ro.denominator));
        return temp;
    }rational operator-(rational ro){
        rational temp;
        temp.setData((numerator*ro.denominator-ro.numerator*denominator),(denominator*ro.denominator));
        return temp;
    }rational operator*(rational ro){
        rational temp;
        temp.setData(numerator*ro.numerator,denominator*ro.denominator);
        return temp;
    }rational operator/(rational ro){
        rational temp;
        temp.setData(numerator*ro.denominator,ro.numerator*denominator);
        return temp;
    }void simplify(){
        int gcd = GCD(fabs(numerator),denominator);
        cout<<*this<<' ';
        if(gcd>1){
            numerator/=gcd;
            denominator/=gcd;
        }
    }

};
ostream& operator<<(ostream& out,rational &ob){
    cout<<ob.numerator<<'/'<<ob.denominator;
    return out;
}
char get(rational& r1,rational& r2){
    char ch,operator_;
    int n,d;
    cin>>n>>ch>>d;
    r1.setData(n,d);
    cin>>operator_;
    cin>>n>>ch>>d;
    r2.setData(n,d);
    return operator_;
}
int main(){
    rational r1,r2;
    int T;
    cin>>T;
    while(T--){
        char operator_=get(r1,r2);
        rational result;
        if(operator_=='+'){
            result = r1+r2;
        }else if(operator_=='-'){
            result = r1-r2;
        }else if(operator_=='*'){
            result = r1*r2;
        }else{
            result = r1/r2;
        }
        result.simplify();
        cout<<"= "<<result<<endl;
    }return 0;
}

মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

Big Big mod

শিরোনামহীন গল্প

Dictionaries and Maps