表达式计算的代码
来源:互联网 发布:照片恢复软件下载 编辑:程序博客网 时间:2024/05/21 12:41
using System;using System.CodeDom;using System.CodeDom.Compiler;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;using Microsoft.CSharp;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { List<string> ExpList = new List<string>() { "23 + 56 * 2", "Math.Pow(2, 2)", "Math.Pow(2, 0.5)", "(3 + 5) * 2 - 7", "1.0 / 3.0 + 7", "((2 + 5) * 2 + (4 * 50)) * 30 - 1", "-10 * 10 - 9", "2 / 10.0 - 4", "3.14 * 3.14 * 2", "2 << 4" }; ExpList.ForEach(i => Console.WriteLine(i + " = " + ExpCalc.Calc(i))); string expwithvar = "(@a + @b) * 10"; double rexpwithvar = ExpCalc.Calc(expwithvar, new Tuple<string, string>("a", "10"), new Tuple<string, string>("b", "5")); Console.WriteLine(rexpwithvar);/*output:23 + 56 * 2 = 135Math.Pow(2, 2) = 4Math.Pow(2, 0.5) = 1.4142135623731(3 + 5) * 2 - 7 = 91.0 / 3.0 + 7 = 7.33333333333333((2 + 5) * 2 + (4 * 50)) * 30 - 1 = 6419-10 * 10 - 9 = -1092 / 10.0 - 4 = -3.83.14 * 3.14 * 2 = 19.71922 << 4 = 32150*/ } } class ExpCalc { private static Random rnd = new Random(); private static string RandomString(string CharList = "abcdefghijklmnopqrstuvwxyz", int length = 1) { string rndstr = ""; for (int i = 1; i <= length; i++) { rndstr += CharList.Substring(rnd.Next(0, CharList.Length), 1); } return rndstr; } private static string RenderText(string Template, Dictionary<string, string> Params) { string result = Template; foreach (var item in Params) { result = result.Replace("@" + item.Key, item.Value); } return result; } public static double Calc(string exp) { CSharpCodeProvider provider = new CSharpCodeProvider(); CompilerParameters cps = new CompilerParameters(); cps.GenerateExecutable = false; cps.GenerateInMemory = true; string classSource = "using System;\n" + "class @classname\n" + "{\n" + "\tpublic double Eval { get { return @exp; } } \n" + "}"; Dictionary<string, string> renderparams = new Dictionary<string, string>(); string classname = RandomString(length: 10); renderparams.Add("classname", classname); renderparams.Add("exp", exp); classSource = RenderText(classSource, renderparams); CompilerResults result = provider.CompileAssemblyFromSource(cps, classSource); Assembly assembly; try { assembly = result.CompiledAssembly; } catch { throw new Exception("Invaild expression: " + exp); } object calcobj = assembly.CreateInstance(classname); PropertyInfo pi = calcobj.GetType().GetProperty("Eval"); double returnvar = 0.0f; returnvar = Convert.ToDouble(pi.GetValue(calcobj, null)); return returnvar; } public static double Calc(string exp, params Tuple<string, string>[] varlist) { double result = 0.00; Dictionary<string, string> dict = new Dictionary<string, string>(); foreach (var i in varlist.AsEnumerable()) { dict.Add(i.Item1, i.Item2); } exp = RenderText(exp, dict); result = Calc(exp); return result; } }}
0 0
- 表达式计算的代码
- 后缀表达式计算的代码实现
- 计算表达式代码
- 基础代码-计算后缀表达式
- 计算器C++代码实现—— 中缀表达式的计算
- java代码:计算以 字符串 形式存在的算术表达式
- java几行代码搞定算术表达式的计算
- 算术表达式的计算
- 算术表达式的计算
- 表达式的计算
- 字符串表达式的计算
- 计算表达式的值
- 表达式计算的中序转后序
- 计算表达式的值
- 前缀表达式的计算
- 表达式计算的问题
- 后缀表达式的计算
- 后缀表达式的计算
- 更新UI
- ThreadPool
- 【C#】面试题整理
- datagridview自绘
- C#调用winhttp组件 POST登录迅雷
- 表达式计算的代码
- 130道C#面试题
- 反编译与调试APK
- 验证码识别技术研究
- 一枚颜值颇高、骨骼清奇的红薯
- 7.AD转换—基于CT117E开发板的STM32库函数编程
- [AHK]读取硬件信息
- 多UI线程WPF程序随机挂起问题
- eclipse install software时始终停留在calculating requirements and depen