diff --git a/as1/Fraction.h b/as1/Fraction.h index fdc6773..06e698b 100644 --- a/as1/Fraction.h +++ b/as1/Fraction.h @@ -60,6 +60,11 @@ class Fraction { return Fraction(this->top() * obj.bottom() + this->bottom() * obj.top(), this->bottom() * obj.bottom()); }; + /* 加法重载(后) */ + friend Fraction operator +(const double d, const Fraction& obj){ + return obj + d; + }; + /* 减法重载 */ Fraction operator -(const Fraction& obj) const{ return Fraction(this->top() * obj.bottom() - this->bottom() * obj.top(), this->bottom() * obj.bottom()); @@ -70,6 +75,11 @@ class Fraction { return Fraction(this->top() * obj.top(), this->bottom() * obj.bottom()); }; + /* 乘法重载(后) */ + friend Fraction operator *(const double d, const Fraction& obj){ + return obj * d; + }; + /* 除法重载 */ Fraction operator /(const Fraction& obj) const{ return Fraction(this->top() * obj.bottom(), this->bottom() * obj.top()); @@ -396,7 +406,7 @@ class Fraction { void Fraction::dec_to_frac_direct(double d) { unsigned short t = get_long_int_length(get_double_decimals_part(d)); - + //cout << t << endl; _top = d * (int)pow(10, t); _bottom = (unsigned int)pow(10, t); } @@ -469,7 +479,7 @@ unsigned long int Fraction::transfer_double_decimals_to_unsigned_long_int(const while(d != 0 && (t % (t/10) == 0 || t % (t/10) == 9) && t > 100) t /= 10; - if((unsigned long int)(d*(1000000000)) % 1000 == 999) return ++t; + //if((unsigned long int)(d*(1000000000)) % 1000 == 999) return ++t; return t; } @@ -549,7 +559,8 @@ void Fraction::confirm_repeat_part(const unsigned long int& t, unsigned int& rep if(k){ unsigned short t_repeat = t / (unsigned int)pow(10, i -1 - len) - (t / (unsigned int)pow(10, i - 1)) * (unsigned int)pow(10, len); - if(t_repeat == repeat && repeat < 100) start = i + 1; + if(t_repeat == repeat && repeat < 10) start = i; + else if(t_repeat == repeat && repeat < 100) start = i + 1; else if(t_repeat == repeat && repeat < 1000) start = i + 2; else start = i; repeat = t_repeat; @@ -577,11 +588,11 @@ void Fraction::repeated_decimal_to_fraction(double d) unsigned int repeat = get_repeat_part(t); - cout << repeat << endl; + //cout << repeat << endl; confirm_repeat_part(t, repeat, start); - cout << repeat << endl; + //cout << repeat << endl; for(int i = 0; i < get_long_int_length(repeat); i++){ @@ -590,13 +601,13 @@ void Fraction::repeated_decimal_to_fraction(double d) down *= (get_long_int_length(t) - start > 0)? (unsigned int)pow(10, (get_long_int_length(t) - start)) : 1; - cout << down << endl; + //cout << down << endl; up = t % (unsigned int)pow(10, get_long_int_length(repeat)) + (t / (unsigned int)pow(10, start)) * ((unsigned int)pow(10, get_long_int_length(repeat)) - 1); up = up + get_double_integer_part(d) * down; - cout << up << endl; + //cout << up << endl; _top = up; _bottom = down; } diff --git a/as1/Fraction.h.gch b/as1/Fraction.h.gch new file mode 100644 index 0000000..74fa7ea Binary files /dev/null and b/as1/Fraction.h.gch differ diff --git a/as1/Fraction_test.cpp b/as1/Fraction_test.cpp index 5b968d7..72c150c 100644 --- a/as1/Fraction_test.cpp +++ b/as1/Fraction_test.cpp @@ -60,8 +60,10 @@ int main() cout << e << " % " << c << " = " << e%c << endl << endl; cout << b << " + " << 4.44 << " = " << b+4.44 << endl; + cout << 4.44 << " + " << b << " = " << 4.44+b << endl; cout << b << " - " << 3 << " = " << b-3 << endl; cout << b << " * " << 2.33333 << " = " << b*2.33333 << endl; + cout << 2.33333 << " * " << b << " = " << 2.33333*b << endl; cout << b << " / " << 5 << " = " << b/5 << endl; cout << e << " % " << 1 << " = " << e%1 << endl << endl; diff --git a/as1/a.exe b/as1/a.exe index 4d74902..1a5ddba 100644 Binary files a/as1/a.exe and b/as1/a.exe differ