1. /* sratio.h by K.Tsuru */
  2. // file ID = 800 BRADIX
  3. /*****************************************
  4. SRational class using SInteger arithmetic
  5. See SFraction class
  6. ******************************************/
  7. #ifndef S_RATIONAL_H
  8. #define S_RATIONAL_H
  9. class SRational { //not derived class
  10. SInteger num, den;
  11. bool reduceDone;
  12. static uint reduceSize;
  13. void reduce(bool must); //801
  14. void SetString(const char *s); // 802
  15. /********* List of unusable functions which have no body.******/
  16. SRational& operator=(const SDouble& a);
  17. SRational& operator=(const SDecimal& a);
  18. //use Set() functions to set SLong values
  19. SRational& operator=(const SLong& a);
  20. SRational(const SLong& n);
  21. SRational(const SLong& n, const SLong& d);
  22. SRational(const SFraction& n);
  23. /************************ end of list **************************/
  24. void DenCheck(); // 803
  25. void RadixCheck() const; // inline function
  26. public:
  27. static uint ReduceSize() { return reduceSize; }
  28. void Reduce(){ reduce(true); }
  29. bool ReduceDone() const { return reduceDone; }
  30. //Set a value
  31. void SetDouble(double n, double d = 1.0L){
  32. num = n; den = d; //includes radix conversion
  33. reduceDone = false;
  34. DenCheck(); // includes check den == 1 or not;
  35. }
  36. void SetLong(long n, long d = 1L); // 805
  37. //The type of "n" and "d" is eather of "SLong" and "SInteger".
  38. void Set(const SLong& n, const SLong& d); // 806
  39. void SetZero(){
  40. num.SetZero(); den.SetSmall(1); reduceDone = true;
  41. }
  42. // constructors
  43. SRational():num(), den(), reduceDone(false){} // default constructor
  44. SRational(double n):num(n), den(1.0L), reduceDone(true){}
  45. SRational(double n, double d):num(n), den(d), reduceDone(false){
  46. DenCheck(); //If |d| == 1.0 it sets reduceDone = True.
  47. }
  48. // constructor by string
  49. SRational(const char* s):num(), den(), reduceDone(false){
  50. SetString(s);
  51. }
  52. // constructor by single SInteger value which converts SR*SI --> SR*(SI/1)
  53. SRational(const SInteger& n):num(n), den(), reduceDone(true){
  54. den.SetSmall(1); RadixCheck();
  55. }
  56. // constructor by two SInteger values
  57. SRational(const SInteger& n, const SInteger& d):num(n), den(d), reduceDone(false){
  58. DenCheck(); RadixCheck(); reduce(false);
  59. }
  60. // copy constructor
  61. SRational(const SRational& a):num(a.num), den(a.den), reduceDone(a.reduceDone){}
  62. SFraction ConvToDec() const; //radix conversion to SFraction 88
  63. virtual ~SRational(){}
  64. //"s" must have a decimal representation, not binary radix.
  65. SRational& operator=(const char *s) { SetString(s); return *this; }
  66. SRational& operator=(double d){
  67. SetDouble(d, 1.0L); // set reduceDone = true; by reduce(false).
  68. return *this;
  69. }
  70. //For details see the file "sfract.h".
  71. SRational& operator=(const SRational& a); // 807
  72. SRational& operator=(const SInteger& a); // 808
  73. SInteger Num() { if(!reduceDone) reduce(true); return num; }
  74. SInteger Den() { if(!reduceDone) reduce(true); return den; }
  75. SInteger NumNR() const { return num; }
  76. SInteger DenNR() const { return den; }
  77. SRational operator+() const { return *this; }
  78. SRational operator-() const {
  79. SRational r(*this);
  80. r.num.ChangeSign(809);
  81. return r;
  82. }
  83. friend SRational RRAdd(const SRational& x, const SRational& y);//810
  84. friend SRational RRSub(const SRational& x, const SRational& y);//811
  85. friend SRational RRMult(const SRational& x, const SRational& y);//812
  86. friend SRational RRDiv(const SRational& x, const SRational& y); //813
  87. friend SRational RsMult(const SRational& x, ulong s); // 814
  88. friend SRational RsDiv(const SRational& x, ulong s); // 815
  89. friend SRational operator*(const SRational& f, double d); // 824 f*d operator d*f is defined below as an inline function.
  90. friend SRational operator/(const SRational& f, double d); // 825
  91. friend SRational operator/(double d, const SRational& f); // 827
  92. SRational& operator+=(const SRational& n){ return *this = RRAdd(*this, n); }
  93. SRational& operator-=(const SRational& n){ return *this = RRSub(*this, n); }
  94. SRational& operator*=(const SRational& n){ return *this = RRMult(*this,n); }
  95. SRational& operator/=(const SRational& n){ return *this = RRDiv(*this, n); }
  96. SRational& operator*=(double d){ return (*this = *this * d); }
  97. SRational& operator/=(double d){ return (*this = *this / d); }
  98. int Sign(int id = 80) const { return num.Sign(id); }
  99. void ChangeSign(int id = 81){ num.ChangeSign(id); }
  100. enum { END_CR = 4, BRACKET = 8 };
  101. long Put(int fig=5, char delmt=' ', int mode = BRACKET); //840
  102. long Puts(int fig=5, char delmt=' ', int mode = END_CR | BRACKET){
  103. return Put(fig, delmt, mode | END_CR);
  104. }
  105. };
  106. inline SRational operator*(double d, const SRational& f){ return f*d; }
  107. inline SRational operator+(const SRational& m, const SRational& n){ // m+n
  108. return RRAdd(m, n);
  109. }
  110. inline SRational operator-(const SRational& m, const SRational& n){ // m-n
  111. return RRSub(m, n);
  112. }
  113. inline SRational operator*(const SRational& m, const SRational& n){ // m*n
  114. return RRMult(m, n);
  115. }
  116. inline SRational operator/(const SRational& m, const SRational& n){ // m/n
  117. return RRDiv(m, n);
  118. }
  119. inline void SRational::RadixCheck() const{
  120. if( !( (num.Type() == num.BIN_INT) && (den.Type() == num.BIN_INT) ) )
  121. num.SetError(num.RADIX_ERR, "SR", 82);
  122. }
  123. int RRCompare(const SRational& m, const SRational& n); // 8001
  124. inline SRational Rabs(const SRational& x){
  125. return (x.Sign(8002) < 0 ) ? (-x) : x;
  126. }
  127. int operator==(const SRational& m, const SRational& n); // 8003
  128. inline int operator>(const SRational& m, const SRational& n){
  129. if(m.Sign(84) != n.Sign(84)) return m.Sign() > n.Sign();
  130. return (RRCompare(m, n) * m.Sign()) > 0;
  131. }
  132. inline int operator<(const SRational& m, const SRational& n){
  133. if(m.Sign(85) != n.Sign(85)) return m.Sign() < n.Sign();
  134. return (RRCompare(m, n) * m.Sign()) < 0;
  135. }
  136. inline int operator!=(const SRational& m, const SRational& n){
  137. return !(m == n);
  138. }
  139. inline int operator>=(const SRational& m, const SRational& n){
  140. if(m.Sign(87) != n.Sign(87)) return m.Sign() > n.Sign();
  141. return (RRCompare(m, n)*m.Sign()) >= 0;
  142. }
  143. inline int operator<=(const SRational& m, const SRational& n){
  144. if(m.Sign(88) != n.Sign(88)) return m.Sign() < n.Sign();
  145. return (RRCompare(m, n)*m.Sign()) <= 0;
  146. }
  147. #endif // S_RATIONAL_H

sratio.h : last modifiled at 2016/09/04 14:21:36(6,308 bytes)
created at 2016/04/11 11:18:59
The creation time of this html file is 2017/10/11 16:07:52 (Wed Oct 11 16:07:52 2017).