引言
咱們說,編程語言的精髓在於封裝,而面嚮對象語言完勝面向過程語言的緣由就是具備更好的可封裝性,而C++就是這樣的一種多範型語言,經常使用而複雜的工做徹底沒必要在每一份源文件中重敲,就好像咱們不須要本身手寫printf()和scanf()同樣,自建一個屬於本身的頭文件,能夠爲編程提供很大的幫助,我今天就要推薦一下我本身原創的分數類(實際上C++STL庫有分數類,只是性能很低,並且使用極其不方便,而個人這個分數類具備不少前者不具有的優勢)。ios
注:此爲原創文章,轉載則務必說明原出處。編程
Code
將下面的代碼存入一個***.h文件中,並在同目錄下的源文件中加上:#include「fraction」便可使用該分數類(使用方法見後面)。編程語言
1 //拱大塏原創 2 //分數類實現~~~0.8.5 3 //fraction:分數 4 //numerator:分子 5 //denominator:分母 6 //輸入分數時請以回車結尾 7 //輸入部分如含有空格,則空格後面的部分會被忽略 8 //fraction可縮寫爲fac,計算機能夠識別 9 10 #include <iostream> 11 #include <cmath> 12 13 #ifndef __fraction_h_ 14 #define __fraction_h_ 15 16 using namespace std; 17 18 namespace std 19 { 20 21 //分數類 22 class fraction 23 { 24 public: 25 static long long MCF(long long a, long long b); 26 27 friend ostream& operator<< (ostream& os,const fraction& the_fraction); 28 friend istream& operator>> (istream& is,fraction& the_fraction); 29 30 friend fraction operator+ (const fraction& the_fraction_a,const fraction& the_fraction_b); 31 friend fraction operator- (const fraction& the_fraction_a,const fraction& the_fraction_b); 32 friend fraction operator* (const fraction& the_fraction_a,const fraction& the_fraction_b); 33 friend fraction operator/ (const fraction& the_fraction_a,const fraction& the_fraction_b); 34 friend fraction operator+= (fraction& the_fraction_a,const fraction& the_fraction_b); 35 friend fraction operator-= (fraction& the_fraction_a,const fraction& the_fraction_b); 36 friend fraction operator*= (fraction& the_fraction_a,const fraction& the_fraction_b); 37 friend fraction operator/= (fraction& the_fraction_a,const fraction& the_fraction_b); 38 friend fraction operator- (const fraction& the_fraction); 39 friend fraction operator++ (fraction& the_fraction); 40 friend fraction operator++ (fraction& the_fraction,int); 41 friend fraction operator-- (fraction& the_fraction); 42 friend fraction operator-- (fraction& the_fraction,int); 43 44 friend bool operator> (fraction& the_fraction_a,const fraction& the_fraction_b); 45 friend bool operator< (fraction& the_fraction_a,const fraction& the_fraction_b); 46 friend bool operator>= (fraction& the_fraction_a,const fraction& the_fraction_b); 47 friend bool operator<= (fraction& the_fraction_a,const fraction& the_fraction_b); 48 friend bool operator== (fraction& the_fraction_a,const fraction& the_fraction_b); 49 friend bool operator!= (fraction& the_fraction_a,const fraction& the_fraction_b); 50 51 fraction(); 52 fraction(long long the_numerator); 53 fraction(long long the_numerator,long long the_denominator); 54 fraction(fraction the_fraction_a,fraction the_fraction_b); 55 56 double decimal(); 57 long long getnum(); 58 long long getden(); 59 void setnum(long long num); 60 void setden(long long den); 61 62 private: 63 long long numerator; 64 long long denominator; 65 66 }; 67 68 69 70 long long fraction::MCF(long long a, long long b) 71 { 72 return a==0? b:MCF(b%a,a); 73 } 74 75 76 77 ostream& operator<< (ostream& os,const fraction& the_fraction) 78 { 79 bool book=the_fraction.numerator>=0&&the_fraction.denominator>=0||the_fraction.numerator<0&&the_fraction.denominator<0; 80 if(book==false) 81 os<<"(-"; 82 if(the_fraction.denominator!=1) 83 os<<abs(the_fraction.numerator)<<"/"<<abs(the_fraction.denominator); 84 else 85 os<<abs(the_fraction.numerator); 86 if(book==false) 87 os<<")"; 88 return os; 89 } 90 91 istream& operator>> (istream& is,fraction& the_fraction) 92 { 93 char input[100]; 94 char ch; 95 bool mid=false; 96 bool is_pt=true; 97 the_fraction.numerator=the_fraction.denominator=0; 98 cin>>input; 99 for(int i=0;;i++) 100 { 101 ch=input[i]; 102 if(ch=='\0') 103 break; 104 if(ch=='-') 105 { 106 is_pt=!is_pt; 107 continue; 108 } 109 if(ch=='/') 110 { 111 mid=true; 112 continue; 113 } 114 if(mid==false) 115 the_fraction.numerator=the_fraction.numerator*10+(ch-'0'); 116 else 117 the_fraction.denominator=the_fraction.denominator*10+(ch-'0'); 118 } 119 if(mid==false) 120 the_fraction.denominator=1; 121 if(the_fraction.denominator==0) 122 { 123 cout<<"False,the denominator == 0!!!"; 124 return is; 125 } 126 long long mcf=fraction::MCF(the_fraction.numerator,the_fraction.denominator); 127 the_fraction.numerator/=mcf; 128 the_fraction.denominator/=mcf; 129 if(!is_pt) 130 the_fraction.numerator=-the_fraction.numerator; 131 return is; 132 } 133 134 135 136 fraction operator+ (const fraction& the_fraction_a,const fraction& the_fraction_b) 137 { 138 long long the_numerator=the_fraction_a.numerator*the_fraction_b.denominator+the_fraction_b.numerator*the_fraction_a.denominator; 139 long long the_denominator=the_fraction_a.denominator*the_fraction_b.denominator; 140 return fraction(the_numerator,the_denominator); 141 } 142 143 fraction operator- (const fraction& the_fraction_a,const fraction& the_fraction_b) 144 { 145 long long the_numerator=the_fraction_a.numerator*the_fraction_b.denominator-the_fraction_b.numerator*the_fraction_a.denominator; 146 long long the_denominator=the_fraction_a.denominator*the_fraction_b.denominator; 147 return fraction(the_numerator,the_denominator); 148 } 149 fraction operator* (const fraction& the_fraction_a,const fraction& the_fraction_b) 150 { 151 long long the_numerator=the_fraction_a.numerator*the_fraction_b.numerator; 152 long long the_denominator=the_fraction_a.denominator*the_fraction_b.denominator; 153 long long mcf=fraction::MCF(the_numerator,the_denominator); 154 the_numerator/=mcf; 155 the_denominator/=mcf; 156 fraction the_fraction(the_numerator,the_denominator); 157 return the_fraction; 158 } 159 160 fraction operator/(const fraction& the_fraction_a,const fraction& the_fraction_b) 161 { 162 return the_fraction_a*fraction(the_fraction_b.denominator,the_fraction_b.numerator); 163 } 164 165 fraction operator+= (fraction& the_fraction_a,const fraction& the_fraction_b) 166 { 167 return the_fraction_a=the_fraction_a+the_fraction_b; 168 } 169 170 fraction operator-= (fraction& the_fraction_a,const fraction& the_fraction_b) 171 { 172 return the_fraction_a=the_fraction_a-the_fraction_b; 173 } 174 175 fraction operator*= (fraction& the_fraction_a,const fraction& the_fraction_b) 176 { 177 return the_fraction_a=the_fraction_a*the_fraction_b; 178 } 179 180 fraction operator/= (fraction& the_fraction_a,const fraction& the_fraction_b) 181 { 182 return the_fraction_a=the_fraction_a/the_fraction_b; 183 } 184 185 fraction operator- (const fraction& the_fraction) 186 { 187 return 0-the_fraction; 188 } 189 190 fraction operator++ (fraction& the_fraction) 191 { 192 the_fraction=the_fraction+1; 193 return the_fraction; 194 } 195 fraction operator++ (fraction& the_fraction,int) 196 { 197 the_fraction=the_fraction+1; 198 return the_fraction-1; 199 } 200 fraction operator-- (fraction& the_fraction) 201 { 202 the_fraction=the_fraction-1; 203 return the_fraction; 204 } 205 fraction operator-- (fraction& the_fraction,int) 206 { 207 the_fraction=the_fraction-1; 208 return the_fraction+1; 209 } 210 211 212 213 bool operator> (fraction& the_fraction_a,const fraction& the_fraction_b) 214 { 215 return the_fraction_a.numerator*the_fraction_b.denominator>the_fraction_b.numerator*the_fraction_a.denominator; 216 } 217 bool operator< (fraction& the_fraction_a,const fraction& the_fraction_b) 218 { 219 return the_fraction_a.numerator*the_fraction_b.denominator<the_fraction_b.numerator*the_fraction_a.denominator; 220 } 221 bool operator>= (fraction& the_fraction_a,const fraction& the_fraction_b) 222 { 223 return the_fraction_a.numerator*the_fraction_b.denominator>=the_fraction_b.numerator*the_fraction_a.denominator; 224 } 225 bool operator<= (fraction& the_fraction_a,const fraction& the_fraction_b) 226 { 227 return the_fraction_a.numerator*the_fraction_b.denominator<=the_fraction_b.numerator*the_fraction_a.denominator; 228 } 229 bool operator== (fraction& the_fraction_a,const fraction& the_fraction_b) 230 { 231 return the_fraction_a.numerator*the_fraction_b.denominator==the_fraction_b.numerator*the_fraction_a.denominator; 232 } 233 bool operator!= (fraction& the_fraction_a,const fraction& the_fraction_b) 234 { 235 return the_fraction_a.numerator*the_fraction_b.denominator!=the_fraction_b.numerator*the_fraction_a.denominator; 236 } 237 238 239 240 fraction::fraction() 241 { 242 numerator=0; 243 denominator=1; 244 } 245 246 fraction::fraction(long long the_numerator) 247 { 248 numerator=the_numerator; 249 denominator=1; 250 } 251 252 fraction::fraction(long long the_numerator,long long the_denominator) 253 { 254 long long mcf=fraction::MCF(the_numerator,the_denominator); 255 numerator=the_numerator/mcf; 256 denominator=the_denominator/mcf; 257 } 258 259 fraction::fraction(fraction the_fraction_a,fraction the_fraction_b) 260 { 261 long long the_numerator=the_fraction_a.numerator*the_fraction_b.denominator; 262 long long the_denominator=the_fraction_a.denominator*the_fraction_b.numerator; 263 long long mcf=fraction::MCF(the_numerator,the_denominator); 264 numerator=the_numerator/mcf; 265 denominator=the_denominator/mcf; 266 } 267 268 double fraction::decimal() 269 { 270 return 1.0*numerator/denominator; 271 } 272 273 long long fraction::getnum() 274 { 275 return numerator; 276 } 277 long long fraction::getden() 278 { 279 return denominator; 280 } 281 void fraction::setnum(long long num) 282 { 283 numerator=num; 284 long long mcf=fraction::MCF(numerator,denominator); 285 numerator/=mcf; 286 denominator/=mcf; 287 } 288 void fraction::setden(long long den) 289 { 290 if(den!=0) 291 { 292 denominator=den; 293 long long mcf=fraction::MCF(numerator,denominator); 294 numerator/=mcf; 295 denominator/=mcf; 296 } 297 else 298 { 299 cout<<"False,the denominator == 0!!!"; 300 } 301 } 302 303 typedef fraction fac; 304 305 } 306 307 #endif
使用方法
一個重載了幾乎全部運算符的分數類。函數
friend fraction operator- (const fraction& the_fraction);性能
friend fraction operator++ (fraction& the_fraction);spa
friend fraction operator++ (fraction& the_fraction,int);code
friend fraction operator-- (fraction& the_fraction);對象
friend fraction operator-- (fraction& the_fraction,int);blog
這些友元函數重載了前置與後置的自增自減運算符,以及取負運算符,如a是一個分數,-a表示a的相反數,a++/a--表示後置遞增/遞減,++a/--a表示前置遞增/遞減。ci
friend ostream& operator<< (ostream& os,const fraction& the_fraction);
friend istream& operator>> (istream& is,fraction& the_fraction);
該函數重載了輸入輸出運算符,使之能夠用於cin/cout對象,如a是分數類對象,其值等於-4/3,cout<<a,將在屏幕上顯示(-4/3),cin>>a將讀取一個整數加一個分數線再加一個整數,如42/43,輸入完畢後以回車結尾,不能夠以空格結尾,能夠任意一個整數前加負號已輸入分數(若輸入-4/-3,則系統會按整數存儲,即4/3)。
friend fraction operator+ (const fraction& the_fraction_a,const fraction& the_fraction_b);
friend fraction operator- (const fraction& the_fraction_a,const fraction& the_fraction_b);
friend fraction operator* (const fraction& the_fraction_a,const fraction& the_fraction_b);
friend fraction operator/ (const fraction& the_fraction_a,const fraction& the_fraction_b);
這四個運算符重載了+-*/四則運算,使之能夠用於分數(計算結果若非最簡分數,系統會自動約分)。
friend fraction operator+= (fraction& the_fraction_a,const fraction& the_fraction_b);
friend fraction operator-= (fraction& the_fraction_a,const fraction& the_fraction_b);
friend fraction operator*= (fraction& the_fraction_a,const fraction& the_fraction_b);
friend fraction operator/= (fraction& the_fraction_a,const fraction& the_fraction_b);
這四個函數重載了+=,-=,*=,/=複合運算符,使之能夠用於分數,將返回其右面的表達式的值。
friend bool operator> (fraction& the_fraction_a,const fraction& the_fraction_b);
friend bool operator< (fraction& the_fraction_a,const fraction& the_fraction_b);
friend bool operator>= (fraction& the_fraction_a,const fraction& the_fraction_b);
friend bool operator<= (fraction& the_fraction_a,const fraction& the_fraction_b);
friend bool operator== (fraction& the_fraction_a,const fraction& the_fraction_b);
friend bool operator!= (fraction& the_fraction_a,const fraction& the_fraction_b);
該6個函數重載了關係運算符,使之能夠用於分數。
fraction();
此爲默認構造函數,將分數初始化爲0/1。
fraction(long long the_numerator);
此爲轉換構造函數,能夠將一個long long類型的數據轉換爲分數,同時也能夠顯示調用,如:fraction a(10);將建立一個值爲10/1的分數。
fraction(long long the_numerator,long long the_denominator);
fraction(fraction the_fraction_a,fraction the_fraction_b);
此爲最主要的構造函數,將兩個參數的商做爲分數的值。
如:fraction a(150,100)將建立一個值爲3/2的分數。
如:fraction a(fraction(2,3),fraction(3,2))將建立一個值爲1/1的分數。
double decimal();
返回分子除以分母的double型值。
long long getnum();
返回分子的值。
long long getden();
返回分母的值。
void setnum(long long num);
接受一個參數,並將其賦給分子(自動約分)。
void setden(long long den);
接受一個非0參數,並將其賦給分母(自動約分)(若參數爲0,則不會賦給分母,並在標準輸出流中輸出錯誤信息:"False,the denominator == 0!!!")。
分子:long long numerator;
分母:long long denominator;
static long long MCF(long long a, long long b);
此函數接受兩個long long型參數,並返回一個值等於他們的最大公因數的long long型值。如fraction::MCF(2,4)的值爲2。
使用fraction時的一切有關代碼中的fraction均可以簡寫爲fac,兩者徹底等價。
將分數類的頭文件與使用它的源代碼文件保存在同一目錄下,先編譯一下頭文件,而後再編譯源代碼文件,不然會有編譯錯誤,編譯後的頭文件若是被更改或轉移到另外一目錄下後也必須從新編譯,不然會有編譯錯誤!!!
後記
此爲原創文章,未經容許,嚴禁轉載!!!(跟我說明後能夠,但要說明出處)