博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#编写的一个简单的命令行计算器源码
阅读量:5880 次
发布时间:2019-06-19

本文共 4458 字,大约阅读时间需要 14 分钟。

将写内容过程中比较常用的内容记录起来,如下的内容是关于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			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" );			return 0;		}	}	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);                           }                     }                  }";	}}   复制代码
                                                                                                                                          

转载于:https://juejin.im/post/5c43c8d551882524b77b7be2

你可能感兴趣的文章
Linux的链接文件-ln命令
查看>>
maven的tomcat插件如何进行debug调试
查看>>
table表头固定
查看>>
截取字符串中两个字符串中的字符串
查看>>
spring xml properties split with comma for list
查看>>
判断点是否在三角形内
查看>>
Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)...
查看>>
在odl中怎样实现rpc
查看>>
leetcode 110 Balanced Binary Tree
查看>>
python活用isdigit方法显示系统进程
查看>>
项目开发总结
查看>>
知行合一
查看>>
jmeter插件之jsonpath提取响应结果和做断言
查看>>
发布支持多线程的PowerShell模块 —— MultiThreadTaskRunner
查看>>
Ubuntu ctrl+alt会导致窗口还原的问题
查看>>
第四十期百度技术沙龙笔记整理
查看>>
推荐系统那点事 —— 基于Spark MLlib的特征选择
查看>>
linux 下RTL8723/RTL8188调试记录(命令行)【转】
查看>>
SpringMVC案例1——对User表进行CRUD操作
查看>>
看雪CTF第十四题
查看>>