由于对各种语言的好奇心,我在这里将用多种语言写下面一个程序:
- 创建一个长度为 10 的整型数组(或其他类似于数组的东西)。
- 定义一个函数(子过程等),其采用 while 循环 3 次(无限循环,用 break 退出),嵌套 for 循环 2 次(在第一次 continue,跳过后面的步骤),每次遍历参数(一个整型数组),并把值放入下面一个函数。然后,返回 1。(程序结束时打印)
- 定义一个函数,若参数为正数,打印这个数字 + “> 0”,若为负数,打印这个数字 + “< 0”,若为零,打印这个数字 +“=0”。打印均不带引号。
为什么要设计一个这样的程序呢?因为这样的程序包含了循环、条件判断、变量、输出、函数等多方面的东西。此处没有控制台输入,是因为某些语言没有输入功能(或输入太复杂)
本文不进行任何代码对比,不引起任何纠纷。
C
main.c
#include <stdio.h>
void printRes(int number) {
if (number > 0) {
printf("%d > 0\n", number);
} else if (number < 0) {
printf("%d < 0\n", number);
} else {
printf("%d = 0\n", number);
}
}
int arrayOpt(int* array, int length) {
int count = 0;
while (1) {
for (int i = 0; i < 2; i++) {
if (i == 0) {
continue;
}
for (int j = 0; j < length; j++) {
printRes(array[j]);
}
}
count++;
if (count == 3) {
break;
}
}
return 1;
}
int main(void) {
int numbers[10] = { 1, 192, -12345, -12325, 3634, 0, 132541, -56614, 65436, -233 };
int res = arrayOpt(numbers, 10);
printf("res = %d\n", res);
return 0;
}C++
main.cpp
#include <iostream>
using namespace std;
void printRes(int number);
int arrayOpt(int* array, int length);
int main() {
int numbers[10] = { 1, 192, -12345, -12325, 3634, 0, 132541, -56614, 65436, -233 };
int res = arrayOpt(numbers, 10);
cout << "res = " << res << endl;
return 0;
}
void printRes(int number) {
if (number > 0) {
cout << number << " > 0" << endl;
} else if (number < 0) {
cout << number << " < 0" << endl;
} else {
cout << number << " = 0" << endl;
}
}
int arrayOpt(int* array, int length) {
int count = 0;
while (true) {
for (int i = 0; i < 2; i++) {
if (i == 0) {
continue;
}
for (int j = 0; j < length; j++) {
printRes(array[j]);
}
}
count++;
if (count == 3) {
break;
}
}
return 1;
}Java
Main.java
public class Main {
public static void main(String []args) {
int[] numbers = { 1, 192, -12345, -12325, 3634, 0, 132541, -56614, 65436, -233 };
int res = arrayOpt(numbers);
System.out.printf("res = %d\n", res);
}
private static int arrayOpt(int[] array) {
int count = 0;
while (true) {
for (int i = 0; i < 2; i++) {
if (i == 0) {
continue;
}
for (int j : array) {
printRes(j);
}
}
count++;
if (count == 3) {
break;
}
}
return 1;
}
private static void printRes(int number) {
if (number > 0) {
System.out.printf("%d > 0\n", number);
} else if (number < 0) {
System.out.printf("%d < 0\n", number);
} else {
System.out.printf("%d = 0\n", number);
}
}
}Go
main.go
package main
import "fmt"
func printRes(number int) {
if number > 0 {
fmt.Printf("%d > 0\n", number)
} else if number < 0 {
fmt.Printf("%d < 0\n", number)
} else {
fmt.Printf("%d = 0\n", number)
}
}
func arrayOpt(array [10]int) int {
var count int = 0
for true {
for i := 0; i < 2; i++ {
if i == 0 {
continue
}
for j := 0; j < 10; j++ {
printRes(array[j])
}
}
count++
if count == 3 {
break
}
}
return 1
}
func main() {
var numbers = [10]int{ 1, 192, -12345, -12325, 3634, 0, 132541, -56614, 65436, -233 }
var res int = arrayOpt(numbers)
fmt.Printf("res = %d\n", res)
}Python & PyPy
main.py
def printRes(number):
if number > 0:
print("{0} > 0".format(number))
elif number < 0:
print("{0} < 0".format(number))
else:
print("{0} = 0".format(number))
def arrayOpt(array):
count = 0
while True:
for i in range(2):
if i == 0:
continue
for j in array:
printRes(j)
count += 1
if count == 3:
break
return 1
numbers = [1, 192, -12345, -12325, 3634, 0, 132541, -56614, 65436, -233]
res = arrayOpt(numbers)
print("res = {0}".format(res))JavaScript
main.js
function printRes(number) {
if (number > 0) {
console.log(number + " > 0");
} else if (number < 0) {
console.log(number + " < 0");
} else {
console.log(number + " = 0");
}
}
function arrayOpt(array) {
var count = 0;
while (true) {
for (let i = 0; i < 2; i++) {
if (i == 0) {
continue;
}
for (let j of array) {
printRes(j);
}
}
count++;
if (count == 3) {
break;
}
}
return 1;
}
var numbers = [1, 192, -12345, -12325, 3634, 0, 132541, -56614, 65436, -233];
var res = arrayOpt(numbers);
console.log("res = " + res);Julia
main.jl
function printRes(number :: Int64)
if number > 0
println("$number > 0")
elseif number < 0
println("$number < 0")
else
println("$number = 0")
end
end
function arrayOpt(array :: Array[Int64]) :: Int64
count :: Int64 = 0
while true
for i :: Int64 = 0 : 1
if i == 0
continue
end
for j :: Int64 in array
printRes(j)
end
end
count += 1
if count == 3
break
end
end
return 1
end
numbers :: Array[Int64] = [1, 192, -12345, -12325, 3634, 0, 132541, -56614, 65436, -233]
res :: Int64 = arrayOpt(numbers)
@show res