1 / 15

บทที่ 7 การกำหนดชนิดข้อมูลใหม่

บทที่ 7 การกำหนดชนิดข้อมูลใหม่. ชนิดข้อมูลในภาษาปาสคาล มีแบบ Integer, real, boolean หรือ char ผู้ใช้สามารถกำหนดชนิดข้อมูลใหม่เองได้ ภายใต้ คำประกาศ TYPE. การกำหนดชนิดข้อมูลใหม่. การประกาศทำได้ 3 แบบ แบบแจงนับ ( Enumerated type) แบบย่อย (Sub range)

Download Presentation

บทที่ 7 การกำหนดชนิดข้อมูลใหม่

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. บทที่ 7การกำหนดชนิดข้อมูลใหม่ • ชนิดข้อมูลในภาษาปาสคาล มีแบบ Integer, real, boolean หรือ char • ผู้ใช้สามารถกำหนดชนิดข้อมูลใหม่เองได้ ภายใต้ คำประกาศ TYPE

  2. การกำหนดชนิดข้อมูลใหม่การกำหนดชนิดข้อมูลใหม่ • การประกาศทำได้ 3 แบบ • แบบแจงนับ(Enumerated type) • แบบย่อย (Sub range) • แบบกำหนดใหม่โดยใช้ข้อมูลเดิม เช่น record array

  3. แบบแจงนับ(Enumerated type) • แบบแจงนับ เป็นการกำหนดประเภทข้อมูลขึ้นใหม่ เพื่อสะดวกต่อการเขียนโปรแกรม เช่น 1 คือ เชียงใหม่ 2 คือ กรุงเทพ if dest = 1 then price := 500 else dest = 2 then price := 1000;

  4. แบบแจงนับ • ถ้า ประกาศ TYPE destination = (Chiangmai,Bangkok,Lampang); VAR dest : destination; ผู้ใช้สามารถเขียนโปรแกรมให้เข้าใจง่ายขึ้นดังนี้ if dest = Chiangmai then price := 500 else if dest = Bangkok then price := 1000;

  5. แบบแจงนับ รูปแบบ TYPE ชื่อแบบข้อมูล = (ไอเดนติไฟเออร์, ไอเดนติไฟเออร์); • ข้อมูลที่ประกาศในวงเล็บต้องเป็น ไอเดนติไฟเออร์เท่านั้น ห้ามเป็นตัวเลข ตัวอักษร หรือ ค่าคงที่ เพราะจะทำให้ค่าซ้ำกับค่าคงที่ที่อยู่ในของมูลประเภทอื่นๆ • กินเนื้อที่ 1 ไบท์ และ ไอเดนติไฟเออร์มีไม่เกิน 256 ตัว

  6. แบบแจงนับ • ของมูลแบบแจงนับเป็นข้อมูลมีลำดับ เริ่มจาก 0 จึงใช้ฟังก์ชัน ord,pred,succ ได้ • การกำหนดแบบแจงนับที่ผิด TYPE even = (2,4,6,8,10); vowel = (‘A’,’E’,’I’,’O’,’U’); day = (‘sun’,’mon’,’tue’,’wed’,’thu’,’fri’,’sat’);

  7. แบบแจงนับ • การกำหนดแบบแจงนับที่ถูกต้อง TYPE status = (single,married,divorced,widow); vowel = (A,E,I,O,U); day = (sun,mon,tue,wed,thu,fri,sat); • อาจประกาศภายใต้ VAR VAR week = (sun,mon,tue,wed,thu,fri,sat);

  8. แบบแจงนับ • การประกาศแบบแจงนับ ไม่สามารถใช้กับคำสั่ง Read กับ Write ได้โดยตรง เช่น read(vowel); write(vowel); ต้องอาศัยคำสั่ง IF หรือ CASE เช่น CASE vowel OF A : write(‘A’); : U : write(‘U’); END;

  9. แบบแจงนับ • โปรแกรมแสดงจำนวนวันในแต่ละเดือน Program dayinmonth; Uses Wincrt; type monthtype(nul,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec);

  10. แบบแจงนับ var month : monthtype; ch :string [3]; begin for month := jan to dec do begin write('month number ',ord(month),' have');

  11. แบบแจงนับ case month of jan,mar,may,jul,aug,oct,dec : writeln(' 31 days'); apr,jun,sep,nov : writeln(' 30 days'); feb : writeln(' 28 or 29 days'); end; end; end.

  12. แบบย่อย (Subrange type) • แบบย่อย เป็นส่วนย่อยที่เป็นช่วงข้อมูลแบบมีลำดับ คือ Integer ,char , boolean หรือ แบบแจงนับ • รูปแบบ type ชื่อแบบข้อมูล = ค่าต่ำสุด..ค่าสูงสุด;

  13. TYPE point = 0.. Maxint; letter = ‘A’..’Z’; workday = mon..fri; weekend = sat..sun; Point เป็นข้อมูลย่อยของ Integer letterเป็นข้อมูลย่อยของ Char workday / weekend เป็นข้อมูลย่อยของ ข้อมูลแบบแจงนับ ตัวอย่างแบบย่อย

  14. แบบกำหนดใหม่โดยใช้ข้อมูลเดิมแบบกำหนดใหม่โดยใช้ข้อมูลเดิม • ผู้ใช้สามารถนำข้อมูลเดิมมาประกอบเป็นโครงสร้างข้อมูลใหม่ เช่น เรคอร์ด อาร์เรย์ เช่น • TYPE roomtype = array [1..5,1..8] of integer;

  15. Type maxstring = string[255]; nametype = string[30]; employrec = record name : string [30]; age : bye; end; Var employ : employrec; prince : array [1..35] of real; name : nametype; procedure read(name:nametype); แบบกำหนดใหม่โดยใช้ข้อมูลเดิม

More Related