|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Example: Grocery StoreIn a grocery store a fast calculation is needed to count the number and type of coins that make up the change remaining from a dollar, so it is a great help to have this logic programmed into the cash register. The following program accepts from the keyboard the price of the purchase (for the sake of simplicity, this is assumed to be less than one dollar) and produces as output the number of quarters, dimes, and nickels remaining from a dollar bill. The program is an application of the integer operators DIV and MOD. { ------------------------------ figure 2-3 ------------------------------ } { Grocery Store } PROGRAM Grocery(INPUT,OUTPUT); VAR Change, TotalPrice, Dollars, Quarters, Dimes, Nickels, Cents:INTEGER; BEGIN WRITE('Enter the total-price in cents: '); READLN(TotalPrice); Change:= 100 - TotalPrice; { Quarters } Quarters:= Change DIV 25; Change:= Change MOD 25; { Dimes } Dimes:= Change DIV 10; Change:= Change MOD 10; { Nickels } Nickels:= Change DIV 5; Change:= Change MOD 5; { Cents } Cents:= Change; WRITELN('The change is:'); WRITELN(Quarters,' Quarters'); WRITELN(Dimes,' Dimes'); WRITELN(Nickels, ' Nickels'); WRITELN(Cents, ' Cents') END. A sample run of the program gives the following: Enter the total-price in cents: 22 ----> Type "22" and press Enter The change is: 3 Quarters 0 Dimes 0 Nickels 3 Cents Turbo Pascal Additional FunctionsTurbo Pascal has a considerable number of additional arithmetic functions. Of these functions, you will especially need two of them:
For example: WRITELN(FRAC(8.22):2:2); produces 0.22 WRITELN(INT(8.22)2:2); produces 8.00 Both functions return real numbers. You can make use of these functions in Drill 2-1. Another couple of functions are used to generate random numbers:
Try these two statements: WRITELN(RANDOM:2:2); WRITELN(RANDOM(n)); where n is an integer variable readout from the keyboard. Use the two statements in a program and look at the results for several runs. They should be different in each run. 2-4 The Character Type: CharThe CHAR type is used to store a single character in Pascal. You can declare a variable of the type CHAR as in the following example: VAR SingleLetter: CHAR; In the main body of the program (between BEGIN and END.) you may assign a single character to the variable SingleLetter like this: SingleLetter:= 'A'; As is clear from this example, a constant literal of the type CHAR must be exactly one character, included in single quotes: 'A' '3' '*' '$' ' ' In order to represent a single quotation (or apostrophe) as a character constant, use two single quotes like this: WRITELN ("Hi There") You can use the output statements WRITELN or WRITE to display a character constant or a character variable: WRITELN('A'); WRITELN(SingleLetter); The character set is internally represented by a one-byte integer code. The universally used code for small computers is the ASCII code (American Standard Code for Information Interchange). The ASCII code includes 256 characters from 0 to 255 (see Appendix A). The first half of the ASCII code (from 0 to 127) is standard on all personal computers. It includes the following characters:
The code also contains punctuation characters and control characters. The second half of the ASCII code is not standard and is implemented differently on different machines. The relative sequence of a character in the ASCII set is called the ordinal number.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |