Last active
October 19, 2022 19:35
-
-
Save classmember/fc1bd262ca5e50a556f2bd478c1c64de to your computer and use it in GitHub Desktop.
A simple fraction class
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
// fraction.cpp, Kolby Heacock | |
// Description: A simple fraction class. | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
// Fraction | |
// takes two integers as parameters during construction | |
// Example: | |
// Fraction fraction(4, 20); | |
class Fraction { | |
private: | |
int num, den; | |
public: | |
Fraction(int n, int d) { | |
num = n; | |
den = d; | |
} | |
// convert fraction object to double value | |
operator double() const { | |
return double(num) / double(den); | |
} | |
// string representation of fraction | |
operator string() const { | |
return to_string(num) + "/" + to_string(den); | |
} | |
// add fraction objects | |
Fraction operator + (Fraction const &right) { | |
return Fraction(num + right.num, (den + right.den) / 2); | |
} | |
// subtract fraction objects | |
Fraction operator - (Fraction const &right) { | |
return Fraction(num - right.num, (den + right.den) / 2); | |
} | |
// multiply fraction objects | |
Fraction operator * (Fraction const &right) { | |
return Fraction(num * right.num, (den + right.den) / 2); | |
} | |
// divide fraction objects | |
Fraction operator / (Fraction const &right) { | |
return Fraction(num / right.num, (den + right.den) / 2); | |
} | |
// modulo fraction objects | |
Fraction operator % (Fraction const &right) { | |
return Fraction(num % right.num, (den + right.den) / 2); | |
} | |
// add fraction object to double | |
double operator + (double const right) { | |
return double(num) / double(den) + right; | |
} | |
// subtract double from fraction object | |
double operator - (double const right) { | |
return double(num) / double(den) - right; | |
} | |
// multiply double with fraction object | |
double operator * (double const right) { | |
return (double(num) / double(den)) * right; | |
} | |
// divide double by fraction object | |
double operator / (double const right) { | |
return (double(num) / double(den)) / right; | |
} | |
// modulus of double by fraction object | |
double operator % (double const right) { | |
return fmod((double(num) / double(den)), right); | |
} | |
// add fraction object to int | |
int operator + (int const right) { | |
return int(num) / int(den) + right; | |
} | |
// subtract int from fraction object | |
int operator - (int const right) { | |
return int(num) / int(den) - right; | |
} | |
// multiply int with fraction object | |
int operator * (int const right) { | |
return (int(num) / int(den)) * right; | |
} | |
// divide int by fraction object | |
int operator / (int const right) { | |
return (int(num) / int(den)) / right; | |
} | |
// modulus of int by fraction object | |
int operator % (int const right) { | |
return fmod((int(num) / int(den)), right); | |
} | |
}; | |
int main() { | |
Fraction f1(2, 5); | |
Fraction f2(4, 5); | |
Fraction f3(6, 7); | |
Fraction f4(14, 7); | |
cout << "Fractions can be added" << endl; | |
cout << string(f2 + f3) << " = " << string(f2) << " + " << string(f3) << endl << endl; // 10/6 = 4/5 + 6/7 | |
cout << "Fractions can be subtracted" << endl; | |
cout << string(f3 - f1) << " = " << string(f3) << " - " << string(f1) << endl << endl; // 4/6 = 6/7 - 2/5 | |
cout << "Fractions can be multiplied" << endl; | |
cout << string(f3 * f1) << " = " << string(f3) << " * " << string(f1) << endl << endl; // 3/6 = 6/7 / 2/5 | |
cout << "Fractions can be divided" << endl; | |
cout << string(f4 / f1) << " = " << string(f4) << " / " << string(f1) << endl << endl; // 4/6 = 6/7 / 2/5 | |
cout << "Fractions can be modulo'd" << endl; | |
cout << string(f4 % f3) << " = " << string(f4) << " % " << string(f3) << endl << endl; // 4/6 = 6/7 % 2/5 | |
cout << "Fractions can be used with floating point numbers" << endl; | |
double sum = f3 + f2 + f1; | |
cout << sum << " = " << string(f3) << " + " << string(f2) << " + " << string(f1) << endl; // 2.4 = 6/7 + 4/5 + 2/5 | |
cout << f2 + 0.1 + 2.9 << " = " << string(f2) << " + " << 0.1 << " + " << 2.9 << endl; // 3.8 = 4/5 + 0.1 + 2.9 | |
cout << f2 - 0.2 << " = " << string(f2) << " - " << 0.2 << endl; // 0.6 = 4/5 - 0.2 | |
cout << f2 * 2.0 << " = " << string(f2) << " * " << 0.2 << endl; // 1.6 = 4/5 * 0.2 | |
cout << f2 / 0.2 << " = " << string(f2) << " / " << 0.2 << endl; // 4 = 4/5 / 0.2 | |
cout << f4 % 3.0 << " = " << string(f4) << " % " << 3.0 << endl << endl; // 2 = 14/7 % 3 | |
cout << "Fractions can be used with integer numbers" << endl; | |
int sum2 = f3 + f2 + f1; | |
cout << sum2 << " = " << string(f3) << " + " << string(f2) << " + " << string(f1) << endl; // 2 = 6/7 + 4/5 + 2/5 | |
cout << f4 + 2 + 5 << " = " << string(f4) << " + " << 2 << " + " << 5 << endl; // 9 = 14/7 + 2 + 5 | |
cout << f4 - 1 << " = " << string(f4) << " - " << 1 << endl; // 1 = 14/7 - 1 | |
cout << f4 * 2 << " = " << string(f4) << " * " << 2 << endl; // 4 = 14/7 * 2 | |
cout << f4 / 2 << " = " << string(f4) << " / " << 2 << endl; // 1 = 14/7 / 2 | |
cout << f4 % 3 << " = " << string(f4) << " % " << 3 << endl << endl; // 2 = 14/7 % 3 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment