A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Fraction Math in C++ page! Here, you'll find the source code for this program as well as a description of how the program works.
#include <iostream>
#include <string>
using namespace std;
int gcd(int a, int b)
{
if (a < 0)
{
a = -a;
}
while (a % b != 0)
{
int i = a;
int j = b;
a = j;
b = i % j;
}
return b;
}
class Fraction
{
private:
int numerator;
int denomenator;
public:
Fraction(int top = 0, int bottom = 1)
{
if (bottom < 0)
{
top = -top;
}
else if (bottom == 0)
{
throw "Error: Denomenator can't be zero.";
}
int hcf = gcd(top, bottom);
numerator = top / hcf;
denomenator = bottom / hcf;
}
Fraction operator-()
{
int top = -numerator;
int bottom = denomenator;
return Fraction(top, bottom);
}
int getnumerator()
{
return numerator;
}
int getdenometor()
{
return denomenator;
}
friend ostream &operator<<(ostream &stream, const Fraction &frac);
friend Fraction operator+(const Fraction f1, const Fraction f2);
friend Fraction operator+(const Fraction f1, const int f2);
friend Fraction operator+(const int f1, const Fraction f2);
friend Fraction operator+=(const Fraction f1, const Fraction f2);
friend Fraction operator-(const Fraction f1, const Fraction f2);
friend Fraction operator-(const Fraction f1, const int f2);
friend Fraction operator-(const int f1, const Fraction f2);
friend Fraction operator-=(const Fraction f1, const Fraction f2);
friend Fraction operator*(const Fraction f1, const Fraction f2);
friend Fraction operator*(const Fraction f1, const int f2);
friend Fraction operator*(const int f1, const Fraction f2);
friend Fraction operator*=(const Fraction f1, const Fraction f2);
friend Fraction operator/(const Fraction f1, const Fraction f2);
friend Fraction operator/(const Fraction f1, const int f2);
friend Fraction operator/(const int f1, const Fraction f2);
friend Fraction operator/=(const Fraction f1, const Fraction f2);
friend bool operator==(const Fraction f1, const Fraction f2);
friend bool operator<(const Fraction f1, const Fraction f2);
friend bool operator>(const Fraction f1, const Fraction f2);
friend bool operator!=(const Fraction f1, const Fraction f2);
friend bool operator<=(const Fraction f1, const Fraction f2);
friend bool operator>=(const Fraction f1, const Fraction f2);
};
ostream &operator<<(ostream &stream, const Fraction &frac)
{
stream << frac.numerator << "/" << frac.denomenator;
return stream;
}
Fraction operator+(const Fraction f1, const Fraction f2)
{
int top = f1.numerator * f2.denomenator + f2.numerator * f1.denomenator;
int bottom = f1.denomenator * f2.denomenator;
return Fraction(top, bottom);
}
Fraction operator-(const Fraction f1, const Fraction f2)
{
int top = f1.numerator * f2.denomenator - f2.numerator * f1.denomenator;
int bottom = f1.denomenator * f2.denomenator;
return Fraction(top, bottom);
}
Fraction operator*(const Fraction f1, const Fraction f2)
{
int top = f1.numerator * f2.numerator;
int bottom = f1.denomenator * f2.denomenator;
return Fraction(top, bottom);
}
Fraction operator/(const Fraction f1, const Fraction f2)
{
int top = f1.numerator * f2.denomenator;
int bottom = f1.denomenator * f2.numerator;
return Fraction(top, bottom);
}
Fraction operator+=(const Fraction f1, const Fraction f2)
{
return f1 + f2;
}
Fraction operator-=(const Fraction f1, const Fraction f2)
{
return f1 - f2;
}
Fraction operator*=(const Fraction f1, const Fraction f2)
{
return f1 * f2;
}
Fraction operator/=(const Fraction f1, const Fraction f2)
{
return f1 / f2;
}
Fraction operator+(const Fraction f1, const int f2)
{
int top = f1.numerator + f2 * f1.denomenator;
int bottom = f1.denomenator;
return Fraction(top, bottom);
}
Fraction operator+(const int f1, const Fraction f2)
{
int top = f1 * f2.denomenator + f2.numerator;
int bottom = f2.denomenator;
return Fraction(top, bottom);
}
Fraction operator-(const Fraction f1, const int f2)
{
int top = f1.numerator - f2 * f1.denomenator;
int bottom = f1.denomenator;
return Fraction(top, bottom);
}
Fraction operator-(const int f1, const Fraction f2)
{
int top = f1 * f2.denomenator - f2.numerator;
int bottom = f2.denomenator;
return Fraction(top, bottom);
}
Fraction operator*(const Fraction f1, const int f2)
{
int top = f1.numerator * f2;
int bottom = f1.denomenator;
return Fraction(top, bottom);
}
Fraction operator*(const int f1, const Fraction f2)
{
int top = f1 * f2.numerator;
int bottom = f2.denomenator;
return Fraction(top, bottom);
}
Fraction operator/(const Fraction f1, const int f2)
{
int top = f1.numerator;
int bottom = f1.denomenator * f2;
return Fraction(top, bottom);
}
Fraction operator/(const int f1, const Fraction f2)
{
int top = f2.numerator;
int bottom = f2.denomenator * f1;
return Fraction(top, bottom);
}
bool operator==(const Fraction f1, const Fraction f2)
{
int result = f1.numerator * f2.denomenator - f2.numerator * f1.denomenator;
if (result == 0)
{
return true;
}
else
{
return false;
}
}
bool operator>(const Fraction f1, const Fraction f2)
{
int result = f1.numerator * f2.denomenator - f2.numerator * f1.denomenator;
if (result > 0)
{
return true;
}
else
{
return false;
}
}
bool operator<(const Fraction f1, const Fraction f2)
{
int result = f1.numerator * f2.denomenator - f2.numerator * f1.denomenator;
if (result < 0)
{
return true;
}
else
{
return false;
}
}
bool operator>=(const Fraction f1, const Fraction f2)
{
return !(f1 < f2);
}
bool operator<=(const Fraction f1, const Fraction f2)
{
return !(f1 > f2);
}
bool operator!=(const Fraction f1, const Fraction f2)
{
return !(f1 == f2);
}
Fraction fromstr(string s)
{
int idx = s.find("/");
if (idx != string::npos)
{
string top = s.substr(0, idx);
string bottom = s.substr(idx + 1);
int p = stoi(top);
int q = stoi(bottom);
return Fraction(p, q);
}
return Fraction(stoi(s));
}
int main(int argc, char const *argv[])
{
// Testing above class methods
if (argc != 4)
{
cout << "Usage: " << argv[0] << " operand1 operator operand2";
exit(1);
}
string s1(argv[1]);
string op(argv[2]);
string s2(argv[3]);
Fraction o1 = fromstr(s1);
Fraction o2 = fromstr(s2);
if (op == "+")
{
cout << (o1 + o2) << endl;
}
else if (op == "-")
{
cout << (o1 - o2) << endl;
}
else if (op == "*")
{
cout << (o1 * o2) << endl;
}
else if (op == "/")
{
cout << (o1 / o2) << endl;
}
else if (op == "==")
{
cout << (o1 == o2) << endl;
}
else if (op == "!=")
{
cout << (o1 != o2) << endl;
}
else if (op == ">")
{
cout << (o1 > o2) << endl;
}
else if (op == "<")
{
cout << (o1 < o2) << endl;
}
else if (op == ">=")
{
cout << (o1 >= o2) << endl;
}
else if (op == "<=")
{
cout << (o1 <= o2) << endl;
}
else
{
cout << "Error: Invalid operand " << op << endl;
exit(1);
}
return 0;
}
Fraction Math in C++ was written by:
This article was written by:
If you see anything you'd like to change or update, please consider contributing.
Let's first take a look at the solution.
In our sample, we include two standard library utilities:
#include <iostream>
#include <string>
Here, we can see that we include the standard I/O for printing messages onto the screen and we include string for performing string operation.
Here, we have defined gcd
function which will give gcd of two numbers a
and b
using Euclid's Algorithm.
int gcd(int a, int b){
if(a<0){
a = -a;
}
while(a%b != 0){
int i = a;
int j = b;
a = j;
b = i%j;
}
return b;
}
From there, we defined the Fraction
class which we'll use to provide new data type for working with fractions:
class Fraction{
private:
int numerator;
int denomenator;
public:
Fraction(int top=0, int bottom=1){
if(bottom < 0){
top = -top;
}
else if(bottom == 0){
throw "Error: Denomenator can't be zero.";
}
int hcf = gcd(top, bottom);
numerator = top / hcf;
denomenator = bottom / hcf;
}
Here, we implement method to handle unary minus operator which just negate numerator:
Fraction operator -(){
int top = -numerator;
int bottom = denomenator;
return Fraction(top, bottom);
}
Below methods are just getters for the private attribute numerator and denomenator:
int getnumerator(){
return numerator;
}
int getdenometor(){
return denomenator;
}
Here, we implement friend
functions for Fraction
class to perform Arithmatic and Relational operation and displaying
output of fraction using cout
. This function just perform operator overloading.
friend ostream &operator<<(ostream &stream, const Fraction &frac);
friend Fraction operator+(const Fraction f1, const Fraction f2);
friend Fraction operator+(const Fraction f1, const int f2);
friend Fraction operator+(const int f1, const Fraction f2);
friend Fraction operator+=(const Fraction f1, const Fraction f2);
friend Fraction operator-(const Fraction f1, const Fraction f2);
friend Fraction operator-(const Fraction f1, const int f2);
friend Fraction operator-(const int f1, const Fraction f2);
friend Fraction operator-=(const Fraction f1, const Fraction f2);
friend Fraction operator*(const Fraction f1, const Fraction f2);
friend Fraction operator*(const Fraction f1, const int f2);
friend Fraction operator*(const int f1, const Fraction f2);
friend Fraction operator*=(const Fraction f1, const Fraction f2);
friend Fraction operator/(const Fraction f1, const Fraction f2);
friend Fraction operator/(const Fraction f1, const int f2);
friend Fraction operator/(const int f1, const Fraction f2);
friend Fraction operator/=(const Fraction f1, const Fraction f2);
friend bool operator==(const Fraction f1, const Fraction f2);
friend bool operator<(const Fraction f1, const Fraction f2);
friend bool operator>(const Fraction f1, const Fraction f2);
friend bool operator!=(const Fraction f1, const Fraction f2);
friend bool operator<=(const Fraction f1, const Fraction f2);
friend bool operator>=(const Fraction f1, const Fraction f2);
};
Here, we overloaded stream operator so that we can use cout directly on Fraction object.
ostream &operator <<(ostream &stream, const Fraction &frac){
stream << frac.numerator << "/" << frac.denomenator;
return stream;
}
Here, we have defined methods for addition operator for different cases. Examples:
Fraction(2, 3) + 6
Fraction(2, 3) + Fraction(4, 5)
7 + Fraction(4, 5)
Fraction operator+(const Fraction f1, const Fraction f2)
{
int top = f1.numerator * f2.denomenator + f2.numerator * f1.denomenator;
int bottom = f1.denomenator * f2.denomenator;
return Fraction(top, bottom);
}
Fraction operator+=(const Fraction f1, const Fraction f2)
{
return f1 + f2;
}
Fraction operator+(const Fraction f1, const int f2)
{
int top = f1.numerator + f2 * f1.denomenator;
int bottom = f1.denomenator;
return Fraction(top, bottom);
}
Fraction operator+(const int f1, const Fraction f2)
{
int top = f1 * f2.denomenator + f2.numerator;
int bottom = f2.denomenator;
return Fraction(top, bottom);
}
Here, we have defined methods for substraction operator for different cases. Examples:
Fraction(2, 3) - 6
Fraction(2, 3) - Fraction(4, 5)
7 - Fraction(4, 5)
Fraction operator-(const Fraction f1, const Fraction f2)
{
int top = f1.numerator * f2.denomenator - f2.numerator * f1.denomenator;
int bottom = f1.denomenator * f2.denomenator;
return Fraction(top, bottom);
}
Fraction operator-=(const Fraction f1, const Fraction f2)
{
return f1 - f2;
}
Fraction operator-(const Fraction f1, const int f2)
{
int top = f1.numerator - f2 * f1.denomenator;
int bottom = f1.denomenator;
return Fraction(top, bottom);
}
Fraction operator-(const int f1, const Fraction f2)
{
int top = f1 * f2.denomenator - f2.numerator;
int bottom = f2.denomenator;
return Fraction(top, bottom);
}
Here, we have defined methods for multiplication operator for different cases. Examples:
Fraction(2, 3) * 6
Fraction(2, 3) * Fraction(4, 5)
7 * Fraction(4, 5)
Fraction operator*(const Fraction f1, const Fraction f2)
{
int top = f1.numerator * f2.numerator;
int bottom = f1.denomenator * f2.denomenator;
return Fraction(top, bottom);
}
Fraction operator*=(const Fraction f1, const Fraction f2)
{
return f1 * f2;
}
Fraction operator*(const Fraction f1, const int f2)
{
int top = f1.numerator * f2;
int bottom = f1.denomenator;
return Fraction(top, bottom);
}
Fraction operator*(const int f1, const Fraction f2)
{
int top = f1 * f2.numerator;
int bottom = f2.denomenator;
return Fraction(top, bottom);
}
Here, we have defined methods for division operator for different cases. Examples:
Fraction(2, 3) / 6
Fraction(2, 3) / Fraction(4, 5)
7 / Fraction(4, 5)
Fraction operator/(const Fraction f1, const Fraction f2)
{
int top = f1.numerator * f2.denomenator;
int bottom = f1.denomenator * f2.numerator;
return Fraction(top, bottom);
}
Fraction operator/=(const Fraction f1, const Fraction f2)
{
return f1 / f2;
}
Fraction operator/(const Fraction f1, const int f2)
{
int top = f1.numerator;
int bottom = f1.denomenator * f2;
return Fraction(top, bottom);
}
Fraction operator/(const int f1, const Fraction f2)
{
int top = f2.numerator;
int bottom = f2.denomenator * f1;
return Fraction(top, bottom);
}
Here, we implemented ==
operator to check if two Fraction
s are equal to each other.
bool operator==(const Fraction f1, const Fraction f2)
{
int result = f1.numerator * f2.denomenator - f2.numerator * f1.denomenator;
if (result == 0)
{
return true;
}
else
{
return false;
}
}
Here, we implemented >
operator to check if first Fraction
is greater than the second Fraction
.
bool operator>(const Fraction f1, const Fraction f2)
{
int result = f1.numerator * f2.denomenator - f2.numerator * f1.denomenator;
if (result > 0)
{
return true;
}
else
{
return false;
}
}
Here, we implemented <
operator to check if first Fraction
is less than the second Fraction
.
bool operator<(const Fraction f1, const Fraction f2)
{
int result = f1.numerator * f2.denomenator - f2.numerator * f1.denomenator;
if (result < 0)
{
return true;
}
else
{
return false;
}
}
Here, we implemented >=
operator to check if first Fraction
is greater than or equal to the second Fraction
.
bool operator>=(const Fraction f1, const Fraction f2)
{
return !(f1 < f2);
}
Here, we implemented <=
operator to check if first Fraction
is less than or equal to the second Fraction
.
bool operator<=(const Fraction f1, const Fraction f2)
{
return !(f1 > f2);
}
Here, we implemented !=
operator to check if two Fraction
s aren't equal to each other.
bool operator!=(const Fraction f1, const Fraction f2)
{
return !(f1 == f2);
}
Here, we implemented function to perform conversion to Fraction
from string
object for that we are using find
method to find /
in operand to differentiate numerator from denomenator after that we are using stoi
to convert string
to integer and finally we are returning newly created Fraction object.
Example: "2/7" -> Fraction(2, 7)
Fraction fromstr(string s)
{
int idx = s.find("/");
if (idx != string::npos)
{
string top = s.substr(0, idx);
string bottom = s.substr(idx + 1);
int p = stoi(top);
int q = stoi(bottom);
return Fraction(p, q);
}
return Fraction(stoi(s));
}
Here, we take varible arguments in main
function from console to perform operation on fractions
if argc
(argument count) is not 4 that's ./fraction (default: ./a.out
) operand1 operator operand2 then it exit the program;
otherwise, it converts those arguments to string object so that we can perform string
to Fraction
conversion using fromstr
function on it.
int main(int argc, char const *argv[])
{
// Testing above class methods
if (argc != 4)
{
cout << "Usage: " << argv[0] << " operand1 operator operand2";
exit(1);
}
string s1(argv[1]);
string op(argv[2]);
string s2(argv[3]);
Fraction o1 = fromstr(s1);
Fraction o2 = fromstr(s2);
From there, we check which operator is sent in the argument and perform operations according to that operator. Example:
if op == "+"
then print o1 + o2
if op == "=="
then print o1 == o2
if (op == "+")
{
cout << (o1 + o2) << endl;
}
else if (op == "-")
{
cout << (o1 - o2) << endl;
}
else if (op == "*")
{
cout << (o1 * o2) << endl;
}
else if (op == "/")
{
cout << (o1 / o2) << endl;
}
else if (op == "==")
{
cout << (o1 == o2) << endl;
}
else if (op == "!=")
{
cout << (o1 != o2) << endl;
}
else if (op == ">")
{
cout << (o1 > o2) << endl;
}
else if (op == "<")
{
cout << (o1 < o2) << endl;
}
else if (op == ">=")
{
cout << (o1 >= o2) << endl;
}
else if (op == "<=")
{
cout << (o1 <= o2) << endl;
}
else
{
cout << "Error: Invalid operand " << op << endl;
exit(1);
}
return 0;
}
To run the fractions operation in cpp program, grab a copy of the fractions.cpp file from GitHub. After that, get the latest version of g++ compiler. Now, all you have to do is run the following from the command line:
g++ fractions.cpp -o fractions
./fractions "1/4" "+" "5/8"
Alternatively, you can always copy the source code into an online c++ compiler. Just make sure you pass some input to your program before you run it.