Formatting Output and Type Conversions
Author |
Message |
BigBear
|
Posted: Fri Feb 08, 2008 3:31 pm Post subject: Formatting Output and Type Conversions |
|
|
This Tutorial will explain how to formate the output of Strings, Integers and Reals and how to convert variable from one type to another.
Formatting Output
Strings %Output
put "Hello" :10 Hello_ _ _ _ _ % 10 characters
put "Hello" : 2 Hello % 5 characters
Integers
put 541 : 7 _ _ _ _ 541 % 7 characters
put 541 : 1 541 % 3 characters
Reals
put 3.4 : 6 _ _ _ 3.4 % 6 characters
put 3.4 : 0 : 3 3.400
put 3.4 : 10 : 3 _ _ _ _ _ 3.400 %10 characters
put 34 : 0 : 3 : 2 3.400e+01 % Sci. Notation
--------------------------------------------------------------------------------------------------------
Type Conversion
Type conversion means converting a variable from one type to another.
Real to Integer %Output
ceil(x) - returns the smallest integer not less than x.
ceil(5.3) 5
ceil(-4.1) -4
floor(x) - returns the largest integer not greater than x.
floor(8.8) 8
floor(-0.1) -1
round(x) - rounds to the nearest integer
round(3.1) 3
round(3.5) 4
Integer to Real %Output
intreal(x) - returns the value of the integer as a real.
intreal(25) 25.0
Integer to String %Output
intstr(10) "10"
String to Integer %Output
strint(x) - converts a string x to its corresponding integer.
strint("100") 100
String to Real %Output
strreal("7.4") 17.4
Using strintok to Error Trap
When you prompt the user for a integer you usually want it to be within say between 0 and 100 if you want to calculate the average mark across 4 tests.
Your code will look like this
code: | %Simple Error Trap Program
var mark1 : int
var mark2 : int
var mark3 : int
var mark4 : int
%We can also group variables of the same type together like this
%var mark1, mark2, mark3, mark4 : int
loop
put "Enter the first four test results."
get mark1, mark2, mark3, mark4
%It will keep asking the user for four marks untill they are all between 0 and 100
exit when mark1 >= 0 and mark1 <= 100 and mark2 >= 0 and mark2 <= 100 and mark3 >= 0 and mark3 <= 100 and mark4 >= 0 and mark4 <= 100
end loop
var marks : int := mark1 + mark2 + mark3 + mark4
put "Your average test result is ", marks div 4, "%" |
We could put a loop on each mark to identify the error immediately
code: | put "Enter the results for your first test.\n1 "..
loop
get mark1
exit when mark1 >=0 and mark1 <=100
end loop |
Just repeat this for each mark we would the user to enter
But what happens if the user enters "hello"?
It crashes so we use strintok
Basically the program looks the same but we declare the variables as string
code: | %Simple Error Trap Program using strintok
var mark1, mark2, mark3, mark4 : string
loop
put "Enter the first four test results."
get mark1, mark2, mark3, mark4
%It will keep asking the user for four marks untill they are all between 0 and 100
if strintok (mark1) and strint (mark1) >= 0 and strint (mark1) <= 100 and strintok (mark2) and strint (mark2) <= 100 and strint (mark2) >= 0 and strintok (mark3) and strint (mark3) <= 100 and
strint (mark3) >= 0 and strintok (mark4) and strint (mark4) >= 0
and strint (mark4) <= 100 then
exit
end if
end loop
var marks : int := strint (mark1) + strint (mark2) + strint (mark3) + strint (mark4)
put "Your avearge test result is ", marks div 4, "%" |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
BigBear
|
Posted: Fri Feb 08, 2008 3:36 pm Post subject: Re: Formatting Output and Type Conversions |
|
|
While I was making this I was wondering how do I make code look that in appears in Turing? |
|
|
|
|
|
Nick
|
Posted: Fri Feb 08, 2008 3:51 pm Post subject: Re: Formatting Output and Type Conversions |
|
|
[syntax="turing"][/syntax] |
|
|
|
|
|
BigBear
|
Posted: Fri Feb 08, 2008 4:02 pm Post subject: Re: Formatting Output and Type Conversions |
|
|
Really then what is the point of have the [code] business? |
|
|
|
|
|
Nick
|
Posted: Fri Feb 08, 2008 4:34 pm Post subject: RE:Formatting Output and Type Conversions |
|
|
psuedo code, small lenghs of code, formatting an ASCII image (since codes font keeps everything spaced), identation, etc... |
|
|
|
|
|
HeavenAgain
|
Posted: Fri Feb 08, 2008 4:55 pm Post subject: RE:Formatting Output and Type Conversions |
|
|
for formatting, you can just briefly explain what is right justified, and left justified, and where they are used, on numbers and string etc
and also
Quote: ceil(x) - returns the smallest integer not less than x.
ceil(5.3) 5
ceil(-4.1) -4
floor(x) - returns the largest integer not greater than x.
floor(8.8) 8
floor(-0.1) -1
have a look at that again
and the if statement in your code looks awfully long, and didn't really show how to use strintok, instead try something like
code: | var myVariable : string
if ( strintok(myVariable) )
put myVariable, "+ 1 = ", strint(myVariable)+1 ," is a number"
else
put "you typed ",myVariable," which is not a number"
end if |
|
|
|
|
|
|
|
|