วันพุธที่ 4 กันยายน พ.ศ. 2556

ภาษาคอมพิวเตอร์ (ภาษาโคบอล)

ภาษาคอมพิวเตอร์
ภาษาโคบอล Cobal

 

ความหมายของภาษาโคบอล (COBOL)

                เป็นภาษาโปรแกรมระดับสูงภาษาหนึ่งที่อยู่มาอย่างยาวนาน COBOL ย่อมาจาก Common Business Oriented Language เป็นภาษาที่นิยมนำไปใช้ทางธุรกิจ ถูกพัฒนาขึ้นเมื่อ ค.ศ. 1959 โดยนักคอมพิวเตอร์กลุ่มหนึ่งที่เรียกตัวเองว่า Conference on Data Systems Languages (CODASYL) และตั้งแต่ปี ค.ศ. 1959 ภาษาโคบอลมีการแก้ไขและปรับปรุงอยู่ตลอด ดังนั้น เพื่อขจัดปัญหาความแตกต่างของตัวภาษาโคบอลในแต่ละเวอร์ชัน สถาบันมาตรฐานแห่งชาติอเมริกัน (ANSI) จึงได้พัฒนามาตรฐานกลางขึ้นมาในปี ค.ศ. 1968 เป็นที่รู้จักกันในนามของ ANS COBOL ต่อมาเมื่อ ปี ค.ศ. 1974 ทาง ANSI ได้นำเสนอ ANS COBOL รุ่นใหม่ที่มีคุณสมบัติที่ดีกว่ารุ่น 1968 และในปี ค.ศ. 1985 ANSI ก็นำเสนออีกรุ่นหนึ่งที่มีคุณสมบัติมากกว่ารุ่นปี 1974

                 ภาษาโคบอล (COBOL : Common Business Oriented Language) คือภาษาคอมพิวเตอร์สำหรับใช้ในงานธุรกิจภาษาแรกของโลก พัฒนาในปีค.ศ. 1962 โดยคณะกรรมการโคดาซิล (The Conference on Data Systems Languages - CODASYL) มีจุดเด่นคือ สามารถใช้งานแฟ้มข้อมูลได้หลายแบบ กำหนดโครงสร้างข้อมูลได้สะดวก มีลักษณะการเขียนโปรแกรม แบบเอกสารอธิบายโปรแกรม ช่วยให้นักพัฒนารุ่นถัดไปเข้าใจได้ง่าย

การเขียนโปรแกรมภาษา COBOL เป็นภาษาที่ง่ายมากภาษาหนึ่ง เพราะไม่มีลูกเล่นให้ใช้มาก ๆ เหมือนพวก VB, C, Pascal หรือ dBase


หน้าที่ของภาษาโคบอล (COBAL)

                    อ่านข้อมูลจากแฟ้มมาประมวลผลทางธุรกิจเป็นหลัก สำหรับ ผู้เรียนมือใหม่ อาจบอกว่าภาษานี้ยาก เพราะพวกเขาอาจไปยึดติดกับการจดจำ division ต่าง ๆ ว่ามีอะไรบ้าง ซึ่งสิ่งเหล่านั้นเป็นเพียงหน้ากาก มิใช่ concept ของภาษา ถ้ามีคู่มือสักเล่ม ก็จะเข้าใจ และแกะหน้ากากเหล่านั้นออกได้ แล้วก้าวให้ลึกเข้าไปสู่ตัวภาษาได้โดยง่าย

ภาษา COBOL ประกอบด้วย 4 Division ได้แก่
1. Identification division การกำหนดชื่อโปรแกรมและชื่อผู้เขียน
2. Environment division การอธิบายเกี่ยวกับคอมพิวเตอร์
3. Data division การอธิบายเกี่ยวกับการประมวลผลข้อมูล
4. Procedure division การอธิบายเกี่ยวกับขั้นตอนการประมวลผล


ข้อดีข้อเสีย ของภาษาโคบอล (COBAL)


ข้อดี คือ สามารถเขียนโปรแกรมเพื่อใช้งานได้ทั้งบนไมโครคอมพิวเตอร์ มินิคอมพิวเตอร์ และเมนเฟรมคอมพิวเตอร์ โดยอาจต้องแก้ไขโปรแกรมเพียงเล็กน้อย และยังสามารถจัดการเกี่ยวกับข้อมูลเข้า/ออก ได้ง่าย มีลักษณะใกล้เคียงกับภาษาอังกฤษ ทำให้เป็นภาษาที่อ่านและเข้าใจได้ง่าย และมีความเป็นระเบียบเรียบร้อยในการเขียนโปรแกรม

ข้อเสีย คือ มีความยาวในการเขียนโปรแกรมค่อนข้างมาก และเยิ่นเย้อ ไม่เหมาะกับการคำนวณที่ซับซ้อน

 
ตัวอย่างคำสั่งภาษาโคบอล (COBAL)

1.move 1 to a
2.add 1 to a
3.compute a = a + 1
4.accept a
5.accept a with background-color is 2 foreground-collor is 0
6.display 5 line 5 col 10
7.display 5 with no advancing
8.display 5 at 0950
9.if a = 1 display "one"
10.if a =1 display "one" else display "other".
11.read xfile at end move "1" to flag-end.
12.perform x-para
13.perform y-para until flag-end = "1"
14.perform z-para 10 times
15.perform z-para until i >= 12
16.stop run.
17.open input file1 fil2
18.open output file3 file4
19.close file1
20.write outrec from inrec before advancing 1 line
21.file-control.
 select salafile assign to "d.dat"
 organization is indexed access is sequential record key is sid.


ตัวอย่างชุดภาษาโคบอล (COBAL)

ตัวอย่างโปรแกรมอย่างง่าย

ตัวอย่าง 1 : การแสดงผลอย่างง่าย
:: ให้เขียนโปรแกรมพิมพ์เลข 5 บนจอภาพ
ไม่ต้องเขียนให้ครบทุก division ก็ compile แล้วสั่งประมวลผลได้
อย่างลืมว่า procedure เริ่มหลักที่ 8 และ display เริ่มหลักที่ 12 เพราะเป็นกฎเหล็กของภาษา
รหัสต้นฉบับ
procedure division.
display 5.

ตัวอย่าง 2 : ผ่าน หรือตก
:: รับค่าไปเปรียบเทียบ แล้วพิมพ์ pass หรือ fail
รหัสต้นฉบับ
working-storage section.
01 s   pic 99.
procedure division.
accept s.
if s > 50  display "pass".
if s <= 50 display "fail".
stop run.


working-storage section.
01 s   pic 99.
procedure division.
accept s.
if s > 50 
  display "pass"
else
  display "fail".
stop run.


ตัวอย่างเกี่ยวกับการประมวลผลแฟ้มข้อมูล

ตัวอย่าง : read and write to file
:: อ่านข้อมูลจากแฟ้มมาพิมพ์โดยสมบูรณ์ (ตามหน้า 6 ในหนังสือ)
ก่อนเขียนโปรแกรม ต้องสร้างแฟ้มข้อมูล inf.txt ซึ่งมีขนาด 34 Byte (17 Byte * 2 Records)
Data sample
012345678901234
012345678901234
ผลการ run โปรแกรมข้างล่างนี้ จะสร้างแฟ้ม outf.txt (34 Byte) ซึ่งเหมือนแฟ้ม inf.txt ทุกประการ

รหัสต้นฉบับ
       identification division.
       program-id.  sample.
       author.      burin  rujjanapan.
       installation.  lcct.
       date-written.  20-09-97.
       date-compiler. 20-09-97.
       environment division.
       configuration section.
              source-computer. pc.
              object-computer. pc.
       input-output section.
       file-control.
              select infile assign to disk.
              select outfile assign to disk.
       data division.
       file section.
       fd infile record contains 17 characters
              label record is standdard
              value of file-id "inf.txt".
       01 inrec pic x(17).
       fd outfile record contains 15 characters
              label record is standard
              value of file-id "outf.txt".
       01 outrec pic x(15).
       working-storage section.
       77 num      pic 99 value 0.
       77 flag-end pic x  value "0".
       procedure division.
       initial-para.
              open input infile
                   output outfile.
              read infile at end move "1" to flag-end.
              perform main-para until flag-end = "1".
              close outfile.
              display num.
              stop run.
       main-para.
              add 1 to num.
              write outrec from inrec before advancing 1 line.
              read infile at end move "1" to flag-end.

อ้างอิง

·         http://www.thaiall.com/cobol/indexo.html
·         http://www.com5dow.com
·         http://www.thaigoodview.com
·         http://krusunanta.net
·         http://th.wikipedia.org


ฝากรูป

 

ไม่มีความคิดเห็น:

แสดงความคิดเห็น