Skip to content

Instantly share code, notes, and snippets.

@zekroTJA
Last active March 12, 2018 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zekroTJA/2ec3ac9e6937703aad3098bfe5ef898e to your computer and use it in GitHub Desktop.
Save zekroTJA/2ec3ac9e6937703aad3098bfe5ef898e to your computer and use it in GitHub Desktop.
A simple fraction class for c++ created while bored in scool class
/*
Simple Fraction Class v.1.2
© 2018 Ringo Hoffmann
~ USAGE ~
Initializing:
Fraction f1(1, 2);
Fraction f2(3.1415);
Output:
cout << f1.str();
cout << f1.dec();
cout << f1;
Calculating:
Fraction f3 = f1 + f2;
Fraction f4 = f1 - f2;
Fraction f5 = f1 * f2;
...
Also cross-type calculations are
possible:
Fraction f6 = 3 * f1 + f2 / 7
*/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Fraction {
private:
long z, n;
public:
/**
Cretae a Fraction instance with nominator
and denominator as parameters.
@param {long} _z numerator
@param {long} _n denumerator
*/
Fraction(long _z = 1, long _n = 1) {
z = _z;
n = _n;
shorten();
}
/**
Cretae a Fraction with a integer number.
@param {long} number
*/
Fraction(int _z) {
z = _z;
n = 1;
}
/**
Create a Fraction by decimal number (doule).
@param {double} _d decimal number
*/
Fraction(double _d) {
// n = 1e5;
// z = (long) (_d * n);
string s = to_string(_d);
double tmp = _d;
long mult = 1;
while (s.find(".000000") == string::npos) {
tmp *= 10;
mult *= 10;
s = to_string(tmp);
}
n = mult;
z = (long) (_d * n);
shorten();
}
/**
Shorts a Fraction.
Generally not needed to proceed, because
Fractions getting shortened by initializing
them.
*/
void shorten() {
long p = z;
long q = n;
long r = p % q;
while (r != 0) {
p = q;
q = r;
r = p % q;
}
z /= q;
n /= q;
}
/**
Return a Fraction as formatted string.
@return {string} Fraction as sring
*/
string str() {
stringstream t;
if (z % n == 0)
return to_string(z / n);
t << z << "/" << n;
return t.str();
}
/**
Return a Fraction as decimal number.
@return {double} Fraction as number
*/
double dec() {
return (double) z / n;
}
/**
Getting numerator as number.
@return {long} numerator
*/
long getZ() {
return z;
}
/**
Getting denumerator as number.
@return {long} denumerator
*/
long getN() {
return n;
}
// OPERATOR +
friend Fraction operator+(const Fraction &f1, const Fraction &f2) {
long _z = f1.z * f2.n + f1.n * f2.z;
long _n = f1.n * f2.n;
return Fraction(_z, _n);
}
friend Fraction operator+(const Fraction &f1, const double &d) {
Fraction f2 = Fraction(d);
long _z = f1.z * f2.n + f1.n * f2.z;
long _n = f1.n * f2.n;
return Fraction(_z, _n);
}
friend Fraction operator+(const double &d, const Fraction &f2) {
Fraction f1 = Fraction(d);
long _z = f1.z * f2.n + f1.n * f2.z;
long _n = f1.n * f2.n;
return Fraction(_z, _n);
}
// OPERATOR -
friend Fraction operator-(const Fraction &f1, const Fraction &f2) {
long _z = f1.z * f2.n - f1.n * f2.z;
long _n = f1.n * f2.n;
return Fraction(_z, _n);
}
friend Fraction operator-(const double &d, const Fraction &f2) {
Fraction f1 = Fraction(d);
long _z = f1.z * f2.n - f1.n * f2.z;
long _n = f1.n * f2.n;
return Fraction(_z, _n);
}
friend Fraction operator-(const Fraction &f1, const double &d) {
Fraction f2 = Fraction(d);
long _z = f1.z * f2.n - f1.n * f2.z;
long _n = f1.n * f2.n;
return Fraction(_z, _n);
}
// OPERATOR *
friend Fraction operator*(const Fraction &f1, const Fraction &f2) {
long _z = f1.z * f2.z;
long _n = f1.n * f2.n;
return Fraction(_z, _n);
}
friend Fraction operator*(const double &d, const Fraction &f2) {
Fraction f1 = Fraction(d);
long _z = f1.z * f2.z;
long _n = f1.n * f2.n;
return Fraction(_z, _n);
}
friend Fraction operator*(const Fraction &f1, const double &d) {
Fraction f2 = Fraction(d);
long _z = f1.z * f2.z;
long _n = f1.n * f2.n;
return Fraction(_z, _n);
}
// OPERATOR /
friend Fraction operator/(const Fraction &f1, const Fraction &f2) {
long _z = f1.z * f2.n;
long _n = f1.n * f2.z;
return Fraction(_z, _n);
}
friend Fraction operator/(const double &d, const Fraction &f2) {
Fraction f1 = Fraction(d);
long _z = f1.z * f2.n;
long _n = f1.n * f2.z;
return Fraction(_z, _n);
}
friend Fraction operator/(const Fraction &f1, const double &d) {
Fraction f2 = Fraction(d);
long _z = f1.z * f2.n;
long _n = f1.n * f2.z;
return Fraction(_z, _n);
}
// OPERATOR <<
friend ostream & operator<<(ostream& stream, const Fraction &f) {
if (f.z % f.n == 0)
stream << (f.z / f.n);
else
stream << f.z << "/" << f.n;
}
};
#include <iostream>
#include <string>
#include "fraction.h"
using namespace std;
int main() {
// Initialization
Fraction f1(1, 2);
Fraction f2(0.125);
// Operations
// - addition
Fraction s_add1 = f1 + f2;
Fraction s_add2 = f1 + 3.25;
// - substraction
Fraction s_sub1 = f1 - f2;
Fraction s_sub2 = 3.125 - f2;
// - multiplication
Fraction s_mult1 = f1 * f2;
Fraction s_mult2 = f2 * 2.45;
// - division
Fraction s_div1 = f1 / f2;
Fraction s_div2 = 2.75 / f1;
// Output
cout << "ADD 1: " << s_add1 << " (" << s_add1.dec() << ")\n"
<< "ADD 2: " << s_add2 << " (" << s_add2.dec() << ")\n"
<< "SUB 1: " << s_sub1 << " (" << s_sub1.dec() << ")\n"
<< "SUB 2: " << s_sub2 << " (" << s_sub2.dec() << ")\n"
<< "MLT 1: " << s_mult1 << " (" << s_mult1.dec() << ")\n"
<< "MLT 2: " << s_mult2 << " (" << s_mult2.dec() << ")\n"
<< "DIV 1: " << s_div1 << " (" << s_div1.dec() << ")\n"
<< "DIV 2: " << s_div2 << " (" << s_div2.dec() << ")\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment