/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All Rights Reserved.
* File Name:
* Prepared by: BAO Zengkai
* Completion date: January 1, May 7, 2012
* Version number:
01. # include <iostream. h> 02. 03. // using namespace STD; 04. 05. class cfraction 06. {07. PRIVATE: 08. 09. int nume; // molecule 10. 11. int deno; // denominator 12. 13. public: 14. 15. cfraction (INT Nu = 0, int de = 1); // constructor, initialized with 16. 17. void simplify (); // simplification (making the denominator of a molecule have no public factor) 18. 19. friend istream & operator> (istream &, cfraction &); 20. 21. friend ostream & operator <(ostream &, cfraction &); 22. 23. cfraction operator + (C Fraction & C); // declare the function of the overload operator + 24. 25. cfraction operator-(cfraction & C); // declares the function of the overload operator + 26. 27. cfraction operator * (cfraction & C); // declares the function of the overloaded operator + 28. 29. cfraction operator/(cfraction & C); // declare the overloaded operator + function 30. 31. bool operator> (cfraction & C); 32. 33. bool operator <(cfraction & C); 34. 35. bool operator >=( cfraction & C); 36. 37. bool operator <= (cfraction & C); 38. 39. bool operator = (cf Raction & C); 40. 41. bool Operator! = (Cfraction & C); 42 .}; 43. 44. cfraction: cfraction (INT Nu, int de) 45. {46. nume = Nu; 47. 48. deno = de; 49 .} 50. 51. istream & operator> (istream & input, cfraction & C) 52. {53. char F; 54. 55. while (1) 56. {57. cout <"Enter the numerator and denominator of the score to be operated in sequence (separated by"/"):"; 58. 59. input> C. nume> F> C. deno; 60. 61. if (F! = '/') 62. cout <"incorrect input format !!! "<Endl; 63. 64. else 65. break; 66 .} 67. return input; 68 .} 69. 70. ostream & operator <(ostream & output, cfraction & C) 71. {72. output <C. nume <"/" <C. deno <Endl; 73. 74. c. simplify (); 75. 76. if (C. nume % C. deno = 0) 77. output <"to the simplest score:" <C. nume/C. deno <Endl; 78. 79. else 80. output <"to the simplest score:" <C. nume <'/' <C. deno <Endl; 81. 82. int N; 83. 84. output <"to true score The number score is: "; 85. 86. if (C. nume % C. deno = 0) 87. output <C. nume/C. deno <Endl; 88. 89. else 90. {91. if (C. nume <C. deno) 92. output <C. nume <'/' <C. deno <Endl; 93. 94. else 95. {96. N = C. nume/C. deno; 97. 98. output <n <"(" <C. nume-N * C. deno <'/' <C. deno <")" <Endl; 99 .} 100 .} 101. 102. return output; 103 .} 104. 105. void cfraction: simplify () // simplify (make the denominator of a molecule have no public factor) 106. {0, 107. Int I, A; 108. 109. if (nume> deno) 110. {111. A = nume; 112 .} 113. else 114. {115. A = deno; 116 .} 117. 118. for (I = A; I> 1; -- I) 119. {120. 121. if (nume % I = 0 & deno % I = 0) 122. {123. nume = nume/I; 124. 125. deno = deno/I; 126 .} 127 .} 128 .} 129. 130. cfraction: Operator + (cfraction & C) 131. {132. return cfraction (nume * C. deno + deno * C. nume, deno * C. deno); 1 33 .} 134. 135. cfraction: Operator-(cfraction & C) 136. {137. return cfraction (nume * C. deno-deno * C. nume, deno * C. deno); 138 .} 139. 140. cfraction: Operator * (cfraction & C) 141. {142. return cfraction (nume * C. nume, deno * C. deno); 143 .} 144. 145. cfraction: Operator/(cfraction & C) 146. {147. return cfraction (nume * C. deno, deno * C. nume); 148 .} 149. 150. bool cf Raction: Operator> (cfraction & C) 151. {152. if (deno> C. deno) 153. {154. return false; 155 .} 156. 157. if (deno <C. deno) 158. {159. return true; 160 .} 161. if (deno = C. deno) 162. {163. if (nume> C. nume) 164. return true; 165. 166. else 167. return false; 168 .} 169 .} 170. 171. bool cfraction: Operator <(cfraction & C) 172. {173. if (deno> C. deno) 174. {175. return true; 176 .} 177. 178. if (Deno <C. deno) 179. {180. return false; 181 .} 182. if (deno = C. deno) 183. {184. if (nume <C. nume) 185. return true; 186. 187. else 188. return false; 189 .} 190 .} 191. 192. bool cfraction: Operator >=( cfraction & C) 193. {194. if (** this <c) 195. return false; 196. 197. else 198. return true; 199 .} 200. 201. bool cfraction: Operator <=( cfraction & C) 202. {203. if (* This> C) 204. return false; 2 05. 206. else 207. return true; 208 .} 209. 210. bool cfraction: Operator = (cfraction & C) 211. {212. if (nume = C. nume & deno = C. deno) 213. return true; 214. 215. else 216. return false; 217 .} 218. 219. bool cfraction: Operator! = (Cfraction & C) 220. {221. if (nume = C. nume & deno = C. deno) 222. return false; 223. 224. else 225. return true; 226 .} 227. 228.int main () 229. {230. cfraction C1, C2, C3; 231. 232. cout <"1. add two scores "<Endl; 233. 234. cin> C1; 235. 236. cout <"C1 =" <c1; 237. 238. cin>> C2; 239. 240. cout <"C2 =" <c2; 241. 242. c3 = C1 + C2; 243. 244. cout <"C1 + C2 =" <c3; 245. 246. cou T <"2. implements subtraction of two scores "<Endl; 247. 248. cfraction C5, C6, C7; 249. 250. cin> 5; 251. 252. cout <"C5 =" <c5; 253. 254. cin> C6; 255. 256. cout <"C6 =" <c6; 257. 258. c7 = C5-c6; 259. 260. cout <"C5-C6 =" <C7; 261. 262. cout <"3. multiply two scores "<Endl; 263. 264. cfraction C8, C9, C10; 265. 266. cin> C8; 267. 268. cout <"C8 =" <C8; 269. 270. cin> C9; 271. 272. cout <"C9 = "<C9, 273. 274. c10 = C8 x C9; 275. 276. cout <"C8 * C9 =" <C10; 277. 278. cout <"4. achieve the division of two scores "<Endl; 279. 280. cfraction C11, C12, C13; 281. 282. cin> C11; 283. 284. cout <"C11 =" <C11; 285. 286. cin> C12; 287. 288. cout <"C12 =" <C12; 289. 290. c13 = C11/C12; 291. 292. cout <"C11/C12 =" <C13; 293. 294. cout <"5. compare two scores that are greater than <Endl; 295. 296. cfraction C14, C15; 297. 298. cin> C14; 299. 300. cout <"C14 =" <C14; 301. 302. cin> C15; 303. 304. cout <"C15 =" <C15; 305. 306. cout <"C14> C15" <(c14.operator> (C15 )? "True": "Not true") <Endl; 307. 308. cout <"6. compare the two scores smaller than "<Endl; 309. 310. cfraction C16, C17; 311. 312. cin> C16; 313. 314. cout <"C16 =" <C16; 315. 316. cin> C17; 317. 318. cout <"C17 =" <C17; 319. 320. cout <"C16 <C17" <(c16.operator <(C17 )? "True": "Not true") <Endl; 321. 322. cout <"7. compare two scores greater than or equal to "<Endl; 323. 324. cfraction 325, C19. 326. cin> C18; 327. 328. cout <"18 =" <329. 330. cin> 19; 331. 332. cout <"C19 =" <C19; 333. 334. cout <"C6> = C19" <(c18.operator> = (C19 )? "True": "Not true") <Endl; 335. 336. cout <"87. compare the two scores that are less than or equal to "<Endl; 337. 338. cfraction C20, C21; 339. 340. cin> C20; 341. 342. cout <"C20 =" <C20; 343. 344. cin> C21; 345. 346. cout <"C21 =" <C21; 347. 348. cout <"C20 <= C21" <(csf-operator <= (C21 )? "True": "Not true") <Endl; 349. 350. cout <"9. achieve the comparison of two equal scores "<Endl; 351. 352. cfraction c22, C23; 353. 354. cin> c22; 355. 356. cout <"c22 =" <c22; 357. 358. cin> C23; 359. 360. cout <"C23 =" <C23; 361. 362. cout <"c22 = C23" <(c22.operator = (C23 )? "True": "Not true") <Endl; 363. 364. cout <"7. compare the two scores that are not equal to "<Endl; 365. 366. cfraction C24, C25; 367. 368. cin> C24; 369. 370. cout <"C24 =" <C24; 371. 372. cin> C25; 373. 374. cout <"C25 =" <C25; 375. 376. cout <"C24! = C25 "<(c24.operator! = (C25 )? "True": "Not true") <Endl; 377. 378. // system ("pause"); 379. 380. Return 0; 381 .}