我等到一个string,是一个十六进制的算式,如何计算如
str2 = "((((((((((0x0fffffff&107)&105)|105)^108)^110)|105)^101)^101)&104)^103)";如何计算出结果

解决方案 »

  1.   

    http://www.cnblogs.com/skyivben/archive/2005/10/31/265861.html
      

  2.   

    // Expression.cs - 动态生成数学表达式并计算其值 
    // 表达式使用 C# 语法,可带一个的自变量(x)。 
    // 表达式的自变量和值均为(double)类型。 
    // 使用举例: 
    //   Expression expression = new Expression("Math.Sin(x)"); 
    //   Console.WriteLine(expression.Compute(Math.PI / 2)); 
    //   expression = new Expression("double u = Math.PI - x;" + 
    //     "double pi2 = Math.PI * Math.PI;" + 
    //     "return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;"); 
    //   Console.WriteLine(expression.Compute(0)); 
     
    using System; 
    using System.CodeDom.Compiler; 
    using Microsoft.CSharp; 
    using System.Reflection; 
    using System.Text; 
     
    namespace Skyiv.Util 

      class Demo
      {
        static void Main()
        {
          string str2 = "((((((((((0x0fffffff&107)&105) |105)^108)^110) |105)^101)^101)&104)^103)"; 
          double a = (new Expression(str2)).Compute(0);
          Console.WriteLine(a);
        }
      }  sealed class Expression 
      { 
        object instance; 
        MethodInfo method; 
         
        public Expression(string expression) 
        {   
          if (expression.IndexOf("return") < 0) expression = "return " + expression + ";"; 
          string className = "Expression"; 
          string methodName = "Compute"; 
          CompilerParameters p = new CompilerParameters(); 
          p.GenerateInMemory = true; 
          CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, string. 
            Format("using System;sealed class {0}{{public double {1}(double x){{{2}}}}}", 
            className, methodName, expression)); 
          if(cr.Errors.Count > 0) 
          { 
            string msg = "Expression(\"" + expression + "\"): \n"; 
            foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n"; 
            throw new Exception(msg); 
          } 
          instance = cr.CompiledAssembly.CreateInstance(className); 
          method = instance.GetType().GetMethod(methodName); 
        } 
         
        public double Compute(double x) 
        { 
          return (double)method.Invoke(instance, new object [] { x }); 
        } 
      } 
      

  3.   


    怎么不一样,计算和验证的结果都是:15// Expression.cs - 动态生成数学表达式并计算其值 
    // 表达式使用 C# 语法,可带一个的自变量(x)。 
    // 表达式的自变量和值均为(double)类型。 
    // 使用举例: 
    //   Expression expression = new Expression("Math.Sin(x)"); 
    //   Console.WriteLine(expression.Compute(Math.PI / 2)); 
    //   expression = new Expression("double u = Math.PI - x;" + 
    //     "double pi2 = Math.PI * Math.PI;" + 
    //     "return 3 * x * x + Math.Log(u * u) / pi2 / pi2 + 1;"); 
    //   Console.WriteLine(expression.Compute(0)); 
     
    using System; 
    using System.CodeDom.Compiler; 
    using Microsoft.CSharp; 
    using System.Reflection; 
    using System.Text; 
     
    namespace Skyiv.Util 

      class Demo
      {
        static void Main()
        {
          string str2 = "((((((((((0x0fffffff&107)&105) |105)^108)^110) |105)^101)^101)&104)^103)"; 
          double a = (new Expression(str2)).Compute(0);
          Console.WriteLine("计算: {0}", a);      int b = ((((((((((0x0fffffff&107)&105) |105)^108)^110) |105)^101)^101)&104)^103);
          Console.WriteLine("验证: {0}", b);
        }
      }
      /* 程序输出:
      计算: 15
      验证: 15
      */  sealed class Expression 
      { 
        object instance; 
        MethodInfo method; 
         
        public Expression(string expression) 
        {   
          if (expression.IndexOf("return") < 0) expression = "return " + expression + ";"; 
          string className = "Expression"; 
          string methodName = "Compute"; 
          CompilerParameters p = new CompilerParameters(); 
          p.GenerateInMemory = true; 
          CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, string. 
            Format("using System;sealed class {0}{{public double {1}(double x){{{2}}}}}", 
            className, methodName, expression)); 
          if(cr.Errors.Count > 0) 
          { 
            string msg = "Expression(\"" + expression + "\"): \n"; 
            foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n"; 
            throw new Exception(msg); 
          } 
          instance = cr.CompiledAssembly.CreateInstance(className); 
          method = instance.GetType().GetMethod(methodName); 
        } 
         
        public double Compute(double x) 
        { 
          return (double)method.Invoke(instance, new object [] { x }); 
        } 
      }