COBOL是您不斷聽到的那些語言之一,也是過去的遺留之一。
事實證明,這是使世界運轉的語言,尤其是在銀行和金融機構中。我在某處讀到,超過70%的業務交易是通過用COBOL編寫的程序進行的。
有多種原因。首先,該語言是為該用例設計的。
畢竟,它被稱為面向商業的COmmon語言。
一種無聊的名字。但這是直截了當的。
另一個原因是它很舊。它於1959年設計,從一開始就用於製造這些系統,沒有人會更改那些運行良好的程序。
這些程序是如此重要,以至於它們只是被維護和改進,而從未從頭開始重寫。
無論如何,您可以在Wikipedia上了解有關COBOL的歷史。這裡的目標是快速介紹該語言,因此,下次您聽到COBOL時,就會知道它的外觀。
安裝GNU COBOL編譯器
安裝gnu-cobol
。
在Mac上,使用自製酒:
brew install gnu-cobol
或使用任何方式可以在操作系統上安裝GNU命令(提示:Homebrew在Win / Linux上也可以使用)
完成此操作後,您將可以訪問cobc
命令。
這是手冊頁:
man cobc
我在網上發現的一些說明涉及安裝IDE(集成開發環境),但是您無需進行任何測試。
用以下代碼編寫您的COBOL程序.cob
文件,並使用進行編譯
cobc -x <filename>.cobWrite the COBOL Hello, World!
I created a hello.cob
file and opened it in VS Code.
Immediately a popup told me some extensions could help with .cob
files. I’m impressed.

I’m going to install the first and most popular, named COBOL, to provide syntax highlighting.
Now add this code to the hello.cob
file:
HELLO
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello, World!".
STOP RUN.Compile it from the command line:
cobc -x hello.coband then run the binary file generated:
./hello
This was simple.
Sum two numbers received from the user
Now create a sum.cob
file:
HELLO
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDITION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUM_1 PIC 9(4).
77 NUM_2 PIC 9(4).
77 SOLVE_SUM PIC 9(4).
PROCEDURE DIVISION.
PARA.
DISPLAY "First number: ".
ACCEPT NUM_1.
DISPLAY "Second number: ".
ACCEPT NUM_2.
COMPUTE SOLVE_SUM = NUM_1 + NUM_2.
DISPLAY "Sum: " SOLVE_SUM.
STOP RUN.Compile it:
cobc -x sum.cobRun it:
./sumand you’ll be asked for 2 numbers, then the program calculates the sum:

Note that I have no idea how those programs run, the instructions meanings, but I just wanted to try it out.
I think this is all the COBOL I’ll ever write in my life.
More computers tutorials:
- Finite State Machines
- The Decimal Number System
- The Binary Number System
- Converting Numbers from Decimal to Binary
- Printable ASCII characters list
- Non-printable ASCII characters list
- How to connect to a Raspberry Pi using a Mac
- How to make sure the Raspberry Pi has always the same IP address
- A very short introduction to COBOL