⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pasl1009.html

📁 This is programing tutorial for people who wants to know programing in PASCAL.Pascal might be not th
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 10</Title></head>
<body background="../tile01.jpg">
<H1><center>Complex ? Not so...</center></h1><p><br><br><br>
<p>Hi ! We meet again ! Ready to step to this chapter ?
This  time,  we're going to discuss complex data type. One of it  has  been
discussed,  record. As I promised in chapter 7, I would  certainly  explain
thoroughly all the weirdos.</p><p>
It all begins with <tt>type</tt>. It is not necessarily before <tt>var</tt> section,  but
if you want to use it as a new variable type, you MUST place it before  the
<tt>var</tt> section.</p><p>
Let's analogue this to the record we have discussed. Record simply declared
in <tt>type</tt> section and then we can freely use it as if it is a new  variable
type. You CAN define new variable types. Look at these examples :</p><pre>

type
   car        = (Honda, Toyota, Mercedes, Volvo, Ford);
   chesspiece = (King, Queen, Rook, Bishop, Knight, Pawn);
   str20      = string[20];
   byteptr    = ^byte;  { This kind of type will be discussed on lesson 2 }

var
  name  : str20;
  mycar : car;
  piece : array [1..32] of chesspiece;
  look  : byteptr;
</pre><p>
Let's  examine <tt>str20=string[20];</tt> Variable <tt>name</tt> is a <tt>str20</tt> type. It means
that <tt>name</tt> is a string variable of 20 characters. We knew that string can
be limited in length, but this type can't. You can use <tt>name</tt> as if it is a
normal string variable.</p><p>
Look at <tt>car</tt> and <tt>chesspiece</tt> types. Is it true ? Yes, it IS. Actually, they
are treated  as integers. These kind of variables are called <b>enumerated
types</b>. You can not assign integers to <tt>mycar</tt> and to <tt>piece</tt>. You assign their
rightful contents instead. Example :</p><hr><pre>

begin
  mycar:=1;            { Illegal }
  mycar:=Honda;        { Legal }
  if mycar=Volvo then  { Legal }
  begin
    clrscr; writeln('I have a volvo');
  end;
  piece[1]:=Pawn;      { Legal }
end.
</pre><hr><p>
Is  it true that compiler treat it as an integer ? Yes, it  is.  Therefore,
Pascal provides these commands : <tt>Ord, Pred, Succ</tt>. <tt>Ord</tt> is
used to know their integer contents. So :</p><pre>

   Ord(Honda) = 0, Ord(Toyota) = 1, Ord(Mercedes) = 2, Ord(Volvo) = 3,
   Ord(Ford)  = 4.

   Ord(King) = 0, Ord(Queen) = 1, Ord(Rook) = 2, Ord(Bishop) = 3, and so on
</pre><p><tt>
Ord</tt> can also be used to get the ASCII number of a character, example :</p><pre>
  n:=Ord('A');      { n = 65 }
</pre><p><tt>
Pred</tt> is used to decrement the enumerated types. It is similar to <tt>Dec</tt>.</p><pre>
  n:=Pred(Ford);    { n = Volvo }         Dec(x);  { x := x - 1 }
  n:=Pred(Bishop);  { n = Rook  }

  x:=Toyota;
  n:=Pred(x);       { n = Honda }
</pre><p><tt>
Succ</tt> is the complement : incrementing enumerated types. Similar to <tt>Inc</tt>.</p><pre>
  n:=Succ(Mercedes); { n = Volvo }        Inc(x);  { x := x + 1 }
  x:=Rook;
  n:=Succ(x);        { n = Bishop }
</pre><p>Easy, right ?? Now, let's look at this special feature :</p><pre>

type
  car = (Honda=1, Toyota, Fiat, Ford=5, Volvo=7, Mercedes);

</pre><p>And look at this :</p><pre>

  Ord(Honda)    = 1
  Ord(Toyota)   = 2
  Ord(Fiat)     = 3
  Ord(Ford)     = 5
  Ord(Volvo)    = 7
  Ord(Mercedes) = 8
</pre><p>
Got what I mean ? You can do that instead of creating constants for similar
behavior. This really makes codes more readable.</p><p>
Yup, this is a short chapter. So, I decided to give extras :</p>
<center><h2>Random number generator !</h2></center>
<p>It  is a very theoritic, but I'll make it short and fun. Stripped down  any
unnecessary theory.</p><p>
Actually,  computer  is a deterministic machine. What does that  mean ?  It
means  that it could not do everything itself. Instead, it only  does  what
the  program wants. So, there is NO random generator. People  simulate  it.
Therefore comes a jargon <b>pseudorandom</b> number.</p><p>
Wait a minute ... how could people simulate it ? Well, look at the
following idea :</p><ol>
<li>Von Neumann idea about squaring numbers and have some digits of them. It
   is obsolete and has many weakness. So, don't bother to discuss it.</li>
<li>Lehmer  idea about linear function. This is brilliant, but it has weakness.
   If we don't handle it carefully, it may fall into disrandomness.</li></ol>
<p>OK, look at Lehmer's idea. His idea can be formulated as this :</p>
<center><table border=1 width=30%><tr><td><center>
x<sub>n+1</sub> = ( a x<sub>n</sub> + c ) mod m</center></td></tr></table></center>
<p>Where a, c, and m are predefined constants; n is the index.</p>
<p>A pretty easy and straight-forward function to implement. It has several
weakness. Three of them are :</p><ol>
<li>Need a number to start with.</li>
<li>If we don't choose a, c, and m carefully, that function may fall
into disrandomness.</li>
<li>The coming number may be negative, as the formula neglects negative
numbers.</li></ol>
<h3>ad. 1.</h3><p>
The number to start with is called the <b>seed</b> (x<sub>0</sub>). If we choose the same
seed, it generates the same sequence of numbers. Suppose a, c, and m is  9,
5,  and 8 consequently. If we always take the seed (x<sub>0</sub>) 0, what we all get
is  the same sequence of : 5, 2, 7, 4, 7, ... The solution is that we  must
take different seed everytime. How ? Ask user to define the seed ? No way !
That's  why we must start every random generator with randomize.  Randomize
simply  select a seed. The seed is taken from timer ticks, which is  always
incremented each time. No one ever knows how much is it. The only thing for
sure is that it always change. Now try this program : (Program 1)</p><hr><pre>
uses crt;
var
  i : byte;
begin
  clrscr;
  randomize;
  for i:=1 to 20 do write(random(100):8);
  readkey;
end.
</pre><hr><p>
Run  it for several times and inspect the sequence of numbers displayed  on
screen carefully. It always change everytime you run it, right ? Now, remove
the <tt>randomize</tt>. Run it for several times and inspect the sequence of numbers.
You see that without randomize, every time you run the program,  you
will find the same sequence. This is the weakness. Got what I mean ?</p>
<h3>ad. 2.</h3>
<p>Well, random function in Borland Pascal, or many other programs seem to
have chosen a, c, and m quite good so that the generated numbers seemed
random. Well, the implementation in the program is :<br>
(Program 2)</p><hr><pre>
var
  seed : longint;

procedure myrandomize;
begin
  seed:=meml[0040:006c];  { This how to get timer ticks }
end;

function myrandom (howbig:word) : word;
begin
  seed:=(seed*a+c) mod m;
  myrandom:=seed mod howbig;
end;
</pre><hr><p>
Add the <tt>myrandomize</tt> and <tt>myrandom</tt> routines into program 1. Then, replace all
<tt>random</tt> and <tt>randomize</tt> with <tt>myrandom</tt> and <tt>myrandomize</tt>. Think about nice number
for a, c, and m. Then run it for several times. See the problem ?</p>
<p>OK, the common problems are :</p><ol>
<li>Non random sequence, like this :<br>
   0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, ...<br>
   2, 4, 6, 8, 0, 2, 4, 6, 8, 0, ...</li>
<li>We've got a repeating short sequence, like this :<br>
   3, 4, 7, 3, 4, 7, 3, 4, 7, ...<br>
   4, 5, 4, 5, 4, 5, ...<br>
   3, 9, 2, 6, 7, 3, 9, 2, 6, 7, ...<br>
   No ! It's not a random generator ! It's a sequence generator !</li>
<li>Good sequence, but ended with the same short sequence, or even the same
   number, like this :<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -