![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
sdf
Dependencies: AvailableMemory mbed-rtos mbed
RationalNum.h@0:1c8f2727e9f5, 2014-04-03 (annotated)
- Committer:
- y7jin
- Date:
- Thu Apr 03 22:56:32 2014 +0000
- Revision:
- 0:1c8f2727e9f5
hello
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
y7jin | 0:1c8f2727e9f5 | 1 | #ifndef _RATIONALNUM_H |
y7jin | 0:1c8f2727e9f5 | 2 | #define _RATIONALNUM_H |
y7jin | 0:1c8f2727e9f5 | 3 | |
y7jin | 0:1c8f2727e9f5 | 4 | #include "mbed.h" |
y7jin | 0:1c8f2727e9f5 | 5 | |
y7jin | 0:1c8f2727e9f5 | 6 | int gcd(int x, int y); |
y7jin | 0:1c8f2727e9f5 | 7 | int lcm(int x, int y); |
y7jin | 0:1c8f2727e9f5 | 8 | |
y7jin | 0:1c8f2727e9f5 | 9 | class RationalNum{ |
y7jin | 0:1c8f2727e9f5 | 10 | friend RationalNum operator + (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 11 | friend RationalNum operator - (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 12 | friend RationalNum operator * (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 13 | friend RationalNum operator / (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 14 | friend bool operator == (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 15 | friend bool operator != (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 16 | friend bool operator >= (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 17 | friend bool operator > (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 18 | friend bool operator <= (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 19 | friend bool operator < (RationalNum lhs, RationalNum rhs); |
y7jin | 0:1c8f2727e9f5 | 20 | private: |
y7jin | 0:1c8f2727e9f5 | 21 | int numerator; |
y7jin | 0:1c8f2727e9f5 | 22 | /*non-zero denominator*/ |
y7jin | 0:1c8f2727e9f5 | 23 | int denominator; |
y7jin | 0:1c8f2727e9f5 | 24 | public: |
y7jin | 0:1c8f2727e9f5 | 25 | RationalNum():numerator(0),denominator(1){} |
y7jin | 0:1c8f2727e9f5 | 26 | RationalNum(int n, int d):numerator(n),denominator(d){normalize();} |
y7jin | 0:1c8f2727e9f5 | 27 | RationalNum(int k):numerator(k),denominator(1){} |
y7jin | 0:1c8f2727e9f5 | 28 | RationalNum(const RationalNum &orig):numerator(orig.numerator),denominator(orig.denominator){normalize();} |
y7jin | 0:1c8f2727e9f5 | 29 | RationalNum& operator = (const RationalNum &rhs){numerator=rhs.numerator;denominator=rhs.denominator;return *this;} |
y7jin | 0:1c8f2727e9f5 | 30 | void print()const{printf("%d\\/%d", numerator, denominator);} |
y7jin | 0:1c8f2727e9f5 | 31 | int getNumerator()const{return numerator;} |
y7jin | 0:1c8f2727e9f5 | 32 | int getDenominator()const{return denominator;} |
y7jin | 0:1c8f2727e9f5 | 33 | void normalize(); |
y7jin | 0:1c8f2727e9f5 | 34 | }; |
y7jin | 0:1c8f2727e9f5 | 35 | |
y7jin | 0:1c8f2727e9f5 | 36 | #endif |