//可以考虑使用 “表达式树”来做,这个是nf3.5才有的功能,如果没有3.5 比如是2.0的话 //那么要这么做估计就比较费事了,首先你得自己建个表达式树,然后还要使用反射编译和执行 //也许要涉及codedom之类。。 //有了3.5的表达式树 就不一样了,一切变得简单起来。见下例,根据楼主你提供的问题的一个 //解决方案。。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions;namespace SimpleTester1.test2 { using e = System.Linq.Expressions.Expression; class Class2 : ITest { #region ITest 成员 /// <summary> /// int a=2,b=3,c=4; ///string hg="a+b*c"; ///如何计算字母字符串hg的结果啊! ///请到手指点! /// </summary> public void Run() { this.c.compute<int>("1+2*3"); Console.Read(); } colculate c = new colculate(); #endregion } public class colculate { public T compute<T>(string expression) where T : struct,IConvertible { T t = default(T); char[] splits = new char[] { '+','-','*','/','^','(',')'}; var values = expression.Split(splits); //e expr; List<e> el = new List<e>(); foreach (var item in values) { var v = e.Constant((T)((object)item) , typeof(T)); el.Add(v); } int point=0; foreach (char item in expression) { switch (item) { case '+': var v=e.Add(el[point],el[++point]); el.Insert(++point , v); break; case '-': var v2 = e.Subtract(el[point] , el[++point]); el.Insert(++point , v2); break; case '*': var v3 = e.Multiply(el[point] , el[++point]); el.Insert(++point , v3); break; case '/': var v4 = e.Divide(el[point] , el[++point]); el.Insert(++point , v4); break; case '^': var v5 = e.Power(el[point] , el[++point]); el.Insert(++point , v5); break; case '(': break; case ')': break; default: break; } } var vl = e.Lambda<Func<T>>(el[point]); var realexp=vl.Compile(); t=realexp.Invoke(); return t; } } } //结果 31 // 这个没有考虑“()”的因素, 如果要加上这个因素那么还要费更多的代码,有兴趣的可以做一做。
对不起上面代码贴错了,是这个 中间有个地方有错上面的那个,using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions;namespace SimpleTester3.test5 { using e = System.Linq.Expressions.Expression; class Class5 : ITest { #region ITest 成员 /// <summary> /// int a=2,b=3,c=4; ///string hg="a+b*c"; ///如何计算字母字符串hg的结果啊! ///请到手指点! /// </summary> public void Run() { var result= this.c.compute<int>("1+2*3+22"); Console.WriteLine(result); Console.Read(); } colculate c = new colculate(); #endregion } public class colculate { public T compute<T>(string expression) where T : struct , IConvertible { T t = default(T); char[] splits = new char[] { '+' , '-' , '*' , '/' , '^' , '(' , ')' }; var values = expression.Split(splits); //e expr; List<e> el = new List<e>(); foreach (var item in values) { //var v = e.Constant((T)((object)item) , typeof(T)); var v = default(e); if (typeof(T) == typeof(int)) v = e.Constant(int.Parse(item) , typeof(int)); if (typeof(T) == typeof(float)) v = e.Constant(float.Parse(item) , typeof(float)); if (typeof(T) == typeof(double)) v = e.Constant(double.Parse(item) , typeof(double)); //if (typeof(T) == typeof(string)) // v = e.Constant(item , typeof(string));
el.Add(v); } int point = 0; foreach (char item in expression) { switch (item) { case '+': var v = e.Add(el[point] , el[++point]); el.Insert(++point , v); break; case '-': var v2 = e.Subtract(el[point] , el[++point]); el.Insert(++point , v2); break; case '*': var v3 = e.Multiply(el[point] , el[++point]); el.Insert(++point , v3); break; case '/': var v4 = e.Divide(el[point] , el[++point]); el.Insert(++point , v4); break; case '^': var v5 = e.Power(el[point] , el[++point]); el.Insert(++point , v5); break; case '(': break; case ')': break; default: break; } } var vl = e.Lambda<Func<T>>(el[point]); var realexp = vl.Compile(); t = realexp.Invoke(); return t; } } }
static void Main(string[] args) { //首先把hg变成 "2+3*4", int a = 2, b = 3, c = 4; string hg = "a+b*c"; hg=hg.Replace("a", "2").Replace("b", "3").Replace("c", "4"); object o = new DataTable().Compute(hg.Replace("=", "").Replace("{", "(").Replace("}", ")").Replace("[", "(").Replace("]", ")"), "");//如果有括号,替换中括号和大括号 Console.WriteLine(Convert.ToDouble(o)); Console.Read(); }
//那么要这么做估计就比较费事了,首先你得自己建个表达式树,然后还要使用反射编译和执行
//也许要涉及codedom之类。。
//有了3.5的表达式树 就不一样了,一切变得简单起来。见下例,根据楼主你提供的问题的一个
//解决方案。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;namespace SimpleTester1.test2 {
using e = System.Linq.Expressions.Expression;
class Class2 : ITest {
#region ITest 成员
/// <summary>
/// int a=2,b=3,c=4;
///string hg="a+b*c";
///如何计算字母字符串hg的结果啊!
///请到手指点!
/// </summary>
public void Run() {
this.c.compute<int>("1+2*3");
Console.Read();
}
colculate c = new colculate();
#endregion
}
public class colculate {
public T compute<T>(string expression)
where T : struct,IConvertible {
T t = default(T);
char[] splits = new char[] { '+','-','*','/','^','(',')'};
var values = expression.Split(splits);
//e expr;
List<e> el = new List<e>();
foreach (var item in values) {
var v = e.Constant((T)((object)item) , typeof(T));
el.Add(v);
}
int point=0;
foreach (char item in expression) {
switch (item) {
case '+':
var v=e.Add(el[point],el[++point]);
el.Insert(++point , v);
break;
case '-':
var v2 = e.Subtract(el[point] , el[++point]);
el.Insert(++point , v2);
break; case '*':
var v3 = e.Multiply(el[point] , el[++point]);
el.Insert(++point , v3);
break;
case '/':
var v4 = e.Divide(el[point] , el[++point]);
el.Insert(++point , v4);
break;
case '^':
var v5 = e.Power(el[point] , el[++point]);
el.Insert(++point , v5);
break; case '(': break;
case ')': break;
default:
break;
}
}
var vl = e.Lambda<Func<T>>(el[point]);
var realexp=vl.Compile();
t=realexp.Invoke();
return t;
}
}
}
//结果
31
//
这个没有考虑“()”的因素, 如果要加上这个因素那么还要费更多的代码,有兴趣的可以做一做。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;namespace SimpleTester3.test5 {
using e = System.Linq.Expressions.Expression;
class Class5 : ITest {
#region ITest 成员
/// <summary>
/// int a=2,b=3,c=4;
///string hg="a+b*c";
///如何计算字母字符串hg的结果啊!
///请到手指点!
/// </summary>
public void Run() {
var result= this.c.compute<int>("1+2*3+22");
Console.WriteLine(result);
Console.Read();
}
colculate c = new colculate();
#endregion
}
public class colculate {
public T compute<T>(string expression)
where T : struct , IConvertible {
T t = default(T);
char[] splits = new char[] { '+' , '-' , '*' , '/' , '^' , '(' , ')' };
var values = expression.Split(splits);
//e expr;
List<e> el = new List<e>();
foreach (var item in values) {
//var v = e.Constant((T)((object)item) , typeof(T));
var v = default(e);
if (typeof(T) == typeof(int))
v = e.Constant(int.Parse(item) , typeof(int));
if (typeof(T) == typeof(float))
v = e.Constant(float.Parse(item) , typeof(float));
if (typeof(T) == typeof(double))
v = e.Constant(double.Parse(item) , typeof(double));
//if (typeof(T) == typeof(string))
// v = e.Constant(item , typeof(string));
el.Add(v);
}
int point = 0;
foreach (char item in expression) {
switch (item) {
case '+':
var v = e.Add(el[point] , el[++point]);
el.Insert(++point , v);
break;
case '-':
var v2 = e.Subtract(el[point] , el[++point]);
el.Insert(++point , v2);
break; case '*':
var v3 = e.Multiply(el[point] , el[++point]);
el.Insert(++point , v3);
break;
case '/':
var v4 = e.Divide(el[point] , el[++point]);
el.Insert(++point , v4);
break;
case '^':
var v5 = e.Power(el[point] , el[++point]);
el.Insert(++point , v5);
break; case '(': break;
case ')': break;
default:
break;
}
}
var vl = e.Lambda<Func<T>>(el[point]);
var realexp = vl.Compile();
t = realexp.Invoke();
return t;
}
}
}
static void Main(string[] args)
{
//首先把hg变成 "2+3*4",
int a = 2, b = 3, c = 4;
string hg = "a+b*c";
hg=hg.Replace("a", "2").Replace("b", "3").Replace("c", "4");
object o = new DataTable().Compute(hg.Replace("=", "").Replace("{", "(").Replace("}", ")").Replace("[", "(").Replace("]", ")"), "");//如果有括号,替换中括号和大括号
Console.WriteLine(Convert.ToDouble(o));
Console.Read();
}