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

📄 pascal.txt

📁 基本语法
💻 TXT
📖 第 1 页 / 共 3 页
字号:
Pascal语法小全  
OBJECT PASCAL PROGRAMING (WRITED BY C.Y.C ATTENTION SYSTEM DEVELOPMENT CO,.)
1.标记(TOKEN)
  1.1 特别符号(Symbols)
      字母(Letters) : A..Z , a..z
      数字(Digits) : 0..9
      十六进位数字(Hex Digits) : 0..9,A..F(OR a..f)
      空白(Blank) : ASCII 32
      单字元符号:+-*/=<>[].,():;^@{}$#
      多字元符号:<=,>=,:=,..,(*,*),(.,.)

  1.2 识别字(Identifiers)
      表示:常数,型态,变数,程序,函数,程式单元,程式,栏位....
      长度:63位内有效,不分大小写
      字首:字母,_
      识别字不得重复,若有重复必需采 限定识别字 : Unit1.IdentName

  1.3 标笺(Label) : 0..9999 or 识别字

  1.4 字元字串
      'ATTN' ----------- ATTN
      'You''ll see' ---- Yoy'll see
      '' --------------- 空字串
      ' ' -------------- 空白字元
      'Line 1'#13#10'Line 2' ------ Line 1
                                    Line 2
  1.5 注释
      { xxxxxxx }

      { xxxxxxxx
         xxxxxx
         xxxxx }

      // xxxxxxxx

2.常数宣告 (使用标记 = )
  2.1一般常数宣告
    CONST
      Min = 0;
      Max = 100;
      Center = ( Max - Min ) Div 2;
      Blank = Chr(32);
      NumChr = Ord('Z') - Ord('A') + 1;
      ErrMsg = 'Out Of Rang';
      ErrDtl = 'Out Of Rang' + ':Item 10';
      Numeric = [ '0'..'9' ];
      Alpha = [ 'A'..'Z','a'..'z'];
      AlphNum = Alpha + Numeric;
  2.1型态常数(Typed constant)宣告
     CONST
       MaxInt : Integer = 9999;
       FixReal : Real = -0.12;
       ListStr : String[4] = 'This';
       AA : Pchar = 'abcedf';
       Dim : Array[0..1,0..1,0..1] of Integer = (((0,1),(2,3)),((4,5),(6,7)));
            { Dim(0,0,0) = 0  Dim(0,0,1) = 1
             Dim(0,1,0) = 2  Dim(0,1,1) = 3
             Dim(1,0,0) = 4  Dim(1,0,1) = 5
             Dim(1,1,0) = 6  Dim(1,1,1) = 7 }
       --------------------------------
       TYPE
         Trec = record
            fl1,fl2 : Integer;
            end;

       CONST
         IntRec : Trec = ( fl1:1;fl2:2);
        ------------------------------------------


3.型态宣告 (使用标记 = ) : 当宣告一个变数时,必须指明其型态
  3.1 例子(1)
    TYPE
      Trang = Integer;
      TNumber = Integer;
      TColor = ( Red , Green , Blue );
      TCharVal = Ord('A')..Ord('Z');
      TtestIndex = 1..100;
      TtestValue = -99..99;
      TtestList = Array[TtestIndex] of Ttestvalue;
      ptestList = ^TtestList;     =>指标型态
      Tdate = Class
         Year : Integer;
         Month : 1..12;
         Day : 1..31;
         Procedure SetDate(D,M,Y:Integer);
         Function ShowDate : String;
      end;
      TMeasureList = Array[1..50] of TMeasuredate;
      Tname = String[80];         =>定长度
      Tsex = (Male , Female);     =>0 1
      PpersonData = record
         Name , FirstName : Tname;
         Age : Integer;
         Married : Boolean;
         TFather,TChild,TSibling : PPersonData;
         Case s: Tsex of          =>0和1之间
              Maie : ( Bearded : Boolean );
              Female : (Pregnant : Boolean );
         End;
      TPersonBuf = Array[0..Size(TPsersonData) - 1 ] of Byte;
      TPeople = File Of TPersonData;      =>定type


 3.2 简单型态
      3.2.1序数型态
           (1)整数型态:
                基础型态(Fundmental)
                  Shortint    -128..127                  8-bit
                  Smallint    -32768..32767              16-bit
                  Longint    -2147483648..2147483647    32-bit
                  Byte      0..255                     8-bit
                  Word     0.65535                   16-bit
                通用型态(Generic)
                  Integer     -2147483648..2147483647    32-bit
                  Cardinal    0..2147483647             32-bit
                    若16bit
                  Integer     -32768..32767             16-bit
                  Cardinal    0.65535                  16-bit

           (2)字元型态
                基础型态(Fundmental)
                  AnsiChar      ASCII                    1-Byt
                  WideChar      Unicode                  2-Byt
                通用型态(Generic)
                  Char          ASCII                    1-Byt

           (3)列举型态(Enumerated Type)
                ==============================================
                TColor = ( Red , Green , Blue );
                ==============================================

           (4)逻辑型态(Boolean Type)
                基础型态(Fundmental)
                ByteBool       0..1                      1-Byt
                WordBool       0..1                      2-Byt
                LongBool       0..1                      4-Byt
                通用型态(Generic)
                Boolean                                  1-Byt
                ==============================================
                Married : Boolean;

                False < True
                Ord(False) = 0
                Ord(True) = 1
                Succ(False)=True
                Pred(True)=False
                ==============================================

           (5)子集型态(Subrang type)
                ==============================================
                TtestIndex = 1..100;
                ==============================================
      3.2.2实数型态
           型态              有效位数           位元组大小
           Real               11-12              6
           Single             7-8                4
           Douible            15-16              8
           ExTended           10-20              10
           Comp               19-20              8
           Currency           19-20              8
           ===================================================
           例子 Real   6byt    共48 bit

               1-1    2-40   41-48
                s      f       e
               有效值 v
               if ( e > 0 ) and  ( e <= 255 ) then
                  v = (-1)**s * 2 ** (e - 129) * (1.f)
               else if e = 0 then v = 0;
           ===================================================

  3.3 字串型态
       3.3.1 基础型态(Fundmental)
             (1) ShortString  短字串 1-255 (又称pascal string)
             (2) AnsiString   长字串 1-2G
             (3) PChr         Null-Terminated string

       3.3.2 通用型态(Generic)
             String 32位元时 等於 AnsiString
                    16位元时 等於 ShortString
           ===================================================
           (1) 字串为一以零为基底的字元阵列(Zero-base character array)
               -----------------------
               TYPE
                 TStringBuf = Array[0..79] of Char;
               const
                 StringBuf : TStringBuf = 'This is test';
               -----------------------

           (2) var s : ShortString
               Length(s) = Ord(s[0])
           (3) 长字串以4-byt指标,指到动态配置之记忆体,没有[0]之长度数值
               一个动态记忆体配置之长字串会自动以一空字元(null Character)结束
           (4) PChr 以字元阵列来储存,结尾以(null Character)结束,使用指标来管理
           (5) PChr型态与String型态在指定叙述上是相容
               例:
                 -----------------------------
                   var p:pchr;

                   begin
                     p := 'this is test';
                   end;
                 -----------------------------
                 Procedure PrintStr(Str:Pchr);

                 呼叫时可用
                    PrintStr('This is test');
                 -----------------------------

  3.4 结构型态(Strictured type)
       3.4.1 阵列型态(Array type)
             TDim1 = Array[0..79] of Char;
             TDim2 = Array[1..3,1..10] of Char;

             VAR
                Dim1 : Tdim1;
                I : Integer;
             begin

                for I := Low(Dim1) to High(Dim1) do
                    Dim1[I] := #32;


             end;
           ===================================================
       3.4.2 记录型态(Record Type)
           TYPE
             TdateRec = Record
                Year : Integer;
                Month : 1..12;
                Day : 1..31;
             end;

           何谓变动栏位 ?

       3.4.3 集合型态(Set Type)
            TYPE
               Tmenber = ( Lee , White , Ston );
               Tmenbers = Set Of Tmenber;


            var Members : Tmenbers;

            begin
               Menbers := [ White , Ston ];

            end;

            集合型态不能超过256个可能值

       3.4.4 档案型态(File Type)
           (1)记录型态档案
              TYPE
                Temployee = record
                      name : string[10];
                      address : string[50];
                end;
                TemployeeFile = File Of Temployee;

              var
                EmployeeFile : TemployeeFile;

                同 EmployeeFile : File Of Temployee;

           (2)文件型态档案

                TMyTextFile = TextFile;
                var
                 MyTextFile : TMyTextFile;
                 or
                 MyTextFile : TextFile;

                 ReadText : String;

              -----------------------------
              AssignFile( MyTextFile , 'c:\MyFile.txt');
              Reset( MyTextFile );
              While Not Eof( MyTextFile ) do
                 begin
                   Readln( MyTextFile , ReadText );


                 end;
              CloseFile( MyTextFile );

              注:Rewrite(MyTextFile);  开启唯写
                 Writeln(MyTextFile , ReadText);

           (3)其它型态档案
              TBook = File Of Char;
              TnumFile = File Of Integer;

        3.4.5 类别型态(Class Type)

         TDBText = class(TCustomLabel)
         private
           FDataLink: TFieldDataLink;
           procedure DataChange(Sender: TObject);
           function GetDataField: string;
           function GetDataSource: TDataSource;
           function GetField: TField;
           function GetFieldText: string;
           procedure SetDataField(const Value: string);
           procedure SetDataSource(Value: TDataSource);
           procedure CMGetDataLink(var Message: TMessage); message CM_GETDATALINK;
         protected
           function GetLabelText: string; override;
           procedure Notification(AComponent: TComponent;
             Operation: TOperation); override;
           procedure SetAutoSize(Value: Boolean); override;
         public
           constructor Create(AOwner: TComponent); override;
           destructor Destroy; override;
           property Field: TField read GetField;
         published
           property Align;
           property Alignment;
           property AutoSize default False;
           property Color;
           property DataField: string read GetDataField write SetDataField;
           property DataSource: TDataSource read GetDataSource write SetDataSource;
           property DragCursor;
           property DragMode;
           property Enabled;
           property Font;
           property ParentColor;
           property ParentFont;
           property ParentShowHint;

⌨️ 快捷键说明

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