- /* sratio.h by K.Tsuru */
- // file ID = 800 BRADIX
- /*****************************************
- SRational class using SInteger arithmetic
- See SFraction class
- ******************************************/
- #ifndef S_RATIONAL_H
- #define S_RATIONAL_H
- class SRational { //not derived class
- SInteger num, den;
- bool reduceDone;
- static uint reduceSize;
- void reduce(bool must); //801
- void SetString(const char *s); // 802
- /********* List of unusable functions which have no body.******/
- SRational& operator=(const SDouble& a);
- SRational& operator=(const SDecimal& a);
- //use Set() functions to set SLong values
- SRational& operator=(const SLong& a);
- SRational(const SLong& n);
- SRational(const SLong& n, const SLong& d);
- SRational(const SFraction& n);
- /************************ end of list **************************/
- void DenCheck(); // 803
- void RadixCheck() const; // inline function
- public:
- static uint ReduceSize() { return reduceSize; }
- void Reduce(){ reduce(true); }
- bool ReduceDone() const { return reduceDone; }
- //Set a value
- void SetDouble(double n, double d = 1.0L){
- num = n; den = d; //includes radix conversion
- reduceDone = false;
- DenCheck(); // includes check den == 1 or not;
- }
- void SetLong(long n, long d = 1L); // 805
- //The type of "n" and "d" is eather of "SLong" and "SInteger".
- void Set(const SLong& n, const SLong& d); // 806
- void SetZero(){
- num.SetZero(); den.SetSmall(1); reduceDone = true;
- }
- // constructors
- SRational():num(), den(), reduceDone(false){} // default constructor
- SRational(double n):num(n), den(1.0L), reduceDone(true){}
- SRational(double n, double d):num(n), den(d), reduceDone(false){
- DenCheck(); //If |d| == 1.0 it sets reduceDone = True.
- }
- // constructor by string
- SRational(const char* s):num(), den(), reduceDone(false){
- SetString(s);
- }
- // constructor by single SInteger value which converts SR*SI --> SR*(SI/1)
- SRational(const SInteger& n):num(n), den(), reduceDone(true){
- den.SetSmall(1); RadixCheck();
- }
- // constructor by two SInteger values
- SRational(const SInteger& n, const SInteger& d):num(n), den(d), reduceDone(false){
- DenCheck(); RadixCheck(); reduce(false);
- }
-
- // copy constructor
- SRational(const SRational& a):num(a.num), den(a.den), reduceDone(a.reduceDone){}
- SFraction ConvToDec() const; //radix conversion to SFraction 88
- virtual ~SRational(){}
- //"s" must have a decimal representation, not binary radix.
- SRational& operator=(const char *s) { SetString(s); return *this; }
- SRational& operator=(double d){
- SetDouble(d, 1.0L); // set reduceDone = true; by reduce(false).
- return *this;
- }
- //For details see the file "sfract.h".
- SRational& operator=(const SRational& a); // 807
- SRational& operator=(const SInteger& a); // 808
- SInteger Num() { if(!reduceDone) reduce(true); return num; }
- SInteger Den() { if(!reduceDone) reduce(true); return den; }
- SInteger NumNR() const { return num; }
- SInteger DenNR() const { return den; }
-
- SRational operator+() const { return *this; }
- SRational operator-() const {
- SRational r(*this);
- r.num.ChangeSign(809);
- return r;
- }
- friend SRational RRAdd(const SRational& x, const SRational& y);//810
- friend SRational RRSub(const SRational& x, const SRational& y);//811
- friend SRational RRMult(const SRational& x, const SRational& y);//812
- friend SRational RRDiv(const SRational& x, const SRational& y); //813
- friend SRational RsMult(const SRational& x, ulong s); // 814
- friend SRational RsDiv(const SRational& x, ulong s); // 815
- friend SRational operator*(const SRational& f, double d); // 824 f*d operator d*f is defined below as an inline function.
- friend SRational operator/(const SRational& f, double d); // 825
- friend SRational operator/(double d, const SRational& f); // 827
-
- SRational& operator+=(const SRational& n){ return *this = RRAdd(*this, n); }
- SRational& operator-=(const SRational& n){ return *this = RRSub(*this, n); }
- SRational& operator*=(const SRational& n){ return *this = RRMult(*this,n); }
- SRational& operator/=(const SRational& n){ return *this = RRDiv(*this, n); }
- SRational& operator*=(double d){ return (*this = *this * d); }
- SRational& operator/=(double d){ return (*this = *this / d); }
- int Sign(int id = 80) const { return num.Sign(id); }
- void ChangeSign(int id = 81){ num.ChangeSign(id); }
- enum { END_CR = 4, BRACKET = 8 };
- long Put(int fig=5, char delmt=' ', int mode = BRACKET); //840
- long Puts(int fig=5, char delmt=' ', int mode = END_CR | BRACKET){
- return Put(fig, delmt, mode | END_CR);
- }
- };
- inline SRational operator*(double d, const SRational& f){ return f*d; }
-
- inline SRational operator+(const SRational& m, const SRational& n){ // m+n
- return RRAdd(m, n);
- }
- inline SRational operator-(const SRational& m, const SRational& n){ // m-n
- return RRSub(m, n);
- }
- inline SRational operator*(const SRational& m, const SRational& n){ // m*n
- return RRMult(m, n);
- }
- inline SRational operator/(const SRational& m, const SRational& n){ // m/n
- return RRDiv(m, n);
- }
-
- inline void SRational::RadixCheck() const{
- if( !( (num.Type() == num.BIN_INT) && (den.Type() == num.BIN_INT) ) )
- num.SetError(num.RADIX_ERR, "SR", 82);
- }
-
- int RRCompare(const SRational& m, const SRational& n); // 8001
- inline SRational Rabs(const SRational& x){
- return (x.Sign(8002) < 0 ) ? (-x) : x;
- }
- int operator==(const SRational& m, const SRational& n); // 8003
- inline int operator>(const SRational& m, const SRational& n){
- if(m.Sign(84) != n.Sign(84)) return m.Sign() > n.Sign();
- return (RRCompare(m, n) * m.Sign()) > 0;
- }
- inline int operator<(const SRational& m, const SRational& n){
- if(m.Sign(85) != n.Sign(85)) return m.Sign() < n.Sign();
- return (RRCompare(m, n) * m.Sign()) < 0;
- }
- inline int operator!=(const SRational& m, const SRational& n){
- return !(m == n);
- }
- inline int operator>=(const SRational& m, const SRational& n){
- if(m.Sign(87) != n.Sign(87)) return m.Sign() > n.Sign();
- return (RRCompare(m, n)*m.Sign()) >= 0;
- }
- inline int operator<=(const SRational& m, const SRational& n){
- if(m.Sign(88) != n.Sign(88)) return m.Sign() < n.Sign();
- return (RRCompare(m, n)*m.Sign()) <= 0;
- }
- #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).