More operators and better output

main
Micke Nordin 5 years ago
parent a5c5b1e955
commit 3878684f85

Binary file not shown.

@ -7,12 +7,15 @@ int main() {
int b = 3; int b = 3;
int c = 1; int c = 1;
int d = 11; int d = 11;
double e = 0.75;
fraction q(a,b); fraction q(a,b);
fraction q2(c,d); fraction q2(c,d);
fraction q3(e);
cout << a << "/" << b << '=' << q << endl; cout << a << "/" << b << '=' << q << endl;
cout << q << "+" << q2 << '=' << q + q2 << endl; cout << q << "+" << q2 << '=' << q + q2 << endl;
cout << q << "-" << q2 << '=' << q - q2 << endl; cout << q << "-" << q2 << '=' << q - q2 << endl;
cout << q << "*" << q2 << '=' << q * q2 << endl; cout << q << "*" << q2 << '=' << q * q2 << endl;
cout << q << "/" << q2 << '=' << q / q2 << endl; cout << q << "/" << q2 << '=' << q / q2 << endl;
cout << e << '=' << q3 << endl;
return 0; return 0;
} }

@ -1,4 +1,5 @@
#include <iostream> #include <iostream>
#include <string>
using namespace std; using namespace std;
@ -22,38 +23,81 @@ class fraction {
return gcd(a, b-a); return gcd(a, b-a);
} }
int get_precision(double a) {
string s = to_string(a);
bool dec = false;
int count = 0;
for(int i = 0; i < s.length(); i++) {
if(s[i] == '.') {
dec = true;
} else {
if(dec) {
count++;
}
}
}
return count;
}
public: public:
// Constructors // Constructors
fraction() {
n = 0;
d = 1;
}
fraction(int a, int b) { fraction(int a, int b) {
int hcf = gcd(a,b); int hcf = gcd(a,b);
n = a / hcf; n = a / hcf;
d = b / hcf; d = b / hcf;
} }
fraction(int a) {
n = a;
d = 1;
}
fraction(double dec) {
int precision = get_precision(dec);
int denominator = 1;
for(int i = 0; i < precision; i++) {
denominator *= 10;
}
int numerator = dec * denominator;
int hcf = gcd(numerator,denominator);
n = numerator / hcf;
d = denominator / hcf;
}
fraction(const fraction &q) { fraction(const fraction &q) {
int hcf = gcd(q.n,q.d); int hcf = gcd(q.n,q.d);
n = q.n / hcf; n = q.n / hcf;
d = q.d / hcf; d = q.d / hcf;
} }
// Member functions
bool sign() const {
return (n >= 0);
}
// Operators // Operators
fraction operator+(const fraction &that) { fraction operator+(const fraction &that) const {
int numerator = this->n * that.d + that.n * this->d; int numerator = this->n * that.d + that.n * this->d;
int denominator = this->d * that.d; int denominator = this->d * that.d;
fraction q(numerator, denominator); fraction q(numerator, denominator);
return q; return q;
} }
fraction operator-(const fraction &that) { fraction operator-(const fraction &that) const {
int numerator = this->n * that.d - that.n * this->d; int numerator = this->n * that.d - that.n * this->d;
int denominator = this->d * that.d; int denominator = this->d * that.d;
fraction q(numerator, denominator); fraction q(numerator, denominator);
return q; return q;
} }
fraction operator*(const fraction &that) { fraction operator*(const fraction &that) const {
int numerator = this->n * that.n; int numerator = this->n * that.n;
int denominator = this->d * that.d; int denominator = this->d * that.d;
fraction q(numerator, denominator); fraction q(numerator, denominator);
return q; return q;
} }
fraction operator/(const fraction &that) { fraction operator*(const int i) const {
fraction a(i);
fraction q(*this * a);
return q;
}
fraction operator/(const fraction &that) const {
fraction a(this->n,this->d); fraction a(this->n,this->d);
fraction b(that.d,that.n); fraction b(that.d,that.n);
return a * b; return a * b;
@ -61,9 +105,53 @@ class fraction {
friend ostream &operator<<( ostream &os, const fraction &q ) { friend ostream &operator<<( ostream &os, const fraction &q ) {
if( q.d == q.n) { if( q.d == q.n) {
os << 1; os << 1;
} else if(q.d == 1) {
os << q.n;
} else { } else {
os << q.n << '/' << q.d; os << q.n << '/' << q.d;
} }
return os; return os;
} }
void operator=(const fraction &q ) {
n = q.n;
d = q.n;
}
void operator=(const int i ) {
n = i;
d = 1;
}
void operator=(const double dec ) {
const fraction q(dec);
*this = q;
}
bool operator>(const fraction &q ) const {
int hcf = gcd(d,q.d);
fraction a(*this * hcf);
fraction b(q * hcf);
return (a.n > b.n);
}
bool operator>(const int i ) const {
fraction q(i);
return (*this > q);
}
bool operator==(const fraction &q ) const {
return ((n == q.n) && (d == q.d));
}
bool operator==(const int i) const {
return ((n == i) && (d == 1));
}
bool operator==(const double dec) const {
fraction a(dec);
return ((n == a.n) && (d == a.d));
}
bool operator!=(const fraction &q ) const {
return ((n != q.n) || (d != q.d));
}
bool operator!=(const int i) const {
return ((n != i) || (d != 1));
}
bool operator!=(const double dec) const {
fraction a(dec);
return ((n != a.n) || (d != a.d));
}
}; };

Binary file not shown.
Loading…
Cancel
Save