/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者:鲍增凯
* 完成日期: 2012 年 5 月 7 日
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者:鲍增凯
* 完成日期: 2012 年 5 月 7 日
* 版 本 号:
01.#include<iostream.h>
02.
03.//using namespace std;
04.
05.class CFraction
06.{
07.private:
08.
09. int nume; // 分子
10.
11. int deno; // 分母
12.
13.public:
14.
15. CFraction(int nu=0,int de=1); //构造函数,初始化用
16.
17. void Simplify(); //化简(使分子分母没有公因子)
18.
19. friend istream& operator >> (istream&, CFraction &);
20.
21. friend ostream& operator << (ostream&, CFraction &);
22.
23. CFraction operator +(CFraction &c); //声明重载运算符 + 的函数
24.
25. CFraction operator -(CFraction &c); //声明重载运算符 + 的函数
26.
27. CFraction operator *(CFraction &c); //声明重载运算符 + 的函数
28.
29. CFraction operator /(CFraction &c); //声明重载运算符 + 的函数
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 == (CFraction &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 << "请依次输入要操作的分数的分子和分母(用“/”隔开):";
58.
59. input >> c.nume >> f >> c.deno;
60.
61. if(f != '/')
62. cout << "输入格式有误!!!" << 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 << "化到最简后的分数为:" << c.nume / c.deno << endl;
78.
79. else
80. output << "化到最简后的分数为:" << c.nume << '/' << c.deno << endl;
81.
82. int n;
83.
84. output << "化到真分数的分数为:";
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() //化简(使分子分母没有公因子)
106.{
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 CFraction::operator +(CFraction &c)
131.{
132. return CFraction (nume * c.deno + deno * c.nume, deno * c.deno);
133.}
134.
135.CFraction CFraction::operator -(CFraction &c)
136.{
137. return CFraction (nume * c.deno - deno * c.nume, deno * c.deno);
138.}
139.
140.CFraction CFraction::operator *(CFraction &c)
141.{
142. return CFraction (nume * c.nume, deno * c.deno);
143.}
144.
145.CFraction CFraction::operator /(CFraction &c)
146.{
147. return CFraction (nume * c.deno, deno * c.nume);
148.}
149.
150.bool CFraction::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;
205.
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.实现两个分数的相加" << 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. cout << "2.实现两个分数的相减" << endl;
247.
248. CFraction c5, c6, c7;
249.
250. cin >> c5;
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.实现两个分数的相乘" << 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 * c9;
275.
276. cout << "c8 * c9 = " << c10;
277.
278. cout << "4.实现两个分数的相除" << 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.实现两个分数大于的比较" << 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)?" 成立":" 不成立") << endl;
307.
308. cout << "6.实现两个分数小于的比较" << 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)?" 成立":" 不成立") << endl;
321.
322. cout << "7.实现两个分数大于等于的比较" << endl;
323.
324. CFraction c18, c19;
325.
326. cin >> c18;
327.
328. cout << "c18 = " << c18;
329.
330. cin >> c19;
331.
332. cout << "c19 = " << c19;
333.
334. cout << "c18 >= c19" << (c18.operator >=(c19)?" 成立":" 不成立") << endl;
335.
336. cout << "87.实现两个分数小于等于的比较" << 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" << (c20.operator <=(c21)?" 成立":" 不成立") << endl;
349.
350. cout << "9.实现两个分数等于的比较" << 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)?" 成立":" 不成立") << endl;
363.
364. cout << "7.实现两个分数不等于的比较" << 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)?" 成立":" 不成立") << endl;
377.
378. // system("pause");
379.
380. return 0;
381.}