======Syntax====== =====Syntax เบื้องต้น===== สำหรับคนที่มีความรู้เกี่ยวกับภาษา Pascal บ้างแล้ว ผมขอสรุป Syntax ไว้ใน Hidden Blog ข้างล่างนี้ เพื่อความสะดวกในการเข้ามาดูครับ ^ Topic ^ Format ^ |Comments styles (3 forms)| //This is line comment; { This is block comment#1 } (* This is block comment#2 *) | |Type declaration| TYPE //Enumerated Type TWeekDay = (Mon,Tue,Wed,Thu,Fri); //Record type TMyRecord = record Name:string; Age:integer; end; //Static Array TStArr1D = array[1..10] of integer; TStArr2D = array[1..3] of array[1..5] of real;//Array (3 x 5) //Static Array with initial values TStArr1D: array[1..3] of integer = (14,25,36); //DynamicArray TDyArr1D = array of boolean; TDyArr2D = array of array of string; //Object TMyObject = object private length, width: integer; public procedure setlength(l: inteter); function getlength: integer; end; //Class TMyClass = class private length, width: integer; public constructor create(l, w: integer); destructor destroy; override; procedure setlength(l: integer); function getlength: integer; end; | |Variable declaration| var MyInt:integer; MyReal:real; x,y,z:boolean; //Multi-declaration MyDay:TWeekDay; Arr1:TStArr1D; Arr2:TStArr2D; MyObject:TMyObject; MyClass:TMyClass; MyInt:integer = 10; //initialize value | |Variable assignment| MyInt := 10; MyReal := 2.521; x:=True; y:=false; z:=true; Arr1[1] := 10; //Array 1-D Arr2[1,3] :=22.85; //Array 2-D | |Numeric operator| z := x + y; //Plus z := x - y; //Minus z := x / y; //Divide z := x div y; //Divide, result: integer type z := x * y; //Multiply z := x % y; //modulus | |Relational operator| A = B //Equal A <> B //Not Equal A > B //Greater than A < B //Less than A >= B //Greater than or equal A <= B //Less than or equal | |Boolean operator| X and Y //And X or Y //Or not X //Not | \\ \\ =====Program Structure===== โครงสร้างของโปรแกรม Pascal โดยทั่วไป จะมีรูปแบบดังนี้ PROGRAM ProgramName ; USES {include units (external .pas files)} CONST {Constant declarations} TYPE {Type declarations} VAR {Variable declarations} {Sub-programs declarations & Implementations} BEGIN {Executable statements (Main)} END. การเขียนโปรแกรมตามโครงสร้างดังกล่าว มีข้อสังเกตเพิ่มเติมดังนี้ *ในส่วนไหนที่ไม่ได้ใช้งาน ให้ตัดออก เช่น ถ้าไม่ได้ใช้ส่วนของ TYPE ก็ให้ตัดออกไป (ดู Example-1) *โปรแกรมย่อย ([[tutorial:ProcedureAndFunction|Procedure/Function]]) โดยปกติจะประกาศหลังจากการประกาศตัวแปร VAR แต่ในความเป็นจริงแล้ว Function/Procedure สามารถประกาศได้ทุกที่ ที่อยู่หลังจากการประกาศ USES *ส่วนประกาศ (Declaration scope) สามารถนำมาใส่ในโปรแกรมย่อยจำพวก [[tutorial:ProcedureAndFunction|Procedure/Function]] ได้ (ยกเว้น uses) โดยส่วนประกาศเหล่านี้ จะกลายเป็น local Scope ของโปรแกรมย่อยนั้น (ดู Example-2) **ตัวอย่าง** การเขียนโปรแกรมหาพื้นที่วงกลม\\ __ข้อสังเกต__: ไม่มีการใช้การประกาศ TYPE จึงตัดคำว่า TYPE นี้ออกจากโครงสร้าง PROGRAM CircularAreaCalculator ; CONST PI = 3.14159; VAR Din:real; Dex:real; CArea:real; BEGIN writeln('Please insert internal diameter'); readln(Din); writeln('Please insert external area'); readln(Dex); CArea:= PI*(Dex*Dex-Din*Din); writeln('Circular area is: ', CArea); END. **ตัวอย่าง** การเขียนโปรแกรมหาพื้นที่วงกลม\\ __ข้อสังเกต__: มีการประกาศ subprogram ที่เป็น procedure showcalcarea ซึ่งจะเห็นว่าภายใน showcalcarea มีส่วนประกาศย่อยคือ const, type, var และ function squrevalue อยู่ภายใน PROGRAM CircularAreaCalculator ; VAR Din:real; Dex:real; CName:string; procedure showcalcarea(nameCircle:string, Din,Dex:real); const PI = 3.14159; type TMyCircular = record name:string; din,dex:real; area_in,area_ex,area_tot:real; end; var Ain,Aex:real; MyCircular:TMyCircular; function squrevalue(x:real):real; begin result:=x*x; end; begin with MyCircular do begin name:=nameCircle; din:=Din; dex:=Dex; area_in:=PI*squrevalue(Din)/4; area_ex:=PI*squrevalue(Dex)/4; area_tot:=area_ex-area_in; end; writeln('Total area is: ',MyCircular.area_tot); end; BEGIN writeln('Please insert name of circle'); readln(CName); writeln('Please insert internal diameter'); readln(Din); writeln('Please insert external area'); readln(Dex); showcalcarea(CName,Din,Dex); END. \\ \\ =====Unit Structure===== บางครั้งการแยกโค๊ดออกเป็นโมดูลย่อยแล้วค่อยเรียกใช้เข้ามาในโปรแกรมหลัก จะทำให้เราสะดวกในการค้นหาและแก้ไข อีกทั้งโค๊ดส่วนนี้ยังสามารถเอาไปใช้ในหลายๆโปรแกรมในภายหน้าได้อีก เราเรียกโมดูลที่เก็บโค๊ดเหล่านี้ว่า Unit \\ Unit คือไฟล์ .pas ที่มีส่วนประกาศหรือตัวแปรต่างๆ (uses, const, type, var) หรือโปรแกรมย่อย (functions/procedures) อยู่ภายใน ประกอบไปด้วยส่วนที่เป็น Public Scope และ Private Scope โดยโครงสร้าง Unit ของ Pascal จะมีรูปแบบดังนี้ unit unitname; //public scope ----- interface {uses, const, type, var, function, procedure ... declaration} //private scope ----- implementation {function and procedure details} end. \\ *__**Public Scope**__ คือ ส่วนที่อยู่ใต้คำว่า interface ลงมา แต่อยู่เหนือคำว่า implementation ทุกครั้งที่มี Program อื่น หรือ Unit อื่น เรียกใช้ Unit ดังกล่าวนี้ (ใช้ใน uses) จะเห็น Variables หรือ Sub-program หรืออะไรก็ตามที่ประกาศไว้ในเฉพาะส่วนนี้เท่านั้น \\ *__**Private Scope**__ คือ ส่วนที่อยู่ใต้คำว่า implementation เป็นส่วนที่ Program อื่น หรือ Unit อื่น ที่เรียกใช้ Unit ดังกล่าวนี้ ไม่สามารถเข้าถึงโดยตรงได้ จะเรียกใช้งานได้ก็ต่อเมื่อเรียกผ่านการ Declaration ในส่วนของ Public Scope เท่านั้น ที่เป็นแบบนี้เพราะ ส่วนของ implementation นั้นมักจะประกอบไปด้วย source code หลายบรรทัด แถมยังมีการเรียกใช้ Variables หรือ Sub-program ซ้ำกันเต็มไปหมด จะเป็นการยากที่จะให้ Compiler มาหา Variables หรือ Sub-program หรืออะไรก็ตาม ในสถานที่นี้ \\ \\ =====ความรู้พื้นฐานก่อนเริ่มเขียน Pascal===== ====Declaration and Implementation==== การเขียนโปรแกรมนอกเหนือจากต้องรู้จักโครงสร้างของโปรแกรมแล้ว ยังต้องทำความเข้าใจกับสองคำนี้ คือ Declaration กับ Implementation ซึ่งนอกจากจะเกี่ยวข้องกับการเขียน Main Program แล้ว ยังเกี่ยวข้องกับ Sub-program ด้วย (Sub-program คือ Procedure/Function หรือเรียกอีกอย่างว่า Method นั่นเอง เพื่อไม่ให้ดูซับซ้อนมากเกินไป ในหัวข้อนี้ จะขอเรียกพวกนี้สั้นๆว่า Sub-program แทน)\\ __**Declaration**__ คือ การประกาศ เป็นส่วนที่ใช้บอก Compiler ว่า Program หรือ Sub-program ดังกล่าว ชื่ออะไร รับ Input หรือส่ง Output อะไรบ้าง เป็นต้น แต่ยังไม่ได้บอกถึงรายละเอียดว่าทำอะไรได้บ้าง __**Implementation**__ คือ ส่วนที่บ่งบอกว่า Program หรือ Sub-program นั้นทำอะไรบ้าง มีขั้นตอนอะไรบ้าง ย้อนกลับไปดูที่โครงสร้างของ Main Program จะแบ่งแยกพื้นที่ได้ดังนี้ * Declaration คือ ส่วนแรก คือ บรรทัดที่ 1-16 * Implementation คือ ระหว่างคำว่า BEGIN ... END. หรือ บรรทัดที่ 18-20 PROGRAM ProgramName ; USES {include units (external .pas files)} CONST {Constant declarations} TYPE {Type declarations} VAR {Variable declarations} {Sub-programs declarations & Implementations} BEGIN {Executable statements (Main)} END. \\ ทีนี้ลองมาดูที่โครงสร้างของ Sub-Program ที่อยู่ภายใน Main Program กันบ้าง ขอยกตัวอย่าง Procedure SubProg และ Function SubFunc ข้างล่างนี้ เราจะแบ่งพื้นที่ของ Sub-Program Declaration กับ Implementation ได้ดังนี้ * Declaration คือ ส่วนแรก ในบรรทัดที่ 6-7 (เป็นการประกาศแบบล่วงหน้า หรือ Forward Declaration ซึ่งจากตัวอย่างนี้ จะมีหรือไม่มีก็ได้) * Implementation คือ บรรทัดที่ 9-17 บ่งบอกว่า Sub Program พวกนี้ ทำอะไรได้บ้าง PROGRAM ProgramName ; VAR {Variable declarations} program SubProg; forward; function SubFunc(inp:string):string; forward; program SubProg; begin writeln('This is subProgram'); end; function SubFunc(inp:string):string; begin result:= inp; end; BEGIN SubProg; //Call SubProg; writeln('Call SubFunc : ',SubFunc('Hello!'); END. สำหรับ Unit เราสามารถแบ่งพื้นที่ได้แบบนี้ * Declaration คือ ส่วนแรก คือ บรรทัดที่ 1-5 * Implementation คือ ระหว่างคำว่า implementation ... end. หรือ บรรทัดที่ 7-9 (สังเกตว่ามันจะคล้ายกับ BEGIN...END. ของ Main Program แค่เปลี่ยนคำแรกเป็น implementation แค่นั้นเอง) unit unitname; interface {uses, const, type, var, procedures/functions ... declaration} implementation {Procedures/Functions implementation here} end. \\ สำหรับ Sub-Program ที่อยู่ภายใต้ Unit จะแบ่งพื้นที่ได้ดังนี้ * Declaration คือ ส่วนแรก ในบรรทัดที่ 6-7 (ส่วนนี้บังคับว่าต้องมี และถือเป็นการ Forward Declaration เหมือนกัน แต่ไม่ต้องใส่คำว่า forward;) * Implementation คือ บรรทัดที่ 10-18 บ่งบอกว่า Sub Program พวกนี้ ทำอะไรบ้าง unit unitname; interface {uses, const, type, var, procedures/functions ... declaration} program SubProg; function SubFunc(inp:string):string; implementation program SubProg; begin writeln('This is subProgram'); end; function SubFunc(inp:string):string; begin result:= inp; end; end. \\ \\ ====Built-in Functions ที่ควรทราบ==== ก่อนการฝึกทำตัวอย่าง หรือเขียนโปรแกรมพื้นฐานง่ายๆ เราควรทำความรู้จักกับ Built-in Function ต่อไปนี้เสียก่อน ^ Functions ^ Descriptions ^ |writeln(string) หรือ writeln(string,value)| แสดงข้อความและค่าของตัวแปร แล้วลงไปบรรทัดถัดไป| |write(string)| แสดงข้อความและค่าของตัวแปร แล้วอยู่บรรทัดเดิม| |readln(variable)| รับค่าใส่ Variable จากการพิมพ์ข้อมูลหน้าจอแล้วกด Enter บางครั้งเราใช้ readln() เฉยๆเพื่อเบรคโปรแกรมไว้ไม่ปิดตัวเอง| **ตัวอย่าง** โปรแกรมคำนวณ Body Mass Index (BMI)\\ __**ข้อสังเกต**__ - readln() (หรือ readln) ในบรรทัดที่ 20 นั้น ถูกใช้เพื่อเบรคหน้าจอไม่ให้ปิดตัวเองลง และผู้ใช้สามารถกด Enter เพื่อผ่านคำสั่งนี้ได้ (จบโปรแกรม) program BMI_Calculation; uses SysUtils; //for function: Format() var Mass,Height,BMI:real; begin write('Please insert your mass (kg): '); readln(Mass); //Get input: Mass write('Please insert your Height (cm): '); readln(Height); //Get input: Height //Calculate BMI:=Mass/(Height*Height/1e4); writeln('----Result----'); writeln('Your BMI is = ',Format('%.2f',[BMI])); readln(); // or readln; end. Compiled Results: Please insert your mass (kg): 60 Please insert your Height (cm): 170 ----Result---- Your BMI is = 20.76 \\