mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
commit
ed9c565556
@ -1,37 +1,38 @@
|
||||
[#]: subject: (How different programming languages do the same thing)
|
||||
[#]: via: (https://opensource.com/article/21/4/compare-programming-languages)
|
||||
[#]: author: (Jim Hall https://opensource.com/users/jim-hall)
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (VeryZZJ)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
||||
How different programming languages do the same thing
|
||||
[#]: subject: "How different programming languages do the same thing"
|
||||
[#]: via: "https://opensource.com/article/21/4/compare-programming-languages"
|
||||
[#]: author: "Jim Hall https://opensource.com/users/jim-hall"
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: "VeryZZJ"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
不同编程语言是如何完成同一件事
|
||||
======
|
||||
Compare 13 different programming languages by writing a simple game.
|
||||
|
||||
通过一个简单的小游戏比较13种编程语言
|
||||
![Developing code.][1]
|
||||
|
||||
Whenever I start learning a new programming language, I focus on defining variables, writing a statement, and evaluating expressions. Once I have a general understanding of those concepts, I can usually figure out the rest on my own. Most programming languages have some similarities, so once you know one programming language, learning the next one is a matter of figuring out the unique details and recognizing the differences.
|
||||
当我开始学习一种新的编程语言时,会把重点放在定义变量、书写声明以及计算表达式,一旦对这些概念有一个大致的了解,通常就能够自己弄清剩下的部分。大多数编程语言都具有相似性,所以如果你掌握了一种编程语言,学习下一种语言的重点就是弄清楚独有的概念以及区分不同。
|
||||
|
||||
To help me practice a new programming language, I like to write a few test programs. One sample program I often write is a simple "guess the number" game, where the computer picks a number between one and 100 and asks me to guess it. The program loops until I guess correctly. This is a very simple program, as you can see using pseudocode like this:
|
||||
我喜欢写一些测试程序来帮助练习新的编程语言。其中我经常写的是一个叫做“猜数字”的小游戏,计算机选出1到100里的任一数字,然后我来猜。程序循环进行,直到猜出正确数字。通过伪代码可以看出,这是个非常简单的程序:
|
||||
|
||||
* The computer picks a random number between 1 and 100
|
||||
* Loop until I guess the random number
|
||||
+ The computer reads my guess
|
||||
+ It tells me if my guess is too low or too high
|
||||
* 计算机在1到100之间选出一个随机数字
|
||||
* 循环进行直到猜出该随机数字
|
||||
+ 计算机读取我的猜测
|
||||
+ 告诉我我的猜测过高还是过低
|
||||
|
||||
Recently, Opensource.com ran an article series that wrote this program in different languages. This was an interesting opportunity to compare how to do the same thing in each language. I also found that most programming languages do things similarly, so learning the next programming language is mostly about learning its differences.
|
||||
Opensource.com 最近发表了一篇文章,用不同的语言写这个程序。这是一个比较不同语言做同样事情的有趣机会。大多数编程语言具有相似性,所以当你在学习下一种新的编程语言时,主要是学习它的独特之处。
|
||||
|
||||
C is an early general-purpose programming language, created in 1972 at Bell Labs by Dennis Ritchie. C proved popular and quickly became a standard programming language on Unix systems. Because of its popularity, many other programming languages adopted a similar programming syntax. That's why learning C++, Rust, Java, Groovy, JavaScript, awk, or Lua is easier if you already know how to program in C.
|
||||
C 语言由 Dennis Ritchie 于1972年在贝尔实验室创建,是一种早期的通用编程语言。C 语言非常受欢迎,并迅速成为 Unix 系统上的标准编程语言。正是因为它的流行,许多其他编程语言也采用了类似的编程语法。这就是为什么如果你已经知道如何使用 C 语言编程,学习 C++、Rust、Java、Groovy、JavaScript、awk 或 Lua 会更容易。
|
||||
|
||||
For example, look at how these different programming languages implement the major steps in the "guess the number" game. I'll skip some of the surrounding code, such as assigning temporary variables, to focus on how the basics are similar or different.
|
||||
接下来我们看看这些不同的编程语言是如何实现 "猜数字 "游戏的主要步骤。我将把重点放在基本元素的相似或不同,跳过一些外围代码,如分配临时变量。
|
||||
|
||||
### The computer picks a random number between one and 100
|
||||
### 计算机在1到100之间选出一个随机数字
|
||||
|
||||
You can see a lot of similarities here. Most of the programming languages generate a random number with a function like `rand()` that you can put into a range on your own. Other languages use a special function where you can specify the range for the random value.
|
||||
你可以看到这里有许多相似之处。大多数编程语言使用类似`rand()` 的函数,你可以设定一个范围来生成随机数。而其他一些语言使用一个特殊的函数来设定范围生成随机数。
|
||||
|
||||
C
|
||||
|
||||
```c
|
||||
// Using the Linux `getrandom` system call
|
||||
getrandom(&randval, sizeof(int), GRND_NONBLOCK);
|
||||
@ -42,45 +43,53 @@ number = rand() % 100 + 1;
|
||||
```
|
||||
|
||||
C++
|
||||
|
||||
```cpp
|
||||
int number = rand() % 100+1;
|
||||
```
|
||||
|
||||
Rust
|
||||
|
||||
```rust
|
||||
let random = rng.gen_range(1..101);
|
||||
```
|
||||
|
||||
Java
|
||||
|
||||
```java
|
||||
private static final int NUMBER = r.nextInt(100) + 1;
|
||||
```
|
||||
|
||||
Groovy
|
||||
|
||||
```groovy
|
||||
int randomNumber = (new Random()).nextInt(100) + 1
|
||||
```
|
||||
|
||||
JavaScript
|
||||
|
||||
```javascript
|
||||
const randomNumber = Math.floor(Math.random() * 100) + 1
|
||||
```
|
||||
|
||||
awk
|
||||
|
||||
```awk
|
||||
randomNumber = int(rand() * 100) + 1
|
||||
```
|
||||
|
||||
Lua
|
||||
|
||||
```lua
|
||||
number = math.random(1,100)
|
||||
```
|
||||
|
||||
### Loop until I guess the random number
|
||||
### 循环进行直到我猜出该随机数字
|
||||
|
||||
Loops are usually done with a flow-control block such as `while` or `do-while`. The JavaScript implementation doesn't use a loop and instead updates the HTML page "live" until the user guesses the correct number. Awk supports loops, but it doesn't make sense to loop to read input because awk is based around data pipelines, so it reads input from a file instead of directly from the user.
|
||||
循环通常是用控制流程来实现的,如`while` 或`do-while`。JavaScript 中的实现没有使用循环,而是 "实时 "更新 HTML 页面,直到用户猜出正确的数字。Awk 虽然支持循环,但是通过循环读取输入信息是没有意义的,因为 awk 是基于数据管道的,所以它从文件而不是直接从用户读取输入信息。
|
||||
|
||||
C
|
||||
|
||||
```c
|
||||
do {
|
||||
…
|
||||
@ -88,6 +97,7 @@ do {
|
||||
```
|
||||
|
||||
C++
|
||||
|
||||
```cpp
|
||||
do {
|
||||
…
|
||||
@ -95,6 +105,7 @@ do {
|
||||
```
|
||||
|
||||
Rust
|
||||
|
||||
```rust
|
||||
for line in std::io::stdin().lock().lines() {
|
||||
…
|
||||
@ -103,6 +114,7 @@ for line in std::io::stdin().lock().lines() {
|
||||
```
|
||||
|
||||
Java
|
||||
|
||||
```java
|
||||
while ( guess != NUMBER ) {
|
||||
…
|
||||
@ -110,6 +122,7 @@ while ( guess != NUMBER ) {
|
||||
```
|
||||
|
||||
Groovy
|
||||
|
||||
```groovy
|
||||
while ( … ) {
|
||||
…
|
||||
@ -118,27 +131,29 @@ while ( … ) {
|
||||
```
|
||||
|
||||
Lua
|
||||
|
||||
```lua
|
||||
while ( player.guess ~= number ) do
|
||||
…
|
||||
end
|
||||
```
|
||||
|
||||
### The computer reads my guess
|
||||
### 计算机读取我的猜测
|
||||
|
||||
Different programming languages handle input differently. So there's some variation here. For example, JavaScript reads values directly from an HTML form, and awk reads data from its data pipeline.
|
||||
不同编程语言对输入的处理方式不同。例如,JavaScript 直接从 HTML 表单中读取数值,而 awk 则从数据管道中读取数据。
|
||||
|
||||
C
|
||||
```c
|
||||
scanf("%d", &guess);
|
||||
```
|
||||
|
||||
C++
|
||||
|
||||
```cpp
|
||||
cin >> guess;
|
||||
```
|
||||
|
||||
Rust
|
||||
|
||||
```rust
|
||||
let parsed = line.ok().as_deref().map(str::parse::<i64>);
|
||||
if let Some(Ok(guess)) = parsed {
|
||||
@ -147,37 +162,43 @@ if let Some(Ok(guess)) = parsed {
|
||||
```
|
||||
|
||||
Java
|
||||
|
||||
```java
|
||||
guess = player.nextInt();
|
||||
```
|
||||
|
||||
Groovy
|
||||
|
||||
```groovy
|
||||
response = reader.readLine()
|
||||
int guess = response as Integer
|
||||
```
|
||||
|
||||
JavaScript
|
||||
|
||||
```javascript
|
||||
let myGuess = guess.value
|
||||
```
|
||||
|
||||
awk
|
||||
|
||||
```awk
|
||||
guess = int($0)
|
||||
```
|
||||
|
||||
Lua
|
||||
|
||||
```lua
|
||||
player.answer = io.read()
|
||||
player.guess = tonumber(player.answer)
|
||||
```
|
||||
|
||||
### Tell me if my guess is too low or too high
|
||||
### 告诉我我的猜测过高还是过低
|
||||
|
||||
Comparisons are fairly consistent across these C-like programming languages, usually through an `if` statement. There's some variation in how each programming language prints output, but the print statement remains recognizable across each sample.
|
||||
在这些类 C 语言中,通常是通过`if`语句进行比较的。每种编程语言打印输出的方式有一些变化,但打印语句在每个样本中都是可识别的。
|
||||
|
||||
C
|
||||
|
||||
```c
|
||||
if (guess < number) {
|
||||
puts("Too low");
|
||||
@ -190,6 +211,7 @@ puts("That's right!");
|
||||
```
|
||||
|
||||
C++
|
||||
|
||||
```cpp
|
||||
if ( guess > number) { cout << "Too high.\n" << endl; }
|
||||
else if ( guess < number ) { cout << "Too low.\n" << endl; }
|
||||
@ -200,6 +222,7 @@ else {
|
||||
```
|
||||
|
||||
Rust
|
||||
|
||||
```rust
|
||||
_ if guess < random => println!("Too low"),
|
||||
_ if guess > random => println!("Too high"),
|
||||
@ -210,6 +233,7 @@ _ => {
|
||||
```
|
||||
|
||||
Java
|
||||
|
||||
```java
|
||||
if ( guess > NUMBER ) {
|
||||
System.out.println("Too high");
|
||||
@ -222,6 +246,7 @@ if ( guess > NUMBER ) {
|
||||
```
|
||||
|
||||
Groovy
|
||||
|
||||
```groovy
|
||||
if (guess < randomNumber)
|
||||
print 'too low, try again: '
|
||||
@ -234,6 +259,7 @@ else {
|
||||
```
|
||||
|
||||
JavaScript
|
||||
|
||||
```javascript
|
||||
if (myGuess === randomNumber) {
|
||||
feedback.textContent = "You got it right!"
|
||||
@ -245,6 +271,7 @@ if (myGuess === randomNumber) {
|
||||
```
|
||||
|
||||
awk
|
||||
|
||||
```awk
|
||||
if (guess < randomNumber) {
|
||||
printf "too low, try again:"
|
||||
@ -257,6 +284,7 @@ if (guess < randomNumber) {
|
||||
```
|
||||
|
||||
Lua
|
||||
|
||||
```lua
|
||||
if ( player.guess > number ) then
|
||||
print("Too high")
|
||||
@ -268,13 +296,14 @@ else
|
||||
end
|
||||
```
|
||||
|
||||
### What about non-C-based languages?
|
||||
### 非类 C 编程语言会怎么样呢?
|
||||
|
||||
Programming languages that are not based on C can be quite different and require learning specific syntax to do each step. Racket derives from Lisp and Scheme, so it uses Lisp's prefix notation and lots of parentheses. Python uses whitespace rather than brackets to indicate blocks like loops. Elixir is a functional programming language with its own syntax. Bash is based on the Bourne shell from Unix systems, which itself borrows from Algol68—and supports additional shorthand notation such as `&&` as a variation of "and." Fortran was created when code was entered using punched cards, so it relies on an 80-column layout where some columns are significant.
|
||||
非类 C 编程语言会有很大的不同,需要学习特定的语法来完成每一步。Racket 源于 Lisp 和 Scheme,所以它使用 Lisp 的前缀符和大量括号。Python 使用空格而不是括号来表示循环之类的块。Elixir 是一种函数式编程语言,有自己的语法。Bash 是基于 Unix 系统中的 Bourne shell,它本身借鉴了 Algol68,并支持额外的速记符,如`&&`作为 "and " 的变体。Fortran 是在使用打孔卡片输入代码的时期创建的,所以它依赖于一些重要列的80-列布局。
|
||||
|
||||
As an example of how these other programming languages can differ, I'll compare just the "if" statement that sees if one value is less than or greater than another and prints an appropriate message to the user.
|
||||
我将通过比较 "if "语句,举例表现这些编程语言的不同。if 判断一个值是否小于或大于另一个值,并向用户打印适当信息。
|
||||
|
||||
Racket
|
||||
|
||||
```racket
|
||||
(cond [(> number guess) (displayln "Too low") (inquire-user number)]
|
||||
[(< number guess) (displayln "Too high") (inquire-user number)]
|
||||
@ -282,6 +311,7 @@ Racket
|
||||
```
|
||||
|
||||
Python
|
||||
|
||||
```python
|
||||
if guess < random:
|
||||
print("Too low")
|
||||
@ -292,6 +322,7 @@ else:
|
||||
```
|
||||
|
||||
Elixir
|
||||
|
||||
```elixir
|
||||
cond do
|
||||
guess < num ->
|
||||
@ -306,12 +337,14 @@ end
|
||||
```
|
||||
|
||||
Bash
|
||||
|
||||
```bash
|
||||
[ "0$guess" -lt $number ] && echo "Too low"
|
||||
[ "0$guess" -gt $number ] && echo "Too high"
|
||||
```
|
||||
|
||||
Fortran
|
||||
|
||||
```fortran
|
||||
IF (GUESS.LT.NUMBER) THEN
|
||||
PRINT *, 'TOO LOW'
|
||||
@ -320,11 +353,11 @@ ELSE IF (GUESS.GT.NUMBER) THEN
|
||||
ENDIF
|
||||
```
|
||||
|
||||
### Read more
|
||||
### Read more 更多
|
||||
|
||||
This "guess the number" game is a great introductory program when learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts and compare each language's details.
|
||||
当你在学习一种新的编程语言时"猜数字 "游戏是一个很友好的入门程序,通过一种简单的方式练习了几个常见的编程概念。通过不同编程语言实现这个简单游戏,你可以理解一些核心概念和每种语言的细节。
|
||||
|
||||
Learn how to write the "guess the number" game in C and C-like languages:
|
||||
学习如何用 C 和类 C 语言编写 "猜数字 "游戏:
|
||||
|
||||
* [C][2], by Jim Hall
|
||||
* [C++][3], by Seth Kenlon
|
||||
@ -335,7 +368,7 @@ Learn how to write the "guess the number" game in C and C-like languages:
|
||||
* [awk][8], by Chris Hermansen
|
||||
* [Lua][9], by Seth Kenlon
|
||||
|
||||
And in non-C-based languages:
|
||||
其他语言:
|
||||
|
||||
* [Racket][10], by Cristiano L. Fontana
|
||||
* [Python][11], by Moshe Zadka
|
||||
@ -349,14 +382,14 @@ via: https://opensource.com/article/21/4/compare-programming-languages
|
||||
|
||||
作者:[Jim Hall][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[VeryZZJ](https://github.com/VeryZZJ)
|
||||
校对:[校对者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/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/code_development_programming.png?itok=M_QDcgz5 (Developing code.)
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/code_development_programming.png?itok=M_QDcgz5 "Developing code."
|
||||
[2]: https://opensource.com/article/21/1/learn-c
|
||||
[3]: https://opensource.com/article/20/12/learn-c-game
|
||||
[4]: https://opensource.com/article/20/12/learn-rust
|
Loading…
Reference in New Issue
Block a user