Pascal语法小全_第1页
Pascal语法小全_第2页
Pascal语法小全_第3页
Pascal语法小全_第4页
Pascal语法小全_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Pascal语法小全 OBJECT PASCAL PROGRAMING (WRITED BY C.Y.C ATTENTION SYSTEM DEVELOPMENT CO,.)1.标记(TOKEN)  1.1 特别符号(Symbols)      字母(Letters) : A.Z , a.z      数字(Digit

2、s) : 0.9      十六进位数字(Hex Digits) : 0.9,A.F(OR a.f)      空白(Blank) : ASCII 32      单字元符号:+-*/=.,():;$#      多字元符号:=,=,:=,.,(*,*),(.,.)&#

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

4、#160;标笺(Label) : 0.9999 or 识别字  1.4 字元字串      'ATTN' - ATTN      'You''ll see' - Yoy'll see      '' -

5、60;空字串      ' ' - 空白字元      'Line 1'#13#10'Line 2' - Line 1                   

6、60;                Line 2  1.5 注释       xxxxxxx        xxxxxxxx         xxxxxx&#

7、160;        xxxxx       / xxxxxxxx2.常数宣告 (使用标记 = )  2.1一般常数宣告    CONST      Min = 0;      Max = 

8、;100;      Center = ( Max - Min ) Div 2;      Blank = Chr(32);      NumChr = Ord('Z') - Ord('A') + 1;  &

9、#160;   ErrMsg = 'Out Of Rang'      ErrDtl = 'Out Of Rang' + ':Item 10'      Numeric =  '0'.'9'   &#

10、160;   Alpha =  'A'.'Z','a'.'z'      AlphNum = Alpha + Numeric;  2.1型态常数(Typed constant)宣告     CONST       MaxInt

11、 : Integer = 9999;       FixReal : Real = -0.12;       ListStr : String4 = 'This'       AA : Pchar = 'a

12、bcedf'       Dim : Array0.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   &#

13、160;         Dim(0,1,0) = 2  Dim(0,1,1) = 3             Dim(1,0,0) = 4  Dim(1,0,1) = 5       &#

14、160;     Dim(1,1,0) = 6  Dim(1,1,1) = 7        -       TYPE         Trec = record     

15、0;      fl1,fl2 : Integer;            end;       CONST         IntRec : Trec = ( fl1:1;fl2:2);&

16、#160;       -3.型态宣告 (使用标记 = ) : 当宣告一个变数时,必须指明其型态  3.1 例子(1)    TYPE      Trang = Integer;      TNumber = Integer;  

17、    TColor = ( Red , Green , Blue );      TCharVal = Ord('A').Ord('Z');      TtestIndex = 1.100;      TtestValue&#

18、160;= -99.99;      TtestList = ArrayTtestIndex of Ttestvalue;      ptestList = TtestList;     =指标型态      Tdate = Class   

19、0;     Year : Integer;         Month : 1.12;         Day : 1.31;         Procedure SetDate(D,M,Y:Integer)

20、;         Function ShowDate : String;      end;      TMeasureList = Array1.50 of TMeasuredate;      Tname = String80;

21、60;        =定长度      Tsex = (Male , Female);     =0 1      PpersonData = record         Name

22、60;, FirstName : Tname;         Age : Integer;         Married : Boolean;         TFather,TChild,TSibling : PPersonData;

23、         Case s: Tsex of          =0和1之间              Maie : ( Bearded : Boolean ); &

24、#160;            Female : (Pregnant : Boolean );         End;      TPersonBuf = Array0.Size(TPsersonData) - 1  

25、;of Byte;      TPeople = File Of TPersonData;      =定type 3.2 简单型态      3.2.1序数型态           (1)整数型态:   &#

26、160;            基础型态(Fundmental)                  Shortint    -128.127          

27、        8-bit                  Smallint    -32768.32767              16-bit 

28、;                 Longint    -2147483648.2147483647    32-bit                  Byte&#

29、160;     0.255                     8-bit                  Word   

30、  0.65535                   16-bit                通用型态(Generic)        

31、0;         Integer     -2147483648.2147483647    32-bit                  Cardinal    0.2147483647

32、60;            32-bit                    若16bit               

33、60;  Integer     -32768.32767             16-bit                  Cardinal    0.65535 

34、;                 16-bit           (2)字元型态                基础型态(Fundmental) 

35、                 AnsiChar      ASCII                    1-Byt    

36、              WideChar      Unicode                  2-Byt        

37、0;       通用型态(Generic)                  Char          ASCII           

38、         1-Byt           (3)列举型态(Enumerated Type)                =        &

39、#160;       TColor = ( Red , Green , Blue );                =           (4)逻辑型态(Boolean

40、60;Type)                基础型态(Fundmental)                ByteBool       0.1     &

41、#160;                1-Byt                WordBool       0.1       

42、60;              2-Byt                LongBool       0.1          

43、;            4-Byt                通用型态(Generic)                Boolean 

44、0;                                1-Byt                =

45、0;               Married : Boolean;                False  True          

46、60;     Ord(False) = 0                Ord(True) = 1                Succ(False)=True  

47、              Pred(True)=False                =           (5)子集型态(Subrang type)  

48、;              =                TtestIndex = 1.100;               

49、; =      3.2.2实数型态           型态              有效位数           位元组大小   &#

50、160;       Real               11-12              6           Sing

51、le             7-8                4           Douible        

52、;    15-16              8           ExTended           10-20      

53、0;       10           Comp               19-20              8

54、0;          Currency           19-20              8           = &

55、#160;         例子 Real   6byt    共48 bit               1-1    2-40   41-48    

56、60;           s      f       e               有效值 v        &#

57、160;      if ( e  0 ) and  ( e = 255 ) then                  v = (-1)*s * 2 * (e 

58、;- 129) * (1.f)               else if e = 0 then v = 0;           =  3.3 字串型态    

59、;   3.3.1 基础型态(Fundmental)             (1) ShortString  短字串 1-255 (又称pascal string)             (2) AnsiString 

60、;  长字串 1-2G             (3) PChr         Null-Terminated string       3.3.2 通用型态(Generic)      

61、       String 32位元时 等於 AnsiString                    16位元时 等於 ShortString           =

62、           (1) 字串为一以零为基底的字元阵列(Zero-base character array)               例              

63、; -               TYPE                 TStringBuf = Array0.79 of Char;       

64、60;       const                 StringBuf : TStringBuf = 'This is test'            

65、;   -           (2) var s : ShortString               Length(s) = Ord(s0)        

66、0;  (3) 长字串以4-byt指标,指到动态配置之记忆体,没有0之长度数值               一个动态记忆体配置之长字串会自动以一空字元(null Character)结束           (4) PChr 以字元阵列来储存,结尾以(null Char

67、acter)结束,使用指标来管理           (5) PChr型态与String型态在指定叙述上是相容               例:               

68、60; -                   var p:pchr;                   begin       

69、;              p := 'this is test'                   end;        &#

70、160;        -                 Procedure PrintStr(Str:Pchr);                 呼叫时可用

71、0;                   PrintStr('This is test');                 -  3.4 结构型态(Strictured&#

72、160;type)       3.4.1 阵列型态(Array type)             TDim1 = Array0.79 of Char;             TDim2 =

73、60;Array1.3,1.10 of Char;             VAR                Dim1 : Tdim1;           

74、;     I : Integer;             begin                for I := Low(Dim1) to High(Dim1) do

75、60;                   Dim1I := #32;             end;           =  

76、;     3.4.2 记录型态(Record Type)           TYPE             TdateRec = Record          &

77、#160;     Year : Integer;                Month : 1.12;                Day : 1.31;

78、0;            end;           何谓变动栏位 ?       3.4.3 集合型态(Set Type)            

79、;TYPE               Tmenber = ( Lee , White , Ston );               Tmenbers = Set Of Tmen

80、ber;            var Members : Tmenbers;            begin               Menbers :=

81、0; White , Ston             end;            集合型态不能超过256个可能值       3.4.4 档案型态(File Type)    

82、0;      (1)记录型态档案              TYPE                Temployee = record       

83、;               name : string10;                      address : string50;   

84、0;            end;                TemployeeFile = File Of Temployee;            

85、  var                EmployeeFile : TemployeeFile;                同 EmployeeFile : File Of Temp

86、loyee;           (2)文件型态档案                TMyTextFile = TextFile;               

87、 var                 MyTextFile : TMyTextFile;                 or        &#

88、160;        MyTextFile : TextFile;                 ReadText : String;              -&

89、#160;             AssignFile( MyTextFile , 'c:MyFile.txt');              Reset( MyTextFile );      

90、60;       While Not Eof( MyTextFile ) do                 begin               

91、60;   Readln( MyTextFile , ReadText );                 end;              CloseFile( MyTextFile );

92、60;             注:Rewrite(MyTextFile);  开启唯写                 Writeln(MyTextFile , ReadText);      &#

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

94、#160;      3.4.5 类别型态(Class Type)         TDBText = class(TCustomLabel)         private           FDataLi

95、nk: TFieldDataLink;           procedure DataChange(Sender: TObject);           function GetDataField: string;         &#

96、160; function GetDataSource: TDataSource;           function GetField: TField;           function GetFieldText: string;      

97、     procedure SetDataField(const Value: string);           procedure SetDataSource(Value: TDataSource);           procedure CMGetDataL

98、ink(var Message: TMessage); message CM_GETDATALINK;         protected           function GetLabelText: string; override;       

99、60;   procedure Notification(AComponent: TComponent;             Operation: TOperation); override;           procedure SetAutoSize(Value:

100、60;Boolean); override;         public           constructor Create(AOwner: TComponent); override;           destructor D

101、estroy; override;           property Field: TField read GetField;         published           property Align; 

102、          property Alignment;           property AutoSize default False;           property Color;   

103、;        property DataField: string read GetDataField write SetDataField;           property DataSource: TDataSource read GetDataSource write Set

104、DataSource;           property DragCursor;           property DragMode;           property Enabled;   

105、60;       property Font;           property ParentColor;           property ParentFont;         

106、;  property ParentShowHint;           property PopupMenu;           property Transparent;           property&#

107、160;ShowHint;           property Visible;           property WordWrap;           property OnClick;   

108、0;       property OnDblClick;           property OnDragDrop;           property OnDragOver;        &

109、#160;  property OnEndDrag;           property OnMouseDown;           property OnMouseMove;           property&

110、#160;OnMouseUp;           property OnStartDrag;         end;         元件之可见性            

111、60;注-1:同一unit中Private,Protect,Public,published皆同Public                 不同unit时其特性不同                 Private : 所属unit才有可见

112、性                 Protect : 只有继承时才有可见性                 Public : 所有uses者皆有可见性     

113、0;           published : 所有uses者皆有可见性,且提供元件设计时之栏位可见性             注-2:若宣告没有注明4P,则表示3.5 指标型态(Pointer type)        3.5.1

114、60;字元指标(character pointer)        在 system unit 中,有下列宣告        TYPE           PAnsiChar = AnsiChar;      

115、     PWideChar = WideChar;           PChar = PAnsiChar;           所以在2.0版中,PChar和PAnsiChar 是一样的       

116、60; 3.5.2 通用指标           TYPE              TMyPointer = Pointer;           通用指标可以被型态转换後来参考 

117、0;3.6 程序型态(Procedure type)         3.6.1 全域程序指标               TYPE                

118、60; TStrProc = Procedure( Const s: String );                  TMyFunc = Function( X:Integer ) : String;       

119、  3.6.2 方法程序指标                 TYPE                    TNotifyEven = Procedure(Sender 

120、;:Tobject) of Object;         3.6.3 程序数值                  一个程序型态变数可以被指定程序数值            

121、60;     TYPE                     TMainForm = Class(TForm)                 &

122、#160;          Procedure ButtonClick(Sender:Tobject);                            end;   &#

123、160;              VAR                       MyForm : TMainForm;       

124、                MyFunc : TMathFunc;                   Function ChgFunc(X:Integer):Integer;Far;  

125、0;                Begin                     Result := X + X;      

126、;             end;                   MyFunc := ChgFunc;            

127、60;      X := MyFunc(X);    等於 X  := ChgFunc(X)                 =           

128、0;     一个程序变数未被指定数值时其数值是 nil,                 但未被指定数值之程序或函数不能执行,                 故安全之呼叫是  

129、               If Assigned(OnClick) Then OnClick(Self);  3.7 变动型态(Variant type)      (1) 变动型态和其它型态一起使用会作自动型态转换   =使用arrey   &

130、#160;    Var             V1,V2,V3,V4,V5  : Variant;             I : Integer;        &

131、#160;    D:Double;   浮点             S:String;        Begin             V1 := 1; 

132、            V2 := 1234.5678;             V3 := 'This is test'            

133、0;V4 := '1000'             V5 := V1 + V2 + V4; 实数 2235.5678              I := V1; I =&#

134、160;1               D := V2; D = 1234.5678              S := V3; S = 'This is test'

135、0;             I := V4; I = 1000              S := V5; S = '2235.5678'      &

136、#160;end;       虽然变动型态提供很大弹性,但它耗用更多记忆体,也比静态型态来得慢       (2)变动阵列          var            A : Variant;  

137、;        =变动型态            I : Integer;          Begin            A := 

138、VarArrayCreate(0,4,VarOleStr);            For I := 0 to 4 Do AI := 'AAAA'            VarArrayRedim( A , 9 )

139、;         =加大            For I := 5 to 9 Do AI := 'BBBB'          end;4.变数宣告 (使用标记 

140、;: ) : 当宣告一个变数时,必须指明其型态  4.1  例子    X,Y,Z : Double;    I,J,K : Integer;    Dig :0.9;    C : Color;    Done,Error : Boole

141、an;         =通常为一个byts    Operater : ( Plus , Munus , Times );    H1,H2  : Set Of Color;    Today : Date;  

142、60; MyDim : Array1.10,1.5 Of Double;  4.2 全域变数 : 在程序和函数之外宣告之变数  4.3 区域变数 : 在程序和函数之内宣告之变  4.3 变数初始化值 : MyInt : Integer = 123; 注意不可为区域变数指定初值  4.4 限定词 : 

143、    4.4.1阵列索引 : MyDimIJ  同MyDimI,J     4.4.2栏位指示词 : MyRec.MyField 在一个with叙述中可以省略栏位指示         参考     4.4.3物件指示词 : form1.Button1.Caption 

144、    4.4.4指标和动态变数: p1,p1 4.5 变数型态转换     例-1       TYPE          TbyteRec = record           

145、; Fl1 , Fl2: Byte;            end;       VAR          W : Word;          B&

146、#160;: Byte;       Begin          W := 256 * 10 + 128;          B := TbyteRec(W).Fl1;  B = 10 &

147、#160;    *             word      换成byte型态          B := TbyteRec(W).Fl2;  B = 128    

148、   end;    例-2       With Sender as Mdbedit do    =代上一层型态           Text := 'This is test'   

149、; 例-3       Mdbedit( Sender ).Text := 'This is test'5.运算式语法  5.1 运算元及运算优先次序      (1),not               

150、;           单运算子运算元  =变数住置      (2)*,/,div,mod,and,shl,shr,as     乘除      (3)+,-,or,xor          

151、60;          加减      (4)=,=,=,in,is           关系  5.2 () 有优先运算评量,由内外之运算优先  5.3 相同优先次序,则由左至右之运算优先  5.4 捷径评量(short-Ci

152、rcuit)      while (I = Length(S) and            (SI  ' ') do                 

153、   Inc(I)      捷径评量严格执行由左至右之运算,一旦运算结果己可知後便停止,可使得      一些原本不合法之运算架构变成可以运算  5.5 运算元分类      5.5.1 数学运算元 : +,-,*,/         &

154、#160;               div 整数相除之商                         mod 整数相除之余数    

155、                      I mod J = I - ( I div J) * J       5.5.2 逻辑运算元 : not ,

156、0;And , Or , Xor , Shl , Shr      5.5.3 字串运算元 : +            S := 'aaaaa' + 'bbbb' s = aaaaabbbb &#

157、160;    5.5.4 集合运算元 : + , - , *            + :连集 -:差集 *:交集      5.5.5 关系运算元:=,=,=,in     =in 判断有效值 &

158、#160;          In : 成员        type        Month = ( Jan , Feb , Mar , Apr , May , Jun ,

159、60;Jul , Aug , Sep , Oct , Nov , Dec );        Spring = set of Feb.Apr.Mar;        Mstr = array 0 . 11  of

160、0;string 3         const          CMstr : Mstr = ( 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' ,

161、  'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' );        var          M

162、0;: Month;          SSpring : Spring;        begin          SSpring :=  Feb.Apr        

163、60;  writeln( 'The Spring Month is : ' );          for M := Jan to Dec do            if M in 

164、;SSpring then              writeln( CMstr Ord( M )  );        end;      5.5.6 类别运算元:is , As   &

165、#160;     if ( Sender is Mbutten ) and            ( Mbutten( Sender ).tag  0 ) then .;         with&

166、#160;Sender As Mdbedit do              text := 'aaaaaa'      5.5.7 位置运算元:      5.5.8 指标运算元:      

167、      var              M , N : integer;              P1 , P2 : integer;   &

168、#160;= 指标            begin              M := 6;              P1 := M; 

169、     =位置              Label1.Caption := 'P1 =    ' + IntToStr( P1 );            &#

170、160; P2 := P1;              N := P2;              Label2.Caption := 'N   =    '

171、60;+ IntToStr( N );            end;6.叙述语法  6.1 goto      label aa;      var i : integer;      begin&

172、#160;       .        .        if (i = 0) then goto aa;      aa:begin         

173、60; .           .         end;      end;  6.2 if      if ( x  10 ) and   &#

174、160;注意关系运算必须()         ( y  5 ) then         z := x + y        注意 没;      else z 

175、;:= 2 * ( x + y );  6.3 case      var        s : string;        r : integer;      begin

176、60;       if s  '' then           begin             case s1 of       =S之第一个BYTE               '1' : r := 1;   

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论