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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#include <iostream>
#include <iomanip>
using namespace std;
class RationalNumber{
private:
int numer;
int denom;
int gCD(int num1, int num2);
void reduce();
public:
RationalNumber(int num1, int num2);
RationalNumber(int num1);
RationalNumber add(RationalNumber& num);
RationalNumber sub(RationalNumber num);
RationalNumber mul(RationalNumber num);
RationalNumber div(RationalNumber num);
bool equals(RationalNumber num);
bool less_than(RationalNumber num);
bool greater_than(RationalNumber num);
RationalNumber input(istream& in);
void output(ostream& out);
};
void RationalNumber::reduce()
{
int r = 0, num1 = abs(numer), num2 = abs(denom);
r = gCD(num1, num2);
numer = numer / r;
denom = denom / r;
}
int gCD(int num1, int num2)
{
while (num1 != num2)
{
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
}
return num1;
}
RationalNumber::RationalNumber(int num1, int num2)
{
numer = num1;
denom = num2;
}
RationalNumber::RationalNumber(int num1)
{
numer = num1;
denom = 1;
}
RationalNumber RationalNumber::add(RationalNumber& num)
{
int num1 = 0, num2 = 0;
int x = 0;
x = num.denom;
num1 = num.numer *denom;
num2 = num.denom * denom;
num1 += numer * x;
RationalNumber added(num1, num2);
return added;
}
RationalNumber RationalNumber::sub(RationalNumber num)
{
int num1 = 0, num2 = 0;
int x = 0, a = 0;
x = num.denom;
a = denom;
numer = numer* x;
denom = denom * x;
num.numer = num.numer * a;
num.denom = num.denom * a;
num1 = numer - num.numer;
num2 = x*a;
RationalNumber subtracted(num1, num2);
return subtracted;
}
RationalNumber RationalNumber::mul(RationalNumber num)
{
int num1 = 0, num2 = 0;
num1 = numer * num.numer;
num2 = denom * num.denom;
RationalNumber multiplied(num1, num2);
return multiplied;
}
RationalNumber RationalNumber::div(RationalNumber num)
{
int num1 = 0, num2 = 0;
num1 = numer * num.denom;
num2 = denom * num.numer;
RationalNumber divided(num1, num2);
return divided;
}
bool RationalNumber::equals(RationalNumber num)
{
double a = 0, b = 0;
a = numer / denom;
b = num.numer / num.denom;
if (a = b)
return true;
else
return false;
}
bool RationalNumber::less_than(RationalNumber num)
{
double a = 0, b = 0;
a = numer / denom;
b = num.numer / num.denom;
if (a < b)
return true;
else
return false;
}
bool RationalNumber::greater_than(RationalNumber num)
{
double a = 0, b = 0;
a = numer / denom;
b = num.numer / num.denom;
if (a > b)
return true;
else
return false;
}
RationalNumber RationalNumber::input(istream& in)
{
cout << "For the Numerator: " << endl;
in >> numer;
cout << "and the Denomonator:" << endl;
in >> denom;
RationalNumber inputed(numer, denom);
return inputed;
}
void RationalNumber::output(ostream& out)
{
reduce();
if (numer < 0 && denom < 0)
out << numer *-1 << "/" << denom *-1 << endl;
else if (numer > 0 && denom < 0)
out << numer *-1 << "/" << denom *-1 << endl;
else if (numer == 0)
out << "Numer is zero, 0" << endl;
else if (denom == 0)
out << "Denom is zero, unavaliable" << endl;
else
out << numer << "/" << denom << endl;
}
int main()
{
RationalNumber op1 = RationalNumber(1, -3);
op1.output(cout);
RationalNumber op2 = RationalNumber(2);
op2.output(cout);
RationalNumber op3 = RationalNumber(-2, -4);
op3.output(cout);
RationalNumber sum = op1.add(op2);
cout << "sum op1 and 2 (5/3): ";
sum.output(cout);
RationalNumber difference = op2.sub(op3);
cout << "difference op2 and 3 (3/2): ";
difference.output(cout);
RationalNumber product = op1.mul(op3);
cout << "product op 1 3(-1/6): ";
product.output(cout);
RationalNumber quotient = op3.div(op2);
cout << "quotient 3 and 2 1/4: ";
quotient.output(cout);
bool lessThan = op1.less_than(op2);
bool greaterThan = op1.greater_than(op3);
bool equalTo = op1.equals(RationalNumber(2, -6));
bool equalTo2 = op1.equals(op2);
cout << lessThan << endl;
cout << greaterThan << endl;
cout << equalTo << endl;
cout << equalTo2 << endl;
}
|