"(2+3)-5*6^2" 是一个字符串,他也有可能是其他的算术表达式
求一算法 能获得他的值本人一直在尝试用 栈实现 理论上栈可以实现.希望高手门把你们的实现代码给我看看学习一下,本贴为学习贴 :)

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/4483/4483898.xml?temp=.4546472
      

  2.   

    using System;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;
    using System.Reflection;
    using System.Text;namespace ConsoleApplication22
    {
     /// <summary>
     /// Class1 的摘要说明。
     /// </summary>
     class Class1
     {
      /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
       //
       // TODO: 在此处添加代码以启动应用程序
       //
       int i = (int)Calc("1+2*3");
       Console.WriteLine(i.ToString());
       Console.ReadLine();  }  public  static  object  Calc(string  expression)  
      {  
       string  className  =  "Calc";  
       string  methodName  =  "Run";  
       expression=expression.Replace("/","*1.0/");
                             
       //  创建编译器实例。  
       ICodeCompiler  complier  =  (new  CSharpCodeProvider().CreateCompiler());  
       //  设置编译参数。  
       CompilerParameters  paras  =  new  CompilerParameters();  
       paras.GenerateExecutable  =  false;  
       paras.GenerateInMemory  =  true;  
     
       //  创建动态代码。  
       StringBuilder  classSource  =  new  StringBuilder();    
       classSource.Append("public  class  "+  className  +"\n");  
       classSource.Append("{\n");  
       classSource.Append("        public  object  "  +  methodName  +  "()\n");  
       classSource.Append("        {\n");  
       classSource.Append("                return  "+  expression  +  ";\n");  
       classSource.Append("        }\n");  
       classSource.Append("}");  
     
       //System.Diagnostics.Debug.WriteLine(classSource.ToString());  
     
       //  编译代码。  
       CompilerResults  result  =  complier.CompileAssemblyFromSource(paras,  classSource.ToString());  
                             
       //  获取编译后的程序集。  
       Assembly  assembly  =  result.CompiledAssembly;  
       
       //  动态调用方法。  
       object  eval  =  assembly.CreateInstance(className);  
       MethodInfo  method  =  eval.GetType().GetMethod(methodName);  
       object reobj = method.Invoke(eval,  null);  
       GC.Collect();
       return reobj;  } }
    }
      

  3.   

    楼上的这个方法很不错,直接生成动态代码进行编译,如果这样的话,还可将这个表达式用SQL语句发到数据库上去计算再返回来,哈哈。
      

  4.   

    无外户两种方式.1. 就是前面讲的,识别运算符,识别数值,处理优先级. 利用栈来逐渐运算2. 偷懒, 可以直接用C# 的动态代码生成,编译. 或者利用script 的对象模型来生成. 有些语言有eval这种函数.
    参考: http://www.codeproject.com/useritems/evaluator.asp当然也可一直接使用jscript.net 语言编译一个dll来计算.比如:
    1) Create a file named CustomEval.js (File | New | File | Script) as
    follows:// JScript source code
    class CustomEval
    {
        static function eval(strExp)
        {
             return eval(strExp);
        }}2) Compile this source code from the command line:jsc /t:library CustomEval.jswhich should produce CustomEval.dll3) Reference this assembly from your VB .Net or C# project as well as
    the Microsoft.JScript assembly.4) Use the eval method of CustomEval as follows (since the method is
    static, you don't need to instantiate CustomEval.    result = CustomEval.eval("2+3")