36372 mbed / Mbed 2 deprecated SP14P1_skeleton

Dependencies:   AvailableMemory mbed-rtos mbed

Fork of helloaabbc by 32314 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RationalNum.cpp Source File

RationalNum.cpp

00001 #include "RationalNum.h"
00002 
00003 int gcd(int x, int y){
00004   if(!x)return y;
00005   while(y){
00006     if(x>y)x=x-y;
00007     else y=y-x;
00008   }
00009   return x;
00010 }
00011 
00012 int lcm(int x, int y){
00013   return x*y/gcd(x,y);
00014 }
00015 
00016 RationalNum operator + (RationalNum lhs, RationalNum rhs){
00017   int l = lcm(lhs.denominator, rhs.denominator);
00018   int c1 = l/lhs.denominator, c2 = l/rhs.denominator;
00019   int n = c1*lhs.numerator+c2*rhs.numerator;
00020   int g = (n>=0) ? gcd(n,l):gcd(0-n, l);
00021   return RationalNum(n/g, l/g);
00022 }
00023 
00024 RationalNum operator - (RationalNum lhs, RationalNum rhs){
00025   int l = lcm(lhs.denominator, rhs.denominator);
00026   int c1 = l/lhs.denominator, c2 = l/rhs.denominator;
00027   int n = c1*lhs.numerator-c2*rhs.numerator;
00028   int g = (n>=0) ? gcd(n,l):gcd(0-n, l);
00029   return RationalNum(n/g, l/g);
00030 }
00031 
00032 RationalNum operator * (RationalNum lhs, RationalNum rhs){
00033   int n = 0, d = 0, g = 0;
00034   n = lhs.numerator * rhs.numerator;
00035   d = lhs.denominator * rhs.denominator;
00036   g = (n>=0) ? gcd(n, d):gcd(0-n,d);
00037   return RationalNum(n/g, d/g); 
00038 }
00039 
00040 RationalNum operator / (RationalNum lhs, RationalNum rhs){
00041   int n = 0, d = 0, g = 0;
00042   n = lhs.numerator * rhs.denominator;
00043   d = lhs.denominator * rhs.numerator;
00044   if(d<0){
00045     d = 0 - d;
00046     n = 0 - n;
00047   }
00048   g = (n>=0) ? gcd(n, d):gcd(0-n,d);
00049   return RationalNum(n/g, d/g); 
00050 }
00051 
00052 bool operator == (RationalNum lhs, RationalNum rhs){
00053   return ((lhs.numerator==rhs.numerator) && (lhs.denominator==rhs.denominator)); 
00054 }
00055 
00056 bool operator != (RationalNum lhs, RationalNum rhs){
00057   return ((lhs.numerator!=rhs.numerator) || (lhs.denominator!=rhs.denominator));
00058 }
00059 
00060 bool operator >= (RationalNum lhs, RationalNum rhs){
00061   int l = lcm(lhs.denominator, rhs.denominator);
00062   int c1 = l/lhs.denominator, c2 = l/rhs.denominator;
00063   int n = c1*lhs.numerator-c2*rhs.numerator;
00064   return n>=0;
00065 }
00066 
00067 bool operator > (RationalNum lhs, RationalNum rhs){
00068   int l = lcm(lhs.denominator, rhs.denominator);
00069   int c1 = l/lhs.denominator, c2 = l/rhs.denominator;
00070   int n = c1*lhs.numerator-c2*rhs.numerator;
00071   return n>0;
00072 }
00073 
00074 bool operator <= (RationalNum lhs, RationalNum rhs){
00075   int l = lcm(lhs.denominator, rhs.denominator);
00076   int c1 = l/lhs.denominator, c2 = l/rhs.denominator;
00077   int n = c1*lhs.numerator-c2*rhs.numerator;
00078   return n<=0;
00079 }
00080 
00081 bool operator < (RationalNum lhs, RationalNum rhs){
00082   int l = lcm(lhs.denominator, rhs.denominator);
00083   int c1 = l/lhs.denominator, c2 = l/rhs.denominator;
00084   int n = c1*lhs.numerator-c2*rhs.numerator;
00085   return n<0;
00086 }
00087 
00088 void RationalNum::normalize(){
00089   if(denominator==0){
00090     error("denominator must be non-zero\r\n");
00091     exit(1);
00092   }else if(numerator==0){
00093     numerator = 0;
00094     denominator = 1;
00095   }else if(denominator<0){
00096     numerator = 0 - numerator;
00097     denominator = 0 - denominator;
00098   }
00099   else{
00100     return;
00101   }
00102 }