Created
January 31, 2018 17:08
-
-
Save soachishti/c293c003fe85aea11ec2eebd241bd0a7 to your computer and use it in GitHub Desktop.
Implement Fraction class with operator overloading.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Name: Syed Owais Ali Chishti | |
Roll no: P14-6011 | |
Task: Implement Fraction class such that it take input like | |
1/2, 4/5, 7/81 | |
Then overload | |
ARITHMETIC | |
+, -, /, %, * | |
ASSIGNMENT | |
+=, -=, *=, /=, %= | |
INCREMENT - DECREMENT | |
post++, ++pre, post--, --pre | |
COMPARISION | |
<, >, <=, >=, !=, == | |
INSERTION EXTRACTION | |
>> , << | |
and also implement [] operator in safearray of Fraction | |
EXTRAS | |
!, &&, ||, ~, = | |
*/ | |
#include <iostream> | |
#include <cstdlib> | |
#pragma warning (disable : 4172) // Returning address of local variable or temporary | |
using namespace std; | |
class Fraction; | |
class safearray; | |
class Fraction{ | |
private: | |
int nominator, denominator; | |
public: | |
Fraction(int n = 0, int d = 1) { | |
nominator = n; | |
denominator = d; | |
simplify(); | |
} | |
void init(int n = 0, int d = 1) { | |
Fraction(n, d); | |
} | |
void simplify() { | |
if (denominator == 0) { | |
cout << "INFINITY"; | |
exit(0); | |
return; | |
} | |
int _gcd = gcd(nominator, denominator); | |
nominator = nominator / _gcd; | |
if (nominator == denominator) | |
{ | |
denominator = 1; | |
} | |
else { | |
denominator = denominator / _gcd; | |
} | |
} | |
void getFraction() { | |
cout << "Enter fraction (eg. 1/2): "; | |
char dummy; | |
getNominator(); | |
cin >> dummy; | |
getDenominator(); | |
simplify(); | |
} | |
void getNominator() { | |
cin >> nominator; | |
} | |
void getDenominator() { | |
cin >> denominator; | |
} | |
void changeNominator(int newValue) { | |
nominator = newValue; | |
} | |
void changeDenominator(int newValue) { | |
denominator = newValue; | |
} | |
void showFraction() { | |
simplify(); | |
if (denominator == 0) cout << "INFINITY!!!"; | |
else if (nominator == 0) cout << 0; | |
else if (denominator == 1) cout << nominator; | |
else cout << nominator << "/" << denominator << endl; | |
} | |
// Return LCM of two numbers | |
int LCM(int a, int b) { | |
int max = (a > b) ? a : b; | |
while (true) | |
{ | |
if (max % a == 0 && max % b == 0) return max; | |
else max++; | |
} | |
} | |
// Return GCD of two numbers | |
int gcd(int a, int b) { | |
int largeNo = (a > b) ? a : b; | |
while (largeNo) { | |
if (a % largeNo == 0 && b % largeNo == 0) return largeNo; | |
largeNo--; | |
} | |
return largeNo; | |
} | |
float getDecimal() { | |
return (static_cast<float>(nominator) / denominator); | |
} | |
// ARITHMETIC - Start | |
Fraction operator + (Fraction tmp) { | |
Fraction newFraction; | |
newFraction.denominator = LCM(denominator, tmp.denominator); | |
newFraction.nominator = (nominator * (denominator / denominator)) + (tmp.nominator * (denominator / tmp.denominator)); | |
return newFraction; | |
} | |
Fraction operator - (Fraction tmp) { | |
Fraction newFraction; | |
newFraction.denominator = LCM(denominator, tmp.denominator); | |
newFraction.nominator = (nominator * (denominator / denominator)) - (tmp.nominator * (denominator / tmp.denominator)); | |
return newFraction; | |
} | |
Fraction operator * (Fraction tmp) { | |
Fraction newFraction; | |
newFraction.nominator = nominator * tmp.nominator; | |
newFraction.denominator = denominator * tmp.denominator; | |
return newFraction; | |
} | |
Fraction operator / (Fraction tmp) { | |
Fraction newFraction; | |
newFraction.nominator = nominator * tmp.denominator; | |
newFraction.denominator = denominator * tmp.nominator; | |
return newFraction; | |
} | |
float operator % (int mod) { | |
float value = nominator / static_cast<float>(denominator); | |
if (value - static_cast<int>(value) > 0) | |
nominator = (static_cast<int>(value) % mod); | |
return value; | |
} | |
// ARITHMETIC - End | |
// ASSIGNMENT - Start | |
float operator %= (int mod) { | |
float value = nominator / static_cast<float>(denominator); | |
if (value - static_cast<int>(value) > 0) | |
return static_cast<float>((static_cast<int>(value) % mod)); | |
else | |
return -999; | |
} | |
Fraction& operator /= (Fraction tmp) { | |
Fraction newFraction; | |
newFraction.nominator = nominator * tmp.denominator; | |
newFraction.denominator = denominator * tmp.nominator; | |
return newFraction; | |
} | |
Fraction& operator += (Fraction tmp) { | |
Fraction newFraction; | |
newFraction.denominator = LCM(denominator, tmp.denominator); | |
newFraction.nominator = (nominator * (denominator / denominator)) + (tmp.nominator * (denominator / tmp.denominator)); | |
return newFraction; | |
} | |
Fraction& operator -= (Fraction tmp) { | |
Fraction newFraction; | |
newFraction.nominator = (nominator * tmp.denominator) - (tmp.nominator * denominator); | |
newFraction.denominator = LCM(denominator, tmp.denominator); | |
return newFraction; | |
} | |
Fraction& operator *= (Fraction tmp) { | |
Fraction newFraction; | |
newFraction.nominator = nominator * tmp.nominator; | |
newFraction.denominator = denominator * tmp.denominator; | |
return newFraction; | |
} | |
// ASSIGNMENT - End | |
// INCREMENT / DECREMENT - Start | |
Fraction& operator ++ () { | |
Fraction newFraction; | |
newFraction.denominator = LCM(denominator, 1); | |
newFraction.nominator = (nominator * (denominator / denominator)) + (1 * (denominator / 1)); | |
return newFraction; | |
} | |
Fraction& operator ++ (int) { | |
Fraction newFraction; | |
newFraction.nominator = nominator; | |
newFraction.denominator = denominator; | |
++(*this); | |
return newFraction; | |
} | |
Fraction& operator -- () { | |
Fraction newFraction; | |
newFraction.denominator = LCM(denominator, 1); | |
newFraction.nominator = (nominator * (denominator / denominator)) - (1 * (denominator / 1)); | |
return newFraction; | |
} | |
Fraction& operator -- (int) { | |
Fraction newFraction; | |
newFraction.nominator = nominator; | |
newFraction.denominator = denominator; | |
--(*this); | |
return newFraction; | |
} | |
// INCREMENT / DECREMENT - End | |
// COMPARISION - Start | |
bool operator > (Fraction tmp) { | |
return (getDecimal() > tmp.getDecimal()); | |
} | |
bool operator < (Fraction tmp) { | |
return (getDecimal() < tmp.getDecimal()); | |
} | |
bool operator == (Fraction tmp) { | |
return (getDecimal() == tmp.getDecimal()); | |
} | |
bool operator >= (Fraction tmp) { | |
return (getDecimal() >= tmp.getDecimal()); | |
} | |
bool operator <= (Fraction tmp) { | |
return (getDecimal() <= tmp.getDecimal()); | |
} | |
bool operator != (Fraction tmp) { | |
return (getDecimal() != tmp.getDecimal()); | |
} | |
// COMPARISION - End | |
// EXTRAS - Start | |
bool operator ! () { | |
return (!getDecimal()); | |
} | |
bool operator && (Fraction tmp) { | |
return (getDecimal() && tmp.getDecimal()); | |
} | |
bool operator || (Fraction tmp) { | |
return (getDecimal() && tmp.getDecimal()); | |
} | |
Fraction& operator = (Fraction tmp) { | |
nominator = tmp.nominator; | |
denominator = tmp.denominator; | |
return *this; | |
} | |
Fraction& operator ~ () { | |
nominator = ~nominator; | |
denominator = ~denominator; | |
return *this; | |
} | |
// EXTRAS - End | |
// INSERTION / EXTRACTION - Start | |
friend ostream& operator << (ostream& _cout, Fraction& f) { | |
if (f.denominator == 0) _cout << "INFINITY!!!"; | |
else if (f.nominator == 0) _cout << 0; | |
else if (f.denominator == 1) _cout << f.nominator; | |
else _cout << f.nominator << "/" << f.denominator << endl; | |
f.simplify(); | |
return _cout; | |
} | |
friend istream& operator >> (istream& _cin, Fraction& f) { | |
cout << "Enter fraction (eg. 1/2): "; | |
char dummy; | |
_cin >> f.nominator; | |
_cin >> dummy; | |
_cin >> f.denominator; | |
f.simplify(); | |
return _cin; | |
} | |
// INSERTION / EXTRACTION - End | |
}; | |
class safearray { | |
Fraction *f; | |
int size; | |
public: | |
safearray(int s = 10) : size(s) { | |
f = new Fraction[size]; | |
} | |
Fraction& operator [] (int i) { | |
if (i >= 0 && i < size) { | |
return f[i]; | |
} | |
else { | |
cout << "!!!Error - Out of Bound!!!"; | |
return f[0]; | |
} | |
} | |
~safearray() { | |
delete[] f; | |
} | |
}; | |
int main() { | |
safearray f(2); | |
f[0].init(1, 2); | |
f[1].init(1, 2); | |
Fraction f3(0, 1), dummy; | |
//f[0].getFraction(); | |
//f[1].getFraction(); | |
cin >> f[0]; // Insertion Operator | |
cin >> f[1]; | |
cout << endl; | |
cout << "First Value: " << f[0]; | |
cout << "First Value: " << f[1]; | |
cout << endl << endl; | |
// ADDITION | |
f3 = f[0] + f[1]; | |
f3 = (f[0] += f[1]); | |
//f3.showFraction(); | |
cout << "Addition: " << f3; | |
// SUBTACTION | |
f3 = f[0] - f[1]; | |
f3 = (f[0] -= f[1]); | |
cout << "Subtraction: " << f3 ; // Extraction Operator | |
cout << endl; | |
// MULTIPLICATION | |
f3 = f[0] * f[1]; | |
f3 = (f[0] *= f[1]); | |
cout << "Multiplication: " << f3; | |
// DIVISION | |
f3 = f[0] / f[1]; | |
f3 = (f[0] /= f[1]); | |
cout << "Division: "; | |
f3.showFraction(); | |
// MODULUS | |
//cout << f[0] % 4; | |
f[0] %= 4; | |
cout << "Modulus f[0] % 4: "; | |
f[0].showFraction(); | |
//POSTFIX WORKING | |
f3 = f[0]++; | |
cout << "Postfix ++: "; | |
f3.showFraction(); | |
f3 = f[0]--; | |
cout << "Postfix --: "; | |
f3.showFraction(); | |
//PREFIX WORKING | |
f3 = ++f[0]; | |
cout << "Prefix ++: "; | |
f3.showFraction(); | |
f3 = --f[0]; | |
cout << "Prefix --: "; | |
f3.showFraction(); | |
cout << endl; | |
//COMPARISION OPERATORS | |
if (f[0] == f[1]) | |
{ | |
cout << "f[0] and f[1] are equal" << endl; | |
} | |
if (f[0] > f[1]) | |
{ | |
cout << "f[0] is greater" << endl; | |
} | |
if (f[0] < f[1]) | |
{ | |
cout << "f[0] is less than f[1]" << endl; | |
} | |
if (f[0] >= f[1]) | |
{ | |
cout << "f[0] is greater or equal f[1]" << endl; | |
} | |
if (f[0] <= f[1]) | |
{ | |
cout << "f[0] is less than or equal f[1]" << endl; | |
} | |
if (f[0] != f[1]) | |
{ | |
cout << "f[0] is not equal to f[1]" << endl; | |
} | |
if (f[0] && f[1]) | |
{ | |
cout << "f[0] && f[1]" << endl; | |
} | |
if (f[0] || f[1]) | |
{ | |
cout << "f[0] || f[1]" << endl; | |
} | |
//Compliment | |
~f[0]; | |
cout << "Complimented Value: " << f[0]; | |
cout << endl | |
<< "Decimal Value: " | |
<< f[0].getDecimal() << endl << endl; | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment