.Net下的算术表达式解析器的实现思路与源码

.Net下的算术表达式解析器的实现思路与源码

作为程序设计人员经常会遇到这样的情况,需要将某个指定的字符串表达式转换为算术表达式并计算其结果.使用Delphi的朋友可以在网上获取第三方控件来实现,而.Net框架类库并没有相关的处理类,正好在前不久的给一所大学开发的Web应用中也需要实现相关的处理.就抽空写了一个相关的处理类(实现了大部分的算术运算操作,需要其他运算可以在现有基础上扩展),现将部分代码贴出共大家参考,希望能够在交流中进步、互助中学习、探讨中深入:

//EnumExpress

using System;

namespace ExpressionTest 

/// <summary> 
/// EnumExpress 的摘要说明。 
/// </summary> 
public enum EnumExpress 

Add,//加号 
Dec,//减号 
Mul,//乘号 
Div,//除号 
Sin,//正玄 
Cos,//余玄 
Tan,//正切 
ATan,//余切 
Sqrt,//平方根 
Pow,//求幂 
None,//无 

}


//ExpressDeal

using System;

namespace ExpressionTest 

/// <summary> 
/// ExpressDeal 的摘要说明。 
/// </summary> 
public class ExpressDeal 

static ExpressDeal() 
{


private double CalculateExpress(string strExpression) 
{

string strTemp=""; 
string strTempB=""; 
string strOne=""; 
string strTwo=""; 
double ReplaceValue=0; 
while (strExpression.IndexOf("+")!=-1 || strExpression.IndexOf("-")!=-1 
|| strExpression.IndexOf("*")!=-1 || strExpression.IndexOf("/")!=-1) 

if (strExpression.IndexOf("*")!=-1) 

strTemp=strExpression.Substring(strExpression.IndexOf("*")+1,strExpression.Length-strExpression.IndexOf("*")-1); 
strTempB=strExpression.Substring(0,strExpression.IndexOf("*"));

strOne=strTempB.Substring(GetPrivorPos(strTempB)+1,strTempB.Length-GetPrivorPos(strTempB)-1);

strTwo=strTemp.Substring(0,GetNextPos(strTemp));

ReplaceValue=Convert.ToDouble(GetExpType(strOne))*Convert.ToDouble(GetExpType(strTwo));

strExpression=strExpression.Replace(strOne+"*"+strTwo,ReplaceValue.ToString()); 

else if (strExpression.IndexOf("/")!=-1) 

strTemp=strExpression.Substring(strExpression.IndexOf("/")+1,strExpression.Length-strExpression.IndexOf("/")-1); 
strTempB=strExpression.Substring(0,strExpression.IndexOf("/"));

strOne=strTempB.Substring(GetPrivorPos(strTempB)+1,strTempB.Length-GetPrivorPos(strTempB)-1);


strTwo=strTemp.Substring(0,GetNextPos(strTemp));


ReplaceValue=Convert.ToDouble(GetExpType(strOne))/Convert.ToDouble(GetExpType(strTwo));

strExpression=strExpression.Replace(strOne+"/"+strTwo,ReplaceValue.ToString()); 

else if (strExpression.IndexOf("+")!=-1) 

strTemp=strExpression.Substring(strExpression.IndexOf("+")+1,strExpression.Length-strExpression.IndexOf("+")-1); 
strTempB=strExpression.Substring(0,strExpression.IndexOf("+"));

strOne=strTempB.Substring(GetPrivorPos(strTempB)+1,strTempB.Length-GetPrivorPos(strTempB)-1);

strTwo=strTemp.Substring(0,GetNextPos(strTemp));

ReplaceValue=Convert.ToDouble(GetExpType(strOne))+Convert.ToDouble(GetExpType(strTwo));

strExpression=strExpression.Replace(strOne+"+"+strTwo,ReplaceValue.ToString()); 

else if (strExpression.IndexOf("-")!=-1) 

strTemp=strExpression.Substring(strExpression.IndexOf("-")+1,strExpression.Length-strExpression.IndexOf("-")-1); 
strTempB=strExpression.Substring(0,strExpression.IndexOf("-"));

strOne=strTempB.Substring(GetPrivorPos(strTempB)+1,strTempB.Length-GetPrivorPos(strTempB)-1);


strTwo=strTemp.Substring(0,GetNextPos(strTemp));

}
CK表达式编辑器(内附完整C#源代码)是一款开放源代码的CK表达式编辑器例子,本程序就是尝试解决需求变化而导致的程序频繁改变问题的。为了增强编辑器的功能,而使用了.Net C# 4.0来做编译器,对类型控制也非常严格,没有C#基础,恐怕是难以操作这个编辑器。表达表编辑界面说明:表达式编辑区可以对部分方法和属性进行颜色区分,对有一定编程经验的朋友有一定帮助。脚本如果勾选了脚本,则表达式编辑区的内容一定要是一个方法,即一定要有return,每一行都有;号。类别包含了默认和用户传入的方法的类别。方法所有可以高亮显示的方法,不过,如果用户传入了新的命名空间,即使不是高亮显示的方法也是可用的。传参区可以查看开发员传入的参数,如果在字典区,可以双击参数名,在表达式编辑区显示。表格定义如果开发员传入了DataTable对象,由表达式编辑区那里可以使用List来引用该DataTable默认对应的实体,编辑器会自动生成新的类型。方法说明区详细介绍各个方法的使用语法。编译生成类这是一个给C#开发员查看的选项卡,有C#开发经验的朋友会非常容易看明白生成的内容,从而调整自己的表达式,不过没C#开发经验的朋友还是不要看为好。注意事项:  1、开发环境为Visual Studio 2010,无数据库,使用.net4.0开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值