home *** CD-ROM | disk | FTP | other *** search
- { -----------------------------------------------------------------------------
-
- NOTICE:
-
- THESE MATERIALS are UNSUPPORTED by OSS! If you do not understand how to
- use them do not contact OSS for help! We will not teach you how to
- program in Pascal. If you find an error in these materials, feel free
- to SEND US A LETTER explaining the error, and how to fix it.
-
- THE BOTTOM LINE:
-
- Use it, enjoy it, but you are on your own when using these materials!
-
-
- DISCLAIMER:
-
- OSS makes no representations or warranties with respect to the contents
- hereof and specifically disclaim all warranties of merchantability or
- fitness for any particular purpose. This document is subject to change
- without notice.
-
- OSS provides these materials for use with Personal Pascal. Use them in
- any way you wish.
-
- -------------------------------------------------------------------------- }
-
-
- { Examples of "str" and "val" routines for Personal Pascal. }
-
- PROGRAM str_val;
-
- TYPE
- { Since we aren't including any of the GEM libraries in this demo, we need
- to declare the STRING type we will work with. }
- str255 = STRING[ 255 ];
-
- VAR
- i, n: integer;
- s: str255;
-
-
-
- { str - Convert the integer in the parameter 'n' to a string in 's'. The
- string may consist of a minus sign ('-'), followed by up to 5 digits of
- the number. The string will be the minimal length which will hold the
- number (i.e., leading plus signs and leading zeros will NOT appear in
- the final string!). }
-
- PROCEDURE str( n: integer; VAR s: str255 );
-
- VAR
- digit, { Holds each digit value of 'n' as it is created }
- divisor, { Division by this is used to find each digit }
- i: integer; { Index in string at which to put next character }
- leading: boolean; { True, if the next digit will be the leading digit }
-
- { add_char - Add a single character to the string, incrementing the current
- index. }
-
- PROCEDURE add_char( c: char );
-
- BEGIN
- i := i + 1;
- s[i] := c;
- END;
-
- BEGIN { str - main routine }
- i := 0; { Start at the beginning of the string }
- IF n < 0 THEN { If the number is negative, add a minus sign }
- BEGIN
- add_char( '-' );
- n := -n;
- END;
- { Now divide the number by decreasing divisors to form each digit-- the
- divisor starts at 10000, since this is the maximum power of 10 which
- will fit into a positive integer. }
- divisor := 10000;
- leading := true;
- WHILE divisor > 0 DO
- BEGIN
- { Get the next digit value. If the digit is not zero, or the digit
- will not be the leading digit, then add it to the string (this
- inhibits the addition of leading zeros). }
- digit := n DIV divisor;
- IF (digit <> 0) OR NOT( leading ) THEN
- BEGIN
- add_char( chr(digit + ord('0')) );
- leading := false;
- END;
- { Throw away the part of the number just used, and decrease the
- divisor so we will get the next digit next time. }
- n := n MOD divisor;
- divisor := divisor DIV 10;
- END;
- { At this point, if the index is still zero, then we didn't add any
- characters to the string! The original number must have been zero, so
- just add that single character. }
- IF i = 0 THEN
- add_char( '0' );
- { Finally, set the length of the string to the final index value. }
- s[0] := chr(i);
- END;
-
-
- { val - Convert the number contained in the string 's' to an integer, and
- return that integer as the function result. We are assuming the caller
- has ensured the string is a valid number, so we we're just going to
- convert characters into the number until a non-digit is encountered, or
- the end of the string is reached. A zero is returned as the function
- value if the string that was passed has zero length. }
-
- FUNCTION val( s: str255 ): integer;
-
- VAR
- { Flag to indicate the number has a leading minus sign }
- minus: boolean;
-
- BEGIN
- { Start with the first character of the string, but first skip leading
- blanks. }
- i := 1;
- WHILE (i < length(s)) AND (s[i] = ' ') DO
- i := i + 1;
- { If there are characters still in the string, convert it to a number }
- n := 0;
- IF length(s) >= i THEN
- BEGIN
- { If first char is '-', we have to negate the number after we finish
- converting the digits. }
- IF s[i] <> '-' THEN
- minus := false
- ELSE
- BEGIN
- minus := true;
- i := i + 1;
- END;
- { While there are more digits in the string, convert characters. }
- WHILE (i <= length(s)) AND (s[i] IN ['0'..'9']) DO
- BEGIN
- n := (n * 10) + ord(s[i]) - ord('0');
- i := i + 1;
- END;
- { Negate the final result, if necessary. }
- IF minus THEN
- n := -n;
- END;
- { Return the converted number as the result of this function. }
- val := n;
- END;
-
-
-
- BEGIN
- FOR i := -10 TO 10 DO
- BEGIN
- readln( s );
- n := val(s);
- write( n, ' = ' );
- str( n, s );
- writeln( s );
- END;
- END.
-