引言
我们说,编程语言的精髓在于封装,而面向对象语言完胜面向过程语言的原因就是具有更好的可封装性,而C++就是这样的一种多范型语言,常用而复杂的工作完全不必在每一份源文件中重敲,就好像我们不需要自己手写printf()和scanf()一样,自建一个属于自己的头文件,可以为编程提供很大的帮助,我今天就要推荐一下我自己原创的分数类(实际上C++STL库有分数类,只是性能很低,而且使用极其不方便,而我的这个分数类具有很多前者不具备的优点)。
注:此为原创文章,转载则务必说明原出处。
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);
friend fraction operator++ (fraction& the_fraction,int);
friend fraction operator-- (fraction& the_fraction);
friend fraction operator-- (fraction& the_fraction,int);
这些友元函数重载了前置与后置的自增自减运算符,以及取负运算符,如a是一个分数,-a表示a的相反数,a++/a--表示后置递增/递减,++a/--a表示前置递增/递减。
二目运算符重载
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,二者完全等价。
将分数类的头文件与使用它的源代码文件保存在同一目录下,先编译一下头文件,然后再编译源代码文件,否则会有编译错误,编译后的头文件如果被更改或转移到另一目录下后也必须重新编译,否则会有编译错误!!!
后记
此为原创文章,未经允许,严禁转载!!!(跟我说明后可以,但要说明出处)