Previous | Table of Contents | Next |
5.2.7.3. Modifications of Data Types
This section shows common procedures and functions that aid in modifying a variable data type.
upcase() and length()
These two functions are commonly seen together, but not always:
function upcase(x: char): char; function length(s: string): byte;
upcase() returns the uppercased version of a letter in the alphabet, if x is a letter of the alphabet. Otherwise, it returns the exact character passed to it. length() returns the literal equivalent of ord(s[0]) in a string s, or the current real length of the string stored. length() may be preferable over ord(s[0]) in some situations, but either is usually good. An example of using both of these functions is shown in Listing 5.26.
Listing 5.26. A demonstration of upcase() and length().
program fig26; function upstr(s: string): string; var i: byte; outstr: string; begin for i := 1 to length(s) do upstr[i] := upcase(s[i]); upstr[0] := s[0]; end; begin writeln(upstr(San Francisco)); end.
copy() and pos()
These two functions work with strings:
copy(s: string, starting_postion: byte, length: byte): string; pos(s,t: string): byte;
pos() returns the starting position of a string s, in the full string t, if it exists. Otherwise, it returns 0. copy() returns the partial substring described by the starting position and length byte specified in an entire string. Listing 5.27 demonstrates both functions, in a situation in which these functions are most often used, text parsing and processing.
Listing 5.27. A demonstration of copy() and pos().
program fig27; { demo of copy and pos } const s: string = The brown dog jumped over the lazy cow.; var result: byte; subst: string; begin result := 11; subst := s; while result <> 0 do { while there still are spaces } begin result := pos( , subst); if result > 0 then writeln(copy(subst, 1, result)); subst := copy(subst, result+1, length(subst) - result); end; writeln(subst); { this means the last word has occurred, write it } end.
5.2.7.4. Mathematical Functions
This section describes mathematical functions and procedures. No prototypes are given because the use of most, if not all, of these functions is simple to understand. They take a numerical variable and return a numerical variable. The following are the mathematical functions (see Listing 28.5):
Listing 5.28. Some mathematical functions.
program fig28; { demonstrates some mathematical functions } const { pi = 3.1415; DO NOT DEFINE THIS! It is already defined for you. } rad: real = pi/180; { one degree = pi / 180 radians } degreeparts: array[1..17] of integer = (0,30,45,60,90,120,135,150,180,210,225,240,270,300,315,330,360); var i: byte; sresult, cresult: real; begin writeln(Simple Trigonometry Table); writeln(Degrees,Sin:5, Cos:10,Tan:10); for i := 1 to 17 do begin sresult := sin(degreeparts[i]*rad); cresult := cos(degreeparts[i]*rad); write(degreeparts[i]:4, sresult:10:3,cresult:10:3); { note due to the conversion from radians that these are not *exactly* accurate, as denoted by the negatives on the 0 values. } { done to indicate the exception in the formula for tan x, which is sin x / cos x ; cos x <> 0 , set wont handle anything beyond byte, so 30 is subtracted, and other values are used. } if (degreeparts[i] - 30) in [60, 240] then writeln(Invalid:10) else writeln(sresult/cresult :10:3); end; end.
Previous | Table of Contents | Next |