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
|
<#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Rational
{
private:
int numerator;
int denominator;
public:
Rational();
Rational(int theNum, int theDen);
Rational(int theNum);
int getNumerator() const;
int getDenominator() const;
friend Rational operator+(const Rational &f1, const Rational &f2);
friend Rational operator-(const Rational &f1, const Rational &f2);
friend Rational operator*(const Rational &f1, const Rational &f2);
friend Rational operator/(const Rational &f1, const Rational &f2);
friend ostream& operator << (ostream& os, Rational& f1);
friend istream& operator >> (istream& is, Rational& f1);
friend bool operator== (const Rational &f1, const Rational &f2);
friend bool operator>= (const Rational &f1, const Rational &f2);
friend bool operator<= (const Rational &f1, const Rational &f2);
friend bool operator> (const Rational &f1, const Rational &f2);
};
Rational::Rational() : numerator(0), denominator(1)
{}
Rational::Rational(int n, int d)
{
numerator = n;
denominator = d;
}
Rational::Rational(int n)
{
numerator = n;
denominator = 1;
}
int Rational::getDenominator() const
{
return denominator;
}
int Rational::getNumerator() const
{
return numerator;
}
Rational operator+(const Rational &f1, const Rational &f2)
{
int ansNum = (f1.numerator*f2.denominator) + (f2.numerator*f1.denominator);
int ansDen = f1.denominator*f2.denominator;
return Rational(ansNum, ansDen);
}
Rational operator-(const Rational &f1, const Rational &f2)
{
int ansNum = (f1.numerator*f2.denominator) - (f2.numerator*f1.denominator);
int ansDen = f1.denominator*f2.denominator;
return Rational(ansNum, ansDen);
}
Rational operator*(const Rational &f1, const Rational &f2)
{
int ansNum = f1.numerator*f2.numerator;
int ansDen = f1.denominator*f2.denominator;
return Rational(ansNum, ansDen);
}
Rational operator/(const Rational &f1, const Rational &f2)
{
int ansNum = f1.numerator*f2.denominator;
int ansDen = f1.denominator*f2.numerator;
return Rational(ansNum, ansDen);
}
ostream& operator << (ostream& os, Rational& f1)
{
os << f1.numerator << "/" << f1.denominator;
return os;
}
istream& operator >> (istream& is, Rational& f1)
{
string fraction;
int i;
getline(is, fraction);
i = fraction.find('/');
cout << "Is at position number:" << i << endl;
string fract1 = fraction.substr(0,i);
string fract2 = fraction.substr(i + 1);
cout << fract1 << " " << fract2;
f1.numerator = stoi(fract1);
f1.denominator = stoi(fract2);
is >> f1.numerator;
is >> f1.denominator;
return is;
}
bool operator==(const Rational& f1, const Rational& f2)
{
int leftSide = (f1.numerator*f2.denominator);
int rightSide = (f1.denominator*f2.numerator);
return (leftSide == rightSide);
}
bool operator>= (const Rational &f1, const Rational &f2)
{
int leftSide = (f2.denominator*f1.numerator);
int rightSide = (f1.denominator*f2.numerator);
return (leftSide >= rightSide);
}
bool operator<= (const Rational &f1, const Rational &f2)
{
int leftSide = (f2.denominator*f1.numerator);
int rightSide = (f1.denominator*f2.numerator);
return (leftSide <= rightSide);
}
bool operator> (const Rational &f1, const Rational &f2)
{
int leftSide = (f2.denominator*f1.numerator);
int rightSide = (f1.denominator*f2.numerator);
return (leftSide > rightSide);
}
int main()
{
Rational n1(1, 2), n2, n3;
cout << n1 << endl;
cin >> n2; // Input 2/3
cout << n2 << endl;
cout << n1 + n2 << endl;
cout << n1 - n2 << endl;
cout << n1 * n2 << endl;
cout << n1 / n2 << endl;
if (n1 == n2)
cout << "n1 equals n2" << endl;
else if (n1 > n2)
cout << n1 << " greater than " << n2 << endl;
else
cout << n1 << " less than " << n2 << endl;
cin >> n3; // Input -1/3
cout << n3 << endl;
if (n3 >= 0)
cout << n3 << " is non-negative" << endl;
else if (n3 <= 0)
cout << n3 << " is non-positive" << endl;
system("pause");
return 0;
}
|