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
|
#include <iostream>
#include <cstdlib>
using namespace std;
class Rational
{
public:
Rational();
Rational(int numerator1);
Rational(int numerator1, int denominator1);
friend Rational operator -(Rational rational1, Rational rational2);
friend Rational operator +(Rational rational1, Rational rational2);
friend Rational operator /(Rational rational1, Rational rational2);
friend Rational operator *(Rational rational1, Rational rational2);
friend bool operator <(Rational rational1, Rational rational2);
friend bool operator >(Rational rational1, Rational rational2);
friend bool operator ==(Rational rational1, Rational rational2);
friend bool operator >=(Rational rational1, Rational rational2);
friend ostream& operator <<(ostream& out,Rational& rational1);
friend istream& operator >>(istream& in, Rational& rational1);
private:
int numerator;
int denominator;
void rationalize_number(int& numerator, int& denominator);
};
int main()
{
Rational test1(24,6), test2(24);
Rational test4;
cout << test1<< endl;
cout << test2<< endl;
cout << test4<<endl;
cout << "testing input function" << endl;
cin >> test4;
cout << endl;
cout << test4 << endl;
system("pause");
}
Rational::Rational() : numerator(0), denominator(1)
{
//empty body
}
Rational::Rational(int numerator1)
{
numerator = abs(numerator1);
denominator = 1;
}
Rational::Rational(int numerator1, int denominator1)
{
rationalize_number(numerator1,denominator1);
numerator = numerator1;
denominator = denominator1;
}
ostream& operator <<(ostream& out,Rational& rational1)
{
out << rational1.numerator <<"/"<<rational1.denominator;
return out;
}
istream& operator >>(istream& in, Rational& rational1)
{
char tempChar;
int numerator = rational1.numerator;
int denominator = rational1.denominator;
in >> tempChar;
if(tempChar == '(')
{
in >> rational1.numerator
>> tempChar;
}
if(tempChar == ',')
{
in >> rational1.denominator
>> tempChar;
}
rationalize_number(numerator,denominator);
cout << rational1;
}
void Rational::rationalize_number(int& numerator, int& denominator)
{
int countDenom;
int countNum;
//cout << numerator<< "/" << denominator << endl;
if(numerator >= denominator)
{
for (countDenom = denominator; countDenom >= 2; countDenom--)
{ //cout << "in for" <<numerator<< "/" << denominator << endl;
//cout << "loop #" << countDenom << endl;
if(denominator%countDenom == 0 && numerator%countDenom == 0)
{
//cout << "if:"<<endl;
//cout << numerator<< "/" << denominator << endl;
denominator = denominator/countDenom;
numerator = numerator/countDenom;
}
//else
//{
// ;
//}
}
}
else if(denominator > numerator)
{
for (countNum = numerator; countNum >= 2; countNum--)
{
if(denominator%countNum == 0 && numerator%countNum == 0)
{
denominator = denominator/countNum;
numerator = numerator/countNum;
}
//else
//{
// ;
//}
}
}
}
|