例如有串字符串:(该列计算表达式:(a*(a+b)))
想要先得到(a*(a+b)),括号内只为字母和一些+-*/
得到并把这两个字母替换成自己的数值,并计算出结果。急求帮助

解决方案 »

  1.   

    public int return(int a,int b)
    {
       return (a*(a+b));
    }
      

  2.   

    http://topic.csdn.net/u/20110430/02/a3a306f8-2e21-4271-b3c5-35e2018933be.html
      

  3.   


                string a = "10";
                string b = "20";
                string str = "(该列计算表达式:(a*(a+b)))";
                Regex reg = new Regex(@"(?i)\([a-z].*?(?:(?:(?<Open>\()[^\(\)]*)*(?:(?<-Open>\))[^\(\)]*)*)*(?(Open)(?!))\)");
                string resultStr = reg.Match(str).Value.Replace("a",a).Replace("b", b);
                DataTable dt = new DataTable();
                int resultInt = Convert.ToInt32(dt.Compute(resultStr, ""));
                Response.Write(resultInt);
    //300
      

  4.   

    <script>
    var str="2*(2+3)";
    alert(eval(str));
    </script>
      

  5.   

    用c#实现js中的eval方法using System;
    using System.Collections;
    using System.ComponentModel;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using Microsoft.JScript;
    using System.Reflection;
    //创建实现js中eval方法的类
    public class EvalMethod
    {
          //初始化加载程序的字符串;
          public static readonly String _jsClass = @"class theEval{ public function Eval(str:String):String {return eval(str)}}";
          //定义对象
          public static object _evalObject=null;
          public static Type _evalType =null;      //构造函数
          static EvalMethod()
          {
                CodeDomProvider _provider = new JScriptCodeProvider();
                ICodeCompiler _iCode = _provider.CreateCompiler();
                CompilerParameters _parameter = new CompilerParameters();
                _parameters.GenerateInMemory = true;
                CompilerResult _result;
                _result = _iCode.CompilerAssemblyFromSource(_parameter,_jsClass);            Assembly _assembly = _result.CompilerAssembly;
                _evalType = _assembly.GetType("theEval");
                _evalObject = Activator.CreateInstance(_evalType );
          }public static object Eval(string str)
    {
              return evalType .InvokeMember("Eval",BindingFlags.InvokeMethod,null,_evalobject,new object[]{str});
    }
    }
    调用:
    object _return = EvalMethod.Eval("2*4+14+2-4");这样得到的效果基本上和js中的eval一样;
      

  6.   


    create function [dbo].[m_charcompute](@bds varchar(1000))
    returns float
    as
    BEGIN
    set @bds = replace(@bds,' ','')--去空格,免得麻烦。
    declare @i int,@j int 
    declare @c1 char(1),@c2 char(1),@c varchar(100)
    declare @v1 float,@v2 float,@v float
    declare  @t table(id int identity(1,1),s varchar(100))
    declare  @s table(id int identity(1,1),s varchar(100))
    declare  @sv table(id int identity(1,1),v float)select @i = 0,@j = len(@bds),@c2 = '',@c = ''
    while @i<@j
    begin
     select @c1 = @c2,@i = @i+1
     select @c2 = substring(@bds,@i,1)
    if charindex(@c2,'.0123456789') > 0 or (@c2 = '-' and @c1 in('','*','-','+','/','('))  begin   select @c = @c + @c2   continue  end
     if @c <> ''  begin insert @t(s)  select @c select @c = '' end
     if charindex(@c2,')')>0
     begin 
       insert @t(s)  select s from @s where id > isnull((select max(id) from @s where s in('(')),0) order by id desc
       delete @s where id >= isnull((select max(id) from @s where s in('(')),0) 
       continue
     end
     if charindex(@c2,'+-)')>0
     begin 
       insert @t(s)  select s from @s where id > isnull((select max(id) from @s where s in('(')),0) order by id desc
       delete @s where id > isnull((select max(id) from @s where s in('(')),0) 
       if @c2 <> ')' insert @s(s) select @c2
       continue
     end
     if charindex(@c2,'*/')>0
     begin 
       insert @t(s)  select s from @s where id > isnull((select max(id) from @s where s in('(','+','-')),0) order by id desc
       delete @s where id > isnull((select max(id) from @s where s in('(','+','-')),0) 
       insert  @s select @c2
       continue
     end
     if charindex(@c2,'(')>0 insert  @s select @c2
    end
    if @c <> '' insert @t(s) select @c
    insert @t(s)  select s from @s order by id desc
    select @i = 0,@j = max(id) from @t 
    while @i < @j
    begin 
     select @i = @i + 1
     select @c = s from @t where id = @i
     if @c = '(' continue
     if @c not in('*','-','+','/')  begin  insert @sv(v) select convert(float,@c) continue end
     select @v2 = v from @sv  delete @sv  where id = (select max(id) from @sv)
     select @v1 = v from @sv  delete @sv  where id = (select max(id) from @sv)
     select @v = case @c when '+' then @v1 + @v2 when '-' then @v1 - @v2
                         when '*' then @v1 * @v2 when '/' then @v1 / @v2 end
     insert @sv(v) select @v
    end
    select @v = v from @sv
    return @v
    end
    --测试数据
    declare @t table (公式 varchar(10),a int,b int)
    insert into @t
    select 'a*(a+b)',1,2 union all
    select 'a*(a+b)',2,3select 公式,a,b,结果=dbo.m_charcompute(
    replace(replace(公式,'a',a),'b',b))
    from @t
    /*
    公式         a           b           结果
    ---------- ----------- ----------- ----------------------
    a*(a+b)    1           2           3
    a*(a+b)    2           3           10
    */