THis is test for 页首

Mono 之 HelloWorld

前言

  使用Mono可以让程序员在Linux上使用C#语言。

第一个程序

  尊早约定,让我们使用C#实现Hello World的输出吧——可以参照:http://mono-project.com/Mono_Basics

进入Linux系统,输入 

vi HelloWorld.cs

在Vim 里敲入以下代码:

using System;
public class HelloWorld
{
  public static void Main()
    {
        Console.WriteLine("Hello Mono World");
    }
}

利用:qw保存后退出

这样我们就有了C#的源文件了,接下来需要将源文件编译一下。

利用Gmcs可以将源文件直接编程成 .exe 文件

 gmcs HelloWorld

输入 命令 

ll Hello*

可以看到以下结果:

-rw-r--r-- 1 root     root      115  5月 29 09:24 HelloWorld
-rwxrwxr-x 1 root    root 3072  5月 29 11:07 HelloWorld.exe*

最后输入

./HelloWorld.exe

Hello Mono World

以上就是HelloWorld的简单例子。

 素数输出

  接下来我们写一个较为复杂一点的程序,该程序根据输入的一个最大值输出素数(使用的是打表法)。这次我们利用Vs2010编写程序。

PrimeGenerator
  1 using System;
  2 using System.Diagnostics;
  3 
  4 namespace GeneratePrimes
  5 {
  6     /// <summary>
  7     /// 使用Eratosthenes的筛选发 技术按素数
  8     /// 
  9     /// 算法概要:对于一个开始与2的整数,划掉2的倍数,找到下一个未被划掉的整数,划掉它所有的倍数,如此反复,知道找到传入最大值的平方根为止。
 10     /// </summary>
 11     /// <param name="p"></param>
 12     /// <returns></returns>
 13     public class PrimeGenerator
 14     {
 15 
 16 
 17         private static bool[] crossedOut;
 18         private static int[] result;
 19         private static Stopwatch timeCalculation=new Stopwatch();
 20 
 21         public static int[] GeneratePrimeNumbers(int maxValue)
 22         {
 23             if (maxValue < 2)
 24             {
 25                 return new int[0];
 26             }
 27             else
 28             {
 29                 StartTiming();
 30                 UncrossIntegersUpTo(maxValue);
 31                 CrossOutMultiples();
 32                 PutUncrossedIntegersIntoResult();
 33                 EndTiming();
 34                 return result;
 35             }
 36         }
 37 
 38         private static void StartTiming()
 39         {
 40             timeCalculation.Reset();
 41             timeCalculation.Start();
 42         }
 43 
 44         private static void EndTiming()
 45         {
 46             timeCalculation.Stop();
 47             CalElapse = timeCalculation.ElapsedMilliseconds; 
 48         }
 49 
 50         private static void PutUncrossedIntegersIntoResult()
 51         {
 52 
 53             result = new int[NumberOfUncrossedIntegers()];
 54 
 55             for (int i = 2, j = 0; i < crossedOut.Length; i++)
 56             {
 57                 if (NotCrossed(i))
 58                     result[j++] = i;
 59             }
 60         }
 61 
 62 
 63         private static int NumberOfUncrossedIntegers()
 64         {
 65             int count = 0;
 66             for (int i = 2; i < crossedOut.Length; i++)
 67             {
 68                 if (NotCrossed(i))
 69                     count++;
 70             }
 71             return count;
 72         }
 73 
 74 
 75         private static void CrossOutMultiples()
 76         {
 77 
 78             int maxPrimeFactor = DetermineIterationLimit();
 79             for (int i = 2; i < maxPrimeFactor + 1; i++)
 80             {
 81                 if (NotCrossed(i))
 82                 {
 83                     CrossOutputMultiplesOf(i);
 84                 }
 85             }
 86         }
 87 
 88 
 89         private static void CrossOutputMultiplesOf(int i)
 90         {
 91             for (int multiple = 2 * i;
 92                 multiple < crossedOut.Length;
 93                 multiple += i)
 94                 crossedOut[multiple] = true;
 95         }
 96 
 97         private static bool NotCrossed(int i)
 98         {
 99             return crossedOut[i] == false;
100         }
101 
102         private static int DetermineIterationLimit()
103         {
104             double iterationLimit = Math.Sqrt(crossedOut.Length) ;
105             return (int)iterationLimit;
106         }
107 
108         private static void UncrossIntegersUpTo(int maxValue)
109         {
110 
111             crossedOut = new bool[maxValue + 1];
112             for (int i = 2; i < crossedOut.Length; i++)
113             {
114                 crossedOut[i] = false;
115             }
116         }
117 
118         public static long CalElapse { get; private set; }
119 
120     }
121 }

PrimeGenerator 类来至于:Robert C. Martin & Micah Martin 的 《Aglie Principles,Patterns,and Practices in C#》 P30 ,我在两位大牛的基础上加上了一点运行时间的计算。

 class Program
    {
        static void Main(string[] args)
        {
            int max = getMaxNumbersFromArgs(args);
           
            PrimeGenerator pg = new PrimeGenerator();
            int[] text = PrimeGenerator.GeneratePrimeNumbers(max);

            Console.WriteLine("Elapsse:{0}ms", PrimeGenerator.CalElapse);
            
            for (int i = 0; i < text.Length; i++)
            {
                Console.Write("{0},", text[i]);
            }
            Console.Read();
        }


        private static int getMaxNumbersFromArgs(string[] args)
        {
            int maxNumbers = 3000;
            if (args != null && args.Length > 0)
            {
                int parseResult;
                if (int.TryParse(args[0], out parseResult))
                {
                    maxNumbers = parseResult;
                }
            }
            return maxNumbers;
        }
    }

主函数用于接收最大输入值和打印素数。

以上代码全部在Vs2010 上编写并编译生成.exe 文件

MoMA

   MoMA 是一个在.net 平台下开发的应用程序迁移到Mono平台时,进行兼容性检查的工具。这是个非常有用的工具,使用它检查.dll或.exe 的代码是否符合 ECMA CLI标准。

工具使用起来很简单选择好要分析的程序集文件,点击next即可。如果结果全部为绿色的勾,那么恭喜你全部通过了!

   将程序拷贝到 Linux 里 然后 输入 ./GeneratePrimes.exe  30000   就可以看到素数不断的打印出来了。如果您感兴趣可以试试:多次在Windows 和Linux 运行该程序,对比一下每次计算的耗时。

 

posted @ 2012-05-29 16:16  DotDot  阅读(867)  评论(0编辑  收藏  举报
页脚