Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Subranges of enumerated types

Once you have declared an enumerated type you can then, declare a subrange of that type. In Pascal, the letters, 'a'..'z' as represented in the ASCII table, are a subrange of the type CHAR, and the letters 'A'..'Z' are another subrange. The numbers 1914..1918, are a subrange of the type INTEGER. The point of using subranges is to make the source code easier to read, for example; if you were declaring a variable which was intended to hold whole numbers representing hours, you could use the type INTEGER, but this would include every INTEGER from MININT to MAXINT, this could clearly produce a program which would accept a ridiculous input. Better to restrict the range of the variable hours to a sensible limit like 0 to 23, This would be done like,

TYPE HOURS = 0..23;


that's all, now a variable of type hours can only accept an INTEGER in that range. Here are some examples of the syntax of the subrange.
Code fragment; Enumerated types, and subranges

TYPE MONTHS = (January,February,March,April,May,June,July, August,September,October,November,December); DAYS = (Sunday,Monday,Tuesday,Wednesday, Thursday,Friday,Saturday); DATE = 1..31; { subrange of type INTEGER } HOURS = 0..23; { subrange of type INTEGER } MINUTES = 0..59; { subrange of type INTEGER } WORKDAY = (Monday,Tuesday,Wednesday, Thursday,Friday); { subrange of type DAYS } SPRING_SEMESTER = (February,March,April, May); { subrange of type MONTHS } AUTUMN_SEMESTER = (September,October,November, December); { subrange of type MONTHS }


The identifiers MONTHS, and DAYS, denote enumerated types. There is nothing referring to them in the standard types of Pascal. Next come the identifiers DATE, HOURS, and MINUTES. Their ranges are of a standard type, therefore they are subranges. If you went on to declare an identifier SUMMER with a range of June, July, August, September. Then that too would be a subrange of the enumerated type MONTHS. This declared list of enumerated types and subranges is used in the next program. The code in the program body could be used within a larger program that held a long range schedule of appointments. The basics of enumerated types are all covered by this program.

Program 11

PROGRAM subrange_demo(INPUT,OUTPUT); { this is a program which uses subranges and enumerated types to create types for an appointment recording program } TYPE MONTH = (January,February,March,April, May,June,July,August,September, October,November,December); DATE = 1..31; DAY = (Sunday,Monday,Tuesday,Wednesday, Thursday,Friday,Saturday); HOUR = 0..23; MINUTE = 0..59; APPOINTMENT = RECORD the_month : MONTH; the_date : DATE; the_day : DAY; the_hour : HOUR; the_minute : MINUTE; END; VAR an_appointment : APPOINTMENT; month_in : MONTH; date_in : DATE; day_in : DAY; hour_in : HOUR; minute_in : MINUTE; BEGIN WRITE('Enter the appointed month : '); READLN(month_in); WRITE('Enter the appointed date : '); READLN(date_in); WRITE('Enter the appointed day : '); READLN(day_in); WRITE('Enter the appointed hour : '); READLN(hour_in); WRITE('Enter the appointed minute : '); READLN(minute_in); WITH an_appointment DO BEGIN the_month := month_in; the_date := date_in; the_day := day_in; the_hour := hour_in; the_minute := minute_in; END; WITH an_appointment DO BEGIN WRITELN; WRITELN('You have an appointment on'); WRITELN('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'); WRITE(day_in,' the ',date_in:2,' of ',month_in); WRITELN(', At ',hour_in:2,':',minute_in:2); END; END. { program subrange_demo }


When the program is run, only data which has been specified by the enumerated types and subranges is valid. If invalid data is entered the program will crash. A program enhancement would be to add data validation. This would prevent a program crash, but the program will run as it stands if data within the correct ranges is supplied.



 

Chapter 4

Sequential Files


This chapter covers:

  • Sequential files.
  • Standard function EOF.


This is going to be the shortest chapter in the book, because by the time you have read this far, learning how to use files is going to be the simplest new thing in the rest of the book. I promise!

File handling

In Pascal, a file is a sequence of characters. These files are called sequential, that is they are read from and written to one character at a time. At this point you will need to know about four new standard procedures required for file handling.

  • The Pascal procedures for file reading.
    RESET(filename) opens a file for reading.
    CLOSE(filename) closes a file, opened for reading.
  • The Pascal procedures for writing to a file.
    REWRITE(filename) opens a file for writing.
    CLOSE(filename,'SAVE') closes a file opened for writing.


Note:- It is important to realise, that the first thing the procedure REWRITE(filename) does is to clear the filename of all data in it. This means that if your program for some reason fails to write data to the file, then at the end of the program run the file will be empty.
Next, there are another couple of things you need to know. The filename has to go in the program heading, and be declared in the variable list. In the variable list, the file is declared as being of type TEXT. You will see this in the first program in this chapter. You can now go on and look at the first program in this chapter. It is a pretty simple and useless program, beyond the point of demonstration, but at least it does that.


Date: 2016-03-03; view: 671


<== previous page | next page ==>
Syntax of the RECORD declaration | Single Dimension Arrays
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)