1 / 101

บทที่ 4 (ส่วนที่ 1)

บทที่ 4 (ส่วนที่ 1). ภาษาปาสคาล (Pascal Language) และการเขียนโปรแกรมแบบลำดับ ( Sequence ). ภาษาปาสคาล ( Pascal). เป็นภาษาระดับสูง เช่นเดียวกับ C, Java, Ada, BASIC, Perl ถูกสร้างโดย Prof. Niklaus Wirth ในปี ค.ศ.1970 ชื่อของภาษาถูกตั้งขึ้นเพื่อเป็นเกียรติแก่ Blaise Pascal

Télécharger la présentation

บทที่ 4 (ส่วนที่ 1)

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. บทที่ 4 (ส่วนที่ 1) ภาษาปาสคาล (Pascal Language) และการเขียนโปรแกรมแบบลำดับ (Sequence) 204101 Introduction to Computer

  2. ภาษาปาสคาล (Pascal) • เป็นภาษาระดับสูง เช่นเดียวกับ C, Java, Ada, BASIC, Perl • ถูกสร้างโดย Prof. Niklaus Wirth ในปี ค.ศ.1970 • ชื่อของภาษาถูกตั้งขึ้นเพื่อเป็นเกียรติแก่ Blaise Pascal • มีลักษณะเหมือนภาษาอังกฤษทำให้ง่ายต่อการโปรแกรม • ผู้เขียนโปรแกรมไม่จำเป็นต้องมีความรู้ด้านฮาร์ดแวร์ • ไม่ขึ้นอยู่กับเครื่อง(Machine Independent) จะนำไปใช้กับเครื่องใดก็ได้ • ต้องมีตัวแปลภาษาเพื่อแปลงจากภาษาระดับสูงเป็นภาษาเครื่อง • ปาสคาลคอมไพเลอร์ (Pascal Compiler) 204101 Introduction to Computer

  3. การทำงานของปาสคาลคอมไพเลอร์การทำงานของปาสคาลคอมไพเลอร์ • Source Program : ไฟล์โปรแกรมของชุดคำสั่งต่างๆ ที่พึ่งเขียนเสร็จด้วยภาษาโปรแกรมคอมพิวเตอร์ • Object Program :ไฟล์โปรแกรมที่ผ่านการแปล (Compile) จากคอมไพเลอร์แล้ว • Executed Program: โปรแกรมที่ผ่านการรวม (Link) เข้ากับไลบราลี (Libraly) ของระบบแล้ว LIBRALY Link Source Program “ซอร์สโปรแกรม” Object Program Executed Program “เอ็กซ์ซีคิวท์โปรแกรม” Compile xxx.pas xxx.obj xxx.exe 204101 Introduction to Computer

  4. การทำงานของปาสคาลคอมไพเลอร์การทำงานของปาสคาลคอมไพเลอร์ • 1 คำสั่ง จะถูกแปลเป็นภาษาเครื่องได้มากกว่า 1 คำสั่ง • การแปลทั้งโปรแกรมให้เป็นภาษาเครื่องในครั้งเดียว • ได้เอ็กซ์ซีคิวท์โปรแกรมเป็นไฟล์ผลลัพธ์ที่สามารถนำไปใช้งานได้ตลอด เช่น โปรแกรมเครื่องคิดเลข • การเปลี่ยนแปลงหรือแก้ไขส่วนใดส่วนหนึ่งของโปรแกรม ต้องมีการคอมไพล์โปรแกรมใหม่ตั้งแต่ต้น • ในระหว่างการคอมไพล์ ถ้ามีข้อผิดพลาด คอมไพเลอร์ก็จะแจ้งให้ทราบเพื่อทำการแก้ไขต่อไป 204101 Introduction to Computer

  5. ส่วนประกอบของภาษาปาสคาลส่วนประกอบของภาษาปาสคาล • ส่วนหัวของโปรแกรม (Header) • ชื่อโปรแกรม(Program Name) • ประกาศตัวแปร • กำหนดชนิดข้อมูลใหม่ • ส่วนตัวของโปรแกรม (Body) • กลุ่มของคำสั่งที่อยู่ระหว่าง beginและ end programFirst; var number : integer; begin writeln('First Program'); end. ตัวอย่างของภาษาปาสคาล 204101 Introduction to Computer

  6. ตัวแปร (Variable) • จองพื้นที่ในหน่วยความจำเพื่อเก็บข้อมูลที่ต้องการใช้ในโปรแกรม • ตัวแปรทุกตัวต้องมีการประกาศ (Declaration) ก่อนการใช้งาน • คำสั่งสำหรับการประกาศตัวแปรในภาษาปาสคาลคือคำสั่งVAR รูปแบบVARName:Type; โดยที่Name ชื่อตัวแปร Typeชนิดข้อมูลของตัวแปร ตัวอย่างVAR Salary :Integer; ตัวอย่างการประกาศตัวแปรชื่อ Salary เพื่อจองพื้นที่สำหรับเก็บข้อมูลชนิดเลขจำนวนเต็ม 204101 Introduction to Computer

  7. ข้อกำหนดในการตั้งชื่อตัวแปรข้อกำหนดในการตั้งชื่อตัวแปร • อักษรตัวแรกต้องเป็นตัวอักษร (A-Z หรือ a-z) หรือ เครื่องหมาย _ (Underscore) เท่านั้น • อักษรตัวอื่น ๆ ต้องเป็นตัวอักษร (A-Z หรือ a-z) หรือ ตัวเลข 0-9 หรือเครื่องหมาย _ เท่านั้น • ห้ามตั้งชื่อตัวแปรซ้ำกับคำสงวน (Reserved Word) • คำสงวนคือคำที่ถูกกำหนดความหมายในการนำไปใช้งานไว้แล้ว เช่น And, Begin, Do, End, For, Not, If, Mod, Not, Or, Program, Repeat, Then, To, Type, Until, และ Var 204101 Introduction to Computer

  8. ข้อกำหนดในการตั้งชื่อตัวแปร (ต่อ) • ห้ามมีช่องว่างภายในชื่อ • ไม่ควรตั้งชื่อตัวแปรซ้ำกับคำมาตรฐาน (Standard Identifier) • คำมาตรฐานคือคำที่ถูกกำหนดความหมายในการนำไปใช้งานไว้แล้ว โดยปกติจะเป็นคำสั่งต่าง ๆ เช่น Write, Writeln, Read และ Readln เป็นต้น • ชื่อตัวแปร ชื่อฟังก์ชัน ชื่อค่าคงที่ • ตัวอักษรตัวเล็กและตัวใหญ่ถือว่าไม่ต่างกัน เช่น Num, NUM และ num มีความหมายเดียวกัน 204101 Introduction to Computer

  9. ตัวอย่างการตั้งชื่อตัวแปรตัวอย่างการตั้งชื่อตัวแปร • Room_number ถูกต้อง • 123ABC ผิด ห้ามขึ้นต้นด้วยตัวเลข • _cAt ถูกต้อง • me@CMU ผิด ห้ามใช้เครื่องหมาย @ • My Book ผิด ห้ามเว้นวรรค 204101 Introduction to Computer

  10. ชนิดข้อมูล (Data Types) • Integer จำนวนเต็ม • Real จำนวนจริงหรือทศนิยม • Char ตัวอักขระ • String ข้อความ • Boolean ค่าความจริง คือจริง (True) หรือ เท็จ (False) 204101 Introduction to Computer

  11. Data Types : Integer and Real • เก็บตัวเลขจำนวนเต็ม (Integer) มีค่าอยู่ในช่วง -32,768 ถึง 32,767 • ใช้พื้นที่ในการเก็บข้อมูล 2 ไบต์ (16 บิต) • ตัวอย่างการประกาศตัวแปร var It : integer; • เก็บตัวเลขจำนวนจริง (real)หรือทศนิยมที่มีค่าอยู่ในช่วง 2.9e-39 ถึง 1.7e38 (2.9 x 10-39 ถึง 1.7 x 1038) • ใช้พื้นที่ในการเก็บข้อมูล 6 ไบต์ (48 บิต) • ตัวอย่างการประกาศตัวแปร var Rl : real; 204101 Introduction to Computer

  12. Data Types : Char (ตัวอักขระ) • ใช้เก็บตัวอักขระ 1 ตัว ในระบบ ASCII • ใช้พื้นที่ในการเก็บข้อมูล 1 ไบต์ (8 บิต) • ตัวอย่างการประกาศตัวแปร var Ch : char; 204101 Introduction to Computer

  13. Data Types : String (ข้อความ) • ใช้เก็บข้อความ 1 ข้อความ (กลุ่มของตัวอักขระ) • รูปแบบการประกาศตัวแปรชนิดข้อความ var Name : string[size]; sizeคือจำนวนตัวอักขระสูงสุดที่สามารถเก็บได้ หากไม่กำหนด sizeคอมไพเลอร์จะกำหนดค่าให้โดยอัตโนมัติเป็น255 • ตัวอย่างการประกาศตัวแปร var St1 : string[10]; var St2 : string; 204101 Introduction to Computer

  14. Data Types : Boolean (ค่าความจริง) • ใช้เก็บค่าความจริง ได้แก่ จริง (True) และ เท็จ (False) • ใช้พื้นที่ในการเก็บข้อมูล 1 ไบต์ (8 บิต) • ตัวอย่างการประกาศตัวแปร var Bl :boolean; 204101 Introduction to Computer

  15. การให้ค่าตัวแปร • ใช้เครื่องหมาย := (Assignment Operator) • การให้ค่าตัวแปรมีรูปแบบ Variable := Value; program My_Program; var In : integer; var Rl : real; varCh:char; varSt:string[30]; var Bl : boolean; begin It := -27; Rl := 821.459; Ch :='F'; St:='Pascal Language'; Bl := true; end. กรณีตัวแปรชนิด charหรือตัวแปรชนิด string ค่าข้อมูลที่จะกำหนดให้กับตัวแปร 2 ประเภทนี้ต้องอยู่ภายใต้เครื่องหมายฟันหนู ' ' (single quote) 204101 Introduction to Computer

  16. ตัวดำเนินการและนิพจน์ (Operators and Expressions) • ตัวดำเนินการ • ตัวดำเนินการทางคณิตศาสตร์ (Arithmetic Operators) • ตัวดำเนินการเปรียบเทียบ (Comparison Operators) • ตัวดำเนินการทางตรรกศาสตร์ (Logical Operators) • นิพจน์ Now +++ 204101 Introduction to Computer

  17. ตัวดำเนินการทางคณิตศาสตร์ (Arithmetic Operators) • +Addition บวก • - Subtraction ลบ • * Multiplication คูณ • / Division หาร • div Integer Division หารจำนวนเต็ม • mod Modulo หารเอาเศษ 204101 Introduction to Computer

  18. ภายในสัญลักษณ์ { } คือ คำอธิบาย(Comment) ไม่มีผลต่อการทำงาน ของโปรแกรม ตัวอย่างการใช้ตัวดำเนินการทางคณิตศาสตร์ หมายเหตุ : ประกาศหลายตัวแปร แต่มีชนิดของตัวแปรเป็นประเภทเดียวกันหมดให้ คั่นแต่ละชื่อตัวแปรด้วยคอมมา PROGRAM My_Program; var x, y, z : integer; BEGIN x := 7; y := 4; z := x + y; { z = 11 } z := x - y;{ z = 3 } z := x * y;{ z = 28 } z := x / y;{ z = 1.75 } z := x div y;{ z = 1 } z := x mod y;{ z = 3 } END. 204101 Introduction to Computer

  19. ตัวดำเนินการเปรียบเทียบ (Comparison Operators) Operator Example Meaning = x = y ให้ค่า True ถ้าค่า x เท่ากับ ค่า y <> x <> y ให้ค่า True ถ้าค่า x ไม่เท่ากับ ค่า y > x > y ให้ค่า True ถ้าค่า x มากกว่า ค่า y < x < y ให้ค่า True ถ้าค่า x น้อยกว่า ค่า y >= x >= y ให้ค่า True ถ้าค่า x มากกว่าหรือเท่ากับ ค่า y <= x <= y ให้ค่า True ถ้าค่า x น้อยกว่าหรือเท่ากับ ค่า y 204101 Introduction to Computer

  20. ตัวดำเนินการทางตรรกศาสตร์ตัวดำเนินการทางตรรกศาสตร์ • And , or และ not x y x and yx or y not(y) T T T T F T F F T T F T F T F F F F 204101 Introduction to Computer

  21. คำอธิบาย(Comment) ไม่มีผลต่อการทำงานของโปรแกรม ตัวอย่างการใช้ตัวดำเนินการทางตรรกศาสตร์ PROGRAMMy_Program; var x, y :integer; var z : boolean; BEGIN x := 7; y := 4; z := x <> y;{z= True } z :=(x > y) and (x = y); {z=True and False so z=False} z :=not(x <= y);{z= not(F) z = True} END. 204101 Introduction to Computer

  22. นิพจน์ (Expressions) • ค่าคงที่หรือตัวแปรที่มีการใช้ตัวดำเนินการ • นิพจน์ทางคณิตศาสตร์ตามปกติ • x2 + 4y + 5 • 8y2 - 8z = 0 • นิพจน์ทางคณิตศาสตร์ในภาษาปาสคาล • x*x + 4*y + 5 • 8*y*y – 8*z = 0 204101 Introduction to Computer

  23. ลำดับความสำคัญของตัวดำเนินการ (Precedence) ลำดับ เครื่องหมาย 1 ( ) 2 - หมายถึงเครื่องหมายลบ เช่น -9 3 not 4 * / div mod and 5 + - or 6 = < <= > >= <> 204101 Introduction to Computer

  24. ตัวอย่างลำดับความสำคัญของตัวดำเนินการ (ต่อ) สมมติว่ามีการกำหนดค่าตัวแปรแต่ละตัวมีการกำหนดค่าดังนี้ x := 16; y := 4; c := 2.5; d := 0.25; ตัวอย่าง 1 c * d - x / y 2.5 * 0.25 - 16 / 4 0.625 - 4 -3.375 โดย x และ y เป็น Integer c และ d เป็น Real 204101 Introduction to Computer

  25. ตัวอย่างลำดับความสำคัญของตัวดำเนินการ (ต่อ) สมมติว่า มีการกำหนดค่าตัวแปรแต่ละตัวมีการกำหนดค่าดังนี้ x := 16; y := 4; c := 2.5; d := 0.25; ตัวอย่าง 2 -(-5) * (x mod y – x div (y+1)) -(-5) * (16 mod 4 – 16 div (4+1)) -(-5) * (16 mod 4 – 16 div (5)) 5 * (16 mod 4 – 16 div 5) 5 * (0 – 3) 5 * (– 3) -15 โดย x และ y เป็น Integer c และ d เป็น Real 204101 Introduction to Computer

  26. ตัวอย่างลำดับความสำคัญของตัวดำเนินการ (ต่อ) สมมติว่า มีการกำหนดค่าตัวแปรแต่ละตัวมีการกำหนดค่าดังนี้ x := 16; y := 4; c := 2.5; d := 0.25; ตัวอย่าง 3 c / d + y mod x <> 2*c – d 2.5 / 0.25 + 4 mod 16 <> 2*2.5 – 0.25 10 + 4 <> 5 – 0.25 14 <> 4.75 True โดย x,y เป็น Integer c,d เป็น Real 204101 Introduction to Computer

  27. คำสั่งรับและแสดงผล • Write และ Writeln • Read และ Readln • การใช้งาน 4 คำสั่งข้างต้น ต้องมีการเพิ่มคำสั่ง uses WinCrt; ไว้ในส่วน Header • คำสั่ง uses คือคำสั่งในการขอใช้ Unit หรือ Libraly ของภาษาปาสคาล (กลุ่มของคำสั่งต่าง ๆ) 204101 Introduction to Computer

  28. Write & Writeln Write(Output, Values); { แสดงผลปกติ } Writeln(Output, Values); { แสดงผลแล้วขึ้นบรรทัดใหม่ } Output ปลายทางที่ต้องการแสดงผล เช่น หน้าจอ ไฟล์ หรือ เครื่องพิมพ์ ถ้าไม่ระบุจะถูกตั้งค่าให้เป็นหน้าจอ Values ข้อความหรือตัวแปร ถ้ามีข้อมูลมากกว่า 1 ต้อง คั่นด้วยเครื่องหมาย , (Comma) และข้อความต้องอยู่ใน เครื่องหมาย ' '(single quote) 204101 Introduction to Computer

  29. ตัวอย่างการใช้ Write & Writeln หมายเหตุ : ประกาศหลายตัวแปร สามารถใช้คำว่า var เพียงครั้งเดียวได้ program My_Program; usesWinCrt; var It : integer; Rl : real; Ch : char; St : string[30]; Bl : boolean; begin It := -27; Rl := 821.459; Ch := 'F'; St := 'Pascal Language'; Bl := true; 204101 Introduction to Computer

  30. ตัวอย่างการใช้ Write & Writeln(ต่อ) Write('WRITE'); write('Integer:', It); Write('Real:', Rl); Write('Char:', Ch); write('String:', St); Write('Boolean:', Bl); END. หมายเหตุ : อย่าลืมว่าคำสั่งในภาษาปาสคาล เป็น แบบ case insensitive หมายความว่าจะเขียนคำสั่งเป็นอักษรตัวพิมพ์เล็กหรือใหญ่ก็ได้ ผลของคำสั่ง Write ที่หน้าจอภาพ(มีบรรทัดเดียว)ของโปรแกรมนี้คือ WRITEInteger:-27Real: 8.2145900000E+02Char:F String:Pascal LanguageBoolean:TRUE 204101 Introduction to Computer

  31. ตัวอย่างการใช้ Write & Writeln(ต่อ) จากตัวอย่างข้างต้น ถ้าเปลี่ยนเป็นใช้คำสั่ง Writeln ดังนี้ Writeln('WRITELN'); Writeln ('Integer:', It); Writeln ('Real:', Rl); Writeln('Char:', Ch); Writeln('String:', St); Writeln('Boolean:', Bl); end. ผลที่แสดงบนหน้าจอภาพจะมี 5 บรรทัดดังนี้ WRITELN Integer:-27 Real: 8.2145900000E+02 Char:F String:Pascal Language Boolean:TRUE 204101 Introduction to Computer

  32. รูปแบบการแสดงผลของจำนวนทศนิยมรูปแบบการแสดงผลของจำนวนทศนิยม Write(Values:a:b); Values จำนวนทศนิยม aจำนวนตำแหน่งทั้งหมดที่จองไว้สำหรับการแสดงผล โดยจะแสดงตัวเลขแบบชิดขวา ** หากข้อมูลที่ต้องการแสดงมีมากกว่าตำแหน่งที่จองไว้ สามารถนำข้อมูลแสดงผลเกินตำแหน่งที่จองได้ ** b จำนวนตำแหน่งหลังจุดทศนิยม 204101 Introduction to Computer

  33. ** เป็นรูปแบบที่ต้องการให้ตัวเลขที่แสดงผลมีทศนิยม 2 ตำแหน่ง โดยส่วนที่ระบุจำนวนทศนิยม เป็นส่วน : ที่เป็นลำดับที่ 2 จึงต้องใส่ข้อมูลให้ถูกต้องรูปแบบและตรงตำแหน่งที่ต้องการกำหนด จึงกำหนดส่วน : ลำดับที่ 1 มีค่าเป็น 1 ** ตัวอย่างรูปแบบการแสดงผลของจำนวนทศนิยม ตัวอย่างของคำสั่ง Writeln Writeln('Real1:', Rl); Writeln('Real2:', Rl:10); Writeln('Real3:', Rl:10:3); Writeln('Real4:', Rl:10:2); Writeln('Real5:', Rl:1:2); การแสดงผลที่หน้าจอภาพ Real1:^8.2145900000E+02 Real2:^8.215E+02 Real3:^^^821.459 Real4:^^^^821.46 Real5:821.46 หมายเหตุ^ แทนช่องว่าง 1 ตำแหน่ง 204101 Introduction to Computer

  34. Read & Readln Read(Input, Variables); { รับข้อมูลทั้งหมด } Readln(Input, Variables); { รับข้อมูลพอดี } Inputต้นทางที่ต้องการรับข้อมูลเข้ามา เช่น แป้นพิมพ์ หรือ ไฟล์ ถ้าไม่ระบุจะถูกตั้งค่าให้เป็น แป้นพิมพ์ Variablesตัวแปรสำหรับเก็บข้อมูลที่รับเข้ามา 204101 Introduction to Computer

  35. ตัวอย่างการใช้ Read program My_Program; uses WinCrt; var num1, num2 : integer; begin write('Input number 1: '); read(num1); write('Input number 2: '); read(num2); writeln('number 1 = ', num1); writeln('number 2 = ', num2); end. ผลลัพธ์การทำงานของโปรแกรม : ตัวอย่าง 1 Input number 1: 25  Input number 2: -59  number 1 = 25 number 2 = -59 หมายเหตุ  แทนความหมายว่าผู้ใช้กดปุ่ม Enter บนคีย์บอร์ดเมื่อผู้ใช้ใส่ข้อมูลเสร็จ 204101 Introduction to Computer

  36. ตัวอย่างการใช้ Read (ต่อ) ผลลัพธ์การทำงานของโปรแกรม : ตัวอย่าง 2 Input number 1: 25 -59  Input number 2: number 1 = 25 number 2 = -59 หมายเหตุ : ถ้าใช้คำสั่ง Read ในการรับข้อมูล ถ้ามีการรับข้อมูลเกิน ข้อมูลตัวที่เกินจะถูกเก็บไว้ เมื่อมีการรับข้อมูลครั้งต่อไป ข้อมูลตัวทีเกินจะถูกนำมาใช้ทันที 204101 Introduction to Computer

  37. ตัวอย่างการใช้ Readln begin write('Input number 1: '); readln(num1); write('Input number 2: '); readln(num2); writeln('number 1 = ', num1); writeln('number 2 = ', num2); end. ผลลัพธ์การทำงานของโปรแกรม : ตัวอย่าง 1 Input number 1: 25  Input number 2: -59  number 1 = 25 number 2 = -59 ตัวอย่าง 2 Input number 1: 25 -48  Input number 2: -59 16  number 1 = 25 number 2 = -59 หมายเหตุ : กรณีใช้คำสั่ง Readln ถ้ามีการรับข้อมูลเกิน ข้อมูลตัวที่เกินจะถูกกำจัดทันที 204101 Introduction to Computer

  38. Start Read Base Read Height Area = 1/2*Base*Height Display Area Stop ตัวอย่าง 1 จงเขียนโปรแกรมในการหาพื้นที่ของรูปสามเหลี่ยมใด ๆ โดยกำหนดให้รับค่าฐานและสูงจากผู้ใช้ วิเคราะห์ ผลลัพธ์คือ พื้นที่ ใช้ตัวแปรเป็น Area (ประเภท Real) ข้อมูลเข้าคือ ฐาน และ สูง ใช้ตัวแปรเป็น Base และ Height ตามลำดับ (ประเภท Real) การประมวลผลคือ พื้นที่ = ½ * ฐาน * สูง 204101 Introduction to Computer

  39. ตัวอย่าง 1 (ต่อ) program Triangle; uses WinCrt; var Base, Height, Area : real; begin write('Input Base : '); readln(Base); write('Input Height : '); readln(Height); Area := 0.5 * Base * Height; writeln('Area = ', Area:1:2); end. ตัวอย่างผลลัพธ์การทำงานของโปรแกรมนี้ Input Base: 7.26  Input Height: 2.68  Area = 9.73 หมายเหตุ : พื้นที่จากการคำนวณเท่ากับ 9.7284 204101 Introduction to Computer

  40. ตัวอย่าง 2 ให้รับค่า x และ y ที่เป็นจำนวนเต็มจากผู้ใช้ แล้วหาค่า z จากสมการ z = x2 - 2y (กำหนดให้แสดงผลค่า z โดยมีทศนิยม 4 ตำแหน่ง) 7xy3 ตัวอย่างผลลัพธ์การทำงาน ของโปรแกรมนี้ Input x : 7  Input y : -5  z = -0.0096 program Equation; uses WinCrt; var x, y : integer; var z : real; begin write('Input x : '); readln(x); write('Input y : '); readln(y); z := (x * x - 2*y) / (7*x*y*y*y); writeln('z = ', z:1:4); end. 204101 Introduction to Computer

  41. ตัวอย่าง 3 จงเขียนโปรแกรมในการรับรายการอาหาร, จำนวน และ ราคา แล้วแสดงผลสิ่งที่รับมาทั้งหมดรวมทั้งราคารวม กำหนดให้ ข้อมูลตัวแปร ประเภทข้อมูล รายการอาหาร Food String จำนวน Amount Integer ราคา Price Real ราคารวม Total Real 204101 Introduction to Computer

  42. ตัวอย่าง 3 (ต่อ) program Restaurant; uses WinCrt; var Food : string[30]; var Amount : integer; var Price, Total : real; begin Write('Input Food: '); Readln(Food); Write('Input Price: '); Readln(Price); Write('Input Amount: '); Readln(Amount); Total := Price * Amount; Writeln('Food:', Food); Writeln('Price: ', Price:1:2); Writeln('Amount: ', Amount); Writeln('Total: ', Total:1:2); end. ตัวอย่างผลลัพธ์การทำงานของโปรแกรมนี้ Input Food: Coke  Input Price: 13.25  Input Amount: 7  Food: Coke Price: 13.25 Amount: 7 Total: 92.75 204101 Introduction to Computer

  43. แบบฝึกหัด 1: เขียนโปรแกรมภาษาปาสคาลจากผังงานต่อไปนี้ 1 Start A=2.3 , B=0.04578, C=-0.0000088 Read T T = T + 273 Y = A + B*T +C*T2 Print Y Stop 204101 Introduction to Computer

  44. แบบฝึกหัด 2: เขียนโปรแกรมภาษาปาสคาลจากผังงานต่อไปนี้ 2 Start Read X Read Y Z =(X+Y)2 Display Z Stop 204101 Introduction to Computer

  45. แบบฝึกหัด 3 • จงเขียนโปรแกรมภาษาปาสคาลรับค่าเลขจำนวนเต็ม x และ y เพื่อคำนวณและแสดงค่า z เมื่อ z = 4x3 +1.5y+10 กำหนดให้แสดงผลค่า z โดยมีทศนิยม 4 ตำแหน่ง • จงเขียนโปรแกรมภาษาปาสคาล ในการคำนวณหาพื้นที่ของรูปสี่เหลี่ยมในส่วนที่แรเงาดังในรูป ซึ่งผู้ใช้จะป้อนรัศมีของวงกลมเข้ามา โดยแสดงผลลัพธ์ออกทางจอภาพ (ให้นักศึกษากำหนด ตัวแปรและชนิดของตัวแปรให้เหมาะสม) 204101 Introduction to Computer

  46. บทที่ 4 (ส่วนที่ 2) การเขียนโปรแกรมภาษาปาสคาลโดยใช้คำสั่งควบคุม (Control Statements) แบบเลือก (Selection) : if-else, case แบบทำซ้ำ (Loop) : while , repeat, for 204101 Introduction to Computer

  47. if-then (ต่อ) if condition then begin {compound st.} statement1; statement2; statement3; … end; if condition then statement; No Yes condition statement(s) condition เงื่อนไขในการตัดสินใจว่าต้องการให้ทำงาน หรือไม่ ถ้าเงื่อนไขเป็นจริง (True) จึงจะทำงาน statement คำสั่งที่ต้องการให้ทำงานในกรณีที่เงื่อนไขเป็นจริง ถ้า statement มีหลายคำสั่ง ต้องอยู่ภายในbeginและend; 204101 Introduction to Computer

  48. START Read A A =‘C’ Yes No write 'Cat' STOP ตัวอย่าง 1.1 จงเขียนโปรแกรมในการรับตัวอักษร 1 ตัว จากผู้ใช้ถ้าตัวอักษรที่รับมาเป็น C ให้แสดงข้อความว่า Cat program Cat; uses WinCrt; var A : char; begin write('Input character : '); readln(A); ifA = ‘C'then writeln('Cat'); end. Input Character: C  Cat - 204101 Introduction to Computer

  49. ตัวอย่าง 1.1 (ต่อ) ตัวอย่างผลลัพธ์ การทำงานของโปรแกรม program Cat; uses WinCrt; var A : char; begin write('Input character : '); readln(A); ifA = ‘C'then writeln(‘I love cat.'); end. Input Character: C  I love cat. - Input Character: G  - Input Character: c  - 204101 Introduction to Computer

  50. ตัวอย่าง 1.2 จงเขียนโปรแกรมในการรับตัวอักษร 1 ตัว จากผู้ใช้ถ้าตัวอักษรที่รับมาเป็น C ให้แสดงข้อความว่า Cat และ I love cat. โดยแสดงข้อความละ 1 บรรทัด program Cat2; uses WinCrt; var A : char; begin write('Input character: '); readln(A); ifA = ‘C'then begin writeln('Cat'); writeln('I love cat.'); end; end. ตัวอย่างผลลัพธ์การทำงานของโปรแกรม Input Character: C  Cat I love cat. Input Character: G  Input Character: c  _ สังเกตว่ามีหลายคำสั่งต้องใส่ begin end; 204101 Introduction to Computer

More Related