1 / 19

การเขียนโปรแกรมคอมพิวเตอร์และอัลกอริธึม

การเขียนโปรแกรมคอมพิวเตอร์และอัลกอริธึม. บทที่ 9 การเขียนโปรแกรมแบบลำดับ ด้วยภาษาปาสคาล (PASCAL). Pascal. โปรแกรมภาษาชั้นสูง แบบโครงสร้าง Structural Language รูปแบบเป็นมาตรฐานโปรแกรมภาษาชั้นสูงเช่นเดียวกับ C , Java กลไกการทำงาน. Source Code .pas. Compiler. Object , Execution Code.

Download Presentation

การเขียนโปรแกรมคอมพิวเตอร์และอัลกอริธึม

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. การเขียนโปรแกรมคอมพิวเตอร์และอัลกอริธึมการเขียนโปรแกรมคอมพิวเตอร์และอัลกอริธึม บทที่ 9 การเขียนโปรแกรมแบบลำดับด้วยภาษาปาสคาล(PASCAL)

  2. Pascal • โปรแกรมภาษาชั้นสูง แบบโครงสร้างStructural Language • รูปแบบเป็นมาตรฐานโปรแกรมภาษาชั้นสูงเช่นเดียวกับ C , Java • กลไกการทำงาน Source Code .pas Compiler Object , Execution Code Turbo Pascal Library

  3. โครงสร้างภาษา • ทุกคำสั่งต้องปิดท้ายด้วย Semi Colon • โปรแกรมจะเริ่มทำงานที่ Begin จบที่ • End • ตัวอักษรเล็กใหญ่ไม่สำคัญ • ไม่จำเป็นต้องมีครบทุกส่วน แต่ต้องมี • Begin – End. Program ชื่อโปรแกรม; Uses ชื่อ Unit หรือ Library; Type กำหนดชนิดตัวแปร; Var กำหนดตัวแปร; Begin คำสั่งต่างๆ; End.

  4. Turbo Pascal Compiler • Turbo.exe > editor + compiler + Runtime • Turbo.tpl > pascal library • Turbo.tph > Help files • *.pas > Source code programs • *.tpu > External Units

  5. Exam Program example; Begin Writeln(“Hello World”); Writeln(“Thank you…”); End. โปรแกรมชื่อ example มีคำสั่ง 2 บรรทัด ทั้ง 2 บรรทัดให้แสดงข้อความออกจอภาพ

  6. Note. • การตั้งชื่อโปรแกรม • Program …………………; • ชื่อต้องเป็นภาษาอังกฤษ ตัวเล็กหรือตัวใหญ่แต่ต้องติดกันหมด สามารถใช้ตัวเลขหรือ _ ร่วมได้ เช่น • Program test; • Program myProgram1; • Program Example_area;

  7. คำสั่ง Write , Writeln • Program PrintOut2;begin  WriteLn ('This is yet another');  WriteLn ('Pascal programming test!');end. • Writeln หรือ Write เป็นคำสั่งให้แสดงข้อความ หรือ อะไรก็ตามที่อยู่ในวงเล็บออกทางจอภาพ

  8. การลบจอภาพ Clrscr • Program Clear; uses crt;begin  ClrScr;  WriteLn (This is written in the top left corner on a cleared screen');end. • Clrscr ต้องใช้ Unit crt เป็นคำสั่งใช้ลบจอภาพ

  9. ตัวแปร (Variables) • ตัวแปร คือ หน่วยความจำสำหรับเก็บข้อมูลค่าใดค่าหนึ่ง เรียกใช้โดยการระบุชื่อซึ่งเราเรียกว่า ตัวแปร • การตั้งชื่อ : ต้องตั้งด้วยภาษาอังกฤษ ตัวเล็กหรือใหญ่ สามารถใช้ตัวเลขร่วมได้ ไม่อนุญาตให้ใช้สัญลักษณ์พิเศษ และต้องไม่ซ้ำกับคำสั่งอื่นๆ

  10. การกำหนดตัวแปร • ตัวแปรต้องกำหนดไว้ในส่วน Var ดังตัวอย่าง Program Declaration;var Name: String;begin.[Your program].end. Program Declaration;var A , Numbers: Integer; X : Real;begin.[Your program].end.

  11. ตัวแปรตัวเลข • ตัวแปรตัวเลขมีขอบเขตการเก็บข้อมูลและแบ่งประเภทย่อยๆ ดังนี้

  12. ตัวแปรทศนิยม

  13. ตัวอย่างการใช้งานตัวแปรตัวอย่างการใช้งานตัวแปร 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.

  14. เกิดอะไรขึ้นถ้า 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. เพราะเหตุใด ?

  15. การคำนวณ • เครื่องหมายในการคำนวณ • + การบวก • - การลบ • * การคูณ • / การหาร • โปรแกรมจะดำเนินการตามลำดับความสำคัญของเครื่องหมาย • sum := number1 + number2;division := number1 / number2;complex := (number1 + number2) * 3;

  16. การคำนวณ(ต่อ) Program Calculate; var number1, number2: Integer; Beginnumber1 := 100; number2 := 45;Write ( ‘ The result of the calculation is: ‘ );WriteLn (number1 + number2); End.

  17. การคำนวณ (ต่อ) 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.

  18. จงเขียนโปรแกรม • คำนวณหาผลรวมของราคาสินค้า • Apple ราคา 500 บาท , Durian ราคา 300 บาท • ส่วนลด 10% • จงแสดงราคาสินค้ารวมทั้งสองอย่างเมื่อหักส่วนลดแล้ว

  19. Continue..

More Related