📄 pasl1008.html
字号:
<HTML><HEAD>
<LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 9</Title></head>
<body background="../tile01.jpg">
<H1><center>Best Records</center></h1><p> <br><br>
<p>Hello ! We meet again ! How are you ? Good ? Thank God ! Ready to take this
lesson ? Good ! Topics discussed this time are :<ol>
<li>What is records</li>
<li>Declarations</li>
<li>3. How to use it (<tt>with...do</tt>)</li>
<li>Nested records</li>
<li>Conditional records</li>
<li>Extras : Sorting methods ! (Bubble, Shell, and Quick)</li></ol>
Let's begin !
<p>Like an array, record is just grouping various data under one name. However
array can only store ONE type of data, but record can store ALL kind of data,
including arrays.</p>
<p>How could we declare a record ? Actually, record is a customized kind of
data type. So, I hereby want to introduce you the <tt>type</tt> part of Pascal
program. The <tt>type</tt> part is placed <b>BEFORE</b> the <tt>var</tt> part. Let's
look at the outline of somewhat-complex-Pascal-program-structure :</p><hr><pre>
program programname;
uses units1, units2, ...;
type
name = variabletype;
:
: (type declarations)
var
(variable declarations)
:
:
(procedures and functions)
:
:
begin (main begin...end block)
:
:
end.
</pre><hr><p>
You can declare variable type anywhere, but normally, before variable declaration.
Here is one example to declare record :</p><hr><pre>
type
TEmployee = record
name : string[25];
address : string[40];
age : byte;
position : string[10];
commision : real;
end;
var
x : TEmployee;
</pre><hr><p>
Record will be no use after its declaration if there are no variable using
it. So, you can use the declaration if you have a variable of that kind of
data. In this case, <tt>x</tt>. So, let's look at the main <tt>begin...end</tt> block :</p><hr><pre>
begin
x.name := 'Paul Doherty';
x.address := '11th Kingston Avenue';
x.age := 35;
x.position := 'Salesman';
x.commision := 0.10;
end.
</pre><hr><p>
Through x, all accesses are available. Beware that : <tt>TEmployee.name</tt> is <b>NOT</b>
the way to access name field.</p><p>
Each data stored inside a record is called FIELDS. So, the <tt>TEmployee</tt> has
fields of name, address, age, position, and commision.</p><p>
Sometime, if we have so many fields inside a record, we find it is bothersome
to write the variable, like the example above : so many x has to be
written. We may want to simplify it. The example above (the main <tt>begin..end</tt>
block) can be written as follows :</p><hr><pre>
begin
with x do
begin
name := 'Paul Doherty';
address := '11th Kingston Avenue';
age := 35;
position := 'Salesman';
commision := 0.10;
end;
end.
</pre><hr><p>
Suppose you have a variable called name, how can Pascal distinct it from
the record fields of name ? Within <tt>with...do</tt> statement, the record field
overrides. If you want to assign the name variable, not the one which is
the field of x, do it outside of <tt>with...do</tt> block.</p><p>
You can use array of records. Suppose you have declared the <tt>TEmployee</tt> record
tag. Now you want to declare an array of it. It's simple :</p><hr><pre>
var
MyEmployee : array[1..100] of TEmployee;
</pre><hr><p>Just like declaring normal arrays. How to access it ? Suppose I access the
first element :</p><hr><pre>
begin
MyEmployee[1].name := 'Paul Doherty';
MyEmployee[1].address := '11th Kingston Avenue';
MyEmployee[1].age := 35;
MyEmployee[1].position := 'Salesman';
MyEmployee[1].commision := 0.10;
end.
</pre><hr><p>With <tt>with...do</tt> it's all the same :</p><hr><pre>
begin
with MyEmployee[1] do
begin
name := 'Paul Doherty';
address := '11th Kingston Avenue';
age := 35;
position := 'Salesman';
commision := 0.10;
end;
end.
</pre><hr><p>
Can we do nested record as looping and conditional do ? Good question ! It
IS perfectly legal. But you may do it like this :</p><hr><pre>
type
TCommision = record
sales : real;
production : real;
absence : real;
end;
TEmployee = record
name : string[25];
address : string[40];
age : byte;
position : string[10];
commision : TCommision;
end;
</pre><hr><p>
In that example, the field commision of <tt>TEmployee</tt> record tag is a record of
<tt>TCommision</tt>. How to access the sales field of commision ? Easy ! Suppose you
have declared x as a variable of <tt>TEmployee</tt> : <tt>x.commision.sales:=0.5;</tt>
Just add another period after <tt>commision</tt> then type <tt>sales</tt>, that's it ! Using
arrays are pretty much the same ! <tt>MyEmployee[1].commision.sales:=0.3;</tt></p>
<p>You may nest records more than 2 steps. To accomplish such task you may use
analogies to the previous example. To access them is quite the same, just
type down the period and subfield name and so on. The deeper you nest, the
longer you type.</p>
<p>You may use <tt>with...do</tt> block inside another <tt>with...do</tt> block to access nested
records. Go on, try it !</p>
<p>One special caution should be taken :<br>
Pascal give a special gift that may not even exist in other programming languages :
<b>CONDITIONAL RECORDS</b>. What is conditional records exactly ? Conditional records
are record with a variable length depending on the contents of a field. That field whose
contents become so important is called pivot field. Let's look at this :</p><hr><pre>
type
TEmployee = record
Name : string[20];
Age : byte;
case department:byte of
0: (sales : longint);
1: (product : integer);
end;
</pre><hr><p>
<tt>TEmployee</tt> has the field of <tt>name</tt>, <tt>age</tt>, and <tt>department</tt>. <tt>Department</tt> is a byte
field. If the <tt>department</tt> is 0, then the <tt>TEmployee</tt> contains <tt>sales</tt> field as
the next field. Or else, in this case 1, it has <tt>product</tt> field as the next
one. The usage is quite rare though. Yet, this is very useful indeed.</p>
<p>Need to know :<br>
You have already known division in Pascal. How can we get the remainder ? Well, use <tt>mod</tt> :
<pre> x := n mod 13; { x is the remainder of n after divided by 13 }
</pre>
<h3>EXTRA !</h3>
<p>As the lesson in this chapter is quite short, I decided to give you some
extras. That is sorting methods. Three of which I'm going to tell you is
the extreme :<ol>
<li>Bubble Sort (Well-known of its simplicity but very slow)</li>
<li>Quick Sort (Well-known of its speed but quite complex)</li>
<li>Shell Sort (Quite fast but not too complex)</li></ol>
<p>The simplest but quite powerful sorting methods among all is bubble sort.
It is very easy to implement. The idea is bubbling up the desired data into
sorted order. Suppose we want to sort the data ascendingly, earlier or
smaller data will come up gradually. The main layout algorithm for this, is
just like this :</p>
<pre>
for i:=1 to NumberOfData-1 do
for j:=i+1 to NumberOfData do
if Data[i]>Data[j] then
swap Data[i] with Data[j];
</pre><p>
That's all ! Very simple. If you want to sort descendingly, replace the '>'
with '<' in If statement. Sorting a record using specific fields, like name
or something just replace the if statement :</p>
<pre>
if Data[i].name<Data[j].name then ....
</pre><p>
Unfortunately there are no first key and second key, as you may note. Pascal
knows only the first key. How to accomplish the second key ? Errrm, you
may sort the data twice. First with the second key, finally with the first
key. Easy, no ?</p><p>
Swapping datas require a temporary variable. The <tt>swap Data[i] ....</tt> can be
changed into :</p>
<pre>
begin
t:=Data[i];
Data[i]:=Data[j];
Data[j]:=t;
end;
</pre><p>
Note that t is a temporary variable. Variable t and the Data must be in the
same type. Suppose Data is an array of byte, t must also an array of byte.
Otherwise, computer will spit an error.</p><p>
Some Pascal version may need special care in swapping records. Suppose t is
a record and Data is an array of record of the same type. You may see the
exchange is done through the fields. Example the record consists of name
and address, the swapping is done like this :</p>
<pre>
begin
t.name:=Data[i].name; { one by one }
t.address:=Data[i].address;
Data[i].name:=Data[j].name;
Data[i].address:=Data[j].address;
Data[j].name:=t.name;
Data[j].address:=t.address;
end;
</pre><p>
Yup ! Simple, eh ? All sorting methods need swapping and the swapping is
done in the same way.</p>
<p>The algorithm :<br>
(If you are curious, you may read through. If you are as just practical as
me, you may skip this part without losing lesson details.)</p>
<center><table width=80% border=1><tr><td>
<p>The idea of bubble sort is just comparing all the data. The
first data is compared to the second, to the third, and so on
until the end of the data. Then the second data compared to the
third, to the fourth, so on. Then the third data to the fourth
and so on ... and so on.</p>
<p>Why it is called as the <b>Bubble sort</b>. It is because that the
comparison and the swapping make the desired data to gradually
come up like a bubble. Example : Suppose we have data like this</p>
<center>5 3 8 4 1 7 6 2 (unsorted)</center>
<p>Follow the algorithm -- i.e. comparing the first and the second
data : 5 and 3. 5 is greater than 3, hence it must be swapped out</p>
<center>3 5 8 4 1 7 6 2 (1<sup>st</sup> step)</center>
<p>Then the first and the third. Since 3 is smaller than 8, no
swapping is necessary. Then the first and the fourth : No swapping either.
The first and the fifth -- since 1 is smaller than 3, it must be swapped :</p>
<center>1 5 8 4 3 7 6 2 (4<sup>th</sup> step)</center>
<p>And so on, till the end of data, the sequence remains the same.
Then the second with the third -- no changes this time. The second
with the fourth. Change must be done :</p>
<center>1 4 8 5 3 7 6 2</center> then becomes :<br>
<center>1 3 8 5 4 7 6 2</center>
<p>And so on :</p><center>
1 2 8 5 4 7 6 3<br>1 2 5 8 4 7 6 3<br>1 2 4 8 5 7 6 3<br>1 2 3 8 5 7 6 4<br>
1 2 3 5 8 7 6 4<br>1 2 3 4 8 7 6 5<br>1 2 3 4 7 8 6 5<br>1 2 3 4 6 8 7 5<br>
1 2 3 4 5 8 7 6<br>1 2 3 4 5 7 8 6<br>1 2 3 4 5 6 8 7<br></center>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -