分数类
1 #include<iostream> 2 #include<math.h> 3 using namespace std; 4 class Fract 5 { 6 public: 7 Fract(){num=0;den=1;} 8 Fract(int a=0,int b) 9 { 10 if(b==0) 11 { 12 cout<<"invalid fraction!!please check it!"; 13 } 14 else 15 { 16 if(a*b<0) 17 { 18 num=-abs(a); 19 den=abs(b); 20 } 21 else 22 { 23 num=abs(a); 24 den=abs(b); 25 } 26 if(num!=0) 27 cutit(num,den); 28 } 29 30 }//构造 31 Fract(const Fract &t){num=t.num;den=t.den;}//复制构造 32 ~Fract(){} 33 34 void Show()const;//show函数 35 void Input();//input函数 36 void cutit(int &a,int &b); 37 friend Fract operator + (const Fract &a,const Fract &b); 38 friend Fract operator - (const Fract &a,const Fract &b); 39 friend Fract operator * (const Fract &a,const Fract &b); 40 friend Fract operator / (const Fract &a,const Fract &b); 41 friend Fract operator - (const Fract &a);//前置负号 42 Fract operator ++ ();//前置 ++ 43 Fract operator ++ (int i);//后置++ 44 45 void operator += (const Fract &b);//混合赋值+= 46 void operator -= (const Fract &b);//混合赋值-= 47 friend ostream &operator <<(ostream &out,const Fract &k);//重载<< 48 friend istream &operator >>(istream &in,Fract &a);//重载>> 49 friend Fract operator * (const Fract &a,int i);//分数乘整数 50 friend Fract operator * (int i,const Fract &a);//整数乘分数 51 friend Fract operator / (const Fract &a,int i);//分数除以整数 52 friend Fract operator / (int i,const Fract &a);//整数除以分数 53 double doublef(){return (double)num/den;}//分数转double 54 55 friend bool operator == (const Fract &a,const Fract &b); 56 friend bool operator != (const Fract &a,const Fract &b); 57 friend bool operator > (const Fract &a,const Fract &b); 58 friend bool operator < (const Fract &a,const Fract &b); 59 private: 60 61 int num; //分子 62 63 int den; //分母 64 65 }; 66 void Fract::cutit(int &a,int &b) 67 { 68 int c,t; 69 int k1=abs(a),k2=abs(b); 70 if(k2>k1) 71 { 72 t=k1; 73 k1=k2; 74 k2=t; 75 } 76 do 77 { 78 c=k1%k2; 79 k1=k2; 80 k2=c; 81 }while(c!=0); 82 a=a/k1; 83 b=b/k1; 84 } 85 void Fract::Show()const 86 { 87 int a=num,b=den; 88 if(a==0) 89 cout<<"0"<<endl; 90 else if(a==b) 91 cout<<"1"<<endl; 92 else if(b==1) 93 cout<<num<<endl; 94 else 95 { 96 cout<<num<<"/"<<den<<endl; 97 } 98 } 99 void Fract::Input() 100 { 101 int a, b; 102 cin>>a>>b; 103 while(b==0) 104 { 105 cout<<"invalid fraction!!please rebuild it!"; 106 cin>>a>>b; 107 } 108 if(b!=0) 109 { 110 if(a*b<0) 111 { 112 num=-abs(a); 113 den=abs(b); 114 } 115 else 116 { 117 num=abs(a); 118 den=abs(b); 119 } 120 cutit(num,den); 121 } 122 } 123 Fract operator+(const Fract &a,const Fract &b) 124 { 125 int k1,k2; 126 k1=a.num*b.den+b.num*a.den; 127 k2=a.den*b.den; 128 return Fract(k1,k2); 129 } 130 Fract operator-(const Fract &a,const Fract &b) 131 { 132 int k1,k2; 133 k1=a.num*b.den-b.num*a.den; 134 k2=a.den*b.den; 135 return Fract(k1,k2); 136 } 137 Fract operator*(const Fract &a,const Fract &b) 138 { 139 return Fract(a.num*b.num,a.den*b.den); 140 } 141 Fract operator/(const Fract &a,const Fract &b) 142 { 143 return Fract(a.num*b.den,a.den*b.num); 144 } 145 Fract operator - (const Fract &a) 146 { 147 return Fract(-a.num,a.den); 148 } 149 Fract Fract::operator ++ () 150 { 151 num+=den; 152 return Fract(num,den); 153 } 154 Fract Fract::operator ++ (int i) 155 { 156 int a=num,b=den; 157 num+=den; 158 return Fract(a,b); 159 } 160 bool operator == (const Fract &a,const Fract &b) 161 { 162 double j1,j2; 163 j1=a.num/a.den; 164 j2=b.num/b.den; 165 if(j1-j2>-0.000001&&j1-j2<0.000001) 166 return 1; 167 else 168 return 0; 169 } 170 bool operator != (const Fract &a,const Fract &b) 171 { 172 double j1,j2; 173 j1=a.num/a.den; 174 j2=b.num/b.den; 175 if(j1-j2>-0.000001&&j1-j2<0.000001) 176 return 0; 177 else 178 return 1; 179 } 180 bool operator > (const Fract &a,const Fract &b) 181 { 182 double j1,j2; 183 j1=a.num/a.den; 184 j2=b.num/b.den; 185 if(j1>j2) 186 return 1; 187 else 188 return 0; 189 } 190 bool operator < (const Fract &a,const Fract &b) 191 { 192 double j1,j2; 193 j1=a.num/a.den; 194 j2=b.num/b.den; 195 if(j1<j2) 196 return 1; 197 else 198 return 0; 199 } 200 void Fract::operator += (const Fract &b) 201 { 202 int k1,k2; 203 k1=num*b.den+b.num*den; 204 k2=den*b.den; 205 cutit(k1,k2); 206 num=k1; 207 den=k2; 208 } 209 void Fract::operator -= (const Fract &b) 210 { 211 int k1,k2; 212 k1=num*b.den-b.num*den; 213 k2=den*b.den; 214 cutit(k1,k2); 215 num=k1; 216 den=k2; 217 } 218 Fract operator * (const Fract &a,int i) 219 { 220 return Fract(a.num*i,a.den); 221 } 222 Fract operator * (int i,const Fract &a) 223 { 224 return Fract(a.num*i,a.den); 225 } 226 Fract operator / (const Fract &a,int i) 227 { 228 return Fract(a.num,a.den*i); 229 } 230 Fract operator / (int i,const Fract &a) 231 { 232 return Fract(i*a.den,a.num); 233 } 234 /* 235 Fract Fract::operator =(double &k) 236 { 237 return a.num/a.den; 238 }*/ 239 ostream &operator <<(ostream &out,const Fract &k) 240 { 241 int a=k.num,b=k.den; 242 if(a==0) 243 cout<<"0"<<endl; 244 else if(a==b) 245 cout<<"1"<<endl; 246 else if(b==1) 247 cout<<k.num<<endl; 248 else 249 { 250 cout<<k.num<<"/"<<k.den<<endl; 251 } 252 return out; 253 } 254 istream &operator >>(istream &in,Fract &a) 255 { 256 int k1,k2; 257 in>>k1>>k2; 258 a=Fract(k1,k2); 259 return in; 260 } 261 int main() 262 263 { 264 265 //测试默认构造函数与输出函数 266 267 Fract f1; 268 269 f1.Show(); //输出:0 270 271 //测试构造函数与输出函数 272 273 Fract f2(2,4); 274 Fract f3(-2,-4); 275 Fract f4(2,-5); 276 Fract f5(-2,5); 277 Fract f6(2,2); 278 Fract f7(0,8); 279 Fract f8(4,-2); 280 281 f2.Show(); //输出:1/2 282 283 f3.Show(); //输出:1/2 284 285 f4.Show(); //输出:-2/5 286 287 f5.Show(); //输出:-2/5 288 289 f6.Show(); //输出:1 290 291 f7.Show(); //输出:0 292 293 f8.Show(); //输出:-2 294 295 //测试复制构造函数 296 297 Fract f9(f2); 298 299 f9.Show(); //输出:1/2 300 301 //测试输入函数 302 303 f1.Input(); //输入: 2 6 304 305 f2.Input(); //输入: 1 8 306 307 f1.Show(); //输出: 1/3 308 309 f2.Show(); //输出: 1/8 310 311 //测试加减乘除 312 313 f3 = f1+f2; 314 315 f3.Show(); //输出:11/24 316 317 f4 = f1-f2; 318 319 f4.Show(); //输出:5/24 320 321 f3 = f1*f2; 322 323 f3.Show(); //输出 1/24 324 325 f4 = f4 / f3; 326 327 f4.Show(); //输出 5 328 329 //测试分数的负(-)、自增(前后置++) 330 331 f3 = -f1; 332 333 f3.Show(); //输出:-1/3 334 335 f3 = ++f1; 336 337 f1.Show(); //输出:4/3 338 339 f3.Show(); //输出:4/3 340 341 f3 = f1++; 342 343 f1.Show(); //输出:7/3 344 345 f3.Show(); //输出:4/3 346 347 //测试关系运算符== != > <以及分数与整数的 乘 除 运算 348 //对应数据 f1=7/3 f2=1/8 f3=4/3 f4=5 f5=-2/5 f6=1 f7=0 f8=-2 f9=1/2 349 if(f2*40==f4) 350 { 351 cout<<"yes1"<<endl; //验证分数乘整数 == 352 } 353 if(40*f2==f4) 354 { 355 cout<<"yes2"<<endl;//验证整数乘分数== 356 } 357 if((f2/3)==(f3/32)) 358 { 359 cout<<"yes3"<<endl;//验证分数除以整数== 360 } 361 if(4/f3!=f3) 362 { 363 cout<<"yes4"<<endl;//验证整数除以分数!= 364 } 365 if(f6>f9) 366 { 367 cout<<"yes5"<<endl;//验证大于 368 } 369 if(f9<f6) 370 { 371 cout<<"yes6"<<endl;//验证小于 372 } 373 //验证+= -= 374 f2+=f3; 375 376 f2.Show(); //输出 35/24 377 378 f4-=f5; 379 380 f4.Show();//输出 27/5 381 382 //验证 分数转double 383 double q=f4.doublef(); 384 cout<<q<<endl; 385 //验证 >> << 386 cin>>f1; //输入 5 10 387 cout<<f1; //输出 1/2 388 389 return 0; 390 }