cool programming — mathematical operation

If we want to input a string, we should use in_string function. To do mathematical operation with the string, we should convert the string into integer and after doing mathematical operation the output should be converted integer to string. At first, we want to take input from user and add 1 with the value. We can do this by using the following program as fact.cl file.


(*
fact.cl
*)
class Main inherits A2I{
main():Object{
(new IO).out_string(i2a(a2i((new IO).in_string())+1).concat("\n"))

};

};

Here, for integer to string and string to integer conversion we should inherit a class A2I which will work for string conversion. A2I class code is given below


(*
a2i.cl
*)
(*
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 {
(let j : Int (let i : Int while i < j loop
{
int i }
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 i2a_aux(next).concat(i2c(i - next * 10))
)
fi
};

};

You need not understand all the codes in the elementary level. You could understand this code later.

To run the code these codes, we should run

coolc fact.cl a2i.cl
spim fact.s


In the program if we input 6 it will output 7.

We could do this type of increment by using fact function
like this


class Main inherits A2I{
main():Object{
(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n"))
};
fact(i: Int):Int{

i+1

};

};

here fact function will return an incremented value. fact function is called in the main function.

If we want to do factorial of a mathematical value, this program will work


class Main inherits A2I{
main():Object{
(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n"))

};
fact(i: Int):Int{

if(i=0)then 1 else i*fact(i-1) fi

};

};

here we use if then conditional logic if i is zero then it will return 1 other than recursively it will call fact function. The end of if statement can be understand by fi keyword. fi means end of if condition.

We could the program by using while statement, the program will be like this


(*
this is factorial programming using while loop

*)
class Main inherits A2I{
main():Object{
(new IO).out_string(i2a(fact(a2i((new IO).in_string()))).concat("\n"))
};
fact(i: Int):Int{
let fact:Int while(not(i=0)) loop
{
fact<-fact*i;
i<-i-1;
}
pool;
fact;
}
};
};

In the program fact is a variable which is also integer type. fact is also the name of the function we could use function name as a variable name in cool programming. in the block of fact, we use while loop which is a conditional loop iterate till not i equal zero. End of loop could be understand by pool keyword which is reverse of loop. We return fact value as a return value.

In this lesson, we learn input string, output string, number conversion, if statement, while loop programming in cool language.