mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
parent
6c74ead70b
commit
ef947a29d0
@ -0,0 +1,188 @@
|
|||||||
|
[#]: subject: "BASIC vs. FORTRAN 77: Comparing programming blasts from the past"
|
||||||
|
[#]: via: "https://opensource.com/article/23/4/basic-vs-fortran-77"
|
||||||
|
[#]: author: "Jim Hall https://opensource.com/users/jim-hall"
|
||||||
|
[#]: collector: "lkxed"
|
||||||
|
[#]: translator: "ChatGPT"
|
||||||
|
[#]: reviewer: "wxy"
|
||||||
|
[#]: publisher: "wxy"
|
||||||
|
[#]: url: "https://linux.cn/article-15897-1.html"
|
||||||
|
|
||||||
|
BASIC 与 FORTRAN 77:比较过去的编程语言
|
||||||
|
======
|
||||||
|
|
||||||
|
![][0]
|
||||||
|
|
||||||
|
> 我通过编写一个示例程序来探索 BASIC 和 FORTRAN 77 中的 FOR 循环,以将数字列表从 1 加到 10。
|
||||||
|
|
||||||
|
如果你和我一样,在 20 世纪七八十年代使用计算机长大,你可能学过一种常见的个人计算机编程语言,名为 BASIC(全称是 “<ruby>初学者的通用符号指令代码<rt>Beginner's All-purpose Symbolic Instruction Code</rt></ruby>”)。那个时期,包括 TRS-80、Apple II 和 IBM PC 在内的每台个人计算机都可以找到 BASIC 实现。当时,我是一个自学的 BASIC 程序员,在尝试了 Apple II 上的 AppleSoft BASIC 后,转向 IBM PC 上的 GW-BASIC,后来在 DOS 上学习了 QuickBASIC。
|
||||||
|
|
||||||
|
但是曾经,一种在科学编程领域受欢迎的语言是 FORTRAN(即 “<ruby>公式翻译<rt>FORmula TRANslation</rt></ruby>”)。尽管在 1990 年对该语言进行的规范以后,该名称更常见的风格是 “Fortran”。
|
||||||
|
|
||||||
|
当我在 1990 年代初作为大学本科物理学生学习物理学时,我利用自己在 BASIC 上的经验学习了 FORTRAN 77。那时我意识到 BASIC 许多概念都来源于 FORTRAN。当然,FORTRAN 和 BASIC 在很多其他方面也存在差异,但我发现了解一点 BASIC 可以帮助我快速学习 FORTRAN 编程。
|
||||||
|
|
||||||
|
我想通过使用两种语言编写相同的程序,展示它们之间的一些相似之处。通过编写一个示例程序来探索 BASIC 和 FORTRAN 77 中的 `FOR` 循环,这个程序将把 1 到 10 之间的数字相加。
|
||||||
|
|
||||||
|
### Bywater BASIC
|
||||||
|
|
||||||
|
BASIC 存在许多种不同的版本,这取决于你的计算机,但该语言总体保持不变。我喜欢的一种 BASIC 版本是 [Bywater BASIC][1],这是一种开源的 BASIC 实现,适用于包括 Linux 和 DOS 在内的不同平台。
|
||||||
|
|
||||||
|
要在 FreeDOS 上使用 Bywater BASIC,你必须首先从 FreeDOS 1.3 Bonus CD 中 [安装该软件包][2]。然后进入 `C:` 目录并输入 `bwbasic` 命令,这将启动 BASIC 解释器。你可以在这个提示符下输入程序:
|
||||||
|
|
||||||
|
```
|
||||||
|
bwBASIC:
|
||||||
|
```
|
||||||
|
|
||||||
|
Bywater BASIC 使用较早的 BASIC 编程标准,需要你在每个程序指令上编写一个行号。将行号视为索引。你可以使用行号轻松地引用程序中的任何指令。当你将程序键入 Bywater BASIC 解释器时,请在每个指令前添加行号:
|
||||||
|
|
||||||
|
```
|
||||||
|
bwBASIC: 10 print "Add the numbers from 1 to 10 ..."
|
||||||
|
bwBASIC: 20 sum = 0
|
||||||
|
bwBASIC: 30 for i = 1 to 10
|
||||||
|
bwBASIC: 40 sum = sum + i
|
||||||
|
bwBASIC: 50 next i
|
||||||
|
bwBASIC: 60 print sum
|
||||||
|
bwBASIC: 70 end
|
||||||
|
```
|
||||||
|
|
||||||
|
可以使用 `list` 命令查看你已经输入到解释器中的程序:
|
||||||
|
|
||||||
|
```
|
||||||
|
bwBASIC: list
|
||||||
|
10 print "Add the numbers from 1 to 10 ..."
|
||||||
|
20 sum = 0
|
||||||
|
30 for i = 1 to 10
|
||||||
|
40 sum = sum + i
|
||||||
|
50 next i
|
||||||
|
60 print sum
|
||||||
|
70 end
|
||||||
|
```
|
||||||
|
|
||||||
|
这个简短的程序演示了 BASIC 中的 `FOR` 循环。 `FOR` 是任何编程语言中最基本的循环构造,允许你迭代一组值。在 Bywater BASIC 中,`FOR` 循环的一般语法看起来像这样:
|
||||||
|
|
||||||
|
```
|
||||||
|
FOR 变量 = 起始值 TO 终止值
|
||||||
|
```
|
||||||
|
|
||||||
|
在这个示例程序中,指令 `for i = 1 to 10` 开始一个循环,迭代值为 1 到 10。在每个循环中,变量 `i` 被设置为新值。
|
||||||
|
|
||||||
|
在 BASIC 中,所有到 `next` 指令前的指令都将作为 `FOR` 循环的一部分执行。因为你可以将一个 `FOR` 循环放入另一个 `FOR` 循环中,Bywater BASIC 使用语法 `NEXT 变量` 来指定要迭代的循环变量。
|
||||||
|
|
||||||
|
在提示符下键入 `run` 来执行程序:
|
||||||
|
|
||||||
|
```
|
||||||
|
bwBASIC: run
|
||||||
|
Add the numbers from 1 to 10 ...
|
||||||
|
55
|
||||||
|
```
|
||||||
|
|
||||||
|
Bywater BASIC 被称为 BASIC 解释器,因为只能从 Bywater BASIC 环境中运行程序。这意味着解释器会处理与操作系统的交互的所有繁重工作,因此你的程序不需要自己完成这个工作。 这样做的代价是,程序在解释环境中运行会比它作为编译程序运行慢一些。
|
||||||
|
|
||||||
|
### FreeBASIC
|
||||||
|
|
||||||
|
另一个流行的 BASIC 实现是 [FreeBASIC][3],这是一个开源的 BASIC 编译器,适用于多个平台,包括 Linux 和 DOS。要使用 FreeBASIC,你需要从 FreeDOS 1.3 Bonus CD 安装 FreeBASIC 包,然后进入 `C:` 目录,你会在这里找到 FreeBASIC 程序。
|
||||||
|
|
||||||
|
FreeBASIC 是一个编译器,因此你首先需要创建一个包含程序指令的源文件,然后使用源代码运行编译器以创建一个可运行的程序。我编写了一个类似于“将 1 到 10 的数字相加”的程序版本,将其保存为 BASIC 文件,并命名为 `sum.bas`:
|
||||||
|
|
||||||
|
```
|
||||||
|
dim sum as integer
|
||||||
|
dim i as integer
|
||||||
|
print "Add the numbers from 1 to 10 ..."
|
||||||
|
sum = 0
|
||||||
|
for i = 1 to 10
|
||||||
|
sum = sum + i
|
||||||
|
next
|
||||||
|
print sum
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
如果你将这段代码与 Bywater BASIC 版本的程序进行比较,你可能会注意到 FreeBASIC 不需要行号。FreeBASIC 实现了一种更现代的 BASIC 版本,使得编写程序时不需要跟踪行号更容易。
|
||||||
|
|
||||||
|
另一个主要的区别是你必须在源代码中定义或声明变量。使用 `DIM` 指令在 FreeBASIC 中声明变量,例如 `dim sum as integer`,以定义一个名为 `sum` 的整数变量。
|
||||||
|
|
||||||
|
现在可以在命令行上使用 `fbc` 编译 BASIC 程序:
|
||||||
|
|
||||||
|
```
|
||||||
|
C:\DEVEL\FBC> fbc sum.bas
|
||||||
|
```
|
||||||
|
|
||||||
|
如果你的代码没有任何错误,编译器将生成一个可以运行的程序。例如,我的程序现在称为 `sum`。运行我的程序将从 1 加到 10:
|
||||||
|
|
||||||
|
```
|
||||||
|
C:\DEVEL\FBC> sum
|
||||||
|
Add the numbers from 1 to 10 ...
|
||||||
|
55
|
||||||
|
```
|
||||||
|
|
||||||
|
### FORTRAN 77
|
||||||
|
|
||||||
|
FORTRAN 编程语言类似于旧式和现代 BASIC 之间的混合体。FORTRAN 比 BASIC 更早出现,而 BASIC 显然从 FORTRAN 中汲取灵感,就像后来的 FORTRAN 版本从 BASIC 中获得启示一样。你可以将 FORTRAN 程序以源代码的形式写成文件,但并不需要在每个地方使用行号。但是,FORTRAN 77 在某些指令中使用行号(称为标签),包括 `FOR` 循环。在 FORTRAN 77 中,`FOR` 实际上被称为 `DO` 循环,它执行相同的功能并具有几乎相同的用法。
|
||||||
|
|
||||||
|
在 FORTRAN 77 中,`DO` 循环的语法如下:
|
||||||
|
|
||||||
|
```
|
||||||
|
DO 行号 变量 = 起始值, 终止值
|
||||||
|
```
|
||||||
|
|
||||||
|
这种情况是需要行号来指示 `DO` 循环结束位置的一种情况。你在 BASIC 中使用了 `NEXT` 指令,但 FORTRAN 需要一个行标签。通常,该行是一个 `CONTINUE` 指令。
|
||||||
|
|
||||||
|
查看这个示例 FORTRAN 程序,了解如何使用 `DO` 循环来循环一组数字。我将此源文件保存为 `sum.f`:
|
||||||
|
|
||||||
|
```
|
||||||
|
PROGRAM MAIN
|
||||||
|
INTEGER SUM,I
|
||||||
|
PRINT *, 'ADD THE NUMBERS FROM 1 TO 10 ...'
|
||||||
|
SUM = 0
|
||||||
|
DO 10 I = 1, 10
|
||||||
|
SUM = SUM + I
|
||||||
|
10 CONTINUE
|
||||||
|
PRINT *, SUM
|
||||||
|
END
|
||||||
|
```
|
||||||
|
|
||||||
|
在 FORTRAN 中,每个程序都需要以 `PROGRAM` 指令开始,并指定程序名称。你可能会将此程序命名为 `SUM`,但随后在程序中不能使用变量 `SUM`。当我学习 FORTRAN 时,我从 C 编程中借鉴了一些东西,并以 `PROGRAM MAIN` 开始了我的所有 FORTRAN 程序,做法类似于 C 程序中的 `main()` 函数,因为我不太可能使用名为 `MAIN` 的变量。
|
||||||
|
|
||||||
|
FORTRAN 中的 `DO` 循环类似于 BASIC 中的 `FOR` 循环。它迭代从 1 到 10 的值。变量 `I` 在每次循环中获取新值。这样可以将 1 到 10 的每个数字相加,并在完成时打印总和。
|
||||||
|
|
||||||
|
你可以在每个平台上找到适合的 FORTRAN 编译器,包括 Linux 和 DOS。FreeDOS 1.3 的 Bonus CD 中包括 OpenWatcom FORTRAN 编译器。在 Linux 上,你可能需要安装一个包来安装 GNU Fortran 支持(在 GNU 编译器集合(GCC)中)。在 Fedora Linux 上,你可以使用以下命令添加 GNU Fortran 支持:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sudo dnf install gcc-gfortran
|
||||||
|
```
|
||||||
|
|
||||||
|
然后你可以使用以下命令编译 `sum.f` 并运行程序:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ gfortran -o sum sum.f
|
||||||
|
$ ./sum
|
||||||
|
ADD THE NUMBERS FROM 1 TO 10 ...
|
||||||
|
55
|
||||||
|
```
|
||||||
|
### 一点不同之处
|
||||||
|
|
||||||
|
我发现 FORTRAN 和 BASIC 非常相似,但也存在一些不同之处。这些语言的核心是不同的,但如果你了解一些 BASIC,你可以学习 FORTRAN,同样,如果你了解一些 FORTRAN,你也可以学习 BASIC。
|
||||||
|
|
||||||
|
如果你想探索这两种语言,有几点需要注意:
|
||||||
|
|
||||||
|
- **FORTRAN 77 使用全大写**,但后来的 FORTRAN 版本允许大小写混用,只要对变量、函数和子程序使用相同的大小写。大多数 BASIC 实现都不区分大小写,这意味着你可以自由地混合大小写字母。
|
||||||
|
- **有许多不同版本的 BASIC**,但它们通常做同样的事情。如果你学会了一种 BASIC 实现方式,很容易学会另一种。注意 BASIC 解释器或编译器的警告或错误信息,查阅手册了解差异。
|
||||||
|
- **某些 BASIC 实现需要使用行号**,例如 Bywater BASIC 和 GW-BASIC。更现代的 BASIC 版本允许你编写不使用行号的程序。FreeBASIC 需要使用 `-lang` 废弃选项编译带有行号的程序。
|
||||||
|
|
||||||
|
*(题图:MJ/dba28597-dd62-4ffe-bb4a-e38874a65239)*
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://opensource.com/article/23/4/basic-vs-fortran-77
|
||||||
|
|
||||||
|
作者:[Jim Hall][a]
|
||||||
|
选题:[lkxed][b]
|
||||||
|
译者:ChatGPT
|
||||||
|
校对:[wxy](https://github.com/wxy)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://opensource.com/users/jim-hall
|
||||||
|
[b]: https://github.com/lkxed/
|
||||||
|
[1]: https://sourceforge.net/projects/bwbasic/
|
||||||
|
[2]: https://opensource.com/article/21/6/freedos-package-manager
|
||||||
|
[3]: https://www.freebasic.net/
|
||||||
|
[0]: https://img.linux.net.cn/data/attachment/album/202306/11/110557i6ef2ep92petw2d1.jpg
|
@ -1,182 +0,0 @@
|
|||||||
[#]: subject: "BASIC vs. FORTRAN 77: Comparing programming blasts from the past"
|
|
||||||
[#]: via: "https://opensource.com/article/23/4/basic-vs-fortran-77"
|
|
||||||
[#]: author: "Jim Hall https://opensource.com/users/jim-hall"
|
|
||||||
[#]: collector: "lkxed"
|
|
||||||
[#]: translator: " "
|
|
||||||
[#]: reviewer: " "
|
|
||||||
[#]: publisher: " "
|
|
||||||
[#]: url: " "
|
|
||||||
|
|
||||||
BASIC vs. FORTRAN 77: Comparing programming blasts from the past
|
|
||||||
======
|
|
||||||
|
|
||||||
If you grew up with computers in the 1970s and 1980s, as I did, you probably learned a common programming language for personal computers called BASIC, or the Beginner's All-purpose Symbolic Instruction Code. You could find BASIC implementations on every personal computer of the era, including the TRS-80, Apple II, and the IBM PC. Back then, I was a self-taught BASIC programmer, experimenting with AppleSoft BASIC on the Apple II before moving to GW-BASIC on the IBM PC and, later, to QuickBASIC on DOS.
|
|
||||||
|
|
||||||
But once upon a time, a popular language for scientific programming was FORTRAN, short for FORmula TRANslation. Although since the 1990 specification of the language, the name is more commonly stylized as "Fortran."
|
|
||||||
|
|
||||||
When I studied physics as a university undergraduate student in the early 1990s, I leveraged my experience in BASIC to learn FORTRAN 77. That was when I realized that BASIC derived many of its concepts from FORTRAN. To be clear, FORTRAN and BASIC differ in lots of other ways, but I found that knowing a little BASIC helped me to learn FORTRAN programming quickly.
|
|
||||||
|
|
||||||
I want to show some similarities between the two languages by writing the same program in both. I'll explore the `FOR` loop in BASIC and FORTRAN 77 by writing a sample program to add a list of numbers from 1 to 10.
|
|
||||||
|
|
||||||
### Bywater BASIC
|
|
||||||
|
|
||||||
BASIC came in many flavors, depending on your computer, but the overall language remained the same. One version of BASIC that I like is [Bywater BASIC][1], an open source implementation of BASIC available for different platforms, including Linux and DOS.
|
|
||||||
|
|
||||||
To use Bywater BASIC on FreeDOS, you must first [install the package][2] from the FreeDOS 1.3 Bonus CD. To run it, go into the C: directory and type `bwbasic`. This command starts the BASIC interpreter. You can enter your program from this prompt:
|
|
||||||
|
|
||||||
```
|
|
||||||
bwBASIC:
|
|
||||||
```
|
|
||||||
|
|
||||||
Bywater BASIC uses an older BASIC programming standard that requires you to write every program instruction with a line number. Think of a line number like an index. You can easily refer to any instruction in the program with line numbers. As you type the program into the Bywater BASIC interpreter, add the line number before each instruction:
|
|
||||||
|
|
||||||
```
|
|
||||||
bwBASIC: 10 print "Add the numbers from 1 to 10 ..."
|
|
||||||
bwBASIC: 20 sum = 0
|
|
||||||
bwBASIC: 30 for i = 1 to 10
|
|
||||||
bwBASIC: 40 sum = sum + i
|
|
||||||
bwBASIC: 50 next i
|
|
||||||
bwBASIC: 60 print sum
|
|
||||||
bwBASIC: 70 end
|
|
||||||
```
|
|
||||||
|
|
||||||
Use the `list` command to view the program you have entered into the interpreter:
|
|
||||||
|
|
||||||
```
|
|
||||||
bwBASIC: list
|
|
||||||
10 print "Add the numbers from 1 to 10 ..."
|
|
||||||
20 sum = 0
|
|
||||||
30 for i = 1 to 10
|
|
||||||
40 sum = sum + i
|
|
||||||
50 next i
|
|
||||||
60 print sum
|
|
||||||
70 end
|
|
||||||
```
|
|
||||||
|
|
||||||
This short program demonstrates the `FOR` loop in BASIC. `FOR` is the most fundamental loop construct in any programming language, allowing you to iterate over a set of values. The general syntax of the `FOR` loop in Bywater BASIC looks like this:
|
|
||||||
|
|
||||||
```
|
|
||||||
FOR var = start TO end
|
|
||||||
```
|
|
||||||
|
|
||||||
In this example program, the instruction `for i = 1 to 10` starts a loop that iterates through the values 1 to 10. At each pass through the loop, the variable `i` is set to the new value.
|
|
||||||
|
|
||||||
In BASIC, all instructions up to the next instruction are executed as part of the `FOR` loop. Because you can put one `FOR` loop inside another, Bywater BASIC uses the syntax `NEXT variable` to specify which loop variable to iterate.
|
|
||||||
|
|
||||||
Type `run` at the prompt to execute the program:
|
|
||||||
|
|
||||||
```
|
|
||||||
bwBASIC: run
|
|
||||||
Add the numbers from 1 to 10 ...
|
|
||||||
55
|
|
||||||
```
|
|
||||||
|
|
||||||
Bywater BASIC is called a BASIC interpreter because you can only run the program from inside the Bywater BASIC environment. This means the interpreter does all the hard work of interacting with the operating system, so your program doesn't need to do that on its own, with the trade-off that the program runs a little slower in the interpreted environment than it might if it were a compiled program.
|
|
||||||
|
|
||||||
### FreeBASIC
|
|
||||||
|
|
||||||
Another popular implementation of BASIC is [FreeBASIC][3], an open source BASIC compiler for several platforms, including Linux and DOS. To use FreeBASIC, you'll need to install the FreeBASIC package from the FreeDOS 1.3 Bonus CD, then change into the C: directory where you'll find the FreeBASIC programs.
|
|
||||||
|
|
||||||
FreeBASIC is a compiler, so you first create a source file with your program instructions, then run the compiler with the source code to create a program you can run. I wrote a similar version of the "add the numbers from 1 to 10" program as this BASIC file, which I saved as `sum.bas`:
|
|
||||||
|
|
||||||
```
|
|
||||||
dim sum as integer
|
|
||||||
dim i as integer
|
|
||||||
print "Add the numbers from 1 to 10 ..."
|
|
||||||
sum = 0
|
|
||||||
for i = 1 to 10
|
|
||||||
sum = sum + i
|
|
||||||
next
|
|
||||||
print sum
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
If you compare this code to the Bywater BASIC version of the program, you may notice that FreeBASIC doesn't require line numbers. FreeBASIC implements a more modern version of BASIC that makes it easier to write programs without keeping track of line numbers.
|
|
||||||
|
|
||||||
Another key difference is that you must define or declare your variables in your source code. Use the `DIM` instruction to declare a variable in FreeBASIC, such as `dim sum as integer`, to define an integer variable called `sum`.
|
|
||||||
|
|
||||||
Now you can compile the BASIC program using `fbc` on the command line:
|
|
||||||
|
|
||||||
```
|
|
||||||
C:\DEVEL\FBC> fbc sum.bas
|
|
||||||
```
|
|
||||||
|
|
||||||
If your code doesn't have any errors in it, the compiler generates a program that you can run. For example, my program is now called `sum`. Running my program adds up the numbers from 1 to 10:
|
|
||||||
|
|
||||||
```
|
|
||||||
C:\DEVEL\FBC> sum
|
|
||||||
Add the numbers from 1 to 10 ...
|
|
||||||
55
|
|
||||||
```
|
|
||||||
|
|
||||||
### FORTRAN 77
|
|
||||||
|
|
||||||
The FORTRAN programming language is like a hybrid between old-style and modern BASIC. FORTRAN came before BASIC, and BASIC clearly took inspiration from FORTRAN, just as later versions of FORTRAN took cues from BASIC. You write FORTRAN programs as source code in a file but you don't use line numbers everywhere. However, FORTRAN 77 does use line numbers (called labels) for certain instructions, including the `FOR` loop. Although in FORTRAN 77, the `FOR` is actually called a `DO` loop, it does the same thing and has almost the same usage.
|
|
||||||
|
|
||||||
In FORTRAN 77, the `DO` loop syntax looks like this:
|
|
||||||
|
|
||||||
```
|
|
||||||
DO label var = start, end
|
|
||||||
```
|
|
||||||
|
|
||||||
This situation is one of the instances where you need a line number to indicate where the `DO` loop ends. You used a `NEXT` instruction in BASIC, but FORTRAN requires a line label instead. Typically, that line is a `CONTINUE` instruction.
|
|
||||||
|
|
||||||
Look at this sample FORTRAN program to see how to use `DO` to loop over a set of numbers. I saved this source file as `sum.f`:
|
|
||||||
|
|
||||||
```
|
|
||||||
PROGRAM MAIN
|
|
||||||
INTEGER SUM,I
|
|
||||||
PRINT *, 'ADD THE NUMBERS FROM 1 TO 10 ...'
|
|
||||||
SUM = 0
|
|
||||||
DO 10 I = 1, 10
|
|
||||||
SUM = SUM + I
|
|
||||||
10 CONTINUE
|
|
||||||
PRINT *, SUM
|
|
||||||
END
|
|
||||||
```
|
|
||||||
|
|
||||||
In FORTRAN, every program needs to start with the PROGRAM instruction, with a name for the program. You might name this program `SUM`, but then you cannot use the variable `SUM` later in the program. When I learned FORTRAN, I borrowed from C programming and started all of my FORTRAN programs with `PROGRAM MAIN`, like the `main()` function in C programs, because I was unlikely to use a variable called `MAIN`.
|
|
||||||
|
|
||||||
The `DO` loop in FORTRAN is similar to the `FOR` loop in BASIC. It iterates over values from 1 to 10. The variable `I` gets the new value at each pass over the loop. This allows you to add each number from 1 to 10 and print the sum when you're done.
|
|
||||||
|
|
||||||
You can find FORTRAN compilers for every platform, including Linux and DOS. FreeDOS 1.3 includes the OpenWatcom FORTRAN compiler on the Bonus CD. On Linux, you may need to install a package to install GNU Fortran support in the GNU Compiler Collection (GCC). On Fedora Linux, you use the following command to add GNU Fortran support:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ sudo dnf install gcc-gfortran
|
|
||||||
```
|
|
||||||
|
|
||||||
Then you can compile `sum.f` and run the program with these commands:
|
|
||||||
|
|
||||||
```
|
|
||||||
$ gfortran -o sum sum.f
|
|
||||||
$ ./sum
|
|
||||||
ADD THE NUMBERS FROM 1 TO 10 ...
|
|
||||||
55
|
|
||||||
```
|
|
||||||
|
|
||||||
### A few differences
|
|
||||||
|
|
||||||
I find that FORTRAN and BASIC are very similar, but with some differences. The core languages are different, but if you know a little of BASIC, you can learn FORTRAN. And if you know some FORTRAN, you can learn BASIC.
|
|
||||||
|
|
||||||
If you want to explore both of these languages, here are a few things to keep in mind:
|
|
||||||
|
|
||||||
- **FORTRAN 77 uses all uppercase,** but later versions of FORTRAN allow mixed cases as long as you use the same capitalization for variables, functions, and subroutines. Most implementations of BASIC are case-insensitive, meaning you can freely mix uppercase and lowercase letters.
|
|
||||||
- **There are many different versions of BASIC,** but they usually do the same thing. If you learn one BASIC implementation, you can easily learn how to use a different one. Watch for warnings or error messages from the BASIC interpreter or compiler, and explore the manual to find the differences.
|
|
||||||
- **Some BASIC implementations require line numbers**, such as Bywater BASIC and GW-BASIC. More modern BASIC versions allow you to write programs without line numbers. FreeBASIC requires the `-lang` deprecated option to compile programs with line numbers.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://opensource.com/article/23/4/basic-vs-fortran-77
|
|
||||||
|
|
||||||
作者:[Jim Hall][a]
|
|
||||||
选题:[lkxed][b]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://opensource.com/users/jim-hall
|
|
||||||
[b]: https://github.com/lkxed/
|
|
||||||
[1]: https://sourceforge.net/projects/bwbasic/
|
|
||||||
[2]: https://opensource.com/article/21/6/freedos-package-manager
|
|
||||||
[3]: https://www.freebasic.net/
|
|
Loading…
Reference in New Issue
Block a user