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

本文介绍了一种在.NET框架下实现算术表达式解析的方法,并提供了部分源代码供参考。该方法能够处理包括加、减、乘、除等基本运算在内的多种算术操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.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));

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值