- Pascal 教程
- Pascal - 首页
- Pascal - 概述
- Pascal - 环境设置
- Pascal - 程序结构
- Pascal - 基本语法
- Pascal - 数据类型
- Pascal - 变量类型
- Pascal - 常量
- Pascal - 运算符
- Pascal - 决策
- Pascal - 循环
- Pascal - 函数
- Pascal - 过程
- Pascal - 变量作用域
- Pascal - 字符串
- Pascal - 布尔值
- Pascal - 数组
- Pascal - 指针
- Pascal - 记录
- Pascal - 变体
- Pascal - 集合
- Pascal - 文件处理
- Pascal - 内存
- Pascal - 单元
- Pascal - 日期和时间
- Pascal - 对象
- Pascal - 类
- Pascal 有用资源
- Pascal - 快速指南
- Pascal - 有用资源
- Pascal - 讨论
Pascal - 单元
一个 Pascal 程序可以由称为单元的模块组成。一个单元可能包含一些代码块,而代码块又由变量和类型声明、语句、过程等组成。Pascal 中有许多内置单元,并且 Pascal 允许程序员定义和编写他们自己的单元,以便以后在各种程序中使用。
使用内置单元
内置单元和用户定义单元都通过 uses 子句包含在程序中。我们已经在Pascal - 变体教程中使用了 variants 单元。本教程解释了如何创建和包含用户定义的单元。但是,让我们首先看看如何在程序中包含内置单元crt:
program myprog; uses crt;
以下示例说明了如何使用crt单元:
Program Calculate_Area (input, output);
uses crt;
var
a, b, c, s, area: real;
begin
textbackground(white); (* gives a white background *)
clrscr; (*clears the screen *)
textcolor(green); (* text color is green *)
gotoxy(30, 4); (* takes the pointer to the 4th line and 30th column)
writeln('This program calculates area of a triangle:');
writeln('Area = area = sqrt(s(s-a)(s-b)(s-c))');
writeln('S stands for semi-perimeter');
writeln('a, b, c are sides of the triangle');
writeln('Press any key when you are ready');
readkey;
clrscr;
gotoxy(20,3);
write('Enter a: ');
readln(a);
gotoxy(20,5);
write('Enter b:');
readln(b);
gotoxy(20, 7);
write('Enter c: ');
readln(c);
s := (a + b + c)/2.0;
area := sqrt(s * (s - a)*(s-b)*(s-c));
gotoxy(20, 9);
writeln('Area: ',area:10:3);
readkey;
end.
这与我们在 Pascal 教程开始时使用的程序相同,编译并运行它以了解更改的效果。
创建和使用 Pascal 单元
要创建一个单元,您需要编写要存储在其中的模块或子程序,并将其保存在扩展名为.pas的文件中。此文件的首行应以关键字 unit 开头,后跟单元的名称。例如:
unit calculateArea;
以下是创建 Pascal 单元的三个重要步骤:
文件名称和单元名称应完全相同。因此,我们的单元calculateArea将保存在名为calculateArea.pas的文件中。
下一行应包含单个关键字interface。在此行之后,您将编写所有将在该单元中出现的函数和过程的声明。
在函数声明之后,写下单词implementation,它也是一个关键字。在包含关键字 implementation 的行之后,提供所有子程序的定义。
以下程序创建名为 calculateArea 的单元:
unit CalculateArea; interface function RectangleArea( length, width: real): real; function CircleArea(radius: real) : real; function TriangleArea( side1, side2, side3: real): real; implementation function RectangleArea( length, width: real): real; begin RectangleArea := length * width; end; function CircleArea(radius: real) : real; const PI = 3.14159; begin CircleArea := PI * radius * radius; end; function TriangleArea( side1, side2, side3: real): real; var s, area: real; begin s := (side1 + side2 + side3)/2.0; area := sqrt(s * (s - side1)*(s-side2)*(s-side3)); TriangleArea := area; end; end.
接下来,让我们编写一个简单的程序来使用我们上面定义的单元:
program AreaCalculation;
uses CalculateArea,crt;
var
l, w, r, a, b, c, area: real;
begin
clrscr;
l := 5.4;
w := 4.7;
area := RectangleArea(l, w);
writeln('Area of Rectangle 5.4 x 4.7 is: ', area:7:3);
r:= 7.0;
area:= CircleArea(r);
writeln('Area of Circle with radius 7.0 is: ', area:7:3);
a := 3.0;
b:= 4.0;
c:= 5.0;
area:= TriangleArea(a, b, c);
writeln('Area of Triangle 3.0 by 4.0 by 5.0 is: ', area:7:3);
end.
当编译并执行上述代码时,它会产生以下结果:
Area of Rectangle 5.4 x 4.7 is: 25.380 Area of Circle with radius 7.0 is: 153.938 Area of Triangle 3.0 by 4.0 by 5.0 is: 6.000
广告