日期:2014-05-17  浏览次数:20859 次

分享一个表达式计算的代码
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);

/*