-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.cpp
More file actions
305 lines (272 loc) · 6.38 KB
/
Copy pathCalculator.cpp
File metadata and controls
305 lines (272 loc) · 6.38 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "Calculator.h"
Expression::Expression() {
operatorr = base1 = base2 = num1 = num2 = "";
}
//Biểu thức
Expression::Expression(string ope, string bs1, string bs2, string n1, string n2) :
operatorr(ope), base1(bs1), base2(bs2), num1(n1), num2(n2)
{
}
//Trả về kết quả biểu thức
string Expression::Answer() {
string output;
//Cả 2 số là int thì sẽ xử lý theo Qint
if (isInt(num1) && isInt(num2)) {
QInt::QInt ans;
QInt::QInt a, b;
//Trường hợp đổi giữa các cơ số
if (base2.size()) {
if (base1 == "2") {
if (base2 == "10") {
ans = QInt::BinToDec(num1);
output = ans.Print();
}
else if (base2 == "16")
output = QInt::BintoHex(num1);
else
output = "Khong ho tro";
}
else if (base1 == "10") {
ans.Scan(num1);
if (base2 == "2")
output = QInt::DecToBin(ans);
else if (base2 == "16")
output = QInt::DecToHex(ans);
else
output = "Khong ho tro";
}
else if (base1 == "16") {
if (base2 == "10") {
ans = QInt::HexToDec(num1);
output = ans.Print();
}
else if (base2 == "2")
output = QInt::HexToBin(num1);
else
output = "Khong ho tro";
}
}
//Trường hợp là 1 phép tính giữa 2 số
else {
//Nhập 2 số vào a b
if (base1 == "10") {
a.Scan(num1);
b.Scan(num2);
}
else if (base1 == "2") {
a = QInt::BinToDec(num1);
b = QInt::BinToDec(num2);
}
else if (base1 == "16") {
a = QInt::HexToDec(num1);
b = QInt::HexToDec(num2);
}
if (operatorr == "+")
ans = a + b;
else if (operatorr == "-")
ans = a - b;
else if (operatorr == "*")
ans = a*b;
else if (operatorr == "/")
if (b != ans)
ans = a / b;
else
return "NaN";
else if (operatorr == ">>") {
//Lúc đầu ans == 0, lặp tới khi b = 0
while (b != ans) {
a.shiftRight();
b--;
}
ans = a;
}
else if (operatorr == "<<") {
while (b != ans) {
a.shiftLeft();
b--;
}
ans = a;
}
else if (operatorr == "~")
ans = ~a;
else if (operatorr == "^")
ans = a^b;
else if (operatorr == "&")
ans = a&b;
else if (operatorr == "|")
ans = a | b;
//Chuyển kết quả về đúng hệ
if (base1 == "10")
output = ans.Print();
else if (base1 == "2")
output = QInt::DecToBin(ans);
else if (base1 == "16")
output = QInt::DecToHex(ans);
}
}
//1 trong 2 số là float => tính theo float
else {
Qfloat::Qfloat a, b, ans;
//Chuyển đổi cơ số
if (base2.size()) {
//Chuyển từ 2 sang 10
if (base1 == "2") {
ans = Qfloat::Qfloat::BinToDec(num1);
output = ans.Print();
}
//Chuyển từ hệ 10 sang 2
else {
ans.Scan(num1);
output = Qfloat::Qfloat::DecToBin(ans);
return output;
}
}
//Thực hiện phép tính
else {
if (base1 == "10") {
a.Scan(num1);
b.Scan(num2);
}
else {
a = Qfloat::Qfloat::BinToDec(num1);
b = Qfloat::Qfloat::BinToDec(num2);
}
if (operatorr == "+")
ans = a + b;
else if (operatorr == "-")
ans = a - b;
else if (operatorr == "*")
ans = a*b;
else if (operatorr == "/")
ans = a / b;
if (base1 == "10")
output = ans.Print();
else
output = Qfloat::Qfloat::DecToBin(ans);
}
}
return Simplify(output);
}
//Nhập và xuất trên file
void Compute(ifstream &ifs, ofstream &ofs) {
int i, j, n;
string buffer;
string bs1, bs2, num1, num2, ope;
vector<string> vec;
bool mode; //True nếu đổi hệ, False nếu cộng trừ nhân chia ...
getline(ifs, buffer);
n = stoi(buffer);
for (i = 0; i < n; i++) {
//buffer.clear();
mode = true;
ope.clear();
bs1.clear();
bs2.clear();
num1.clear();
num2.clear();
getline(ifs, buffer);
vec = stringtok(buffer, ' ');
//Chuyển đổi cơ số, dòng có 3 thành phần
if (vec.size() == 3) {
bs1 = vec[0];
bs2 = vec[1];
num1 = vec[2];
}
//Tính toán, dòng có 4 thành phần
else if (vec.size() == 4) {
bs1 = vec[0];
num1 = vec[1];
ope = vec[2];
num2 = vec[3];
}
//Khác, in ra dòng trống
else {
ofs << endl;
continue;
}
Expression a(ope, bs1, bs2, num1, num2);
ofs << Simplify(a.Answer()) << endl;
}
}
//Kiểm tra xem chuỗi s có phải số nguyên không
bool isInt(string s) {
int n = s.size();
for (int i = 0; i < n; i++)
if (s[i] == '.')
return false;
return true;
}
//In ra menu với các option truyền vào
string Menu(vector<string> option) {
int n = option.size();
string ans;
for (int i = 0; i < n; i++)
cout << option[i] << endl;
cout << "Ban chon: ";
cin >> ans;
return ans;
}
//Tính toán khi không dùng tham số dòng lệnh
void ComputeMenus() {
string choice;
string num1, num2;
string bs1, bs2;
string ope;
start: choice = Menu({ "1. Chuyen doi co so","2. Tinh toan" });
if (choice == "1") {
cout << "Nhap he so ban nhap va he so can chuyen sang: ";
cin >> bs1 >> bs2;
cout << "Nhap so can chuyen: ";
cin >> num1;
Expression exp("", bs1, bs2, num1, "");
cout << "Ket qua la: " << exp.Answer() << endl << endl;
cout << "Ban muon tinh tiep? (Y/N) ";
cin >> choice;
if (choice == "Y" || choice == "y")
goto start;
}
else if (choice == "2") {
cout << "Nhap he so: ";
cin >> bs1;
cout << "Nhap phep tinh: ";
cin >> num1 >> ope >> num2;
Expression exp(ope, bs1, "", num1, num2);
cout << "Ket qua la: " << exp.Answer() << endl << endl;
cout << "Ban muon tinh tiep? (Y/N) ";
cin >> choice;
if (choice == "Y" || choice == "y")
goto start;
}
}
//Xóa số 0 đầu chuỗi
string Simplify(string& s) {
int n = s.size();
bool neg = false;
if (s[0] == '-') {
neg = true;
s = s.substr(1);
}
while (s[0] == '0' && s != "0" && s[1] != '.')
s = s.substr(1);
if (neg)
s.insert(s.begin(), '-');
return s;
}
//Tách chuỗi theo kí tự t, bỏ đi t
vector<string> stringtok(string s, char t) {
int n = s.size();
int i = 0;
string tmp;
vector<string> ans;
s.push_back(t);
while (i < n) {
while (s[i] != t)
tmp.push_back(s[i++]);
i++;
if (tmp[0] != 0) {
ans.push_back(tmp);
tmp.clear();
}
}
return ans;
}