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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include "rational.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
Rational Rational::operator << (Rational r)
{
cout << r.getNumerator() << "/" << r.getDenominator();
}
Rational Rational::operator + (Rational r1, Rational r2)
{
int nm1, nm2, nmsum;
int dn;
Rational sum;
dn = r1.getDenominator() * r2.getDenominator();
nm1 = dn * r1.getNumerator();
nm2 = dn * r2.getNumerator();
sum.reduce(nmsum, dn);
sum.setDenominator(dn);
nmsum = nm1 + nm2;
sum.setNumerator(nmsum);
return sum;
}
Rational Rational::operator - (Rational r1, Rational r2)
{
int nm1, nm2, nmdiff;
int dn;
Rational diff;
dn = r1.getDenominator() * r2.getDenominator();
nm1 = dn * r1.getNumerator();
nm2 = dn * r2.getNumerator();
diff.reduce(nmdiff, dn);
diff.setDenominator(dn);
nmsum = nm1 - nm2;
diff.setNumerator(nmdiff);
return diff;
}
Rational Rational::operator * (Rational r1, Rational r2)
{
int nm;
int dn;
Rational prod;
dn = r1.getDenominator() * r2.getDenominator();
nm = r2.getNumerator() * r2.getNumerator();
prod.reduce(nm, dn);
prod.setNumerator(nm);
prod.setDenominator(dn);
return prod;
}
Rational Rational::operator / (Rational r1, Rational r2)
{
int nm;
int dn;
Rational quot;
nm = r1.getNumerator() * r2.getDenominator();
dn = r2.getNumerator() * r1.getDenominator();
quot.reduce(nm, dn);
quot.setNumerator(nm);
quot.setDenominator(dn);
return quot;
}
Rational Rational::operator = (Rational r1, Rational r2)
{
int nmTemp;
int dnTemp;
nmTemp1 = r1.getNumerator();
dnTemp1 = r1.getDenominator();
r2.setNumerator(nmTemp1);
r2.setDenominator(dnTemp1);
return r2;
}
bool Rational::operator < (Rational r1, Rational r2, bool compare)
{
int nm1, nm2;
int dn;
dn = r1.getDenominator() * r2.getDenominator();
nm1 = r1.getNumerator() * dn;
nm2 = r2.getNumerator() * dn;
if (nm1 < nm2)
{
compare = 1;
}
else
{
compare = 0;
}
return compare;
}
bool Rational::operator > (Rational r1, Rational r2, bool compare)
{
int nm1, nm2;
int dn;
dn = r1.getDenominator() * r2.getDenominator();
nm1 = r1.getNumerator() * dn;
nm2 = r2.getNumerator() * dn;
if (nm1 > nm2)
{
compare = 1;
}
else
{
compare = 0;
}
return compare;
}
bool Rational::operator == (Rational r1, Rational r2, bool compare)
{
int nm1, nm2;
int dn;
dn = r1.getDenominator() * r2.getDenominaror();
nm1 = r1.getNumerator() * dn;
nm2 = r2.getNumerator() * dn;
if (nm1 == nm2)
{
compare = 1;
}
else
{
compare = 0;
}
return compare;
}
|