-
Notifications
You must be signed in to change notification settings - Fork 0
/
Práctica OFICIAL.pas
534 lines (448 loc) · 14.8 KB
/
Práctica OFICIAL.pas
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
PROGRAM Practica;
CONST
MIN = 1;
MAX = 50;
TYPE
T_keep_numbers = array[MIN..MAX] of integer;
T_numbers = RECORD
keep_numbers: t_keep_numbers;
number_lenght: integer;
IsPositive: boolean;
END;
VAR
number_string: string[50];
big_number1: T_numbers;
big_number2: T_numbers;
result: T_numbers;
operation: char;
overflow: boolean;
result2: boolean;
FUNCTION IsANumber(character:char):boolean;
BEGIN
case character of
'1', '2', '3','4','5','6','7','8','9','0' : BEGIN
IsANumber:=true;
end;
else IsANumber:=False;
end;
END;{==================FIN ISANUMBER=======================================}
FUNCTION CharToInt(character:char):integer;
BEGIN
case character of
'1' : CharToInt:=1;
'2' : CharToInt:=2;
'3' : CharToInt:=3;
'4' : CharToInt:=4;
'5' : CharToInt:=5;
'6' : CharToInt:=6;
'7' : CharToInt:=7;
'8' : CharToInt:=8;
'9' : CharToInt:=9;
'0' : CharToInt:=0;
end;
END;{===================END CHARTOINT=======================================}
PROCEDURE ReadNumbers(var number_string: string[50]; var big_number: T_numbers);{=======================}
var
i:integer;
number:char;
BEGIN
number_string:='';
i:=1;
big_number.Ispositive:=true;
repeat
read(number);
if((ord(number)<>10) and (i<=50))then {Compruebo que el number no sea enter}
begin
if(IsANumber(number))then {Compruebo si number es un numero }
begin
number_string:= number_string+number;
i:=i+1;
end
else if(number='-')then
big_number.Ispositive:=false;
end;
until ord(number)=10;
big_number.number_lenght:=i-1;
END; {====================FIN READNUMBBER======================================}
FUNCTION NumberStringTokeepNumbers(number_string: string[50]; big_number: T_numbers): t_keep_numbers;
VAR
a:integer;
n:integer;
BEGIN
n:= big_number.number_lenght;
for a:=1 to big_number.number_lenght do { rellenar el array alreves del string para operar}
begin
numberstringTokeepNumbers[a]:=CharToInt(number_string[n]);
n:= n -1;
end;
END;{============================END NUMBERSTRING TO KEEPNUMBERS===================}
PROCEDURE ShowNumber(big_number:t_numbers);
var
a:integer;
BEGIN
if( big_number.IsPositive = false) then
write('-');
for a:=big_number.number_lenght downto MIN do
write(big_number.keep_numbers[a]);
writeln();
END;{=====================END SHOWNUMBER==========================================}
PROCEDURE fill (VAR big_number_1 :t_numbers; VAR big_number_2: t_numbers);
var
i: integer;
BEGIN
FOR i:= big_number_2.number_lenght + 1 TO big_number_1.number_lenght DO
big_number_2.keep_numbers[i]:= 0;
END;{=====================END FILL==========================================}
FUNCTION NumberOFDigits ( big_number : T_numbers): integer;
VAR
counter: integer;
BEGIN
counter:= MAX;
WHILE (big_number.keep_numbers[counter] = 0) AND (counter >= 1) DO
BEGIN
counter:= counter - 1;
END;
NumberOfDigits:= counter;
END;{=====================END NumberOFDigits=========================================}
{*******************FUNCIÓN IGUAL******************************************}
FUNCTION Igual (big_number_1, big_number_2: t_numbers):boolean;
VAR
i: integer;
BEGIN
i:=1;
IF (big_number_1.ispositive) AND (big_number_2.ispositive) AND (NumberOFDigits (big_number_1) = NumberOFDigits (big_number_2)) THEN
BEGIN
WHILE (big_number_1.keep_numbers[i] = big_number_2.keep_numbers[i]) AND (i< NumberOFDigits (big_number_1)) DO
i:= i+1;
IF (i= NumberOFDigits (big_number_1)) AND (big_number_1.keep_numbers[i] = big_number_2.keep_numbers[i]) THEN
Igual:= TRUE;
END
ELSE IF ( NOT big_number_1.ispositive) AND (NOT big_number_2.ispositive) AND (NumberOFDigits (big_number_1) = NumberOFDigits (big_number_2)) THEN
BEGIN
WHILE (big_number_1.keep_numbers[i] = big_number_2.keep_numbers[i]) AND (i< NumberOFDigits (big_number_1)) DO
i:= i+1;
IF (i= NumberOFDigits (big_number_1)) AND (big_number_1.keep_numbers[i] = big_number_2.keep_numbers[i]) THEN
Igual:= TRUE;
END
ELSE
Igual:= FALSE;
END;{*********************END IGUAL***********************************}
{**********************MAYOR X QUE Y**********************************}
FUNCTION Mayor (big_number_1, big_number_2: t_numbers):boolean;
VAR
i: integer;
BEGIN
IF (big_number_1.ispositive) AND (NOT big_number_2.ispositive) THEN
Mayor:= TRUE
ELSE IF (NOT big_number_1.ispositive) AND (big_number_2.ispositive) THEN
Mayor:= FALSE
ELSE IF ( big_number_1.ispositive) AND (big_number_2.ispositive) THEN
BEGIN
IF NumberOFDigits(big_number_1) > NumberOFDigits(big_number_2) THEN
Mayor:= TRUE
ELSE IF NumberOFDigits(big_number_1) < NumberOFDigits(big_number_2) THEN
Mayor:= FALSE
ELSE BEGIN
i:= NumberOFDigits(big_number_1);
WHILE (i >=1) AND (big_number_1.keep_numbers[i] = big_number_2.keep_numbers[i]) DO
i:= i- 1;
IF (big_number_1.keep_numbers[i] > big_number_2.keep_numbers[i]) THEN
Mayor:= TRUE
ELSE
Mayor:= FALSE;
END;
END {IF}
ELSE IF (NOT big_number_1.ispositive) AND (NOT big_number_2.ispositive) THEN
BEGIN
IF NumberOFDigits(big_number_1) < NumberOFDigits(big_number_2) THEN
Mayor:= TRUE
ELSE IF NumberOFDigits(big_number_1) > NumberOFDigits(big_number_2) THEN
Mayor:= FALSE
ELSE BEGIN
i:= NumberOFDigits(big_number_1);
WHILE (i >=1) AND (big_number_1.keep_numbers[i] = big_number_2.keep_numbers[i]) DO
i:= i- 1;
IF (big_number_1.keep_numbers[i] < big_number_2.keep_numbers[i]) THEN
Mayor:= TRUE
ELSE
Mayor:= FALSE;
END;
END;
END;{*********************END MAYOR***********************************}
{**********************MENOR X QUE Y**********************************}
FUNCTION Menor (big_number_1, big_number_2: t_numbers):boolean;
VAR
i: integer;
BEGIN
IF (big_number_1.ispositive) AND (NOT big_number_2.ispositive) THEN
Menor:= FALSE
ELSE IF (NOT big_number_1.ispositive) AND (big_number_2.ispositive) THEN
Menor:= TRUE
ELSE IF (big_number_1.ispositive) AND (big_number_2.ispositive) THEN
BEGIN
IF NumberOFDigits(big_number_1) > NumberOFDigits(big_number_2) THEN
Menor:= FALSE
ELSE IF NumberOFDigits(big_number_1) < NumberOFDigits(big_number_2) THEN
Menor:= TRUE
ELSE BEGIN
i:= NumberOFDigits(big_number_1);
WHILE (i >=1) AND (big_number_1.keep_numbers[i] = big_number_2.keep_numbers[i]) DO
i:= i- 1;
IF (big_number_1.keep_numbers[i] < big_number_2.keep_numbers[i]) THEN
Menor:= TRUE
ELSE
Menor:= FALSE;
END; {IF}
END
ELSE IF (NOT big_number_1.ispositive) AND (NOT big_number_2.ispositive) THEN
BEGIN
IF NumberOFDigits(big_number_1) < NumberOFDigits(big_number_2) THEN
Menor:= FALSE
ELSE IF NumberOFDigits(big_number_1) > NumberOFDigits(big_number_2) THEN
Menor:= TRUE
ELSE BEGIN
i:= NumberOFDigits(big_number_1);
WHILE (i >=1) AND (big_number_1.keep_numbers[i] = big_number_2.keep_numbers[i]) DO
i:= i- 1;
IF (big_number_1.keep_numbers[i] > big_number_2.keep_numbers[i]) THEN
Menor:= TRUE
ELSE
Menor:= FALSE;
END; {IF}
END;
END;{*********************END MENOR***********************************}
PROCEDURE SumNumbers ( var overflow: boolean; big_number_1: t_numbers; big_number_2: t_numbers; var result_total: t_numbers); FORWARD;
{***********************SUBTRACT**************************************}
PROCEDURE Subtrac (var overflow: boolean; big_number_1: t_numbers; big_number_2: t_numbers; var result_total: t_numbers);
VAR
counter: integer;
i: INTEGER;
greater_number: integer;
aux: t_numbers;
BEGIN
IF (big_number_1.ispositive) AND (not big_number_2.ispositive) THEN
BEGIN
result_total.ispositive:= TRUE;
big_number_2.ispositive:= TRUE;
SumNumbers ( overflow, big_number1, big_number2, result);
END
ELSE
IF (not big_number_1.ispositive) AND (big_number_2.ispositive) THEN
BEGIN
result_total.ispositive:= FALSE;
big_number_1.ispositive:= TRUE;
big_number_2.ispositive:= TRUE;
SumNumbers ( overflow, big_number1, big_number2, result);
END
ELSE
IF (big_number_1.ispositive) AND (big_number_2.ispositive) AND mayor (big_number_1, big_number_2) THEN
BEGIN
result_total.ispositive:= TRUE;
IF big_number_1.number_lenght > big_number_2.number_lenght THEN
BEGIN
greater_number:= big_number_1.number_lenght;
fill( big_number_1, big_number_2);
end
ELSE
begin
fill( big_number_2, big_number_1);
greater_number:= big_number_2.number_lenght;
end;
counter:= 0;
FOR i:= MIN TO greater_number DO
BEGIN
IF (big_number_1.keep_numbers[i] >= (big_number_2.keep_numbers[i] + counter) ) THEN
BEGIN
result_total.keep_numbers[i]:= (big_number_1.keep_numbers[i] - (big_number_2.keep_numbers[i] + counter));
counter:=0;
END
ELSE BEGIN
result_total.keep_numbers[i]:= ((big_number_1.keep_numbers[i]+10) -(big_number_2.keep_numbers[i] + counter));
counter:= 1;
END;
END; {FOR}
result_total.number_lenght:= NumberOFDigits(result_total) ;
END
ELSE
IF (big_number_1.ispositive) AND (big_number_2.ispositive) AND mayor (big_number_2, big_number_1) THEN
BEGIN
result_total.ispositive:= FALSE;
aux:= big_number_1;
big_number_1:= big_number_2;
big_number_2:= aux;
{Resto al revés, y el resultado lo cambio de signo}
IF big_number_1.number_lenght > big_number_2.number_lenght THEN
BEGIN
greater_number:= big_number_1.number_lenght;
fill( big_number_1, big_number_2);
end
ELSE
begin
fill( big_number_2, big_number_1);
greater_number:= big_number_2.number_lenght;
end;
counter:= 0;
FOR i:= MIN TO greater_number DO
BEGIN
IF (big_number_1.keep_numbers[i] >= (big_number_2.keep_numbers[i] + counter) ) THEN
BEGIN
result_total.keep_numbers[i]:= (big_number_1.keep_numbers[i] - (big_number_2.keep_numbers[i] + counter));
counter:=0;
END
ELSE BEGIN
result_total.keep_numbers[i]:= ((big_number_1.keep_numbers[i]+10) -(big_number_2.keep_numbers[i] + counter));
counter:= 1;
END;
END; {FOR}
result_total.number_lenght:= NumberOFDigits(result_total) ;
END;
IF (NOT big_number_1.ispositive) AND (NOT big_number_2.ispositive) AND mayor (big_number_1, big_number_2) THEN
BEGIN
result_total.ispositive:= FALSE;
IF big_number_1.number_lenght > big_number_2.number_lenght THEN
BEGIN
greater_number:= big_number_1.number_lenght;
fill( big_number_1, big_number_2);
end
ELSE
begin
fill( big_number_2, big_number_1);
greater_number:= big_number_2.number_lenght;
end;
counter:= 0;
FOR i:= MIN TO greater_number DO
BEGIN
IF (big_number_1.keep_numbers[i] >= (big_number_2.keep_numbers[i] + counter) ) THEN
BEGIN
result_total.keep_numbers[i]:= (big_number_1.keep_numbers[i] - (big_number_2.keep_numbers[i] + counter));
counter:=0;
END
ELSE BEGIN
result_total.keep_numbers[i]:= ((big_number_1.keep_numbers[i]+10) -(big_number_2.keep_numbers[i] + counter));
counter:= 1;
END;
END; {FOR}
result_total.number_lenght:= NumberOFDigits(result_total) ;
END
ELSE
IF (NOT big_number_1.ispositive) AND (NOT big_number_2.ispositive) AND mayor (big_number_2, big_number_1) THEN
BEGIN
result_total.ispositive:= TRUE;
aux:= big_number_1;
big_number_1:= big_number_2;
big_number_2:= aux;
{Resto al revés, y el resultado lo cambio de signo}
IF big_number_1.number_lenght > big_number_2.number_lenght THEN
BEGIN
greater_number:= big_number_1.number_lenght;
fill( big_number_1, big_number_2);
end
ELSE
begin
fill( big_number_2, big_number_1);
greater_number:= big_number_2.number_lenght;
end;
counter:= 0;
FOR i:= MIN TO greater_number DO
BEGIN
IF (big_number_1.keep_numbers[i] >= (big_number_2.keep_numbers[i] + counter) ) THEN
BEGIN
result_total.keep_numbers[i]:= (big_number_1.keep_numbers[i] - (big_number_2.keep_numbers[i] + counter));
counter:=0;
END
ELSE BEGIN
result_total.keep_numbers[i]:= ((big_number_1.keep_numbers[i]+10) -(big_number_2.keep_numbers[i] + counter));
counter:= 1;
END;
END; {FOR}
result_total.number_lenght:= NumberOFDigits(result_total) ;
END;
END; {************************END SUBSTRACT*********************************}
PROCEDURE SumNumbers ( var overflow: boolean; big_number_1: t_numbers; big_number_2: t_numbers; var result_total: t_numbers);
VAR
counter: integer;
i: INTEGER;
greater_number: integer;
BEGIN
IF (big_number_1.ispositive) AND (not big_number_2.ispositive) THEN
BEGIN
result_total.ispositive:= FALSE;
Subtrac( overflow, big_number1, big_number2, result);
END;
IF (not big_number_1.ispositive) AND (big_number_2.ispositive) THEN
BEGIN
result_total.ispositive:= FALSE;
Subtrac( overflow, big_number2, big_number1, result);
END;
IF (big_number_1.ispositive) AND (big_number_2.ispositive) THEN
result_total.ispositive:= TRUE;
IF big_number_1.number_lenght > big_number_2.number_lenght THEN
BEGIN
greater_number:= big_number_1.number_lenght;
fill( big_number_1, big_number_2);
end
ELSE
begin
fill( big_number_2, big_number_1);
greater_number:= big_number_2.number_lenght;
end;
counter:= 0;
FOR i:= MIN TO greater_number DO
BEGIN
result_total.keep_numbers[i]:= (big_number_1.keep_numbers[i] + big_number_2.keep_numbers[i] + counter) mod 10;
counter:= ( big_number_1.keep_numbers[i] + big_number_2.keep_numbers[i] + counter ) div 10;
END;
IF greater_number < MAX THEN
result_total.keep_numbers[greater_number + 1]:= counter
ELSE
overflow:= TRUE;
result_total.number_lenght:= NumberOFDigits(result_total) ;
END;
{*********************COMIENZO DEL CUERPO***********************************}
BEGIN
readln(operation);
ReadNumbers(number_string, big_number1);
big_number1.keep_numbers:= numberStringTokeepNumbers(number_string, big_number1);
ReadNumbers(number_string, big_number2);
big_number2.keep_numbers:=numberStringTokeepNumbers(number_string, big_number2);
CASE operation OF
'-': BEGIN
overflow:= FALSE;
Subtrac ( overflow , big_number1, big_number2, result);
IF overflow THEN
writeln('DESBORDAMIENTO')
ELSE
ShowNumber(result);
END;
'+': BEGIN
overflow:= FALSE;
SumNumbers ( overflow, big_number1, big_number2, result);
IF overflow THEN
writeln('DESBORDAMIENTO')
ELSE
ShowNumber(result);
END;
'=': BEGIN
result2:= Igual (big_number1, big_number2);
IF result2 THEN
writeln('TRUE')
ELSE
writeln('FALSE');
END;
'>': BEGIN
result2:= Mayor (big_number1, big_number2);
IF result2 THEN
writeln('TRUE')
ELSE
writeln('FALSE');
END;
'<': BEGIN
result2:= Menor (big_number1, big_number2);
IF result2 THEN
writeln('TRUE')
ELSE
writeln('FALSE');
END;
END;
readln();
END.