Child bounds type, collection type, record type, file type

Source: Internet
Author: User

Child boundary type

If we define a variable i is an integer type, then the value of I is in the Pascal of the microcomputer system, using a 2-byte definition notation with a value range of -32768~32767. In fact, the values of the variables used in each program have a definite range.
For example, a person's age generally does not exceed 150, a class of students not more than 100 people, the number of months in a year does not exceed 12, the number of days in January does not exceed 31, and so on.
If we can specify the range of variables used in the program, it is convenient to check out the illegal data, which can better guarantee the correctness of the program operation. It also saves memory space to some extent.
The sub-boundary type is a good solution to the above problem. In addition, in the definition of an array, it is common to the sub-boundary type to specify the range of the array subscript,

Definition of a child boundary type

Rules for the operation of sub-boundary type data

⒈ An operation rule that can use a base type also applies to that type's sub-bounds type.
For example, you can use an integer variable, or you can use a sub-boundary type data that is based on an integral type.
The ⒉ operation rules for base types also apply to the type of the subtype.
For example, Div,mod requires that the data to participate in the operation be integer, and therefore can be any of the sub-bounds of the integer type data.
3. Different sub-boundary type data with the same base type can be mixed.
For example: with the following instructions:
Type a=1..100; b=1.1000; c=1..500;
var x:a; Y:b; T:C;
Z:integer;
The following statements are also valid:
Z:=SQR (x) +y+t;
The following statement:
T:=x+y+z;
When the value of X+y+z is within the 1~500 range, it is legal, otherwise an error occurs.

Practice

By using the sub-boundary type as the condition statement designator, a program for distinguishing the numbers, uppercase and lowercase letters and special characters is compiled.
If the character is a number, the output ' digits '
Output ' upper-aseletters ' if the character is in uppercase
Output ' lower-caseletters ' if the character is a lowercase letter
Otherwise output ' special charactors '


Collection type

A collection is a whole that consists of elements that have some common characteristics. In Pascal, a collection is made up of a set of data elements that have the same ordered type, which is called the base type of the collection.

Description of definitions and variables for collection types

The general form of a collection type is: Set of < base type >;
Description
The ① base type can be any order type, not a real or other constructed type. At the same time, the number of the base type data must not exceed 255. For example, the following instructions are legal:
Type
Letters=set of ' A ' ... ' Z ';
Numbers=set of 0..9;
S1=set of Char;
② As with other custom types, you can combine type descriptions with variable descriptions. For example:
Type numbers=set of 0..9;
var s:numbers;
With Var s:set of 0..9;

Operation of the collection

⒈ Assignment operation
The set variable can only be assigned by an assignment statement, cannot be assigned by a read statement, or directly output the value of a set variable through a write (or writeln) statement.
The ⒉, intersection, and difference operations of a set
There are three kinds of operations that can be performed on the collection, alternating, and differential, and each operation can have only one operator and two operands, and the result is still a set. Three operators are denoted by "+", "*", "-" respectively. Note the difference between them and arithmetic operations.
Relational operations for ⒊ collections
Collections can perform equality or inequality, include or include relational operations, and test whether an element is in a collection. The operators used are: =, <>, >=, <=, in. They are all two mesh operations, and the operands of the first 4 operators are compatible collection types, the right side of the last operator is the collection, and the left side is the same expression as the collection base type.

Type weekday= (SUN,MON,TUE,WED,THU,FRI,SAT);
Week=set of weekday;
Subnum=set of 1..50;
Write down the values of the following expressions:
⑴[SUN,SAT]+[SUN,TUE,FRI]
⑵[sun,fri]*[mon,tue]
⑶[wun,sat]*[sun. Sat
⑷[sun]-[mon,tue]
⑸[mon]-[mon,tue]
⑹[sun. SAT]-[MON,SUN,SAT]
⑺[1,2,3,5]=[1,5,3,2]
⑻[1,2,3,4]<>[1. 4]
⑼[1,2,3,5]>=[1. 3]
⑽[1. 5]<=[1. 4]
⑾[1,2,3]<=[1. 3]
⑿2 in[1. 10]

Practice

1. Enter a series of characters, counting the numeric characters, alphabetic characters, and other characters respectively. Input '? ' After the end.
2. Call a random function to produce 10 distinct random integers (0<=x<=40), put in the collection and output together (5 rows).
3, write a decoding program, will input a string of characters, (only lowercase letters, numbers and spaces, entered with a period end) translated into the original code. The decoding rules are as follows:
① Digital 0,1,2,3,...,9 respectively and the letter A,b,c,...,j interchange;
② letters K,m,p,t,y respectively and their subsequent interchange;
⑶ other letters and spaces remain unchanged.
4, using sets of sieve method to find the prime number within 1--100

Record type

Arrays are a convenient and flexible tool for organizing and processing large quantities of data in a program, but there is a basic limitation in the use of arrays: all elements in an array must have the same type. However, in the actual problem may encounter another kind of data, it is composed of the different nature of the composition, that its various components may have various types. For example, data about a student contains the following items:
Study number String type
Name String type
Age-Integral
Sex character type
Result-based array
Pascal provides us with a type of structure called a record. In a record, you can include different types of data that are related to each other.

Definition of record type

In Pascal, a record consists of a set of components called "domains", each of which can have different types.
The general form of the record type definition:
Record
< domain 1>:< type 1>;
< domain 2>:< type 2>;
: :
< domain n>:< type n>;
End


Definition of record type


Nesting of record types

Open Field Statement



Practice

Read the 10 date, and then output the date of the second day for each date. The format of the input date is month, day, year, such as 9-30-1993, and the format of the output is 10/1/1993
Analysis: A record variable can be used today to represent a date. If you know the date after a date to be updated to the next day, you should determine whether the date entered is the last day of the current month, or the last day of the year.

File type

In the program design, often need to enter a large number of data from the keyboard, the operation is very cumbersome, but also prone to error, at the same time, after the program is running also often produces a large number of output data (results), which gives the correctness of the results of the test and testing procedures of the wrong also brought great trouble. Can there be a way for the program to automatically read data from somewhere, and then save the results of the program to the specified place? Of course, this is the "file" type in Pascal.

In Pascal, a file is defined as a sequential collection of elements of the same type. The number of elements contained in the file, called the length of the file. A file of length 0, that is, a file that does not contain any one element, is called an empty file. From a definition, a file is similar to an array, but they differ significantly. The length of the array is fixed, and the length of the file is not fixed and can be very long, and the array can be accessed from any array element by subscript, and the file must be accessed one after the other, until it is found or finished; The array can be assigned as a whole and the file is not.

Features of file types

⑴ Sequential Nature
Files are composed of elements of the same type, they are arranged and stored in a certain order, the general reading of the data in the file or output file content, must be from the file header to the end of the file in order to access. Each file type variable defined in Pascal is automatically accompanied by a file pointer that points to an element in the file and operates on that element.
⑵ Permanent
All of the data types described earlier, such as arrays, records, collections, and so on, are temporarily opened and stored in memory space while the program is running, so they are not permanent, and files are stored on external storage media such as disks, so they can be stored permanently. Also, the data is stored as a file and can be called by other programs to become shared data.
⑶ Large capacity
Since memory capacity is limited, data storage capacity is often limited when we define and use data types such as arrays, and files are stored in external memory such as disks, so the capacity can be large and even unlimited.

Common functions and procedures for file operations

General steps for file operations

In Pascal, regardless of which type of file you use, you must follow these steps, and of course, the specific statements and formats of different types of file operations may be slightly different.
1, before using the file, the file type and variables must be described;
2, the actual file on the disk (external file) and Pascal program in the file (internal file) to establish the association;
3. Open the file, point the file pointer to the beginning position, prepare for the document reading and writing;
4, read and write the file;
5, after the use of files, be sure to remember to close the file, to ensure the integrity and reliability of the file, otherwise it will cause file processing errors. Remember to remember!!!

Example (simplified version)

Begin
Assign (input, ' input.txt ');
Assign (output, ' output.txt ');
Reset (input);
Rewrite (output);
{Program Body part}
Close (input);
Close (output);
End.

Practice

1. Copy the contents of the text file Input.txt into a new text file output.txt.
2. Enter the records of n students from the keyboard (record entry number, language, maths, English, total score), deposit into the file, find out the total score of each student, and finally output all the data to the screen.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Child interface type, collection type, record type, file type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.