Switch to long long

main
Micke Nordin 4 years ago
parent d534e766a3
commit ef7fbc5ae8

@ -11,11 +11,19 @@ int main() {
fraction q(a,b); fraction q(a,b);
fraction q2(c,d); fraction q2(c,d);
fraction q3(e); fraction q3(e);
cout.precision(10);
cout << a << "/" << b << '=' << q << endl; cout << a << "/" << b << '=' << q << endl;
cout << a << "/" << b << '=' << q.to_double() << endl;
cout << c << "/" << d << '=' << q2 << endl;
cout << c << "/" << d << '=' << q2.to_double() << endl;
cout << q << "+" << q2 << '=' << q + q2 << endl; cout << q << "+" << q2 << '=' << q + q2 << endl;
cout << q << "+" << q2 << '=' << (q + q2).to_double() << endl;
cout << q << "-" << q2 << '=' << q - q2 << endl; cout << q << "-" << q2 << '=' << q - q2 << endl;
cout << q << "-" << q2 << '=' << (q - q2).to_double() << endl;
cout << q << "*" << q2 << '=' << q * q2 << endl; cout << q << "*" << q2 << '=' << q * q2 << endl;
cout << q << "*" << q2 << '=' << (q * q2).to_double() << endl;
cout << q << "/" << q2 << '=' << q / q2 << endl; cout << q << "/" << q2 << '=' << q / q2 << endl;
cout << q << "/" << q2 << '=' << (q / q2).to_double() << endl;
cout << e << '=' << q3 << endl; cout << e << '=' << q3 << endl;
return 0; return 0;
} }

@ -5,9 +5,9 @@ using namespace std;
class fraction { class fraction {
private: private:
signed int n = 0; signed long long n = 0;
signed int d = 0; signed long long d = 1;
int gcd (int a, int b) const { signed long long gcd (signed long long a, signed long long b) const {
if (b == 0) { if (b == 0) {
return a; return a;
} }
@ -15,13 +15,18 @@ class fraction {
} }
int get_precision(double a) { int get_precision(double a) {
string s = to_string(a); string s = to_string(a);
bool dec = false; int i = s.length() - 1;
while (s[i] == '0'){
i--;
}
s.erase(i+1,s.length());
bool point = false;
int count = 0; int count = 0;
for(int i = 0; i < s.length(); i++) { for(int i = 0; i < s.length(); i++) {
if(s[i] == '.') { if(s[i] == '.') {
dec = true; point = true;
} else { } else {
if(dec) { if(point) {
count++; count++;
} }
} }
@ -31,66 +36,77 @@ class fraction {
public: public:
// Constructors // Constructors
fraction() { fraction() {
n = 0; }
d = 1; fraction(const fraction &q) {
signed long long hcf = gcd(q.n,q.d);
n = q.n / hcf;
d = q.d / hcf;
}
fraction(signed long long a, signed long long b) {
signed long long hcf = gcd(a,b);
n = a / hcf;
d = b / hcf;
} }
fraction(int a, int b) { fraction(int a, int b) {
int hcf = gcd(a,b); signed long long hcf = gcd(a,b);
n = a / hcf; n = (signed long long) a / hcf;
d = b / hcf; d = (signed long long) b / hcf;
} }
fraction(int a) { fraction(int a) {
n = a; fraction q(a, 1);
d = 1; n = q.n;
d = q.d;
} }
fraction(double dec) { fraction(double dec) {
if(dec < 0) {
dec = dec * (double) -1;
}
int precision = get_precision(dec); int precision = get_precision(dec);
int denominator = 1; signed long long denominator = 1;
for(int i = 0; i < precision; i++) { for(int i = 0; i < precision; i++) {
denominator *= 10; denominator *= 10;
} }
int numerator = dec * denominator; signed long long numerator = dec * denominator;
int hcf = gcd(numerator,denominator); fraction q(numerator, denominator);
n = numerator / hcf; n = q.n;
d = denominator / hcf; d = q.d;
}
fraction(const fraction &q) {
int hcf = gcd(q.n,q.d);
n = q.n / hcf;
d = q.d / hcf;
} }
// Member functions // Member functions
bool sign() const { int get_sign() const {
return (n >= 0); return (!(n >= 0) != !(d >=0)) ? -1 : 1;
} }
int get_n() const{ signed long long get_n() const {
return n; return n;
} }
int get_d() const{ signed long long get_d() const {
return d; return d;
} }
double to_double() const {
double dec = (double) n / (double) d ;
return dec;
}
// Operators // Operators
fraction operator+(const fraction &that) const { fraction operator+(const fraction &that) const {
int numerator = this->n * that.d + that.n * this->d; signed long long numerator = this->n * that.d + that.n * this->d;
int denominator = this->d * that.d; signed long long denominator = this->d * that.d;
fraction q(numerator, denominator); fraction q(numerator, denominator);
return q; return q;
} }
fraction operator-(const fraction &that) const { fraction operator-(const fraction &that) const {
int numerator = this->n * that.d - that.n * this->d; signed long long numerator = this->n * that.d - that.n * this->d;
int denominator = this->d * that.d; signed long long denominator = this->d * that.d;
fraction q(numerator, denominator); fraction q(numerator, denominator);
return q; return q;
} }
fraction operator*(const fraction &that) const { fraction operator*(const fraction &that) const {
int numerator = this->n * that.n; signed long long numerator = this->n * that.n;
int denominator = this->d * that.d; signed long long denominator = this->d * that.d;
fraction q(numerator, denominator); fraction q(numerator, denominator);
return q; return q;
} }
fraction operator*(const int i) const { fraction operator*(const int i) const {
int numerator = this->n * i; signed long long numerator = this->n * i;
int denominator = this->d; signed long long denominator = this->d;
fraction q(numerator, denominator); fraction q(numerator, denominator);
return q; return q;
} }
@ -105,7 +121,7 @@ class fraction {
} else if(q.d == 1) { } else if(q.d == 1) {
os << q.n; os << q.n;
} else { } else {
os << q.n << '/' << q.d; os << '(' << q.n << '/' << q.d << ')';
} }
return os; return os;
} }
@ -114,15 +130,16 @@ class fraction {
d = q.d; d = q.d;
} }
void operator=(const int i ) { void operator=(const int i ) {
n = i; n = (signed long long) i;
d = 1; d = (signed long long) 1;
} }
void operator=(const double dec ) { void operator=(const double dec ) {
const fraction q(dec); const fraction q(dec);
*this = q; n = q.n;
d = q.d;
} }
bool operator>(const fraction &q ) const { bool operator>(const fraction &q ) const {
int hcf = gcd(d,q.d); signed long long hcf = gcd(d,q.d);
fraction a(*this * hcf); fraction a(*this * hcf);
fraction b(q * hcf); fraction b(q * hcf);
return (a.n > b.n); return (a.n > b.n);
@ -132,23 +149,25 @@ class fraction {
return (*this > q); return (*this > q);
} }
bool operator==(const fraction &q ) const { bool operator==(const fraction &q ) const {
return ((n == q.n) && (d == q.d)); return ((n == q.n) && (d == q.d) );
} }
bool operator==(const int i) const { bool operator==(const int i) const {
return ((n == i) && (d == 1)); fraction q(i);
return (*this == q);
} }
bool operator==(const double dec) const { bool operator==(const double dec) const {
fraction a(dec); fraction q(dec);
return ((n == a.n) && (d == a.d)); return (*this == q);
} }
bool operator!=(const fraction &q ) const { bool operator!=(const fraction &q ) const {
return ((n != q.n) || (d != q.d)); return ( (n != q.n) || (d != q.d) );
} }
bool operator!=(const int i) const { bool operator!=(const int i) const {
return ((n != i) || (d != 1)); fraction q(i);
return (*this != q);
} }
bool operator!=(const double dec) const { bool operator!=(const double dec) const {
fraction a(dec); fraction q(dec);
return ((n != a.n) || (d != a.d)); return (*this != q);
} }
}; };

Loading…
Cancel
Save