卒挪 发表于 2025-5-29 19:41:43

Ubuntu 中的编程语言(上)

在上一篇随笔中,使用 C# 和 Java 语言来求解一五八二年十月四日是星期几。现在,让我们在 Ubuntu 10.04 操作系统中使用多种编程语言来求解这个问题。其中有“2010年6月编程语言排行榜”中前二十名中的:Java、C、C++、(Visual)Basic、C#、Python、JavaScript、Ruby,还有排名四十以后的:Scala、F#。下面就具体讲述如何在 Ubuntu 操作系统中安装这些编程语言。然后通过编写求解这个具体问题的程序,并编译和运行,或者解释执行,或者在交互窗口中执行,使得对这些编程语言有最初步的了解。
Java

让我们从2010年6月编程语言排行榜中的第一名 Java 开始吧。下面就是 GregorianTest.java 程序:
import java.util.*;

public class GregorianTest
{
public static void main(String[] args)
{
    GregorianCalendar dt = new GregorianCalendar(1582, 10 - 1, 4);
    System.out.println(dt.getTime());
    dt.add(Calendar.DAY_OF_MONTH, 1);
    System.out.println(dt.getTime());
}
}注意,java.util.GregorianCalendar 类的构造函数中月份的取值范围是从 0 到 11。
安装 OpenJDK,编译和运行:
ben@ben-1520:~/work$ <strong>sudo apt-get install openjdk-6-jdk</strong>
ben@ben-1520:~/work$ <strong>java -version</strong>
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8) (6b18-1.8-0ubuntu1)
OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)
ben@ben-1520:~/work$ <strong>javac GregorianTest.java</strong>
ben@ben-1520:~/work$ <strong>java GregorianTest</strong>
<font color="#ff0000">Thu Oct 04 00:00:00 CST 1582
Fri Oct 15 00:00:00 CST 1582</font>
ben@ben-1520:~/work$ 可以看出,Java 语言很好地解决了这个问题:儒略历1582年10月4日星期四的下一天是格里历1582年10月15日星期五。
Scala

在2010年6月编程语言排行榜中排名四十三位的 Scala 是 Java 平台上的一门新兴的语言。老赵在“在.NET 平台上使用Scala语言(上):初尝”中说:“我非常希望它可以取代Java这种劣质语言,让Java平台的生产力上一个台阶。”
Scala 也支持 .NET 平台,请参见“也谈在 .NET 平台上使用 Scala 语言(上)”。
下面就是 GregorianTest.scala 程序:
object GregorianTest extends Application {
val dt = new java.util.GregorianCalendar(1582, 10 - 1, 4)
println(dt.getTime())
dt.add(java.util.Calendar.DAY_OF_MONTH, 1)
println(dt.getTime())
}安装 Scala SDK,编译和运行(scalac 是编译器,scala 用于运行编译后的程序,也可以当作交互窗口使用):
ben@ben-1520:~/work$ <strong>sudo apt-get install scala</strong>
<font color="#800080">ben@ben-1520:~/work$ <strong>scala</strong>
Welcome to Scala version 2.7.7final (OpenJDK 64-Bit Server VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.
scala> <strong>Math.Pi</strong>
res0: Double = 3.141592653589793
scala> <strong>:quit</strong></font>
ben@ben-1520:~/work$ <strong>scalac -version</strong>
Scala compiler version 2.7.7final -- (c) 2002-2008 LAMP/EPFL
ben@ben-1520:~/work$ <strong>scalac GregorianTest.scala</strong>
ben@ben-1520:~/work$ <strong>scala GregorianTest</strong>
<font color="#ff0000">Thu Oct 04 00:00:00 CST 1582
Fri Oct 15 00:00:00 CST 1582</font>
ben@ben-1520:~/work$ 由于使用 Java 平台的 java.util.GregorianCalendar 类,运行结果和 Java 语言是一样的。
Visual Basic.NET

(Visual)Basic 语言在2010年6月编程语言排行榜中排名第五位。下面就是 GregorianTest.vb 程序:
01:Module GregorianTest
02:    Sub Main()
03:      Dim dt As DateTime
04:      dt = New DateTime(1582, 10, 4)
05:      Console.WriteLine(dt.ToString("dddd yyyy-MM-dd"))
06:      Console.WriteLine(dt.AddDays(1).ToString("dddd yyyy-MM-dd"))
07:    End Sub
08:End Module安装 Visual Basic.NET 编译器,编译和运行:
ben@ben-1520:~/work$ <strong>sudo apt-get install mono-vbnc</strong>
ben@ben-1520:~/work$ <strong>vbnc GregorianTest.vb</strong>
Visual Basic.Net Compiler version 0.0.0.5914 (Mono 2.4.2 - r)
Copyright (C) 2004-2008 Rolf Bjarne Kvinge. All rights reserved.
Assembly 'GregorianTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
saved successfully to '/home/ben/work/GregorianTest.exe'.
Compilation successful
Compilation took 00:00:01.9895840
ben@ben-1520:~/work$ <strong>./GregorianTest.exe</strong>
<font color="#ff0000">星期一 1582-10-04
星期二 1582-10-05</font>
ben@ben-1520:~/work$ 在 .NET 平台中,DateTime 把格里历外推到1582年10月15日之前,取代儒略历,从而错误地认为1582年10月4日是星期一(实际上应该是星期四)。
C#

C# 语言在2010年6月编程语言排行榜中排名第六位。下面就是 GregorianTest.cs 程序:
01:using System;
02:
03:class GregorianTest
04:{
05:    static void Main()
06:    {
07:      var dt = new DateTime(1582, 10, 4);
08:      Console.WriteLine(dt.ToString("dddd yyyy-MM-dd"));
09:      Console.WriteLine(dt.AddDays(1).ToString("dddd yyyy-MM-dd"));
10:    }
11:}安装 C# 编译器,编译和运行:
ben@ben-1520:~/work$ <strong>sudo apt-get install mono-devel</strong>
ben@ben-1520:~/work$ <strong>gmcs --version</strong>
Mono C# compiler version 2.4.4.0
ben@ben-1520:~/work$ <strong>gmcs GregorianTest.cs</strong>
ben@ben-1520:~/work$ <strong>./GregorianTest.exe</strong>
<font color="#ff0000">星期一 1582-10-04
星期二 1582-10-05</font>
ben@ben-1520:~/work$ 不出所料,运行结果和 Visual Basic.NET 是一样的。
.NET Framework Base Class Library 中有 System.Globalization.GregorianCalendar  类,我们来看看使用该类的 GregorianTest2.cs 程序:
01:using System;
02:using System.Globalization;
03:
04:class Program
05:{
06:    static void Main()
07:    {
08:      var dt = new JulianCalendar().ToDateTime(1582, 10, 4, 0, 0, 0, 0);
09:      Console.WriteLine(dt == new DateTime(1582, 10, 15).AddDays(-1));
10:      WriteLine(new JulianCalendar(), dt);
11:      WriteLine(new GregorianCalendar(), dt.AddDays(1));
12:    }
13:
14:    static void WriteLine(Calendar cal, DateTime dt)
15:    {
16:      Console.WriteLine("{0,-9} {1:D4}-{2:D2}-{3:D2}",
17:      cal.GetDayOfWeek(dt), cal.GetYear(dt), cal.GetMonth(dt), cal.GetDayOfMonth(dt));
18:    }
19:}该程序的运行结果如下所示:
ben@ben-1520:~/work$ <strong>gmcs GregorianTest2.cs</strong>
ben@ben-1520:~/work$ <strong>./GregorianTest2.exe</strong>
True
<font color="#ff0000">Thursday1582-10-04
Friday    1582-10-15</font>
ben@ben-1520:~/work$ 看来如果由用户自己指定使用儒略历还是格里历,.NET 平台的 System.Globalization.Calendar 相关的类还是能够正常工作的。
F#

F# 语言在2010年6月编程语言排行榜中排名第四十五位。下面就是 GregorianTest.fs 程序:
1:let dt = System.DateTime(1582, 10, 4)
2:printfn "%s" (dt.ToString("dddd yyyy-MM-dd"))
3:printfn "%s" (dt.AddDays(1.0).ToString("dddd yyyy-MM-dd"))安装 F# 编译器:
ben@ben-1520:~/work$ <strong>wget http://download.microsoft.com/
download/1/3/B/13BE2B98-E487-4032-9441-22D4D2F4FAAC/fsharp.zip</strong>
ben@ben-1520:~/work$ <strong>sudo unzip -q fsharp.zip -d /usr/local/bin/</strong>
ben@ben-1520:~/work$ <strong>rm fsharp.zip</strong>
ben@ben-1520:~/work$ <strong>cd /usr/local/bin/FSharp-2.0.0.0</strong>
ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ <strong>sudo chmod +x install-mono.sh
</strong>ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ <strong>sudo wget http://anonsvn.mono-project.com/
source/trunk/mcs/class/mono.snk
</strong>ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ <strong>sudo ./install-mono.sh</strong>
-- Resigning FSharp.Core.dll with mono.snk
Assembly bin/FSharp.Core.dll signed.
-- Installing FSharp DLLS into the GAC
Installed bin/FSharp.Core.dll into the gac (/usr/lib/mono/gac)
ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0$ <strong>cd ~/work</strong>
ben@ben-1520:~/work$ <strong>cat >> ~/.bashrc <<!</strong>
>
> <strong># set PATH for Microsoft F#
</strong>> <strong>if [ -d "/usr/local/bin/FSharp-2.0.0.0/bin" ] ; then
</strong>> <strong>    PATH="/usr/local/bin/FSharp-2.0.0.0/bin:\$PATH"
</strong>> <strong>fi
</strong>> <strong>!</strong>
ben@ben-1520:~/work$ <strong>exit</strong> fsi.exe 是 F# 交互窗口,fsc.exe 是 F# 编译器:
<font color="#800080">ben@ben-1520:~/work$ <strong>fsi.exe</strong>
Microsoft (R) F# 2.0 Interactive build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> <strong>System.Environment.OSVersion;;</strong>
val it : System.OperatingSystem =
Unix 2.6.32.22 {Platform = Unix;
                  ServicePack = "";
                  Version = 2.6.32.22;
                  VersionString = "Unix 2.6.32.22";}
> <strong>#quit;;</strong>
- Exit...</font>
ben@ben-1520:~/work$ <strong>fsc.exe GregorianTest.fs</strong>
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
ben@ben-1520:~/work$ <strong>./GregorianTest.exe</strong>
<font color="#ff0000">星期一 1582-10-04
星期二 1582-10-05</font>
ben@ben-1520:~/work$ 运行结果和 GregorianTest.cs 的一样。但 F# 源程序比 C# 源程序更简洁。
C

C 语言在2010年6月编程语言排行榜中排名第二位。下面就是 GregorianTest.c 程序:
01:#include <stdio.h>
02:#include <time.h>
03:
04:int main()
05:{
06:    struct tm date = { 0, 0, 0, 4, 10 - 1, 1582 - 1900 };
07:    time_t seconds = mktime(&date);
08:
09:    fputs(asctime(localtime(&seconds)), stdout);
10:    seconds += 24 * 3600;
11:    fputs(asctime(localtime(&seconds)), stdout);
12:    return 0;
13:}注意 tm 结构中年份是从 1900 年起始的,月份的取值范围是从 0 到 11。虽然 C 语言的标准库中没有计算某一日期的下一天的函数,但是通过将 seconds 变量增加 24 * 3600 秒可以达到同样的目的。
安装 C 编译器,编译和运行:
ben@ben-1520:~/work$ <strong>sudo apt-get install gcc</strong>
ben@ben-1520:~/work$ <strong>gcc --version</strong>
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
ben@ben-1520:~/work$ <strong>gcc -o GregorianTest GregorianTest.c</strong>
ben@ben-1520:~/work$ <strong>./GregorianTest</strong>
<font color="#ff0000">Mon Oct4 00:00:00 1582
Tue Oct5 00:00:00 1582
</font>ben@ben-1520:~/work$ 运行结果和 .NET 平台的编程语言一样。
注:如果使用 Microsoft Visual Studio 2010 的 C++ 编译器,tm 结构不允许 1900 年以前的日期。
C++

C++ 语言在2010年6月编程语言排行榜中排名第三位。下面就是 GregorianTest.cpp 程序:
01:#include <iostream>
02:#include "boost/date_time/gregorian/gregorian.hpp"
03:
04:using namespace std;
05:using namespace boost::gregorian;
06:
07:void writeline(date dt);
08:
09:int main()
10:{
11:    date dt(1582, 10, 4);
12:    writeline(dt);
13:    date_duration dd(1);
14:    writeline(dt + dd);
15:}
16:
17:void writeline(date dt)
18:{
19:    cout << dt.day_of_week() << " " << dt << endl;
20:} 运行结果和 .NET 平台的编程语言一样。
Ruby

Ruby 语言在2010年6月编程语言排行榜中排名第十二位。下面就是 GregorianTest.rb 程序:
ben@ben-1520:~/work$ <strong>sudo apt-get install g++ libboost-dev</strong>
ben@ben-1520:~/work$ <strong>g++ --version</strong>
g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ben@ben-1520:~/work$ <strong>g++ –o GregorianTest GregorianTest.cpp</strong>
ben@ben-1520:~/work$ <strong>./GregorianTest</strong>
<font color="#ff0000">Mon 1582-Oct-04
Tue 1582-Oct-05</font>
ben@ben-1520:~/work$ 安装 ruby 和 irb (交互式 ruby),解释执行:
from datetime import date, timedelta
dt = date(1582, 10, 4)
print dt.isoweekday(), dt.isoformat()
dt = dt + timedelta(1)
print dt.isoweekday(), dt.isoformat() 运行结果和 Java 平台的编程语言一样。
JavaScript

JavaScript 语言在2010年6月编程语言排行榜中排名第十一位。下面就是 GregorianTest.html 程序:
<font color="#800080">ben@ben-1520:~/work$ <strong>python</strong>
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> <strong>import os</strong>
>>> <strong>os.getcwd()</strong>
'/home/ben/work'
>>> <strong>exit()</strong></font>
ben@ben-1520:~/work$ <strong>python GregorianTest.py</strong>
<font color="#ff0000">1 1582-10-04
2 1582-10-05</font>
ben@ben-1520:~/work$ 我没有在 JavaScript 语言的标准库中找到计算某一日期的下一天的函数。
在 Firefox 浏览器中的运行结果如下图所示:
 
运行结果和 C 语言一样。
 
更多的编程语言将在下一篇随笔中介绍。
 
参考资料


[*]Ubuntu
[*]Mono: DistroPackages/Ubuntu
[*]Mono: Visual Basic.NET support
[*]Microsoft F# April 2010 CTP
[*]GCC, the GNU Compiler Collection
[*]Boost C++ Libraries
[*]Ruby Programming Language
[*]Python Programming Language
[*]The Scala Programming Language
[*]OpenJDK

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Ubuntu 中的编程语言(上)