View Code
1 #include<iostream>
2 #include<stdlib.h>
3 using namespace std;
4 class Rational
5 {
6 public:
7 Rational(int num,int denom);
8 Rational operator+(Rational rhs);
9 Rational operator-(Rational rhs);
10 Rational operator*(Rational rhs);
11 Rational operator/(Rational rhs);
12
13 void print();
14
15 private:
16
17 void normalize();
18
19 int numerator;
20 int denominator;
21 };
22
23 Rational::Rational(int num,int denom)
24 {
25 numerator = num;
26 denominator = denom;
27 normalize();
28 }
29 void Rational::normalize()
30 {
31 if(denominator < 0)
32 {
33 numerator = -numerator;
34 denominator = -denominator;
35 }
36 int a = abs(numerator);
37 int b = abs(denominator);
38
39 while(b>0)
40 {
41 int t = a % b;
42 a = b;
43 b = t;
44 }
45
46 numerator /= a;
47 denominator /= a;
48 }
49 Rational Rational::operator+(Rational rhs)
50 {
51 int a = numerator;
52 int b = denominator;
53 int c = rhs.numerator;
54 int d = rhs.denominator;
55
56 int e = a*b + c*d;
57 int f = b*d;
58
59 return Rational(e,f);
60 }
61
62 Rational Rational::operator-(Rational rhs)
63 {
64 int a = numerator;
65 int b = denominator;
66 int c = rhs.numerator;
67 int d = rhs.denominator;
68
69 int e = a*b - c*d;
70 int f = b*d;
71
72 return Rational(e,f);
73 }
74 Rational Rational::operator*(Rational rhs)
75 {
76 int a = numerator;
77 int b = denominator;
78 int c = rhs.numerator;
79 int d = rhs.denominator;
80
81 int e = a*c;
82 int f = b*d;
83
84 return Rational(e,f);
85 }
86 Rational Rational::operator/(Rational rhs)
87 {
88 int a = numerator;
89 int b = denominator;
90 int c = rhs.numerator;
91 int d = rhs.denominator;
92
93 int e = a*d;
94 int f = b*c;
95
96 return Rational(e,f);
97 }
98 void Rational::print()
99 {
100 if(numerator % denominator == 0)
101 cout << numerator / denominator;
102 else
103 cout << numerator << "/" << denominator;
104 }
105 int main()
106 {
107 Rational f1(2,16);
108 Rational f2(15,16);
109 f1.print();
110 cout << "+";
111 f2.print();
112 cout << "=";
113 Rational res = f1 + f2;
114 res.print();
115 cout << endl;
116
117 f1.print();
118 cout << "-";
119 f2.print();
120 cout << "=";
121 res = f1 - f2;
122 res.print();
123 cout << endl;
124
125 f1.print();
126 cout<< "*";
127 f2.print();
128 cout<< "=";
129 res = f1 * f2;
130 res.print();
131 cout << endl;
132
133 f1.print();
134 cout << "/";
135 f2.print();
136 cout << "=";
137 res = f1 / f2;
138 res.print();
139 cout << endl;
140 system("pause");
141
142 return 0;
143 }
2 #include<stdlib.h>
3 using namespace std;
4 class Rational
5 {
6 public:
7 Rational(int num,int denom);
8 Rational operator+(Rational rhs);
9 Rational operator-(Rational rhs);
10 Rational operator*(Rational rhs);
11 Rational operator/(Rational rhs);
12
13 void print();
14
15 private:
16
17 void normalize();
18
19 int numerator;
20 int denominator;
21 };
22
23 Rational::Rational(int num,int denom)
24 {
25 numerator = num;
26 denominator = denom;
27 normalize();
28 }
29 void Rational::normalize()
30 {
31 if(denominator < 0)
32 {
33 numerator = -numerator;
34 denominator = -denominator;
35 }
36 int a = abs(numerator);
37 int b = abs(denominator);
38
39 while(b>0)
40 {
41 int t = a % b;
42 a = b;
43 b = t;
44 }
45
46 numerator /= a;
47 denominator /= a;
48 }
49 Rational Rational::operator+(Rational rhs)
50 {
51 int a = numerator;
52 int b = denominator;
53 int c = rhs.numerator;
54 int d = rhs.denominator;
55
56 int e = a*b + c*d;
57 int f = b*d;
58
59 return Rational(e,f);
60 }
61
62 Rational Rational::operator-(Rational rhs)
63 {
64 int a = numerator;
65 int b = denominator;
66 int c = rhs.numerator;
67 int d = rhs.denominator;
68
69 int e = a*b - c*d;
70 int f = b*d;
71
72 return Rational(e,f);
73 }
74 Rational Rational::operator*(Rational rhs)
75 {
76 int a = numerator;
77 int b = denominator;
78 int c = rhs.numerator;
79 int d = rhs.denominator;
80
81 int e = a*c;
82 int f = b*d;
83
84 return Rational(e,f);
85 }
86 Rational Rational::operator/(Rational rhs)
87 {
88 int a = numerator;
89 int b = denominator;
90 int c = rhs.numerator;
91 int d = rhs.denominator;
92
93 int e = a*d;
94 int f = b*c;
95
96 return Rational(e,f);
97 }
98 void Rational::print()
99 {
100 if(numerator % denominator == 0)
101 cout << numerator / denominator;
102 else
103 cout << numerator << "/" << denominator;
104 }
105 int main()
106 {
107 Rational f1(2,16);
108 Rational f2(15,16);
109 f1.print();
110 cout << "+";
111 f2.print();
112 cout << "=";
113 Rational res = f1 + f2;
114 res.print();
115 cout << endl;
116
117 f1.print();
118 cout << "-";
119 f2.print();
120 cout << "=";
121 res = f1 - f2;
122 res.print();
123 cout << endl;
124
125 f1.print();
126 cout<< "*";
127 f2.print();
128 cout<< "=";
129 res = f1 * f2;
130 res.print();
131 cout << endl;
132
133 f1.print();
134 cout << "/";
135 f2.print();
136 cout << "=";
137 res = f1 / f2;
138 res.print();
139 cout << endl;
140 system("pause");
141
142 return 0;
143 }