有理数的四则运算
链接:http://www.nowcoder.com/pat/6/problem/4060
题目描述
本题要求编写程序,计算2个有理数的和、差、积、商。
输入描述:
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分
母不为0。
输出描述:
分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的
最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中
没有超过整型范围的整数。
输入例子:
5/3 0/6
输出例子:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
思路:多加几个变量保存输入的有理数,1.用来保存最简形式 2.用来保存计算形式,就是通分过的形式。代码写起来比较复杂,还有输入格式的要求也比较多。
浮点错误:出现了除以0的情况。
1 #include "iostream" 2 #include <iomanip> 3 #include <string.h> 4 #include <string> 5 #include <vector> 6 #include <cmath> 7 #include <cctype> 8 #include <algorithm> 9 using namespace std; 10 11 typedef struct YLS{ 12 int z; 13 int fz; 14 int fm; 15 }YLS; 16 17 void change(YLS &yls) 18 { 19 if(yls.fm == 0) return; 20 if(yls.fz/yls.fm != 0) 21 { 22 yls.z += yls.fz/yls.fm; 23 yls.fz = yls.fz%yls.fm; 24 } 25 } 26 void tongFen(YLS &yls1, YLS &yls2) 27 { 28 29 int t1, t2; 30 t1 = yls1.fm; 31 t2 = yls2.fm; 32 yls1.fz *= t2; 33 yls1.fm *= t2; 34 yls2.fz *= t1; 35 yls2.fm *= t1; 36 } 37 void output1(YLS yls1) 38 { 39 int mark = 0; 40 if(yls1.z<0 || yls1.fz<0 ||yls1.fm<0) 41 { 42 mark = 1; 43 cout <<'('; 44 } 45 if(yls1.fz==0 && yls1.z==0) 46 { 47 cout <<0; 48 } 49 else 50 { 51 if(yls1.z == 0) 52 { 53 if(yls1.fm < 0) 54 { 55 yls1.fm = -yls1.fm; 56 yls1.fz = -yls1.fz; 57 } 58 cout <<yls1.fz <<"/" <<yls1.fm; 59 } 60 else 61 { 62 if(yls1.fz == 0) 63 { 64 cout <<yls1.z; 65 } 66 else 67 { 68 if(yls1.fm < 0) 69 { 70 yls1.fm = -yls1.fm; 71 yls1.fz = -yls1.fz; 72 } 73 if(yls1.fz < 0) 74 { 75 if(yls1.z > 0) yls1.z = -yls1.z; 76 yls1.fz = - yls1.fz; 77 } 78 cout <<yls1.z <<" " <<yls1.fz <<"/" <<yls1.fm; 79 } 80 } 81 } 82 if(mark == 1) cout <<')'; 83 cout <<" " <<"/" <<" "; 84 cout <<0; 85 cout <<" " <<"=" <<" "; 86 cout <<"Inf"; 87 } 88 void output(YLS yls1, YLS yls2, char c, YLS yls3) 89 { 90 int mark = 0; 91 if(yls1.z<0 || yls1.fz<0 || yls1.fm<0) 92 { 93 mark = 1; 94 cout <<'('; 95 } 96 if(yls1.fz==0 && yls1.z==0) 97 { 98 cout <<0; 99 } 100 else 101 { 102 if(yls1.z == 0) 103 { 104 if(yls1.fm < 0) 105 { 106 yls1.fm = -yls1.fm; 107 yls1.fz = -yls1.fz; 108 } 109 cout <<yls1.fz <<"/" <<yls1.fm; 110 } 111 else 112 { 113 if(yls1.fz == 0) 114 { 115 cout <<yls1.z; 116 } 117 else 118 { 119 if(yls1.fm < 0) 120 { 121 yls1.fm = -yls1.fm; 122 yls1.fz = -yls1.fz; 123 } 124 if(yls1.fz < 0) 125 { 126 if(yls1.z > 0) yls1.z = -yls1.z; 127 yls1.fz = - yls1.fz; 128 } 129 cout <<yls1.z <<" " <<yls1.fz <<"/" <<yls1.fm; 130 } 131 } 132 } 133 if(mark == 1) cout <<')'; 134 cout <<" " <<c <<" "; 135 mark = 0; 136 if(yls2.z<0 || yls2.fz<0 || yls2.fm<0) 137 { 138 mark = 1; 139 cout <<'('; 140 } 141 if(yls2.fz==0 && yls2.z==0) 142 { 143 cout <<0; 144 } 145 else 146 { 147 if(yls2.z == 0) 148 { 149 if(yls2.fm < 0) 150 { 151 yls2.fm = -yls2.fm; 152 yls2.fz = -yls2.fz; 153 } 154 cout <<yls2.fz <<"/" <<yls2.fm; 155 } 156 else 157 { 158 if(yls2.fz == 0) 159 { 160 cout <<yls2.z; 161 } 162 else 163 { 164 if(yls2.fm < 0) 165 { 166 yls2.fm = -yls2.fm; 167 yls2.fz = -yls2.fz; 168 } 169 if(yls2.fz < 0) 170 { 171 if(yls2.z > 0) yls2.z = -yls2.z; 172 yls2.fz = - yls2.fz; 173 } 174 cout <<yls2.z <<" " <<yls2.fz <<"/" <<yls2.fm; 175 } 176 } 177 } 178 if(mark == 1) cout <<')'; 179 cout <<" " <<"=" <<" "; 180 mark = 0; 181 if(yls3.z<0 || yls3.fz<0 || yls3.fm<0) 182 { 183 mark = 1; 184 cout <<'('; 185 } 186 if(yls3.fz==0 && yls3.z==0) 187 { 188 cout <<0; 189 } 190 else 191 { 192 if(yls3.z == 0) 193 { 194 if(yls3.fm < 0) 195 { 196 yls3.fm = -yls3.fm; 197 yls3.fz = -yls3.fz; 198 } 199 cout <<yls3.fz <<"/" <<yls3.fm; 200 } 201 else 202 { 203 if(yls3.fz == 0) 204 { 205 cout <<yls3.z; 206 } 207 else 208 { 209 if(yls3.fm < 0) 210 { 211 yls3.fm = -yls3.fm; 212 yls3.fz = -yls3.fz; 213 } 214 if(yls3.fz < 0) 215 { 216 if(yls3.z > 0) yls3.z = -yls3.z; 217 yls3.fz = -yls3.fz; 218 } 219 cout <<yls3.z <<" " <<yls3.fz <<"/" <<yls3.fm; 220 } 221 } 222 } 223 if(mark == 1) cout <<')'; 224 } 225 int gcd(int x, int y) 226 { 227 if(y != 0) 228 {if(x%y == 0) 229 { 230 return y; 231 } 232 else 233 { 234 return gcd(y, x%y); 235 }} 236 } 237 int main() 238 { 239 YLS yls1, yls2, yls3, yls4; 240 yls1.z = 0; 241 cin >>yls1.fz; 242 getchar(); 243 cin >>yls1.fm; 244 yls2.z = 0; 245 cin >>yls2.fz; 246 getchar(); 247 cin >>yls2.fm; 248 249 yls3 = yls1; 250 yls4 = yls2; 251 int gys = 0; 252 if(yls2.fz != 0) 253 { 254 gys = gcd(yls2.fz, yls2.fm); 255 } 256 if(gys != 0) 257 {yls2.fz /= gys; 258 yls2.fm /= gys;} 259 gys = 0; 260 if(yls1.fz != 0) 261 { 262 gys = gcd(yls1.fz, yls1.fm); 263 } 264 if(gys != 0) 265 {yls1.fz /= gys; 266 yls1.fm /= gys;} 267 change(yls1); 268 change(yls2); 269 270 YLS ylsX,ylsY,ylsZ; 271 ylsX = yls3; 272 ylsY = yls4; 273 tongFen(ylsX, ylsY); 274 ylsZ.z = ylsX.z+ylsY.z; 275 ylsZ.fz = ylsX.fz+ylsY.fz; 276 ylsZ.fm = ylsX.fm; 277 gys = 0; 278 if(ylsZ.fz != 0) 279 { 280 gys = gcd(ylsZ.fz, ylsZ.fm); 281 } 282 if(gys != 0) 283 {ylsZ.fz /= gys; 284 ylsZ.fm /= gys;} 285 change(ylsZ); 286 output(yls1, yls2, '+', ylsZ); 287 cout <<endl; 288 289 ylsZ.z = ylsX.z-ylsY.z; 290 ylsZ.fz = ylsX.fz-ylsY.fz; 291 ylsZ.fm = ylsX.fm; 292 gys = 0; 293 if(ylsZ.fz != 0) 294 { 295 gys = gcd(ylsZ.fz, ylsZ.fm); 296 } 297 if(gys != 0) 298 {ylsZ.fz /= gys; 299 ylsZ.fm /= gys;} 300 change(ylsZ); 301 output(yls1, yls2, '-', ylsZ); 302 cout <<endl; 303 304 ylsZ.z = 0; 305 ylsZ.fz = yls3.fz*yls4.fz; 306 ylsZ.fm = yls3.fm*yls4.fm; 307 change(ylsZ); 308 gys = 0; 309 if(ylsZ.fz != 0) 310 { 311 gys = gcd(ylsZ.fz, ylsZ.fm); 312 } 313 if(gys != 0) 314 {ylsZ.fz /= gys; 315 ylsZ.fm /= gys;} 316 output(yls1, yls2, '*', ylsZ); 317 cout <<endl; 318 319 YLS yls5; 320 yls5.fm = yls4.fz; 321 yls5.fz = yls4.fm; 322 if(yls4.fz == 0) 323 { 324 output1(yls1); 325 cout <<endl; 326 } 327 else 328 { 329 ylsZ.z = 0; 330 ylsZ.fz = yls3.fz*yls5.fz; 331 ylsZ.fm = yls3.fm*yls5.fm; 332 change(ylsZ); 333 gys = 0; 334 if(ylsZ.fz != 0) 335 { 336 gys = gcd(ylsZ.fz, ylsZ.fm); 337 } 338 if(gys != 0) 339 {ylsZ.fz /= gys; 340 ylsZ.fm /= gys;} 341 output(yls1, yls2, '/', ylsZ); 342 cout <<endl; 343 } 344 return 0; 345 }