注册 登录
  • 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

OC/C/C++ 水墨上仙 3041次浏览 已收录 手机上查看

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

using System;
using System.CodeDom.Compiler;
using System.Globalization;
using System.Reflection;
using System.Threading;
namespace RobvanderWoude
{
	class ClCalc
	{
		static int Main( string[] args )
		{
			string expression = string.Empty;
			foreach ( string arg in args )
			{
				expression += " " + arg;
			}
			expression = expression.Trim( );
			try
			{
				Thread.CurrentThread.CurrentCulture = new CultureInfo( "en-US" );
				Double result = JScriptEvaluator.EvalToDouble( expression );
				Console.WriteLine( "{0} = {1}", expression, result );
				try
				{
					return Convert.ToInt32( result );
				}
				catch ( Exception )
				{
					return 0;
				}
			}
			catch ( Exception e )
			{
				return WriteError( e.Message );
			}
		}
		public static int WriteError( Exception e )
		{
			return WriteError( e == null ? null : e.Message );
		}
		public static int WriteError( string errorMessage )
		{
			/*
			ClCalc,  Version 1.00
			Command Line Calculator
			Usage:  CLCALC  expression
			Notes:  Result is displayed on screen and returned as exit code ("errorlevel").
			        Exit code is integer value of result or 0 in case of error or overflow.
			        "Culture" is set to "en-US", so use and expect decimal dots, no commas.
			        Based on Eval function (using JScript) by "markman":
			        www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function
			Written by Rob van der Woude
			http://www.robvanderwoude.com
			*/
			if ( string.IsNullOrEmpty( errorMessage ) == false )
			{
				Console.Error.WriteLine( );
				Console.ForegroundColor = ConsoleColor.Red;
				Console.Error.Write( "ERROR: " );
				Console.ForegroundColor = ConsoleColor.White;
				Console.Error.WriteLine( errorMessage );
				Console.ResetColor( );
			}
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "ClCalc,  Version 1.00" );
			Console.Error.WriteLine( "Command Line Calculator" );
			Console.Error.WriteLine( );
			Console.Error.Write( "Usage:  " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( "CLCALC  expression" );
			Console.ResetColor( );
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "Notes:  Result is displayed on screen and returned as exit code (\"errorlevel\")." );
			Console.Error.WriteLine( "        Exit code is integer value of result or 0 in case of error or overflow." );
			Console.Error.WriteLine( "        Culture is set to \"en-US\", so use and expect decimal dots, not commas." );
			Console.Error.WriteLine( "        Based on Eval function (using JScript) by \"markman\":" );
			Console.ForegroundColor = ConsoleColor.DarkGray;
			Console.Error.WriteLine( "        www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function" );
			Console.ResetColor( );
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "Written by Rob van der Woude" );
			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
			return 0;
		}
	}
	// Eval function using JScript, by "markman"
	// http://www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function
	public class JScriptEvaluator
	{
		public static int EvalToInteger( string statement )
		{
			string s = EvalToString( statement );
			return int.Parse( s.ToString( ) );
		}
		public static double EvalToDouble( string statement )
		{
			string s = EvalToString( statement );
			return double.Parse( s );
		}
		public static string EvalToString( string statement )
		{
			object o = EvalToObject( statement );
			return o.ToString( );
		}
		public static object EvalToObject( string statement )
		{
			return _evaluatorType.InvokeMember(
							  "Eval",
							  BindingFlags.InvokeMethod,
							  null,
							  _evaluator,
							  new object[] { statement }
						);
		}
		static JScriptEvaluator( )
		{
			CodeDomProvider provider = new Microsoft.JScript.JScriptCodeProvider( );
			CompilerParameters parameters;
			parameters = new CompilerParameters( );
			parameters.GenerateInMemory = true;
			CompilerResults results;
			results = provider.CompileAssemblyFromSource( parameters, _jscriptSource );
			Assembly assembly = results.CompiledAssembly;
			_evaluatorType = assembly.GetType( "Evaluator.Evaluator" );
			_evaluator = Activator.CreateInstance( _evaluatorType );
		}
		private static object _evaluator = null;
		private static Type _evaluatorType = null;
		private static readonly string _jscriptSource =
			  @"package Evaluator
                  {
                     class Evaluator
                     {
                           public function Eval(expr : String) : String
                           {
                              return eval(expr);
                           }
                     }
                  }";
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#编写的一个简单的命令行计算器代码
喜欢 (0)
[开心洋葱]
分享 (0)
关于作者:
水墨上仙
……
加载中……