-
Notifications
You must be signed in to change notification settings - Fork 0
/
atoi.cl
executable file
·121 lines (110 loc) · 2.94 KB
/
atoi.cl
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
(*
The class A2I provides integer-to-string and string-to-integer
conversion routines. To use these routines, either inherit them
in the class where needed, have a dummy variable bound to
something of type A2I, or simpl write (new A2I).method(argument).
*)
(*
c2i Converts a 1-character string to an integer. Aborts
if the string is not "0" through "9"
*)
class A2I {
c2i(char : String) : Int {
if char = "0" then 0 else
if char = "1" then 1 else
if char = "2" then 2 else
if char = "3" then 3 else
if char = "4" then 4 else
if char = "5" then 5 else
if char = "6" then 6 else
if char = "7" then 7 else
if char = "8" then 8 else
if char = "9" then 9 else
{ abort(); 0; } -- the 0 is needed to satisfy the typchecker
fi fi fi fi fi fi fi fi fi fi
};
(*
i2c is the inverse of c2i.
*)
i2c(i : Int) : String {
if i = 0 then "0" else
if i = 1 then "1" else
if i = 2 then "2" else
if i = 3 then "3" else
if i = 4 then "4" else
if i = 5 then "5" else
if i = 6 then "6" else
if i = 7 then "7" else
if i = 8 then "8" else
if i = 9 then "9" else
{ abort(); ""; } -- the "" is needed to satisfy the typchecker
fi fi fi fi fi fi fi fi fi fi
};
(*
a2i converts an ASCII string into an integer. The empty string
is converted to 0. Signed and unsigned strings are handled. The
method aborts if the string does not represent an integer. Very
long strings of digits produce strange answers because of arithmetic
overflow.
*)
a2i(s : String) : Int {
if s.length() = 0 then 0 else
if s.substr(0,1) = "-" then ~a2i_aux(s.substr(1,s.length()-1)) else
if s.substr(0,1) = "+" then a2i_aux(s.substr(1,s.length()-1)) else
a2i_aux(s)
fi fi fi
};
(*
a2i_aux converts the usigned portion of the string. As a programming
example, this method is written iteratively.
*)
a2i_aux(s : String) : Int {
(let int : Int <- 0 in
{
(let j : Int <- s.length() in
(let i : Int <- 0 in
while i < j loop
{
int <- int * 10 + c2i(s.substr(i,1));
i <- i + 1;
}
pool
)
);
int;
}
)
};
(*
i2a converts an integer to a string. Positive and negative
numbers are handled correctly.
*)
i2a(i : Int) : String {
if i = 0 then "0" else
if 0 < i then i2a_aux(i) else
"-".concat(i2a_aux(i * ~1))
fi fi
};
(*
i2a_aux is an example using recursion.
*)
i2a_aux(i : Int) : String {
if i = 0 then "" else
(let next : Int <- i / 10 in
i2a_aux(next).concat(i2c(i - next * 10))
)
fi
};
};
class Main inherits IO {
main () : Object {
let a : Int <- (new A2I).a2i("678987"),
b : String <- (new A2I).i2a(678987) in
{
out_int(a) ;
out_string(" == ") ;
out_string(b) ;
out_string("\n");
}
} ;
} ;