关于COBOL的简短介绍

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>.cob

Write 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.cob

and 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.cob

Run it:

./sum

and 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: