Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
0
Indexable
Never
#include <iostream>
#include "function.h" // include definition of class Rational
using namespace std;

/*int gcd(int a, int b){
    return (b == 0) ? a : gcd(b, a % b);
}*/

Rational::Rational(int a, int b){
    numerator=a;
    denominator=b;
    return;
}

void Rational::reduce(){
    int w;
    if(gcd(numerator,denominator)!=1){
        w=gcd(numerator,denominator);
        numerator /= w;
        denominator /= w;
    }
    if((numerator>0 && denominator<0) || (numerator<0 && denominator<0)){
        numerator *= -1;
        denominator *= -1;
    }
}

void Rational::printRational () const{
    cout<< numerator << "/" << denominator <<endl;
    return;
}

Rational Rational::multiplication( const Rational &d ) const{
    int big,small;
    Rational x;
    x.numerator= numerator*d.numerator;
    x.denominator=denominator*d.denominator;
    x.reduce();
    return x;
}

Rational Rational::addition( const Rational &d ) const{
    int big,small;
    Rational x;
    x.numerator=numerator*d.denominator + d.numerator*denominator;
    x.denominator=denominator*d.denominator;
    x.reduce();
    return x;
}