首页 > 代码库 > 「恢复训练」一个有理数类

「恢复训练」一个有理数类

难得又写一次代码,距离上一次已经有半年左右了吧。

虽然误打误撞进了数学系,但果然还是喜欢Coding的感觉。

恢复训练

连右值须为const引用,=须重载为成员函数都忘了。

R.I.P.

 

程序清单:

 

Rational.h

 1 /* 2  * Rational.h 3  * 4  *  Created on: 2014年11月22日 5  *      Author: suncc 6  */ 7 #include<iostream> 8  9 #ifndef RATIONAL_H_10 #define RATIONAL_H_11 12 typedef unsigned long long RalType;13 14 namespace Real15 {16 17 RalType gcd(RalType p,RalType q);    //gcd18 19 class Rational20 {21 public:22     RalType    deno,nume;                //分母,分子23     short    sign;                    //符号24     void reduce();                    //约分25     void common(Rational &tar);        //通分26     Rational();27     virtual ~Rational();28     friend std::ostream & operator << (std::ostream & out, const Rational &obj);29     friend std::istream & operator >> (std::istream & in, Rational &obj);30 31     friend Rational operator - (const Rational &rhs);32 33     friend bool operator > (const Rational & lhs, const Rational & rhs);34     friend bool operator < (const Rational & lhs, const Rational & rhs);35     friend bool operator >= (const Rational & lhs, const Rational & rhs);36     friend bool operator <= (const Rational & lhs, const Rational & rhs);37     friend bool operator == (const Rational & lhs, const Rational & rhs);38     friend bool operator != (const Rational & lhs, const Rational & rhs);39 40 41     friend Rational operator + (const Rational & lhs, const Rational & rhs);42     friend Rational operator - (const Rational & lhs, const Rational & rhs);43     friend Rational operator * (const Rational & lhs, const Rational & rhs);44     friend Rational operator / (const Rational & lhs, const Rational & rhs);45     Rational operator += (const Rational & rhs);46     Rational operator -= (const Rational & rhs);47     Rational operator *= (const Rational & rhs);48     Rational operator /= (const Rational & rhs);49     Rational operator = (const Rational & rhs);50 };51 52 std::ostream & operator << (std::ostream &out,Rational &obj);53 std::istream & operator >> (std::istream &in,Rational &obj);54 Rational abs(Rational X);55 56 } /* namespace Real */57 58 #endif /* RATIONAL_H_ */

 

Rational.cpp

  1 /*  2  * Rational.cpp  3  *  4  *  Created on: 2014年11月22日  5  *      Author: suncc  6  */  7   8 #include "Rational.h"  9 #include <cstring> 10 #include <exception> 11 #include <algorithm> 12 namespace Real 13 { 14 RalType gcd(RalType p, RalType q) 15 { 16     RalType r; 17     if(q>p) 18         std::swap(p,q); 19     if(q == 0) 20     { 21         if(p == 0) 22             std::unexpected(); 23         else 24             return p; 25     } 26         r = p%q; 27     while(r != 0) 28     { 29         p = q; 30         q = r; 31         r = p%q; 32     } 33     return q; 34 } 35  36 Rational::Rational() 37 { 38     deno=1; 39     nume=0; 40     sign=1; 41 } 42  43 Rational::~Rational() 44 { 45 } 46  47 std::ostream & operator << (std::ostream &out,const Rational &obj) // const Rational &obj 48 { 49     //obj.reduce(); 50     out << ((obj.sign==1)?"":"-") << obj.nume << / << obj.deno; 51     return out; 52 } 53  54 std::istream & operator >> (std::istream &in,Rational &obj) 55 { 56     /* 57      * Input length should be less than 201 chars. 58      */ 59     char seq; 60     RalType tmp(0); 61     if (in.peek() == -) 62     { 63         in.ignore(1,\n); 64         obj.sign = -1; 65     } 66     else 67         obj.sign = 1; 68  69     in.get(seq); 70     while(seq>=0 && seq<=9) 71     { 72         tmp = tmp*10+seq-0; 73         in.get(seq); 74     } 75     if(seq == /) 76         obj.nume = tmp; 77     else 78         std::unexpected(); 79  80     tmp = 0; 81     in.get(seq); 82     while(seq>=0 && seq<=9) 83     { 84         tmp = tmp*10+seq-0; 85         in.get(seq); 86     } 87     if (tmp != 0) 88         obj.deno = tmp; 89     else 90         std::unexpected(); 91     obj.reduce(); 92     return in; 93 } 94  95 bool operator > (const Rational & lhs, const Rational & rhs) 96 { 97     if(lhs.sign > rhs.sign) 98         return true; 99     if(lhs.sign < rhs.sign)100         return true;101     Rational tl=lhs,tr=rhs;102     tl.common(tr);103     tr.common(tl);104     if(tl.sign > 0)105         return tl.nume > tr.nume;106     else107         return !(tl.nume > tr.nume);108 }109 bool operator < (const Rational & lhs, const Rational & rhs)110 {111     return rhs > lhs;112 }113 bool operator >= (const Rational & lhs, const Rational & rhs)114 {115     return !(lhs < rhs);116 }117 bool operator <= (const Rational & lhs, const Rational & rhs)118 {119     return !(lhs > rhs);120 }121 bool operator == (const Rational & lhs, const Rational & rhs)122 {123     return (lhs>=rhs)&&(lhs<=rhs);124 }125 bool operator != (const Rational & lhs, const Rational & rhs)126 {127     return (lhs>rhs)||(lhs<rhs);128 }129 130 Rational operator + (const Rational & lhs, const Rational & rhs)131 {132     Rational tl=lhs,tr=rhs;133     if (tl.sign*tr.sign > 0)134     {135         tl.common(tr);136         tr.common(tl);137         tl.nume += tr.nume;138         tl.reduce();139         return tl;140     }// TODO Finish the operator in field141     else142     {143         if(abs(tr)>abs(tl))144             std::swap(tl,tr);145         Rational ret;146         ret.sign = tl.sign;147         tl.common(tr);148         tr.common(tl);149         ret.deno = tl.deno;150         ret.nume = tl.nume-tr.nume;151         ret.reduce();152         return ret;153     }154 }155 Rational Rational::operator +=(const Rational & rhs)156 {157     *this = *this + rhs;158     return *this;159 }160 161 Rational operator - (const Rational & rhs)162 {163     Rational ret = rhs;164     ret.sign *= -1;165     ret.reduce();166     return ret;167 }168 Rational Rational::operator -=(const Rational & rhs)169 {170     *this = *this - rhs;171     return *this;172 }173 174 Rational operator - (const Rational & lhs, const Rational & rhs)175 {176     return lhs + (-rhs);177 }178 Rational operator * (const Rational & lhs,const Rational & rhs)179 {180     Rational ret,tl=lhs;181     ret = rhs;182     tl.reduce();ret.reduce();183     ret.sign *= tl.sign;184     ret.deno *= tl.deno;185     ret.nume *= tl.nume;186     if(ret.deno == 0)187         std::unexpected();188     ret.reduce();189     return ret;190 }191 Rational Rational::operator *= (const Rational & rhs)192 {193     *this = *this * rhs;194     return *this;195 }196 197 Rational operator / (const Rational & lhs, const Rational & rhs)198 {199     Rational ret=rhs;200     if(ret.nume == 0)201         std::unexpected();202     std::swap(ret.nume,ret.deno);203     ret *= lhs;204     return ret;205 }206 Rational Rational::operator /= (const Rational & rhs)207 {208     *this = *this / rhs;209     return *this;210 }211 212 Rational Rational::operator = (const Rational & rhs)213 {214     if (rhs.deno == 0)215         std::unexpected();216     this->deno = rhs.deno;217     this->nume = rhs.nume;218     this->sign = rhs.sign;219     return *this;220 }221 222 void Rational::reduce()223 {224     RalType gik= gcd(this->deno,this->nume);225     this->deno /= gik;226     this->nume /= gik;227     if(this->nume == 0)228         this->sign = 1;229 }230 231 void Rational::common(Rational &tar)232 {233     RalType gik =gcd(this->deno,tar.deno);234     RalType to = this->deno * tar.deno / gik;235     this->nume *= tar.deno / gik;236     this->deno = to;237 }238 239 Rational abs(Rational x)240 {241     Rational ax=x;242     ax.sign = 1;243     return ax;244 }245 246 } /* namespace Real */

 

 

很好,接下来是用这个实现高斯消元了~

请祝我早日恢复元气~

「恢复训练」一个有理数类