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

📄 pasl1011.html

📁 This is programing tutorial for people who wants to know programing in PASCAL.Pascal might be not th
💻 HTML
字号:
<HTML><HEAD><LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 12</Title></head>
<body background="../tile01.jpg">
<H1><center>Unit 1 Ready !</center></h1><p><br><br><br><br>
<p>Hi ! Nice to meet you ! This time we will discuss about units. What is
units anyway ? It's like having a library of commands, like <tt>crt</tt> unit, that
we often use so long. Now, we're gonna make one.</p><p>
What's so different with units ? Well, units have structures like this :</p><hr><pre>
{This code must be saved with name WHAT.PAS}
unit what;

interface
uses ....
var
  ...    { Visible global variables }

{ Procedures &amp; functions definitions }
procedure myproc;
function  myfunc: word;
:
:

implementation
var
  ...    { Invisible global variables }

procedure myproc;
begin
  :  { the routines }
  :
end;

function myfunc:word;
begin
  :  { the routines }
  :
end;

:
:  { other procedure's / function's routines }
:

begin
  :   { Initialization code }
  :
end.
</pre><hr><p>
That was the structure. The <tt>interface</tt> part contains definitions of procedures
or functions and the variables. All definitions ( procedures, functions,
and variables ) in this part are visible to the user of your unit.
It means that the user who put your unit in their <tt>uses</tt> clause can  invoke
the declared procedure/function and can easily change the variables.</p><p>
The <tt>implementation</tt> part contains the contents of all procedures and variables
which are either visible or not. Variable declaration in this part is
NOT visible. Means that user cannot modify them. Only the procedure or function
inside that unit can.</p>
<p><b>Attention : Unit name must comply to the filename</b>.<br>
Look at this example :</p><hr><pre>
{ This unit must be stored in MYUNIT.PAS }
unit myunit;
interface
uses crt;

var
  x   : byte;
  y,z : integer;

procedure a;
procedure b;
function  c:byte;

implementation
var
  p,q,r : shortint;

procedure a;
begin
  :
  :
end;

procedure d;
begin
  :
  :
end;

procedure b;
begin
  :
  :
end;

function c: byte;
begin
  :
  :
end;

procedure e;
begin
  :
  :
end;

begin
  :  { Initialization code }
  :
end.
</pre><hr><p>Suppose I make a program using the unit above :</p><hr><pre>
uses myunit;

var
  n : byte;
begin
  a;      { legal }
  b;      { legal }
  n:=c;   { legal }
  x:=1;   { legal }
  y:=-1;  { legal }
  z:=14;  { legal }
  
  d;      { illegal, because it is invisible }
  e;      { illegal, because it is invisible }
  p:=-1;  { illegal, because it is invisible }
end.
</pre><hr><p>
I can call procedure a, b, and function c, within my program, but not procedure
d, and e. Access to variable x, y, and z is legal, but not to p, q, and r.</p><p>
Procedure d and e can only be invoked INSIDE that unit, for example procedure b
call procedure d. The invokability rules are just the same as normal
program, i.e. d can call a, but a cannot call d.</p><p>
Easy,  right ?  Now, what's all this for ? If you have made  procedures  or
functions and you think that you will use them a lot in the future, you may
want this placed in a unit so that you can re-use them.</p><p>
Mind this : This is a VERY important feature and you shall heed it.</p><p>
Start thinking of procedures or functions that you often use, then place it
in  a unit. Think seriously, because I will ask you that in the quiz.  Look
at this unit (<tt>MYUNIT.PAS</tt>). It is a simple unit, but works !</p><hr><pre>
unit myunit;
interface
uses crt;

procedure clearbkgr(color : byte);
procedure makewindow(x1, y1, x2, y2, color : byte);
procedure writexy(x, y : byte ; mess : string);
function  ucase(st:string):string;

implementation

procedure clearbkgr(color : byte);
begin
  textbackground(color);
  clrscr;
end;

{ Invisible to unit user, this is internal function }
function fill(s : string; count : byte; what: char):string;
begin
  fillchar(s,count+1,what);
  s[0]:=chr(count);
  fill:=s;
end;

procedure makewindow(x1, y1, x2, y2, color : byte);
var
  s  : string;
  i  : byte;
begin
  textattr:=color;
  s:=fill(s,x2-x1-1,#205);
  gotoxy(x1,y1); write(#201+s+#187);
  gotoxy(x1,y2); write(#200+s+#188);
  s:=fill(s,x2-x1-1,' ');
  s:=#186+s+#186;
  for i:=1 to y2-y1-1 do
  begin
    gotoxy(x1,y1+i); write(s);
  end;
end;

procedure writexy(x, y : byte; mess : string);
begin
  gotoxy(x,y); write(mess);
end;

function ucase(st:string):string;
var
  i : byte;
begin
  for i:=1 to length(st) do st[i]:=upcase(st[i]);
  ucase:=st;
end;

begin
  { Leave this empty if you don't have any initialization code }
end.
</pre><hr><p>
What  is the initialization codes ? If you think that you need to  do  routines
before the unit is ready to use, you may do it in the main <tt>begin...end</tt>
block. It's similar to preparation code, that is to prepare your unit.
For  example : You feel that some variables need to be set, do it  in  that
section. If you need some setup code need to be established, do that too in
the same way. Initialization codes is done before the <tt>begin...end</tt> block  of
the main program that use your unit. Suppose the program is like this :</p><pre>

:
:
uses unit_a, unit_b, unit_c;
:
:
:
begin
  :
  :
end.
</pre><p>
Initialization code in <tt>unit_a</tt> is done first, then <tt>unit_b</tt>, then <tt>unit_c</tt>, then
the main <tt>begin...end</tt>.</p>
<p>This is quite a short lesson, and this time <b>EXTRA</b> !</p>
<p>Converting numbers into bits, vice versa. Easy ! Divide the number by 2 :</p><pre>

                53
            2 ------ 1      (53 mod 2 = 1)
                26          (53 div 2 = 26)
            2 ------ 0      (26 mod 2 = 0)
                13          (26 div 2 = 13)
            2 ------ 1      (13 mod 2 = 1)
                 6          (13 div 2 = 6)
            2 ------ 0      ( 6 mod 2 = 0)
                 3          ( 6 div 2 = 1)
            2 ------ 1      ( 3 mod 2 = 1)
                 1          ( 3 div 2 = 1)
</pre><p>
Stop here, since 1 is less than 2. So the binary form of 53 is 110101. Look
at this example :</p><pre>
               100    /|\     41             37
            2 ------ 0 |   2 ---- 1       2 ---- 1
                50     |      20             18
            2 ------ 0 |   2 ---- 0       2 ---- 0
                25     |      10              9
            2 ------ 1 |   2 ---- 0       2 ---- 1
                12     |       5              4
            2 ------ 0 |   2 ---- 1       2 ---- 0
                 6     |       2              2
            2 ------ 0 |   2 ---- 0       2 ---- 0
                 3     |       1              1
            2 ------ 1 |
                 1     | Direction to read
</pre><p>So, 100 is 1100100 in binary, 41 is 101001, and 37 is 100101. Easy, no ?
Now, how to convert binary digits (bits) into decimals ? Suppose we have :</p><ul>
<li>1010010 = 1 x 2<sup>6</sup> + 1 x 2<sup>4</sup> + 1 x 2<sup>1</sup> = 64+16+2 = 82</li>
<li>0100001 = 1 x 2<sup>5</sup> + 1 x 2<sup>0</sup> = 32+1 = 33</li>
<li>0111100 = 1 x 2<sup>5</sup> + 1 x 2<sup>4</sup> + 1 x 2<sup>3</sup> + 1 x 2<sup>2</sup> = 32+16+8+4 = 60</li>
</ul><p>Shorter one :</p><ul>
<li>1001 = 1 x 2<sup>3</sup> + 0 x 2<sup>2</sup> + 0 x 2<sup>1</sup> + 1 x 2<sup>0</sup> = 8+0+0+1 = 9</li>
<li>1011 = 1 x 2<sup>3</sup> + 0 x 2<sup>2</sup> + 1 x 2<sup>1</sup> + 1 x 2<sup>0</sup> = 8+0+2+1 = 11</li>
</ul><p>
Easy, right ? If you want to convert numbers to hexadecimal, it is just similar, but change the
divisor 2 into 16, like this :</p><pre>

      100            41            53           37           47
  16 ----- 4     16 ---- 9    16 ------ 5   16 ---- 5    16 ---- 15
        6             2             3            2            2
</pre><p>
Means that 100 = 64 hex, 41 = 29 hex, 53 = 35 hex, 37 = 25 hex, 47 = 2F hex
Any number exceed 9 is decoded like this :</p><pre>
   10   into   A
   11   into   B          61             44            58
   12   into   C     16 ------ 13   16 ------ 12  16 ------ 10
   13   into   D           3              2             3
   14   into   E     61 = 3D hex     44 = 2C hex    58 = 3A hex
   15   into   F
</pre><p>Converting back to decimal is the same :</p><ul>
<li>3D Hex =  3 x 16<sup>1</sup> + 13 x 16<sup>0</sup> = 48 + 13 = 61</li>
<li>67 Hex =  6 x 16<sup>1</sup> +  7 x 16<sup>0</sup> = 96 +  7 = 103</li>
<li>103 Hex =  1 x 16<sup>2</sup> +  0 x 16<sup>1</sup> +  3 x 16<sup>0</sup> = 256 + 0 + 3 = 259</li>
</ul>
<p>And so on.<br>
That's all folks for this time ! Good bye ! Don't hesitate to mail
<a href="../faq.html">me</a> !</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="pasq1011.html">To the quiz</a><br>
<A HREF="pasl1010.html">Back to Chapter 11</A> about sets<BR>
<A HREF="pasl1012.html">To Chapter 13</A> about text files<BR>
<A HREF="../mylink.html">My page of programming link</A><BR>
<A HREF="../faq.html">Contact me</A>
<hr><P Class="cpy">By : Roby Joehanes, &copy; 1997, 2000</P>
</BODY></HTML>

⌨️ 快捷键说明

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