Useful functions
Author |
Message |
ericfourfour
|
Posted: Wed Jan 10, 2007 8:35 pm Post subject: Useful functions |
|
|
I have decided to post a bunch of simple functions that are commonly used. Feel free to post more.
These can be used, for example, in a "yes or no" situation:
Turing: | fcn getChoiceCustom (trueChoice, falseChoice,
displayMessage, invalidInputMessage : string) : boolean
var choice : string
var trueChoiceLower : string := Str.Lower (trueChoice )
var falseChoiceLower : string := Str.Lower (falseChoice )
loop
if displayMessage not= "" then
put displayMessage
end if
get choice
choice := Str.Lower (choice )
exit when choice = trueChoiceLower or choice = falseChoiceLower
if invalidInputMessage not= "" then
put invalidInputMessage
end if
end loop
result choice = trueChoiceLower
end getChoiceCustom
fcn getChoice (trueChoice, falseChoice : string) : boolean
result getChoiceCustom (trueChoice, falseChoice,
trueChoice + " or " + falseChoice + "?", "Invalid Input")
end getChoice |
This one capitalizes a string:
Turing: | fcn capitalize (text : string) : string
if text not= "" then
result Str.Upper (text (1)) + text (2 .. *)
end if
result ""
end capitalize " |
This one swaps the case of a string:
Turing: | fcn swapCase (text : string) : string
var newText := ""
for i : 1 .. length (text )
var upperCase := Str.Upper (text (i ))
if upperCase = text (i ) then
newText + = Str.Lower (text (i ))
else
newText + = upperCase
end if
end for
result newText
end swapCase |
Here are a few for getting integers from the keyboard:
Turing: | fcn getIntCustom (displayMessage, invalidInputMessage : string) : int
loop
var numInput : string
if displayMessage not= "" then
put displayMessage
end if
get numInput
if numInput >= "0" and numInput <= "9" and strintok (numInput ) then
result strint (numInput )
end if
if invalidInputMessage not= "" then
put invalidInputMessage
end if
end loop
end getIntCustom
fcn getInt () : int
result getIntCustom ("Enter an integer.", "Invalid Input.")
end getInt
fcn getIntBetweenCustom (low, high : int,
displayMessage, invalidInputMessage : string) : int
loop
var num : int := getIntCustom (displayMessage, invalidInputMessage )
if num >= low and num <= high then
result num
end if
if invalidInputMessage not= "" then
put invalidInputMessage
end if
end loop
end getIntBetweenCustom
fcn getIntBetween (low, high : int) : int
result getIntBetweenCustom (low, high,
"Enter an integer between " + intstr (low ) + " and " +
intstr (high ) + ".", "Invalid Input.")
end getIntBetween |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|