找回密码
 立即注册
首页 业界区 业界 Ubuntu 中的编程语言(上)

Ubuntu 中的编程语言(上)

卒挪 2025-5-29 19:41:43
在上一篇随笔中,使用 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 程序:
  1. import java.util.*;
  2. public class GregorianTest
  3. {
  4.   public static void main(String[] args)
  5.   {
  6.     GregorianCalendar dt = new GregorianCalendar(1582, 10 - 1, 4);
  7.     System.out.println(dt.getTime());
  8.     dt.add(Calendar.DAY_OF_MONTH, 1);
  9.     System.out.println(dt.getTime());
  10.   }
  11. }
复制代码
注意,java.util.GregorianCalendar 类的构造函数中月份的取值范围是从 0 到 11。
安装 OpenJDK,编译和运行:
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install openjdk-6-jdk</strong>
  2. ben@ben-1520:~/work$ <strong>java -version</strong>
  3. java version "1.6.0_18"
  4. OpenJDK Runtime Environment (IcedTea6 1.8) (6b18-1.8-0ubuntu1)
  5. OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)
  6. ben@ben-1520:~/work$ <strong>javac GregorianTest.java</strong>
  7. ben@ben-1520:~/work$ <strong>java GregorianTest</strong>
  8. <font color="#ff0000">Thu Oct 04 00:00:00 CST 1582
  9. Fri Oct 15 00:00:00 CST 1582</font>
  10. 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 程序:
  1. object GregorianTest extends Application {
  2.   val dt = new java.util.GregorianCalendar(1582, 10 - 1, 4)
  3.   println(dt.getTime())
  4.   dt.add(java.util.Calendar.DAY_OF_MONTH, 1)
  5.   println(dt.getTime())
  6. }
复制代码
安装 Scala SDK,编译和运行(scalac 是编译器,scala 用于运行编译后的程序,也可以当作交互窗口使用):
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install scala</strong>
  2. <font color="#800080">ben@ben-1520:~/work$ <strong>scala</strong>
  3. Welcome to Scala version 2.7.7final (OpenJDK 64-Bit Server VM, Java 1.6.0_18).
  4. Type in expressions to have them evaluated.
  5. Type :help for more information.
  6. scala> <strong>Math.Pi</strong>
  7. res0: Double = 3.141592653589793
  8. scala> <strong>:quit</strong></font>
  9. ben@ben-1520:~/work$ <strong>scalac -version</strong>
  10. Scala compiler version 2.7.7final -- (c) 2002-2008 LAMP/EPFL
  11. ben@ben-1520:~/work$ <strong>scalac GregorianTest.scala</strong>
  12. ben@ben-1520:~/work$ <strong>scala GregorianTest</strong>
  13. <font color="#ff0000">Thu Oct 04 00:00:00 CST 1582
  14. Fri Oct 15 00:00:00 CST 1582</font>
  15. ben@ben-1520:~/work$
复制代码
由于使用 Java 平台的 java.util.GregorianCalendar 类,运行结果和 Java 语言是一样的。
Visual Basic.NET

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

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

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

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

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

Ruby 语言在2010年6月编程语言排行榜中排名第十二位。下面就是 GregorianTest.rb 程序:
  1. ben@ben-1520:~/work$ <strong>sudo apt-get install g++ libboost-dev</strong>
  2. ben@ben-1520:~/work$ <strong>g++ --version</strong>
  3. g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
  4. Copyright (C) 2009 Free Software Foundation, Inc.
  5. This is free software; see the source for copying conditions.  There is NO
  6. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  7. ben@ben-1520:~/work$ <strong>g++ –o GregorianTest GregorianTest.cpp</strong>
  8. ben@ben-1520:~/work$ <strong>./GregorianTest</strong>
  9. <font color="#ff0000">Mon 1582-Oct-04
  10. Tue 1582-Oct-05</font>
  11. ben@ben-1520:~/work$
复制代码
安装 ruby 和 irb (交互式 ruby),解释执行:
  1. from datetime import date, timedelta
  2. dt = date(1582, 10, 4)
  3. print dt.isoweekday(), dt.isoformat()
  4. dt = dt + timedelta(1)
  5. print dt.isoweekday(), dt.isoformat()
复制代码
运行结果和 Java 平台的编程语言一样。
JavaScript

JavaScript 语言在2010年6月编程语言排行榜中排名第十一位。下面就是 GregorianTest.html 程序:
  1. <font color="#800080">ben@ben-1520:~/work$ <strong>python</strong>
  2. Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
  3. [GCC 4.4.3] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> <strong>import os</strong>
  6. >>> <strong>os.getcwd()</strong>
  7. '/home/ben/work'
  8. >>> <strong>exit()</strong></font>
  9. ben@ben-1520:~/work$ <strong>python GregorianTest.py</strong>
  10. <font color="#ff0000">1 1582-10-04
  11. 2 1582-10-05</font>
  12. ben@ben-1520:~/work$
复制代码
我没有在 JavaScript 语言的标准库中找到计算某一日期的下一天的函数。
在 Firefox 浏览器中的运行结果如下图所示:
1.png
 
运行结果和 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

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册