![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
sdf
Dependencies: AvailableMemory mbed-rtos mbed
RationalNum.cpp@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 | #include "RationalNum.h" |
y7jin | 0:1c8f2727e9f5 | 2 | |
y7jin | 0:1c8f2727e9f5 | 3 | int gcd(int x, int y){ |
y7jin | 0:1c8f2727e9f5 | 4 | if(!x)return y; |
y7jin | 0:1c8f2727e9f5 | 5 | while(y){ |
y7jin | 0:1c8f2727e9f5 | 6 | if(x>y)x=x-y; |
y7jin | 0:1c8f2727e9f5 | 7 | else y=y-x; |
y7jin | 0:1c8f2727e9f5 | 8 | } |
y7jin | 0:1c8f2727e9f5 | 9 | return x; |
y7jin | 0:1c8f2727e9f5 | 10 | } |
y7jin | 0:1c8f2727e9f5 | 11 | |
y7jin | 0:1c8f2727e9f5 | 12 | int lcm(int x, int y){ |
y7jin | 0:1c8f2727e9f5 | 13 | return x*y/gcd(x,y); |
y7jin | 0:1c8f2727e9f5 | 14 | } |
y7jin | 0:1c8f2727e9f5 | 15 | |
y7jin | 0:1c8f2727e9f5 | 16 | RationalNum operator + (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 17 | int l = lcm(lhs.denominator, rhs.denominator); |
y7jin | 0:1c8f2727e9f5 | 18 | int c1 = l/lhs.denominator, c2 = l/rhs.denominator; |
y7jin | 0:1c8f2727e9f5 | 19 | int n = c1*lhs.numerator+c2*rhs.numerator; |
y7jin | 0:1c8f2727e9f5 | 20 | int g = (n>=0) ? gcd(n,l):gcd(0-n, l); |
y7jin | 0:1c8f2727e9f5 | 21 | return RationalNum(n/g, l/g); |
y7jin | 0:1c8f2727e9f5 | 22 | } |
y7jin | 0:1c8f2727e9f5 | 23 | |
y7jin | 0:1c8f2727e9f5 | 24 | RationalNum operator - (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 25 | int l = lcm(lhs.denominator, rhs.denominator); |
y7jin | 0:1c8f2727e9f5 | 26 | int c1 = l/lhs.denominator, c2 = l/rhs.denominator; |
y7jin | 0:1c8f2727e9f5 | 27 | int n = c1*lhs.numerator-c2*rhs.numerator; |
y7jin | 0:1c8f2727e9f5 | 28 | int g = (n>=0) ? gcd(n,l):gcd(0-n, l); |
y7jin | 0:1c8f2727e9f5 | 29 | return RationalNum(n/g, l/g); |
y7jin | 0:1c8f2727e9f5 | 30 | } |
y7jin | 0:1c8f2727e9f5 | 31 | |
y7jin | 0:1c8f2727e9f5 | 32 | RationalNum operator * (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 33 | int n = 0, d = 0, g = 0; |
y7jin | 0:1c8f2727e9f5 | 34 | n = lhs.numerator * rhs.numerator; |
y7jin | 0:1c8f2727e9f5 | 35 | d = lhs.denominator * rhs.denominator; |
y7jin | 0:1c8f2727e9f5 | 36 | g = (n>=0) ? gcd(n, d):gcd(0-n,d); |
y7jin | 0:1c8f2727e9f5 | 37 | return RationalNum(n/g, d/g); |
y7jin | 0:1c8f2727e9f5 | 38 | } |
y7jin | 0:1c8f2727e9f5 | 39 | |
y7jin | 0:1c8f2727e9f5 | 40 | RationalNum operator / (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 41 | int n = 0, d = 0, g = 0; |
y7jin | 0:1c8f2727e9f5 | 42 | n = lhs.numerator * rhs.denominator; |
y7jin | 0:1c8f2727e9f5 | 43 | d = lhs.denominator * rhs.numerator; |
y7jin | 0:1c8f2727e9f5 | 44 | if(d<0){ |
y7jin | 0:1c8f2727e9f5 | 45 | d = 0 - d; |
y7jin | 0:1c8f2727e9f5 | 46 | n = 0 - n; |
y7jin | 0:1c8f2727e9f5 | 47 | } |
y7jin | 0:1c8f2727e9f5 | 48 | g = (n>=0) ? gcd(n, d):gcd(0-n,d); |
y7jin | 0:1c8f2727e9f5 | 49 | return RationalNum(n/g, d/g); |
y7jin | 0:1c8f2727e9f5 | 50 | } |
y7jin | 0:1c8f2727e9f5 | 51 | |
y7jin | 0:1c8f2727e9f5 | 52 | bool operator == (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 53 | return ((lhs.numerator==rhs.numerator) && (lhs.denominator==rhs.denominator)); |
y7jin | 0:1c8f2727e9f5 | 54 | } |
y7jin | 0:1c8f2727e9f5 | 55 | |
y7jin | 0:1c8f2727e9f5 | 56 | bool operator != (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 57 | return ((lhs.numerator!=rhs.numerator) || (lhs.denominator!=rhs.denominator)); |
y7jin | 0:1c8f2727e9f5 | 58 | } |
y7jin | 0:1c8f2727e9f5 | 59 | |
y7jin | 0:1c8f2727e9f5 | 60 | bool operator >= (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 61 | int l = lcm(lhs.denominator, rhs.denominator); |
y7jin | 0:1c8f2727e9f5 | 62 | int c1 = l/lhs.denominator, c2 = l/rhs.denominator; |
y7jin | 0:1c8f2727e9f5 | 63 | int n = c1*lhs.numerator-c2*rhs.numerator; |
y7jin | 0:1c8f2727e9f5 | 64 | return n>=0; |
y7jin | 0:1c8f2727e9f5 | 65 | } |
y7jin | 0:1c8f2727e9f5 | 66 | |
y7jin | 0:1c8f2727e9f5 | 67 | bool operator > (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 68 | int l = lcm(lhs.denominator, rhs.denominator); |
y7jin | 0:1c8f2727e9f5 | 69 | int c1 = l/lhs.denominator, c2 = l/rhs.denominator; |
y7jin | 0:1c8f2727e9f5 | 70 | int n = c1*lhs.numerator-c2*rhs.numerator; |
y7jin | 0:1c8f2727e9f5 | 71 | return n>0; |
y7jin | 0:1c8f2727e9f5 | 72 | } |
y7jin | 0:1c8f2727e9f5 | 73 | |
y7jin | 0:1c8f2727e9f5 | 74 | bool operator <= (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 75 | int l = lcm(lhs.denominator, rhs.denominator); |
y7jin | 0:1c8f2727e9f5 | 76 | int c1 = l/lhs.denominator, c2 = l/rhs.denominator; |
y7jin | 0:1c8f2727e9f5 | 77 | int n = c1*lhs.numerator-c2*rhs.numerator; |
y7jin | 0:1c8f2727e9f5 | 78 | return n<=0; |
y7jin | 0:1c8f2727e9f5 | 79 | } |
y7jin | 0:1c8f2727e9f5 | 80 | |
y7jin | 0:1c8f2727e9f5 | 81 | bool operator < (RationalNum lhs, RationalNum rhs){ |
y7jin | 0:1c8f2727e9f5 | 82 | int l = lcm(lhs.denominator, rhs.denominator); |
y7jin | 0:1c8f2727e9f5 | 83 | int c1 = l/lhs.denominator, c2 = l/rhs.denominator; |
y7jin | 0:1c8f2727e9f5 | 84 | int n = c1*lhs.numerator-c2*rhs.numerator; |
y7jin | 0:1c8f2727e9f5 | 85 | return n<0; |
y7jin | 0:1c8f2727e9f5 | 86 | } |
y7jin | 0:1c8f2727e9f5 | 87 | |
y7jin | 0:1c8f2727e9f5 | 88 | void RationalNum::normalize(){ |
y7jin | 0:1c8f2727e9f5 | 89 | if(denominator==0){ |
y7jin | 0:1c8f2727e9f5 | 90 | error("denominator must be non-zero\r\n"); |
y7jin | 0:1c8f2727e9f5 | 91 | exit(1); |
y7jin | 0:1c8f2727e9f5 | 92 | }else if(numerator==0){ |
y7jin | 0:1c8f2727e9f5 | 93 | numerator = 0; |
y7jin | 0:1c8f2727e9f5 | 94 | denominator = 1; |
y7jin | 0:1c8f2727e9f5 | 95 | }else if(denominator<0){ |
y7jin | 0:1c8f2727e9f5 | 96 | numerator = 0 - numerator; |
y7jin | 0:1c8f2727e9f5 | 97 | denominator = 0 - denominator; |
y7jin | 0:1c8f2727e9f5 | 98 | } |
y7jin | 0:1c8f2727e9f5 | 99 | else{ |
y7jin | 0:1c8f2727e9f5 | 100 | return; |
y7jin | 0:1c8f2727e9f5 | 101 | } |
y7jin | 0:1c8f2727e9f5 | 102 | } |