例如有串字符串:(该列计算表达式:(a*(a+b)))
想要先得到(a*(a+b)),括号内只为字母和一些+-*/
得到并把这两个字母替换成自己的数值,并计算出结果。急求帮助
想要先得到(a*(a+b)),括号内只为字母和一些+-*/
得到并把这两个字母替换成自己的数值,并计算出结果。急求帮助
解决方案 »
- DataList中嵌入GridView中求列和问题
- 关于SQL server数据库连接的问题
- 求教用户控件图片显示不了的问题
- 多个 XML 节点循环共用一个 XSL 模板问题
- 写了一个datagrid,当鼠标放上去那行值的背景颜色就变,但是变会来的不对
- 求一篇关于sql数据库的中英文翻译!谢谢
- 不同的frame之间如何相互控制?
- 请教一个问题,怎么样才能让DataGrid取出来的记录有背景呢?
- 不能new ActiveXObject("Scripting.FileSystemObject")????
- @@@@ 在多个客户端运行web应用程序时,如何控制数据库并发问题?@@@@
- DropDownList的问题
- confirm对话框是或否
{
return (a*(a+b));
}
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
var str="2*(2+3)";
alert(eval(str));
</script>
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一样;
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
*/