Indentation

main
Micke Nordin 2 years ago
parent 8dda4fe71b
commit aec0995aaf
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -5,181 +5,175 @@
using namespace std; using namespace std;
class fraction { class fraction {
private: private:
signed long long n = 0; signed long long n = 0;
signed long long d = 1; signed long long d = 1;
signed long long gcd (signed long long a, signed long long 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;
} }
return gcd(b, a % b); return gcd(b, a % b);
} }
int get_precision(double a) { int get_precision(double a) {
string s = to_string(a); string s = to_string(a);
int i = s.length() - 1; int i = s.length() - 1;
while (s[i] == '0'){ while (s[i] == '0') {
i--; i--;
} }
s.erase(i+1,s.length()); s.erase(i + 1, s.length());
bool point = false; 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] == '.') {
point = true; point = true;
} else { } else {
if(point) { if (point) {
count++; count++;
} }
} }
} }
return count; return count;
} }
public:
// Constructors public:
fraction() { // Constructors
} fraction() {}
fraction(const fraction &q) { fraction(const fraction &q) {
signed long long hcf = gcd(q.n,q.d); signed long long hcf = gcd(q.n, q.d);
n = q.n / hcf; n = q.n / hcf;
d = q.d / hcf; d = q.d / hcf;
} }
fraction(signed long long a, signed long long b) { fraction(signed long long a, signed long long b) {
signed long long hcf = gcd(a,b); signed long long hcf = gcd(a, b);
n = a / hcf; n = a / hcf;
d = b / hcf; d = b / hcf;
} }
fraction(int a, int b) { fraction(int a, int b) {
signed long long hcf = gcd(a,b); signed long long hcf = gcd(a, b);
n = (signed long long) a / hcf; n = (signed long long)a / hcf;
d = (signed long long) b / hcf; d = (signed long long)b / hcf;
} }
fraction(int a) { fraction(int a) {
fraction q(a, 1); fraction q(a, 1);
n = q.n; n = q.n;
d = q.d; d = q.d;
} }
fraction(double dec) { fraction(double dec) {
int precision = get_precision(dec); int precision = get_precision(dec);
signed long long 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;
} }
signed long long numerator = dec * denominator; signed long long numerator = dec * denominator;
fraction q(numerator, denominator); fraction q(numerator, denominator);
n = q.n; n = q.n;
d = q.d; d = q.d;
} }
// Member functions // Member functions
int get_sign() const { int get_sign() const { return (!(n >= 0) != !(d >= 0)) ? -1 : 1; }
return (!(n >= 0) != !(d >=0)) ? -1 : 1; signed long long get_n() const { return n; }
} signed long long get_d() const { return d; }
signed long long get_n() const { double to_double() const {
return n; double dec = (double)n / (double)d;
} return dec;
signed long long get_d() const { }
return d; // Operators
} fraction operator+(const fraction &that) const {
double to_double() const { signed long long numerator = this->n * that.d + that.n * this->d;
double dec = (double) n / (double) d ; signed long long denominator = this->d * that.d;
return dec; fraction q(numerator, denominator);
} return q;
// Operators }
fraction operator+(const fraction &that) const { fraction operator-(const fraction &that) const {
signed long long numerator = this->n * that.d + that.n * this->d; signed long long numerator = this->n * that.d - that.n * this->d;
signed long long 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 {
signed long long numerator = this->n * that.d - that.n * this->d; signed long long numerator = this->n * that.n;
signed long long 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 int i) const {
signed long long numerator = this->n * that.n; signed long long numerator = this->n * i;
signed long long denominator = this->d * that.d; signed long long denominator = this->d;
fraction q(numerator, denominator); fraction q(numerator, denominator);
return q; return q;
} }
fraction operator*(const int i) const { fraction operator/(const fraction &that) const {
signed long long numerator = this->n * i; fraction a(this->n, this->d);
signed long long denominator = this->d; fraction b(that.d, that.n);
fraction q(numerator, denominator); return a * b;
return q; }
} friend ostream &operator<<(ostream &os, const fraction &q) {
fraction operator/(const fraction &that) const { signed long long num = q.n;
fraction a(this->n,this->d); signed long long den = q.d;
fraction b(that.d,that.n); string s = "";
return a * b; if (q.n < 0) {
} s = "-";
friend ostream &operator<<( ostream &os, const fraction &q ) { num = num * -1;
signed long long num = q.n; }
signed long long den = q.d; if (q.d < 0) {
string s = ""; s = "-";
if(q.n < 0) { den = den * -1;
s = "-"; }
num = num * -1; if (q.get_sign() == 1) {
} string s = "";
if(q.d < 0) { }
s = "-"; if (q.d == q.n) {
den = den * -1; os << s << 1;
} } else if (q.d == 1) {
if(q.get_sign() == 1) { os << s << num;
string s = ""; } else {
} os << s << '(' << num << '/' << den << ')';
if( q.d == q.n) { }
os << s << 1; return os;
} else if(q.d == 1) { }
os << s << num; void operator=(const fraction &q) {
} else { n = q.n;
os << s << '(' << num << '/' << den << ')'; d = q.d;
} }
return os; void operator=(const int i) {
} n = (signed long long)i;
void operator=(const fraction &q ) { d = (signed long long)1;
n = q.n; }
d = q.d; void operator=(const double dec) {
} const fraction q(dec);
void operator=(const int i ) { n = q.n;
n = (signed long long) i; d = q.d;
d = (signed long long) 1; }
} bool operator>(const fraction &q) const {
void operator=(const double dec ) { signed long long hcf = gcd(d, q.d);
const fraction q(dec); fraction a(*this * hcf);
n = q.n; fraction b(q * hcf);
d = q.d; return (a.n > b.n);
} }
bool operator>(const fraction &q ) const { bool operator>(const int i) const {
signed long long hcf = gcd(d,q.d); fraction q(i);
fraction a(*this * hcf); return (*this > q);
fraction b(q * hcf); }
return (a.n > b.n); bool operator==(const fraction &q) const {
} return ((n == q.n) && (d == q.d));
bool operator>(const int i ) const { }
fraction q(i); bool operator==(const int i) const {
return (*this > q); fraction q(i);
} return (*this == q);
bool operator==(const fraction &q ) const { }
return ((n == q.n) && (d == q.d) ); bool operator==(const double dec) const {
} fraction q(dec);
bool operator==(const int i) const { return (*this == q);
fraction q(i); }
return (*this == q); bool operator!=(const fraction &q) const {
} return ((n != q.n) || (d != q.d));
bool operator==(const double dec) const { }
fraction q(dec); bool operator!=(const int i) const {
return (*this == q); fraction q(i);
} return (*this != q);
bool operator!=(const fraction &q ) const { }
return ( (n != q.n) || (d != q.d) ); bool operator!=(const double dec) const {
} fraction q(dec);
bool operator!=(const int i) const { return (*this != q);
fraction q(i); }
return (*this != q);
}
bool operator!=(const double dec) const {
fraction q(dec);
return (*this != q);
}
}; };

Loading…
Cancel
Save