1 #include<iostream>
2 #include<cmath>
3 #include<cstdlib>
4 int simt(int ,int );
5 int simb(int, int);
6 #include "fraction.h"
7 using std::cout;
8 using std::endl;
9 using std::ends;
10 using std::cin;
11 int simt(int x, int y)
12 {
13 int fx = x;
14 if (x == 0 || y == 0)
15 exit(0);
16 if (x < 0)
17 x = abs(x);
18 if (y < 0)
19 y = abs(y);
20 while (x != y)
21 {
22 if (x > y)
23 x = x - y;
24 else
25 y = y - x;
26 }
27 return fx / x;
28 }
29 int simb(int x, int y)
30 {
31 int fy = y;
32 if (x == 0 || y == 0)
33 exit(0);
34 if (x < 0)
35 x = abs(x);
36 if (y < 0)
37 y = abs(y);
38 while (x != y)
39 {
40 if (x > y)
41 x = x - y;
42 else
43 y = y - x;
44 }
45 return fy / x;
46 }
47 void Fraction::input()
48 {
49 cin >> top >> bottom;
50 if (bottom == 0)
51 {
52 cout << "wrong input" << endl;
53 system("pause");
54 exit(0);
55 }
56 }
57 void Fraction::output()
58 {
59 cout << top << "/" << bottom<<ends;
60 }
61 Fraction::Fraction(int x1 , int y1 ) :top(x1), bottom(y1)
62 {
63 if (bottom == 0)
64 {
65 cout << "wrong input" << endl;
66 system("pause");
67 exit(0);
68 }
69 }
70 void Fraction::add(Fraction c2)
71 {
72 if (c2.bottom == 0)
73 {
74 cout << "wrong input" << endl;
75 system("pause");
76 exit(0);
77 }
78 top = top * c2.bottom;
79 c2.top = c2.top*bottom;
80 bottom = bottom * c2.bottom;
81 c2.bottom = bottom;
82 cout << "add complete" << endl;
83 cout << simt((top+c2.top),bottom) << "/" << simb((top+c2.top),bottom) << endl;
84 }
85 void Fraction::sub(Fraction c3)
86 {
87 if (bottom==0&&c3.bottom == 0)
88 {
89 cout << "wrong input" << endl;
90 system("pause");
91 exit(0);
92 }
93 top = top * c3.bottom;
94 c3.top = c3.top*bottom;
95 bottom = bottom * c3.bottom;
96 c3.bottom = bottom;
97 cout << "sub complete" << endl;
98 cout << simt((top - c3.top),bottom) << "/" <<simb((top-c3.top),bottom) << endl;
99 }
100 void Fraction::mult(Fraction c4)
101 {
102 if (bottom == 0 && c4.bottom == 0)
103 {
104 cout << "wrong input" << endl;
105 system("pause");
106 exit(0);
107 }
108 top = top * c4.top;
109 bottom = bottom * c4.bottom;
110 cout << "mult complete" << endl;
111 cout <<simt(top,bottom)<<"/"<<simb(top,bottom)<< endl;
112 }
113 void Fraction::div(Fraction c5)
114 {
115 if (bottom == 0 && c5.bottom == 0)
116 {
117 cout << "wrong input" << endl;
118 system("pause");
119 exit(0);
120 }
121 top = top * c5.bottom;
122 bottom = bottom * c5.top;
123 cout << "div complete" << endl;
124 cout << simt(top,bottom)<<"/"<<simb(top,bottom)<<endl;
125 }
126 void Fraction::compare(Fraction c6,Fraction c7)
127 {
128 Fraction c1, c2;
129 c1.top = c6.top * c7.bottom;
130 c2.top = c7.top*c6.bottom;
131 if (c1.top > c2.top)
132 {
133 c6.output();
134 cout << ">";
135 c7.output();
136 }
137 else
138 {
139 c6.output();
140 cout << "<";
141 c7.output();
142 }
143
144 }