Ubuntu 中的编程语言(上)

Java

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


1

2

3

4

5

6

7

8

9

10

11

12

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,编译和运行:

[email protected]:~/work$ sudo apt-get install openjdk-6-jdk
[email protected]:~/work$ java -version
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)
[email protected]:~/work$ javac GregorianTest.java
[email protected]:~/work$ java GregorianTest
Thu Oct 04 00:00:00 CST 1582
Fri Oct 15 00:00:00 CST 1582
[email protected]:~/work$

可以看出,Java 语言很好地解决了这个问题:儒略历1582年10月4日星期四的下一天是格里历1582年10月15日星期五。

Scala

2010年6月编程语言排行榜中排名四十三位的 Scala 是 Java 平台上的一门新兴的语言。老赵在“在.NET
平台上使用Scala语言(上):初尝
”中说:“我非常希望它可以取代Java这种劣质语言,让Java平台的生产力上一个台阶。”

Scala 也支持 .NET 平台,请参见“也谈在 .NET 平台上使用 Scala 语言(上)”。

下面就是 GregorianTest.scala 程序:


1

2

3

4

5

6

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 用于运行编译后的程序,也可以当作交互窗口使用):

[email protected]:~/work$ sudo apt-get install scala
[email protected]:~/work$ scala
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> Math.Pi
res0: Double = 3.141592653589793
scala> :quit
[email protected]:~/work$ scalac -version
Scala compiler version 2.7.7final -- (c) 2002-2008 LAMP/EPFL
[email protected]:~/work$ scalac GregorianTest.scala
[email protected]:~/work$ scala GregorianTest
Thu Oct 04 00:00:00 CST 1582
Fri Oct 15 00:00:00 CST 1582
[email protected]:~/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 编译器,编译和运行:

[email protected]:~/work$ sudo apt-get install mono-vbnc
[email protected]:~/work$ vbnc GregorianTest.vb
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
[email protected]:~/work$ ./GregorianTest.exe
星期一 1582-10-04
星期二 1582-10-05
[email protected]:~/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# 编译器,编译和运行:

[email protected]:~/work$ sudo apt-get install mono-devel
[email protected]:~/work$ gmcs --version
Mono C# compiler version 2.4.4.0
[email protected]:~/work$ gmcs GregorianTest.cs
[email protected]:~/work$ ./GregorianTest.exe
星期一 1582-10-04
星期二 1582-10-05
[email protected]:~/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:  }

该程序的运行结果如下所示:

[email protected]:~/work$ gmcs GregorianTest2.cs
[email protected]:~/work$ ./GregorianTest2.exe
True
Thursday  1582-10-04
Friday    1582-10-15
[email protected]:~/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# 编译器:

[email protected]:~/work$ wget http://download.microsoft.com/
download/1/3/B/13BE2B98-E487-4032-9441-22D4D2F4FAAC/fsharp.zip
[email protected]:~/work$ sudo unzip -q fsharp.zip -d /usr/local/bin/
[email protected]:~/work$ rm fsharp.zip
[email protected]:~/work$ cd /usr/local/bin/FSharp-2.0.0.0
[email protected]:/usr/local/bin/FSharp-2.0.0.0$ sudo chmod +x install-mono.sh
[email protected]:/usr/local/bin/FSharp-2.0.0.0$ sudo wget http://anonsvn.mono-project.com/
source/trunk/mcs/class/mono.snk
[email protected]:/usr/local/bin/FSharp-2.0.0.0$ sudo ./install-mono.sh
-- 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)
[email protected]:/usr/local/bin/FSharp-2.0.0.0$ cd ~/work
[email protected]:~/work$ cat >> ~/.bashrc <<!
>
> # set PATH for Microsoft F#
> if [ -d "/usr/local/bin/FSharp-2.0.0.0/bin" ] ; then
>     PATH="/usr/local/bin/FSharp-2.0.0.0/bin:\$PATH"
> fi
> !
[email protected]:~/work$ exit

fsi.exe 是 F# 交互窗口,fsc.exe 是 F# 编译器:

[email protected]:~/work$ fsi.exe
Microsoft (R) F# 2.0 Interactive build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> System.Environment.OSVersion;;
val it : System.OperatingSystem =
  Unix 2.6.32.22 {Platform = Unix;
                  ServicePack = "";
                  Version = 2.6.32.22;
                  VersionString = "Unix 2.6.32.22";}
> #quit;;
- Exit...
[email protected]:~/work$ fsc.exe GregorianTest.fs
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
[email protected]:~/work$ ./GregorianTest.exe
星期一 1582-10-04
星期二 1582-10-05
[email protected]:~/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 编译器,编译和运行:

[email protected]:~/work$ sudo apt-get install gcc
[email protected]:~/work$ gcc --version
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.
[email protected]:~/work$ gcc -o GregorianTest GregorianTest.c
[email protected]:~/work$ ./GregorianTest
Mon Oct  4 00:00:00 1582
Tue Oct  5 00:00:00 1582
[email protected]:~/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:  }

安装 C++ 编译器、boost 库,编译和运行:

[email protected]:~/work$ sudo apt-get install g++ libboost-dev
[email protected]:~/work$ g++ --version
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.

[email protected]:~/work$ g++ –o GregorianTest GregorianTest.cpp
[email protected]:~/work$ ./GregorianTest
Mon 1582-Oct-04
Tue 1582-Oct-05
[email protected]:~/work$

运行结果和 .NET 平台的编程语言一样。

Python

Python 语言在2010年6月编程语言排行榜中排名第七位。下面就是 GregorianTest.py 程序:


1

2

3

4

5

from datetime
import date,
timedelta

dt
= date(1582,
10,
4)

print dt.isoweekday(),
dt.isoformat()

dt
= dt
+ timedelta(1)

print dt.isoweekday(),
dt.isoformat()

Ubuntu 操作系统中已经预装了 Python。可以把 python 作为交互窗口,也可以解释执行:

[email protected]:~/work$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getcwd()
‘/home/ben/work‘
>>> exit()
[email protected]:~/work$ python GregorianTest.py
1 1582-10-04
2 1582-10-05
[email protected]:~/work$

运行结果和 .NET 平台的编程语言一样。

Ruby

Ruby 语言在2010年6月编程语言排行榜中排名第十二位。下面就是 GregorianTest.rb 程序:


1

2

3

4

require
‘date‘

dt
= Date:
:civil(1582,
10,
4)

puts
dt.asctime()

puts
dt.
next().asctime()

安装 ruby 和 irb (交互式 ruby),解释执行:

[email protected]:~/work$ sudo apt-get install ruby irb
[email protected]:~/work$ ruby --version
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
[email protected]:~/work$ irb --version
irb 0.9.5(05/04/13)
[email protected]:~/work$ irb
irb(main):001:0> Time::now
=> Mon Jun 14 16:54:21 +0800 2010
irb(main):002:0> exit
[email protected]:~/work$ ruby GregorianTest.rb
Thu Oct  4 00:00:00 1582
Fri Oct 15 00:00:00 1582

运行结果和 Java 平台的编程语言一样。

JavaScript

JavaScript 语言在2010年6月编程语言排行榜中排名第十一位。下面就是 GregorianTest.html 程序:

01:  <html xmlns="http://www.w3.org/1999/xhtml">
02:    <head>
03:      <title>Gregorian Calendar</title>
04:      <script type="text/javascript">
05:        function testGregorian()
06:        {
07:          var date = new Date(1582, 10 - 1, 4);
08:          document.write(date.toString());
09:        }
10:      </script>
11:      </head>
12:    <body>
13:      <script type="text/javascript">
14:        testGregorian();
15:      </script>
16:    </body>
17:  </html>

我没有在 JavaScript 语言的标准库中找到计算某一日期的下一天的函数。

在 Firefox 浏览器中的运行结果如下图所示:

运行结果和 C 语言一样。

版权声明:本文为博主http://www.zuiniusn.com 原创文章,未经博主允许不得转载。

时间: 2024-08-25 03:44:49

Ubuntu 中的编程语言(上)的相关文章

Ubuntu 中的编程语言(下)

Fortran Fortran 语言在2010年6月编程语言排行榜中排名第三十一位.下面就是 GregorianTest.for 程序: 我没有在 Fortran 语言的标准库中找到设置指定日期的函数,只好从 1970-01-01 往回倒数 141,438 天得到 1582-10-04 . 安装 GNU Fortran 编译器,编译和运行: [email protected]:~/work$ sudo apt-get install gfortran [email protected]:~/wo

ubuntu中在Launcher上添加Android Studio的运行图标

运行命令创建desktop文件: sudo gedit /usr/share/applications/android_studio.desktop 打开窗口后输入以下内容,注意Exec和Icon要修改成自己系统下Android Studio的路径. [Desktop Entry]Type=ApplicationName=Android StudioExec="/opt/android-studio/bin/studio.sh" %fIcon=/opt/android-studio/b

解决Ubuntu中phpmyadmin对数据上传上限2M

本文部分参考自:http://www.myhack58.com/Article/sort099/sort0102/2011/29396.htm 原文有少量错误或者过时的(相对于ubuntu15来说)内容,已做修改. Ubuntu中php.ini的路径和其他linux不一样,路径为:/etc/php5/apache2/php.ini sudo gedit /etc/php5/apache2/php.ini 1.查找 post_max_size,指通过表单POST给PHP的所能接收的最大值,包括表单

Linux 桌面玩家指南:17. 在 Ubuntu 中使用 deepin-wine,解决一些依赖 Windows 的痛点问题

特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之间的内容按照数学公式进行排版,从而导致评论区格式混乱.如果大家的评论中用到了$,但是又不是为了使用数学公式,就请使用\$转义一下,谢谢. 想从头阅读该系列吗?下面是传送门: Linux 桌面玩家指南:01. 玩转 Linux 系统的方法论 Linux 桌面玩家指南:02. 以最简洁的方式打造实用的

在Ubuntu中安装Docker

前言 网上已经有很多介绍Docker安装的文章,自己的安装过程记录一下,为了博客文章结构的连贯性,为写下一篇R和Docker的相遇做为环境基础,同时也给自己一个备忘. 目录 Docker是什么? 在Linux Ubuntu中安装Docker Docker镜像仓库 制作自己的Docker镜像 上传Docker镜像到公共仓库 完整文章:http://blog.fens.me/linux-docker-install/

如何在ubuntu中搭建ruby开发环境

Ubuntu是一个以桌面应用为主的linux操作系统,在进行项目开发的时候,需要在ubuntu中安装ruby开发(http://www.maiziedu.com/course/ruby-px/)语言,主要是考虑到项目的高可变性,以及由此产生的高适应性,所以选择用ruby.跟Java相比,ruby的确比较灵活,能写出千变万化的代码. 但是,比较可悲的是,在windows7上面搭建ruby环境问题比较多.无论是基于cgywin,还是直接使用netbeans都不尽如人意. 因为想调整webserver

ubuntu中的django安装配置与操作

1 在对django安装(http://www.maiziedu.com/course/others/307-3024/)前,我们要确保python软件已经安装,ubuntu中默认有安装的.我们只需要安装django即可,解压django压缩文件,进入解压后的文件夹,运行,python setup.py install即可安装django到python中. 2 创建django项目,首先在任意位置创建文件夹,这里是在python用户下创建work文件夹,接着进入work运行一下命令即可创建dja

关于ubuntu中的软件安装

在ubuntu中一般使用apt-get来安装软件工具, 例如 sudo apt-get install g++ apt-get会在镜像库中找到你需要的软件镜像(例如 g++)来安装,那么apt-get 是在哪里找的呢? 在 ubuntu中,文件/etc/apt/sources.list中写着一系列linux镜像源的服务器地址,apt-get就是在这些服务器上找安装文件的. 关于sources.list的更多内容查看   关于Ubuntu的sources.list 的总结 还有一种办法就是离线安装

Ubuntu中Nginx的安装与配置

Ubuntu中Nginx的安装与配置 1.Nginx介绍 Nginx是一个非常轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和 反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器. 2.对PHP支持 目前各种web 服务器对PHP的支持一共有三种: (1)通过web 服务器内置的模块来实现,例如Apache的mod_php5,类似的Apache内置的mod_perl 可以对perl支持. (2)通过CGI来实现,这个就好比之前per