190 likes | 263 Views
การเขียนโปรแกรมคอมพิวเตอร์และอัลกอริธึม. บทที่ 9 การเขียนโปรแกรมแบบลำดับ ด้วยภาษาปาสคาล (PASCAL). Pascal. โปรแกรมภาษาชั้นสูง แบบโครงสร้าง Structural Language รูปแบบเป็นมาตรฐานโปรแกรมภาษาชั้นสูงเช่นเดียวกับ C , Java กลไกการทำงาน. Source Code .pas. Compiler. Object , Execution Code.
E N D
การเขียนโปรแกรมคอมพิวเตอร์และอัลกอริธึมการเขียนโปรแกรมคอมพิวเตอร์และอัลกอริธึม บทที่ 9 การเขียนโปรแกรมแบบลำดับด้วยภาษาปาสคาล(PASCAL)
Pascal • โปรแกรมภาษาชั้นสูง แบบโครงสร้างStructural Language • รูปแบบเป็นมาตรฐานโปรแกรมภาษาชั้นสูงเช่นเดียวกับ C , Java • กลไกการทำงาน Source Code .pas Compiler Object , Execution Code Turbo Pascal Library
โครงสร้างภาษา • ทุกคำสั่งต้องปิดท้ายด้วย Semi Colon • โปรแกรมจะเริ่มทำงานที่ Begin จบที่ • End • ตัวอักษรเล็กใหญ่ไม่สำคัญ • ไม่จำเป็นต้องมีครบทุกส่วน แต่ต้องมี • Begin – End. Program ชื่อโปรแกรม; Uses ชื่อ Unit หรือ Library; Type กำหนดชนิดตัวแปร; Var กำหนดตัวแปร; Begin คำสั่งต่างๆ; End.
Turbo Pascal Compiler • Turbo.exe > editor + compiler + Runtime • Turbo.tpl > pascal library • Turbo.tph > Help files • *.pas > Source code programs • *.tpu > External Units
Exam Program example; Begin Writeln(“Hello World”); Writeln(“Thank you…”); End. โปรแกรมชื่อ example มีคำสั่ง 2 บรรทัด ทั้ง 2 บรรทัดให้แสดงข้อความออกจอภาพ
Note. • การตั้งชื่อโปรแกรม • Program …………………; • ชื่อต้องเป็นภาษาอังกฤษ ตัวเล็กหรือตัวใหญ่แต่ต้องติดกันหมด สามารถใช้ตัวเลขหรือ _ ร่วมได้ เช่น • Program test; • Program myProgram1; • Program Example_area;
คำสั่ง Write , Writeln • Program PrintOut2;begin WriteLn ('This is yet another'); WriteLn ('Pascal programming test!');end. • Writeln หรือ Write เป็นคำสั่งให้แสดงข้อความ หรือ อะไรก็ตามที่อยู่ในวงเล็บออกทางจอภาพ
การลบจอภาพ Clrscr • Program Clear; uses crt;begin ClrScr; WriteLn (This is written in the top left corner on a cleared screen');end. • Clrscr ต้องใช้ Unit crt เป็นคำสั่งใช้ลบจอภาพ
ตัวแปร (Variables) • ตัวแปร คือ หน่วยความจำสำหรับเก็บข้อมูลค่าใดค่าหนึ่ง เรียกใช้โดยการระบุชื่อซึ่งเราเรียกว่า ตัวแปร • การตั้งชื่อ : ต้องตั้งด้วยภาษาอังกฤษ ตัวเล็กหรือใหญ่ สามารถใช้ตัวเลขร่วมได้ ไม่อนุญาตให้ใช้สัญลักษณ์พิเศษ และต้องไม่ซ้ำกับคำสั่งอื่นๆ
การกำหนดตัวแปร • ตัวแปรต้องกำหนดไว้ในส่วน Var ดังตัวอย่าง Program Declaration;var Name: String;begin.[Your program].end. Program Declaration;var A , Numbers: Integer; X : Real;begin.[Your program].end.
ตัวแปรตัวเลข • ตัวแปรตัวเลขมีขอบเขตการเก็บข้อมูลและแบ่งประเภทย่อยๆ ดังนี้
ตัวอย่างการใช้งานตัวแปรตัวอย่างการใช้งานตัวแปร Program Var_Print; var numbers : Integer; values : Real; begin numbers := 23; values := 30.5; WriteLn ('The variable numbers got the value:'); WriteLn (numbers); WriteLn (‘values is ‘,values); end.
เกิดอะไรขึ้นถ้า Program Var_Print; var numbers : Integer; values : Real; begin numbers := 7.5; values := 20; WriteLn ('The variable numbers got the value:'); WriteLn (numbers); WriteLn (‘values is ‘,values); end. เพราะเหตุใด ?
การคำนวณ • เครื่องหมายในการคำนวณ • + การบวก • - การลบ • * การคูณ • / การหาร • โปรแกรมจะดำเนินการตามลำดับความสำคัญของเครื่องหมาย • sum := number1 + number2;division := number1 / number2;complex := (number1 + number2) * 3;
การคำนวณ(ต่อ) Program Calculate; var number1, number2: Integer; Beginnumber1 := 100; number2 := 45;Write ( ‘ The result of the calculation is: ‘ );WriteLn (number1 + number2); End.
การคำนวณ (ต่อ) Program Triangle_Area; var High , Base: Integer; Area : Real; BeginHigh := 10; Base := 25; Area := 0.5 * Base * High;Write ( ‘ The area result of the triangle is: ‘ );WriteLn ( Area:8:2 ); Readln; End.
จงเขียนโปรแกรม • คำนวณหาผลรวมของราคาสินค้า • Apple ราคา 500 บาท , Durian ราคา 300 บาท • ส่วนลด 10% • จงแสดงราคาสินค้ารวมทั้งสองอย่างเมื่อหักส่วนลดแล้ว