%%Calculator
%By : Ilia Tchoumakine AKA Entz
/*
for best results change ur OOT Preferences:
File>OOT Preferences
under run wondows, change settings to rows/columns
set rows to at least 15
set columns to at least 55
click on apply
*/
var input : string (1) %user input
input := ""
var x := 50 %position of input to prevent orverlap
var countl := 1 % keeps count of how many characters entered
var numberr := 0.0 %used to determine if number is still being entered
var numbers := "" %string entered from input for first number
var firstnumb := true % so that only one firstnum is used
var dot := true %used to fix the decimal bug
var secondnumb := true %used to determine if number is still being entered
var secondnumberr := 0.0
var secondnumbers := ""
var dot2 := true
var funktion := "" %the action to perform
var answer := 0.0
procedure vars
x := 50 %position of input to prevent orverlap
countl := 1
numberr := 0.0
numbers := "" %string enterd from input for first number
firstnumb := true % so that only one firstnum is used
dot := true
secondnumb := true
secondnumberr := 0.0
secondnumbers := ""
dot2 := true
funktion := "" %the action to perform
input := ""
answer := 0.0
end vars
%make funtions
procedure waiting % provided by Mr. Borer (from advent2)
var dummy : string (1)
locate (25, 50)
setscreen ("nocursor")
put "Press any key to continue" ..
getch (dummy)
setscreen ("cursor")
cls
end waiting
procedure answ %used for exceptions in answer for each calculator function
if answer > maxint
then
put "The answer is too high for the calculator to show"
elsif
answer < minint
then
put "The answer is too low for the calculator to show"
%If the answer is too long it starts using symbols and such (ex. 324324e25)
%this part prevents the user from having to use another
%calculator to see what answer they got on my calculator
%by using the length of the possible free space as a guide
elsif length ("answer") >= maxcol + (maxcol - 50 - x)
then
cls
put "The answer is too long for the calculator"
waiting
cls
else
%if no errors, the answer is output below the
%user-entered input
locate (11, 51)
put "=", answer
waiting
cls
end if
end answ
procedure add %adds user-netered numbers
answer := numberr + secondnumberr
answ
end add
procedure multi %multiplies user-entered numbers
answer := numberr * secondnumberr
answ
end multi
procedure divi %divided user-entered numbers
%handles the cannot divide by zero error
%checks if secondnumberr is a zero, then puts out
%error message and restarts the calculator
if secondnumberr = 0
then
cls
put "Cannot divide by zero"
vars
waiting
else
answer := numberr / secondnumberr
answ
end if
end divi
procedure minus %subtracts user-entered numbers
answer := numberr - secondnumberr
locate (11, 51)
put "=", answer
waiting
cls
end minus
%Only shows the numbers and functions, decimals
%used by the calculator
%Keeps track of length of input
procedure outp (i : string)
x += 1
locate (10, x) % y- postition(row), x - postion(col)
put i ..
countl += 1
end outp
%main program that calls everrything up
%depending on condition met
%user input asked here
procedure main
loop
locate (1, 1)
%calculates the length allowed on any ootwindow
%which is bigger then 50 columns
if countl >= maxcol + (maxcol - 50 - x)
then
vars
cls
put "You entered too many numbers"
delay (2000)
cls
end if
%asks for user input and sets up screen
put "Enter numbers and functions into the calculator"
put "Enter a number then a function then another number"
put "(1 2 3 4 5 6 7 8 9 0 . / * + - )"
put "Enter = to see answer"
%allows me to control what I want to show on the screen
%by not showing everything the user types
setscreen ("noecho")
setscreen ("nocursor")
getch (input)
%the decimal can;t be the first thing in a number
%so this part checks if something was already entered for numbers
if input = "."
then
if dot = true and length (numbers) = 0
then
cls
put "Have to enter a decimal after a number"
vars
waiting
end if
end if
%does the same thing as above except for secondnumbers
if input = "." and firstnumb = false
then
if dot2 = true and length (secondnumbers) = 0
then
cls
put "Have to enter a number before decimal"
vars
waiting
end if
end if
%if digit is entered, checks which number is being entered(1 or 2)
%and allocates digits to that number
if input = "1" or input = "2" or input = "3" or input = "4"
or input = "5" or input = "6" or input = "7"
or input = "9" or input = "0" or input = "."
then
if
firstnumb = true
then
%adds more digits onto input each time
numbers += input
elsif secondnumb = true and firstnumb = false
then
%adds more digits onto input each time
secondnumbers += input
%converts string to a real numebr usable by calculator
secondnumberr := strreal (secondnumbers)
end if
end if
%if a function is entered, it stops addign to numberr
%and stores a value for funktion
if input = "-" or input = "+" or input = "*" or input
= "/"
then
firstnumb := false
funktion := input
numberr := strreal (numbers)
end if
if input = "="
then
secondnumb := false
%these check what function was used and selects
%the appropriate procedure
if funktion = "+"
then
add
elsif funktion = "-"
then
minus
elsif funktion = "*"
then
multi
elsif funktion = "/"
then
divi
end if
waiting
vars
cls
end if
%case used to output the numbers, functions used by
%the calculator in an organize fashion, one after another
case input of
label "1" :
outp (input)
label "2" :
outp (input)
label "3" :
outp (input)
label "4" :
outp (input)
label "5" :
outp (input)
label "6" :
outp (input)
label "7" :
outp (input)
label "8" :
outp (input)
label "9" :
outp (input)
label "0" :
outp (input)
label "." :
outp (input)
label "-" :
outp (input)
label "+" :
outp (input)
label "*" :
outp (input)
label "/" :
outp (input)
label "=" :
outp (input)
label :
end case
end loop
end main
%calls up the menu and starts the calculator
main