Pascal根底语法(一)

序言

有时无聊,翻看pascal,奈何看完即忘.于是记录之
pascal据说将被NOIP停用,我没有去验证.管他呢,原本只是好奇

环境

用的收费的编译器 fpc [https://www.freepascal.org/]

编辑器轻易找个本人喜爱的就行, IDE 能够选 lazarus[https://www.lazarus-ide.org/],跨平台

pascal版本有更迭,反对的方言很多,基于目前的最佳实际,所有代码里应用 ${mode objfpc } {$H+}{$J-}{$I+}{$R+}
{$H+} 开启AnsiString

{$J-} const常量不可批改

{$I+} i/o谬误检测

{$R+} 开启越界查看
注: {$mode objfpc}也可改为{$mode delphi}
此编译指令须要写在uses之前
pascal不辨别大小写,习惯使然,则采纳和C#相似的编码标准

正文

pascal的正文有多种可选,前两种反对多行,最初一行是单行正文,pascal的正文容许嵌套,然而tp和delphi不反对,为了可移植,倡议正文不要嵌套

(* some comments *){ some comments }// some comments

关键字(保留字)

语言内置的具备非凡含意的标识符,关键字列表如下

absolute  and  array  asm  begin  case  const  constructor  destructor  div  do  downto  else  end  file  for  function  goto  if  implementation  in  inherited  inline  interface  label  mod  nil  not  object  of  operator  or  packed  procedure  program  record  reintroduce  repeat  self  set  shl  shr  string  then  to  type  unit  until  uses  var  while  with  xor  as  class  dispinterface  except  exports  finalization  finally  initialization  inline  is  library  on  out  packed  property  raise  resourcestring  threadvar  try  

失常状况写不能定义和关键字一样的标识符.有时必须为之(比方调用了c语言开发的dll)则能够在后面加上&符号

var      &var : integer;       begin      &var:=1;      Writeln(&var);    end.

标识符

标识符能够应用字母,数字,下划线(_), 不能用数字结尾, 长度1-127

数据类型概览

以下分类参考freepascal的文档

根本类型

类型sizeof符号位
byte1
word2
longword4
Cardinal4
qword8
shortint1
smallint2
longint4
int648
integer2 or 4

整型: byte,shortint,smallint,word,integer,longint,int64...
数值默认是十进制,如果16进制应用$前缀, 8进制应用&前缀,2进制应用%前缀. TP(Turbo Pascal)和delphi不反对 8进制和2进制的前缀模式

浮点型: real,single,double,extended,comp,currency
布尔型:boolean

字符串类型

字符串: shortstring,ansistring,widestring,unicodestring,pchar
字符串应用单引号, 不反对相似C语言的\r\n\t等模式的本义.而是应用相似上面的办法
应用LINEENDING常量能够跨平台的换行.

var a:string;begina := 'contain tab '#9' example ';a:='contain new line '#13#10' example';a:='contain new line '+LINEENDING+' example';end.

构造类型

子界,枚举,数组,汇合,类,文件
具体应用见后续

指针

var i:integer;p: ^integer; //定义

p:=@i;i=p^; //取地址,取值

看起来不如c语言的指针操作晦涩,应该是不激励应用指针吧

根本构造

program {程序名}uses {逗号分隔应用的Unit}const {常量定义}var {全局变量定义}//函数定义function foo();boolean;{ 局部变量定义 }begin...end;//过程定义procedure bar();{ 局部变量定义 }begin...end;begin { 主程序开始}...end. { 主程序完结 }

这个可执行程序的构造,dll的创立见后续
上面是Unit的构造,大同小异

unit {Unit名称};interface//函数申明function foo();boolean;//过程申明procedure bar();implementation//函数实现function foo();boolean{ 局部变量定义 }beginend;//过程实现procedure bar();{ 局部变量定义 }beginend;end.

var a:integer; //变量定义形式

a:=10;//赋值形式

函数和过程

函数有返回值,过程没有.其它都一样

function 函数名(变量名1:变量类型;变量名2:变量类型...):返回类型;var    变量名3:变量类型;    变量名4:变量类型;begin    函数定义    result := 返回值     end;    

返回值也能够应用 函数名:= 返回值, 集体喜爱用result:=返回值,另外result能够用于递归调用

function 过程名(变量名1:变量类型;变量名2:变量类型...);var    变量名3:变量类型;    变量名4:变量类型;begin    过程定义end;    

循环构造

while-do 循环

while (condition) do S;//for examplewhile i<10 do begini := i-1;writeln(i);end;

for 循环

for < variable-name > := < initial_value > to [down to] < final_value > do    S;//for examplefor i:=1 to 10 do writeln(i);for i:=10 downto 1 do writeln(i);

until循环

repeat S until condition;// for examplerepeat    sum := sum+ i;    i := i-1until i=0;

break,continue用于跳出循环这个和C语言都一样
当然还有goto语句,集体简直不必

分支构造

if condition then S;if condition then S1 else S2;

留神没有else if

case (expression) of   L1 : S1;   L2: S2;   ...   ...   Ln: Sn;end;case (expression) of   L1 : S1;   L2,L3 : S2;   ...   ...   Ln: Sn;else   Sm;end;

代码示例

//仅作语法演示,无具体需要
demo01.pas

Program demo01;${mode objfpc } {$H+}{$J-}{$I+}{$R+}Uses SysUtils,Common;Const PI = 3.14;Function FactTail(n,a:integer): longint;Begin  If n < 0 Then result := 0  Else    Begin      If n<=2 Then result := a      Else        result := FactTail(n-1,n*a);    EndEnd;Function Sum(n:integer): integer;Var   s: integer;  i: integer;Begin  s := 0;  For i:=1 To n Do    s := s+i;  result := s;End;Function Mean(n:integer): real;Var   s: integer;  i:integer;Begin  s := 0;  i := n;  Repeat    s := s + n;    i := i-1;  Until i=0;  result := s*1.0/n;End;Var a,b,c: integer;Begin  a := 10;  b := 20;  c := 10;  Swap(a,b);  writeln(FactTail(5,1), ', ',Sum(c),', ',Mean(c));End.

common.pas

Unit common;${mode objfpc } {$H+}{$J-}{$I+}{$R+}InterfaceFunction RectangleArea(l,w :real ): real;Function CircleArea(r :real ): real;Function TriangleArea(a,b,c :real ): real;Procedure Swap(Var a:integer;Var b:integer);ImplementationProcedure Swap(Var a:integer;Var b:integer);Var   temp: integer;Begin  temp := a;  a := b;  b := temp;End;Function RectangleArea(l,w :real ): real;Begin  result := l*w;End;Function CircleArea(r :real ): real;Const PI =  3.14;Begin  result := PI*r*r;End;Function TriangleArea(a,b,c :real ): real;Var s : real;Begin  s := (a+b+c)/2.0;  result := sqrt(s*(s-a)*(s-b)*(s-c));End;End.