📄 pasl1010.html
字号:
<HTML><HEAD><LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 11</Title></head>
<body background="../tile01.jpg">
<H1><center>It's All Set</center></h1><p><br><br><br><br>
<p>Hi ! Nice to meet you again !This time, we'll discuss about :<ol>
<li>Sets declaration</li><li>Using sets</li><li>Sets of constants</li></ol><p>
Set's actually a special feature of Pascal that cannot be found in other
programming languages. It merely took for granted most of the times, though
it is very good to employ it in our program. Beside it helps much the readability,
it ease our job in thinking about sets. It is intended to extend
the array, but programmers seem to ignore it, prefer using array instead of
sets.</p><p>
Well, personally I also seldom use sets. I have a little knowledge of this,
but I thought it was sufficient enough for practical use. Many books ignore
this special feature, but I'll try to cover it as best and as clearly as
possible.</p><p>
What is sets exactly ? Sets in programming is quite the same as in mathematics.
Sets in mathematics is like this :</p><pre>
A = { 1, 2, 3, 4, 5 }
</pre><p>So does sets. OK, how can we declare sets ?</p><pre>
type
days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
var
allday : set of days;
</pre><p>
You cannot write its contents or read to add it. Well, how can we use it ?
Run this program :</p><hr><pre>
type
days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
var
allday : set of days;
workday : set of Monday .. Friday;
thisday : days;
begin
thisday:=Monday;
if thisday in workday then
writeln('This day I''m in work')
else
writeln('This day I''m on holiday');
end.
</pre><hr><p>
Now, change <tt>thisday:=Monday</tt> into <tt>thisday:=Saturday</tt>, and run it again. See
the difference ? Look at the word <tt>in</tt> in <tt>if thisday in ...</tt>. You know its
function now ?</p><p>
Suppose I have the variable of myday, it is defined as set of days, like
this :</p><pre>
var
myday : set of days;
</pre><p>
Naturally, myday contents is Sunday, Monday, ... through Saturday. Suppose
Wednesday is my bad day, so that I don't want it included inside myday. You
do this :</p><pre>
exclude(myday,Wednesday);
</pre><p>Then again, excluding Friday :</p><pre>
exclude(myday,Friday);
</pre><p>After that, you change your mind, including Friday again :</p><pre>
include(myday,Friday);
</pre><p>Look at this program :</p><hr><pre>
type
days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
var
allday : set of days;
myday : set of days;
workday : set of Monday .. Friday;
thisday : days;
begin
exclude(myday,Wednesday);
exclude(myday,Friday);
thisday:=Friday;
if thisday in myday then
writeln('Friday is just my day !')
else
writeln('Friday is not my day !');
include(myday,Friday); { Changing your mind }
if thisday in myday then
writeln('I change my mind : Friday is just my day !')
else
writeln('Friday is still not my day !');
end.
</pre><hr><p>
Yes, you're brilliant ! You may guess the output be like this :</p><pre>
Friday is not my day !
I change my mind : Friday is just my day !
</pre><p>Got the idea ? Good ! The word <tt>in</tt> is not just comparing one variable to a
set but comparing sets, too. Look at this :</p><pre>
if workday in allday then { This is true since all elements in workday }
: { exist in allday. }
:
if allday in workday then { This is false, because there are elements }
: { in allday which are not exist in workday, }
: { they are Sunday and Saturday. }
</pre><p>Look at workday definition :</p><hr><pre>
var
workday : set of Monday..Friday;
begin
include(workday,Saturday); { This is illegal, since it is out of range }
end.
</pre><hr><p>
You cannot include elements that is out of range. But you may include
elements more than once even you have already got it inside the set, like
this :</p><pre>
include(allday,Monday); { Legal }
include(allday,Monday); { Legal too }
</pre><p>You might not need the sets since it can be formed inside our program. Look
at these legal statements :</p><pre>
thisday:=Tuesday;
if thisday in [Monday..Friday] then { True }
:
:
</pre><p>or like this :</p><pre>
thisday:=Tuesday;
if thisday in [Monday,Thursday..Saturday] { False }
:
:
</pre><p>This is the practical use :</P><hr><pre>
uses crt;
var
c : char;
begin
write('Yes or no (Y/N) ?');
repeat
c:=upcase(readkey);
until c in ['Y','N'];
end.
</pre><hr><p>Run that program then figure it out ! Then, this program :</p><hr><pre>
uses crt;
type
allletter = set of 'A'..'Z';
var
c : char;
vowel : set of 'A','E','I','O','U';
letter : set of allletter;
begin
write('Enter a vowel');
repeat
c:=upcase(readkey);
until c in vowel;
write('Enter a letter');
repeat
c:=upcase(readkey);
until c in letter;
end.
</pre><hr><p>
Practice a lot, that was good. This is quite a short lesson, but this time,
no extra. Sorry :) and no quiz too. It is quite easy and I think you've got
the most of the lesson.</p><p>Sayonara, my friend !</p>
<hr><p><B><H3>Where to go ?</H3></B><p>
<A HREF="../news.html">Back to main page</A><BR>
<A HREF="pasles01.html">Back to Pascal Tutorial Lesson 1 contents</A><BR>
<A HREF="pasl1009.html">Back to Chapter 10</A> about complex data types<BR>
<A HREF="pasl1011.html">To Chapter 12</A> about making custom units<BR>
<A HREF="../mylink.html">My page of programming link</A><BR>
<A HREF="../faq.html">Contact me here</A>
<hr><P class="cpy">By : Roby Joehanes, © 1997, 2000</P>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -