include iostream include string include algorithm include vector inclu

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
class RatNum {
public:
RatNum();
~RatNum();
RatNum(const string &s);
RatNum(const bool &sgn, const long long &numr, const long long &denumr);
RatNum plus(RatNum obj);
RatNum minus(RatNum obj);
RatNum mult(RatNum obj);
RatNum div(RatNum obj);
string out();
RatNum negate();
void normalize();
private:
bool sign;
long long intg;
long long num;
long long denum;
};
string to_string1(long long n) {
string res = "";
while (n) {
res += char(n % 10 + '0');
n /= 10;
}
reverse(res.begin(), res.end());
return res;
}
long long gcd(long long a, long long b) {
return (b) ? gcd(b, a%b) : a;
}
int main(){
string s;
string sign;
getline(cin, s);
RatNum r1(s);
getline(cin, sign);
getline(cin, s);
RatNum r2(s);
if (sign == "+")
cout << r1.plus(r2).out() << endl;
else if (sign == "-")
cout << r1.minus(r2).out() << endl;
else if (sign == "*")
cout << r1.mult(r2).out() << endl;
else
cout << r1.div(r2).out() << endl;
system("pause");
return 0;
}
RatNum::RatNum(){
sign = 0;
intg = 0;
num = 0;
denum = 1;
}
RatNum::~RatNum(){}
RatNum::RatNum(const string &s){
int pos = 0;
if (s[0] == '-') {
pos++;
sign = 0;
}
else
sign = 1;
if (s[pos] == 0) {
intg = 0;
num = 0;
denum = 1;
return;
}
auto it = s.find(' ');
if (it != s.npos) {
auto jt = s.find('/');
if (jt != s.npos) {
intg = atoll(s.substr(pos, it).c_str());
num = atoll(s.substr(it + 1, jt - it).c_str());
denum = atoll(s.substr(jt + 1, s.npos).c_str());
}
else {
intg = atoll(s.substr(pos, s.npos).c_str());
num = 0;
denum = 1;
}
}
else {
auto jt = s.find('/');
if (jt != s.npos) {
intg = 0;
num = atoll(s.substr(pos, jt).c_str());
denum = atoll(s.substr(jt + 1, s.npos).c_str());
}
else {
intg = atoll(s.substr(pos, s.npos).c_str());
num = 0;
denum = 1;
}
}
normalize();
}
RatNum::RatNum(const bool &sgn, const long long &numr, const long long &denumr){
sign = sgn;
intg = 0;
num = numr;
denum = denumr;
normalize();
}
RatNum RatNum::plus(RatNum obj) {
RatNum tmp(*this);
if (sign == obj.sign) {
tmp.intg += obj.intg;
tmp.num = (num * obj.denum) + (obj.num * denum);
tmp.denum = denum * obj.denum;
tmp.normalize();
return tmp;
}
else {
if (sign) {
return minus(RatNum(obj).negate());
}
else {
return obj.minus(tmp.negate());
}
}
}
RatNum RatNum::minus(RatNum obj) {
RatNum tmp(*this);
if (sign == obj.sign) {
if (sign) {
tmp.intg -= obj.intg;
tmp.num = (num * obj.denum) - (obj.num * denum);
tmp.denum = denum * obj.denum;
}
else {
return obj.negate().minus(tmp.negate());
}
if (tmp.intg < 0) {
tmp.sign = 0;
tmp.intg = -tmp.intg;
tmp.intg--;
tmp.num = tmp.denum - tmp.num;
}
else if (tmp.intg > 0) {
tmp.sign = 1;
}
else if (tmp.num < 0) {
tmp.sign = 0;
tmp.num = -tmp.num;
}
tmp.normalize();
return tmp;
}
else {
if (sign) {
return plus(RatNum(obj).negate());
}
else {
return ((tmp.negate()).plus(obj)).negate();
}
}
}
RatNum RatNum::mult(RatNum obj) {
RatNum tmp;
tmp.num = (intg * denum + num) * (obj.intg * obj.denum + obj.num);
tmp.denum = denum * obj.denum;
tmp.normalize();
if (sign == obj.sign) {
tmp.sign = 1;
}
else {
tmp.sign = 0;
}
return tmp;
}
RatNum RatNum::div(RatNum obj) {
RatNum tmp;
tmp.intg = 0;
tmp.denum = obj.intg * obj.denum + obj.num;
tmp.num = obj.denum;
tmp.sign = obj.sign;
return mult(tmp);
}
void RatNum::normalize() {
while (num > denum) {
intg += (num / denum);
num %= denum;
}
if (num == denum) {
intg++;
num = 0;
denum = 1;
}
long long g = gcd(num, denum);
num /= g;
denum /= g;
return;
}
RatNum RatNum::negate() {
RatNum tmp(*this);
tmp.sign = !tmp.sign;
return tmp;
}
string RatNum::out() {
normalize();
if (intg == 0 && num == 0)
return "0";
string res = "";
if (sign == 0)
res += "-";
if (intg)
res += to_string1(intg);
if (num) {
if (intg)
res += " ";
res += to_string1(num);
res += "/";
res += to_string1(denum);
}
return res;
}