Indentation

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

@ -5,10 +5,10 @@
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;
} }
@ -17,41 +17,41 @@ class fraction {
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:
public:
// Constructors // Constructors
fraction() { 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);
@ -61,7 +61,7 @@ class fraction {
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;
@ -70,17 +70,11 @@ class fraction {
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 {
return n;
}
signed long long get_d() const {
return d;
}
double to_double() const { double to_double() const {
double dec = (double) n / (double) d ; double dec = (double)n / (double)d;
return dec; return dec;
} }
// Operators // Operators
@ -109,59 +103,59 @@ class fraction {
return q; return q;
} }
fraction operator/(const fraction &that) const { 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;
} }
friend ostream &operator<<( ostream &os, const fraction &q ) { friend ostream &operator<<(ostream &os, const fraction &q) {
signed long long num = q.n; signed long long num = q.n;
signed long long den = q.d; signed long long den = q.d;
string s = ""; string s = "";
if(q.n < 0) { if (q.n < 0) {
s = "-"; s = "-";
num = num * -1; num = num * -1;
} }
if(q.d < 0) { if (q.d < 0) {
s = "-"; s = "-";
den = den * -1; den = den * -1;
} }
if(q.get_sign() == 1) { if (q.get_sign() == 1) {
string s = ""; string s = "";
} }
if( q.d == q.n) { if (q.d == q.n) {
os << s << 1; os << s << 1;
} else if(q.d == 1) { } else if (q.d == 1) {
os << s << num; os << s << num;
} else { } else {
os << s << '(' << num << '/' << den << ')'; os << s << '(' << num << '/' << den << ')';
} }
return os; return os;
} }
void operator=(const fraction &q ) { void operator=(const fraction &q) {
n = q.n; n = q.n;
d = q.d; d = q.d;
} }
void operator=(const int i ) { void operator=(const int i) {
n = (signed long long) i; n = (signed long long)i;
d = (signed long long) 1; d = (signed long long)1;
} }
void operator=(const double dec ) { void operator=(const double dec) {
const fraction q(dec); const fraction q(dec);
n = q.n; n = q.n;
d = q.d; d = q.d;
} }
bool operator>(const fraction &q ) const { bool operator>(const fraction &q) const {
signed long long 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);
} }
bool operator>(const int i ) const { bool operator>(const int i) const {
fraction q(i); fraction q(i);
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 {
fraction q(i); fraction q(i);
@ -171,8 +165,8 @@ class fraction {
fraction q(dec); fraction q(dec);
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 {
fraction q(i); fraction q(i);

Loading…
Cancel
Save