-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
536 lines (456 loc) · 11.2 KB
/
main.cpp
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include <iostream>
#define swap(a, b) \
{ \
a += b; \
b = a - b; \
a -= b; \
}
using namespace std;
struct adad{ // alamat va meghdar adad voroodi
int alamat;
char* number=new char[1000];
};
int tool_int(int* a){ // tedad ragham araye intigeri
int i=0;
while(a[i]!=-11){
i++;
}
return i;
}
void rahat(char* &a, char n){ // dar halati ke + vared nashode va ragham aval bayad dar araye gharar begirad
char* b=new char[1000];
int i=0;
while(a[i]!='\0'){
b[i+1]=a[i];
i++;
}
b[0]=n;
b[i+1]='\0';
delete[] a;
a=b;
}
int moghayese(int* a , int* b){ // a bozorgtar= 0 b bozorgtar= 1 else 2
if(tool_int(a)==tool_int(b)){
int i=0; // moghayese voroodi ha
while(a[i]!=-11 || b[i]!=-11){
if(a[i]>b[i])
return 0;
if(a[i]<b[i])
return 1;
i++;
}
}
else if(tool_int(a)>tool_int(b)){
return 0;
}
else if(tool_int(a)<tool_int(b)){
return 1;
}
return 2;
}
void makoos(int *a) // sar va tah kardan adad baraye asan sazi mohasebe
{
int length = tool_int(a);
for (int j = 0; j < length / 2; j++)
{
swap(a[j], a[length - j - 1]);
}
}
void tabdil_char_int(char *a, int *b) // tabdil reshte voroodi be araye int baraye mohaseb
{
int j = 0;
while (a[j] != '\0')
{
b[j] = (int)a[j] - 48; // 48 adad aski char '0'
j++;
}
b[j] = -11;
}
void tabdil_int_char(char *a, int *b) // tabdil hasel javab be halat reshte baraye chap
{
int j = 0;
while (b[j] != -11)
{
a[j] = (char)b[j] + 48; // 48 adad aski char '0'
j++;
}
a[j] = '\0';
}
void enteghal(int *a, int *komaki, int shift) // baraye shift dadan adad baad az zarb har ragham
{
int j = 0;
while (a[j] != -11)
{
komaki[j] = a[j];
j++;
}
for (int x = 0; x < shift; x++)
{
komaki[j + x] = 0;
}
komaki[j + shift] = -11;
}
void sum(int *a, int *b, int *c); // tabe jame
void minius(int *a, int *b, int *c); // tabe menha
void zarb(int *a, int u); // tabe zarb har ragham dar adad digar
void sum_prime(int *a, int *b); // tabe jam konande maghadir zarb shode
void zarb_asli(int *a, int *b, int *c); // tabe anjam dahande nahaii zarb
void dequeue(char* &queue); // eslah konande 0 posht adad dar gozar ha
int main()
{
int *a = new int[1000];
int *b = new int[1000]; // 2 adad voroodi va hasel dar ghaleb araye az int
int *c = new int[1000];
char *c2 = new char[1000]; // khorooji dar ghaleb reshte (reshte shodeye c)
char amalyat; // jam zarb ya menha
cin >> amalyat;
adad a2;
char alamat;
cin>>alamat; // alamat adad
if(alamat=='+'||(alamat>='0'&& alamat<='9')){ // adad mosbat
a2.alamat=0;
}
else if(alamat=='-'){ // adad manfi
a2.alamat=1;
}
if(alamat>='0'&&alamat<='9'){ // eslah adadi ke mosbat ast va ragham aval bayad bargardad
cin >> a2.number;
rahat(a2.number,alamat);
}else{
cin >> a2.number;
}
tabdil_char_int(a2.number, a); // tabdil voroodi aval be int*
adad b2;
char alamat2;
cin>>alamat2;
if(alamat2=='+'||(alamat2>='0'&& alamat2<='9')){
b2.alamat=0;
}
else if(alamat2=='-'){
b2.alamat=1;
}
if(alamat2>='0'&&alamat2<='9'){
cin >> b2.number;
rahat(b2.number,alamat2);
}else{
cin >> b2.number;
}
tabdil_char_int(b2.number, b);
int mogh=moghayese(a,b); // taiin inke voroodi aval bozorgtar ast ya dovomi
int test=0;
if(mogh==1){ // flag baraye inke voroodi dovom bozorgtar ast
test=1;
}
if (amalyat == '+') // gharar ast voroodi ha jam shavand
{
if(a2.alamat==0 && b2.alamat==0){ // do adad mosbat
if(test==1)
sum(b, a, c); // agar adad dovom bozorgtar ast jaha avaz shavad
else
sum(a,b,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2); // eslah 0 ghabl adad dar halat gozar
cout << c2;
}
else if(a2.alamat==1 && b2.alamat==1){ // do adad manfi
if(test==1)
sum(b, a, c);
else
sum(a,b,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout<<'-'<< c2;
}
else if(a2.alamat==0 && b2.alamat==1){ // avali + dovomi -
if(test==1){
minius(b,a,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout<<'-'<< c2;
}
else{
minius(a,b,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout << c2;
}
}
else if(a2.alamat==1 && b2.alamat==0){ // avali - dovomi +
if(test==1){
minius(b,a,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout<< c2;
}
else
minius(a,b,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout<<'-'<< c2;
}
}
else if (amalyat == '-') // tafrigh do adad
{
if(a2.alamat==0 && b2.alamat==0){ // har do +
if(test==1){
minius(b,a,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout<<'-'<<c2;
}
else{
minius(a,b,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout<< c2;
}
}
else if(a2.alamat==1 && b2.alamat==1){ // har do -
if(test==1){
minius(b,a,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout<< c2;
}
else{
minius(a,b,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout<<'-'<< c2;
}
}
else if(a2.alamat==0 && b2.alamat==1){ // avali + dovomi -
if(test==1)
sum(b,a,c);
else
sum(a,b,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout << c2;
}
else if(a2.alamat==1 && b2.alamat==0){ // avali - dovomi +
if(test==1)
sum(b,a,c);
else
sum(a,b,c);
tabdil_int_char(c2, c);
if(c2[0]=='0')
dequeue(c2);
cout<<'-'<< c2;
}
}
else if (amalyat == '*') // zarb
{
if(a2.alamat==0 && b2.alamat==0){ // har do +
zarb_asli(a, b, c);
tabdil_int_char(c2, c);
cout << c2;
}
else if(a2.alamat==1 && b2.alamat==1){ // har do -
zarb_asli(a, b, c);
tabdil_int_char(c2, c);
cout << c2;
}
else if(a2.alamat==0 && b2.alamat==1){ // avali + dovomi -
zarb_asli(a, b, c);
tabdil_int_char(c2, c);
cout<<'-'<< c2;
}
else if(a2.alamat==1 && b2.alamat==0){ // avali - dovomi +
zarb_asli(a, b, c);
tabdil_int_char(c2, c);
cout<<'-'<< c2;
}
}
delete[] a;
delete[] b;
delete[] c;
delete[] c2;
delete[] a2.number;
delete[] b2.number;
return 0;
}
void sum(int *a, int *b, int *c) // tabe jam
{
makoos(a); // makoos karda adad baraye tashil mohasebe
makoos(b);
int j = 0;
while (a[j] != -11 && b[j] != -11)
{
c[j] = a[j] + b[j]; // jam kardan ragham be ragham
j++;
}
if (a[j] != -11)
{
while (a[j] != -11)
{
c[j] = a[j]; // ragham ezafe ke digari 0 darad
j++;
}
}
else
{
while (b[j] != -11)
{
c[j] = b[j]; // ragham ezafe ke digari 0 darad
j++;
}
}
c[j] = -11;
for (int x = 0; x < j - 1; x++)
{
c[x + 1] = c[x + 1] + c[x] / 10; // enteghal 1 ezafe be ragham baadi
c[x] = c[x] % 10;
}
if (c[j - 1] > 9)
{
c[j] = c[j - 1] / 10;
c[j - 1] = c[j - 1] % 10; // ragham akhar agar do raghami shod
c[j + 1] = -11;
}
makoos(a);
makoos(b); // adadha be halat asli barmigardan
makoos(c);
}
void minius(int *a, int *b, int *c) // tabe menha
{
makoos(a);
makoos(b);
int j = 0;
while (a[j] != -11 && b[j] != -11)
{
c[j] = a[j] - b[j];
j++;
}
if (a[j] != -11)
{
while (a[j] != -11)
{
c[j] = a[j];
j++;
}
}
else
{
while (b[j] != -11)
{
c[j] = b[j];
j++;
}
}
c[j] = -11;
for (int x = 0; x < j - 1; x++)
{ if(c[x]<0){
c[x+1]-=1; // gharz gereftan az ragham baghal
c[x]+=10;
}
}
makoos(a);
makoos(b);
makoos(c);
}
void zarb(int *a, int u) // zarb yek ragham dar kol yek adad
{
int j = 0;
makoos(a);
while (a[j] != -11)
{
a[j] = a[j] * u;
j++;
}
for (int x = 0; x < j - 1; x++)
{
a[x + 1] = a[x + 1] + a[x] / 10;
a[x] = a[x] % 10;
}
if (a[j - 1] > 9)
{
a[j] = a[j - 1] / 10;
a[j - 1] = a[j - 1] % 10;
a[j + 1] = -11;
}
makoos(a);
}
void sum_prime(int *a, int *b) // jam kardan maghadir zarbi
{
makoos(a);
makoos(b);
int j = 0;
int *c = new int[1000];
while (a[j] != -11 && b[j] != -11)
{
c[j] = a[j] + b[j];
j++;
}
if (a[j] != -11)
{
while (a[j] != -11)
{
c[j] = a[j];
j++;
}
}
else
{
while (b[j] != -11)
{
c[j] = b[j];
j++;
}
}
c[j] = -11;
for (int x = 0; x < j - 1; x++)
{
c[x + 1] += c[x] / 10;
c[x] %= 10;
}
if (c[j - 1] > 9)
{
c[j] = c[j - 1] / 10;
c[j - 1] %= 10;
c[j + 1] = -11;
j++;
}
makoos(a);
makoos(b);
makoos(c);
for (;j >= 0;j--)
{
a[j] = c[j];
}
}
void zarb_asli(int *a, int *b, int *c) // majmooe hame karhaye zarb
{
int n = tool_int(a);
int *komaki = new int[1000];
c[0] = 0;
c[1] = -11;
for (int j = 0; j < n; j++)
{
enteghal(b, komaki, j);
zarb(komaki, a[n - 1 - j]);
sum_prime(c, komaki);
}
}
void dequeue(char* &queue){ // eslah halat 0 posht adad
char* komaki=new char[1000];
int i=0;
while(queue[i]!='\0'){
komaki[i]=queue[i+1];
i++;
}
komaki[i]='\0';
delete[] queue;
queue=komaki;
}