1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
#include "rational.h"
using namespace std;
Rational ( ) : num(0), den(1) { }
Rational ( int n) : num(n), den(1) { }
Rational (int n, int d) : num(n), den(d){if (d==0) den=1;}
Rational ( const Rational& r) : num(r.num), den(r.den) { }
int getNum ( ) const {return num;}
int getDen ( ) const {return den;}
void setNum(int n){num=n;}
void setDen(int d){
if(d==0) den=1;
else den=d;}
void set(int n, int d) {num=n; den=d;}
void reduce();
Rational operator +(const Rational& r){
Rational temp;
temp.num=r.num*t.den+r.den*t.num;
temp.den=r.den*t.den;
temp.reduce();
return temp;}
Rational operator -(const Rational& r, const Rational& t){
Rational temp;
temp.num=num*r.den-den*r.num;
temp.den=den*r.den;
temp.reduce();
return temp;}
Rational operator *(const Rational& r, const Rational& t){
Rational temp;
temp.num=num*r.num;
temp.den=den*r.den;
temp.reduce();
return temp;}
Rational operator /(const Rational& r, const Rational& t){
Rational temp;
temp.num=num*r.den;
temp.den=den*r.num;
temp.reduce();
return temp;}
bool isPositive( )const {
if(num*den > 0) return true;
return false;
}
bool isNegative( )const{
if(num*den < 0) return true;
return false;
}
/*cout << num << "/" << den << endl;}*/
ostream& operator << (ostream& outs, const Rational& R){
outs << r.num << r.den << endl;
return outs;
}
istream& operator >> (istream& ins, Rational& r){
char ch;
cout <<" Enter a numerator, then enter a '/', then enter a denominator"; ins >> r.num >> ch >> r.den << endl;
return ins:
}
/*{ char ch;
cin >> num >> ch >> den;
reduce( );}*/
int gcd( int p, int q)
{if (p==0) return q;
return gcd(q%p,p);}
void reduce( )
{int factor =gcd(num, den);
num/=factor; den/=factor;
if (den < 0) {
num=-num;
den=-den;}
}
| |