[C#] C#编写的一个简单的命令行计算器代码 →→→→→进入此内容的聊天室

来自 , 2019-05-02, 写在 C#, 查看 110 次.
URL http://www.code666.cn/view/141aa4fe
  1. using System;
  2. using System.CodeDom.Compiler;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using System.Threading;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10.         class ClCalc
  11.         {
  12.                 static int Main( string[] args )
  13.                 {
  14.                         string expression = string.Empty;
  15.                         foreach ( string arg in args )
  16.                         {
  17.                                 expression += " " + arg;
  18.                         }
  19.                         expression = expression.Trim( );
  20.                         try
  21.                         {
  22.                                 Thread.CurrentThread.CurrentCulture = new CultureInfo( "en-US" );
  23.                                 Double result = JScriptEvaluator.EvalToDouble( expression );
  24.                                 Console.WriteLine( "{0} = {1}", expression, result );
  25.                                 try
  26.                                 {
  27.                                         return Convert.ToInt32( result );
  28.                                 }
  29.                                 catch ( Exception )
  30.                                 {
  31.                                         return 0;
  32.                                 }
  33.  
  34.                         }
  35.                         catch ( Exception e )
  36.                         {
  37.                                 return WriteError( e.Message );
  38.                         }
  39.  
  40.                 }
  41.  
  42.                 public static int WriteError( Exception e )
  43.                 {
  44.                         return WriteError( e == null ? null : e.Message );
  45.                 }
  46.  
  47.                 public static int WriteError( string errorMessage )
  48.                 {
  49.                         /*
  50.                         ClCalc,  Version 1.00
  51.                         Command Line Calculator
  52.  
  53.                         Usage:  CLCALC  expression
  54.  
  55.                         Notes:  Result is displayed on screen and returned as exit code ("errorlevel").
  56.                                 Exit code is integer value of result or 0 in case of error or overflow.
  57.                                 "Culture" is set to "en-US", so use and expect decimal dots, no commas.
  58.                                 Based on Eval function (using JScript) by "markman":
  59.                                 www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function
  60.  
  61.                         Written by Rob van der Woude
  62.                         http://www.robvanderwoude.com
  63.                         */
  64.                         if ( string.IsNullOrEmpty( errorMessage ) == false )
  65.                         {
  66.                                 Console.Error.WriteLine( );
  67.                                 Console.ForegroundColor = ConsoleColor.Red;
  68.                                 Console.Error.Write( "ERROR: " );
  69.                                 Console.ForegroundColor = ConsoleColor.White;
  70.                                 Console.Error.WriteLine( errorMessage );
  71.                                 Console.ResetColor( );
  72.                         }
  73.  
  74.                         Console.Error.WriteLine( );
  75.                         Console.Error.WriteLine( "ClCalc,  Version 1.00" );
  76.                         Console.Error.WriteLine( "Command Line Calculator" );
  77.                         Console.Error.WriteLine( );
  78.                         Console.Error.Write( "Usage:  " );
  79.                         Console.ForegroundColor = ConsoleColor.White;
  80.                         Console.Error.WriteLine( "CLCALC  expression" );
  81.                         Console.ResetColor( );
  82.                         Console.Error.WriteLine( );
  83.                         Console.Error.WriteLine( "Notes:  Result is displayed on screen and returned as exit code (\"errorlevel\")." );
  84.                         Console.Error.WriteLine( "        Exit code is integer value of result or 0 in case of error or overflow." );
  85.                         Console.Error.WriteLine( "        Culture is set to \"en-US\", so use and expect decimal dots, not commas." );
  86.                         Console.Error.WriteLine( "        Based on Eval function (using JScript) by \"markman\":" );
  87.                         Console.ForegroundColor = ConsoleColor.DarkGray;
  88.                         Console.Error.WriteLine( "        www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function" );
  89.                         Console.ResetColor( );
  90.                         Console.Error.WriteLine( );
  91.                         Console.Error.WriteLine( "Written by Rob van der Woude" );
  92.                         Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  93.                         return 0;
  94.                 }
  95.         }
  96.  
  97.  
  98.         // Eval function using JScript, by "markman"
  99.         // http://www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function
  100.  
  101.         public class JScriptEvaluator
  102.         {
  103.                 public static int EvalToInteger( string statement )
  104.                 {
  105.                         string s = EvalToString( statement );
  106.                         return int.Parse( s.ToString( ) );
  107.                 }
  108.  
  109.                 public static double EvalToDouble( string statement )
  110.                 {
  111.                         string s = EvalToString( statement );
  112.                         return double.Parse( s );
  113.                 }
  114.  
  115.                 public static string EvalToString( string statement )
  116.                 {
  117.                         object o = EvalToObject( statement );
  118.                         return o.ToString( );
  119.                 }
  120.  
  121.                 public static object EvalToObject( string statement )
  122.                 {
  123.                         return _evaluatorType.InvokeMember(
  124.                                                           "Eval",
  125.                                                           BindingFlags.InvokeMethod,
  126.                                                           null,
  127.                                                           _evaluator,
  128.                                                           new object[] { statement }
  129.                                                 );
  130.                 }
  131.  
  132.                 static JScriptEvaluator( )
  133.                 {
  134.                         CodeDomProvider provider = new Microsoft.JScript.JScriptCodeProvider( );
  135.  
  136.                         CompilerParameters parameters;
  137.                         parameters = new CompilerParameters( );
  138.                         parameters.GenerateInMemory = true;
  139.  
  140.                         CompilerResults results;
  141.                         results = provider.CompileAssemblyFromSource( parameters, _jscriptSource );
  142.  
  143.                         Assembly assembly = results.CompiledAssembly;
  144.                         _evaluatorType = assembly.GetType( "Evaluator.Evaluator" );
  145.  
  146.                         _evaluator = Activator.CreateInstance( _evaluatorType );
  147.                 }
  148.  
  149.                 private static object _evaluator = null;
  150.                 private static Type _evaluatorType = null;
  151.                 private static readonly string _jscriptSource =
  152.                           @"package Evaluator
  153.                  {
  154.                     class Evaluator
  155.                     {
  156.                           public function Eval(expr : String) : String
  157.                           {
  158.                              return eval(expr);
  159.                           }
  160.                     }
  161.                  }";
  162.         }
  163. }
  164. //csharp/7330

回复 "C#编写的一个简单的命令行计算器代码"

这儿你可以回复上面这条便签

captcha