MAKE A TABLE OF ASCII VALUES
Program ASCII;
Uses
Crt;
Const
Space = CHR(32); { Space = ' ' }
Tilde = CHR(126); { Tilde = '~' }
Var
CH: Char;
Char_count: Integer;
Procedure Print_char_and_ASCII(X: Char);
Begin
Textcolor(White);
Write(X :3);
Textcolor(Yellow);
Write(ORD(X) :4)
End;
Begin
Clrscr;
Char_count := 0;
Writeln;
Writeln('The ASCII character is in white; its ASCII ordinal value is in yellow.');
Writeln;
For CH := Space to Tilde Do
Begin
Char_count := Char_count + 1;
Print_Char_and_ASCII(CH);
If Char_count MOD 10 = 0
Then Writeln;
End;
Repeat
Textcolor(White);
GOTOXY(1, 15);
Write('Enter a keyboard character, use 0 to quit: ');
Readln(CH);
GOTOXY(1, 17);
Textcolor(White);
Write('The predecessor of ');
Textcolor(Lightred);
Write(CH);
Textcolor(White);
Write(' is ');
Textcolor(Yellow);
Write(PRED(CH));
Textcolor(White);
Write(' and it''s successor is ');
Textcolor(LightBlue);
Write(SUCC(CH))
Until (CH = '0');
GOTOXY(1, 23);
Textcolor(Lightmagenta);
Write('Program Complete.');
Readln
End.
SETS IN TURBO PASCAL
In Turbo Pascal, sets are unordered collections of related objects which may
be thought of as an entity. Each member of the set must be of the same
SIMPLE type except real (i.e., all are integer, or character or Boolean).
If integers are used to make a set, the integers must be taken from the range
0..255. The objects in a set are called its elements. Thus, Pascal sets may
not have elements of mixed kind. Sets differ from "lists" in that the order
of the objects in a set is unimportant. For example, the user-defined range
types defined below are different (they could not both appear in the same
program, however, because of duplicate identifiers used in their definitions):
Type
Days1 = (Monday, Wednesday, Tuesday, Thursday, Friday);
Type
Days2 = (Monday, Tuesday, Wednesday, Thursday, Friday);
But the set constants X and Y defined below are identical:
Type
Valid_integer = 0..255;
Work_days = Set of Valid_integer;
Var
X, Y: Work_days;
Begin
X := [1, 2, 3, 4, 5];
Y := [1, 3, 4, 2, 5];
As you might suspect, sets are not supported by the Read and Write procedures.
However, Pascal supports a rich variety of operations on sets, summarized
below; suppose A, B, C and D are set constants defined by
A = [1, 3, 5, 6, 7, 8],
B = [1, 2, 4, 7, 8, 9],
C = [1, 2, 4], and
D = [1, 2, 3, 4, 5].
Pascal Command Definition of Value Example
Returned
A + B union of sets [1,2,3,4,5,6,7,8,9]
A * B intersection of sets [1, 7, 8]
A - B Set difference [3, 5, 6]
A = B Boolean; true if the A = B is false
sets are identical
A <= B Boolean; true if every A <= B is true
elt of A is an elt of B
The utility of sets is usually seen best in the use of the IN statement
which will allow us to test whether or not an element is in a given set.
For example, if Ch is of type Character and S = ['a', 'e', 'i', 'o', 'u'],
then the following loop will print out the characters in S:
For Ch := 'a' to 'z' Do
If Ch IN S
Then Write(Ch :2);
To print out the characters not in S, use the statement: If NOT(Ch IN S) ...
This feature of Pascal also makes it easier to control menus. For example,
suppose Response is of type Char and we wish to devise a menu of options
depending on the value in Response. One possibility is to quit the menu when
Response is Q:
Repeat
Writeln('Enter your choice (use Q to quit): ');
Readln(Response);
{ Do whatever required when Response <> 'Q' }
Until (Response IN ['q', 'Q']);
A SIMPLE SETS DEMONSTRATION: COUNT VOWELS IN A SENTENCE
Program Simple;
Uses
Crt;
Const
MaxLen = 80;
Type
Set_of_vowels = Set of 'A'..'z';
Full_string = String[MaxLen];
String_range = 1..MaxLen;
Var
Vowel_count: Integer;
Line: Full_string;
Vowels: Set_of_vowels;
Count: String_range;
Begin
Vowels := ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
Clrscr;
Writeln('Enter a sentence: ');
Readln(Line);
Vowel_count := 0;
For Count := 1 to Length(Line) Do
If Line[Count] IN Vowels
Then Vowel_count := Vowel_count + 1;
Writeln;
GOTOXY(1, 6);
Textcolor(White);
Case Vowel_count of
1: Write('There was vowel in your sentence.');
0, 2..MaxLen: Write('There were vowels in your sentence.')
End;
Textcolor(Yellow);
GOTOXY(12, 6);
Write(Vowel_count);
GOTOXY(1, 10);
Write('Program complete.');
Readln
End.
{ An alternative way to define Vowels is via a so-called "typed constant",
which is allowed in Turbo Pascal. The beginning of the program would be as
follows:
Const
Maxlen = 80;
Vowels: Set of 'A'..'z' = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
Type
Full_string = String[MaxLen];
String_range = 1..MaxLen;
Var
Vowel_count: Integer;
Line: Full_string;
Count: String_range;
Begin
Clrscr; etc. }
---------------------------------------------------------------------------------------
Program output from ASCII:
---------------------------------------------------------------------------------------
The ASCII character is in white; its ASCII ordinal value is in yellow.
32 ! 33 " 34 # 35 $ 36 % 37 & 38 ' 39 ( 40 ) 41
* 42 + 43 , 44 - 45 . 46 / 47 0 48 1 49 2 50 3 51
4 52 5 53 6 54 7 55 8 56 9 57 : 58 ; 59 < 60 = 61
> 62 ? 63 @ 64 A 65 B 66 C 67 D 68 E 69 F 70 G 71
H 72 I 73 J 74 K 75 L 76 M 77 N 78 O 79 P 80 Q 81
R 82 S 83 T 84 U 85 V 86 W 87 X 88 Y 89 Z 90 [ 91
\ 92 ] 93 ^ 94 _ 95 ` 96 a 97 b 98 c 99 d 100 e 101
f 102 g 103 h 104 i 105 j 106 k 107 l 108 m 109 n 110 o 111
p 112 q 113 r 114 s 115 t 116 u 117 v 118 w 119 x 120 y 121
z 122 { 123 | 124 } 125 ~ 126
Enter a keyboard character, use 0 to quit: 0
The predecessor of 0 is / and it's successor is 1
Program Complete.
---------------------------------------------------------------------------------------
Program output from SETS:
---------------------------------------------------------------------------------------
Enter a sentence:
Now is the time for all good men to come to the aid of their party.
There were 21 vowels in your sentence.
Program complete.
---------------------------------------------------------------------------------------
Enter a sentence:
Rsts dfzyz dptxzzxly qssrtbv.
There were 0 vowels in your sentence.
Program complete.
---------------------------------------------------------------------------------------
Enter a sentence:
aaaaaaaaeeeeiiioooooooouuuAAAEEIIIIOOOUUUU
There were 42 vowels in your sentence.
Program complete.