-
Notifications
You must be signed in to change notification settings - Fork 14
/
writeinteger.pl0
90 lines (85 loc) · 1.57 KB
/
writeinteger.pl0
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
{ read and write an integer }
const E_OK = 0, E_INVALID = 1, E_OVERFLOW = 2;
var integer, err, a;
procedure read_integer;
var c, n, running, sign, i;
begin
err := E_OK;
running := 1;
n := 0;
sign := 1;
i := 0;
while running = 1 do
begin
readChar into c;
{ if ( i = 0 and ( c = 43 or c = 45 ) }
if (i + (c - 43) * (c - 45)) = 0 then
begin
if c = 43 then sign := 1
else if c = 45 then sign := -1;
readChar into c;
end;
{ if ( c = -1 or c = 10 ) }
if ((c + 1) * (c - 10)) = 0 then
begin
running := 0;
integer := n * sign;
if i = 0 then err := E_INVALID
else if i > 19 then
if n < 0 then
err := E_OVERFLOW;
end
{ if ( c < 48 or c > 57 ) }
else if ((c - 48) * (57 - c)) < 0 then
begin
err := E_INVALID;
running := 0;
end
else begin
c := c - 48;
n := n * 10;
n := n + c;
i := i + 1;
end;
end;
end;
procedure write_integer;
var n, m, d, c;
begin
n := integer;
d := 1;
if n < 0 then n := -n;
while n > 0 do
begin
n := n / 10;
d := d * 10;
end;
n := integer;
if n = 0 then writeChar 48;
if n < 0 then begin writeChar 45; n := -n end;
while d >= 10 do
begin
d := d / 10;
m := n / d;
n := n - m * d;
c := m + 48;
writeChar c
end;
end;
begin
call read_integer;
a := 96;
if err = E_INVALID then
begin
writeStr 'invalid\n';
exit 1;
end
else if err = E_OVERFLOW then
begin
writeStr 'overflow\n';
exit 1;
end
else
call write_integer;
writeChar 10;
end.