Implementation and source code for arithmetic expression parsers under .NET.

zhaozj2021-02-16  37

As a case where the program designer often encounters such a situation, it is necessary to convert a specified string expression into an arithmetic expression and calculate the result. Use Delphi friends can get third-party controls online to implement, and .NET framework The class library does not have a related processing class. It is just that it is necessary to achieve relevant processing in the web applications that have been developed to a university. Just a related processing class (realize most of the arithmetic operations, need Other operations can be expanded on the existing basis), and some code will be posted on the reference, and I hope to be able to make progress, mutual assistance in communication, and discuss in-depth:

// enumexpress

Using system;

Namespace ExpressionTest {///

/// ENUMEXPRESS summary description. /// public enum enumexpress {add, // plus number DEC, // minus MUL, // Multiply Div, // Semi SIN, / / ​​Zhengxuan Cos, // Yu Xuan TAN, / / 正 正 ATAN, // 余切 SQRT, // square root POW, // Ask for power none, //}}

// Expressdeal

Using system;

Namespace ExpressionTest {///

/// ExpressDeal's summary description. /// 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 ("*") 1, strexpression.length-strexpression .INDEXOF ("*") - 1); strTempb = strexpression.substring (0, strelixpression.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); startwo = 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) {straTemp = 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) {straTemp = strexpression.substring (strexpression.indexof ("-") 1, strexpression.Length-strexpression.indexof ("-") - 1); strTemPB = strexpression.substring (0 Strexpression.Indexof ("-")); strone = ST rTempB.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 ());}} return Convert.ToDouble (strExpression);} private double CalculateExExpress (string strExpression, EnumExpress ExpressType) {Double RetValue = 0;

switch (ExpressType) {case EnumExpress.Sin: retValue = Math.Sin (Convert.ToDouble (strExpression)); break; case EnumExpress.Cos: retValue = Math.Cos (Convert.ToDouble (strExpression)); break; case EnumExpress. tan: retValue = Math.Tan (Convert.ToDouble (strExpression)); break; case EnumExpress.ATan: retValue = Math.Atan (Convert.ToDouble (strExpression)); break; case EnumExpress.Sqrt: retValue = Math.Sqrt ( Convert.ToDouble (strExpression)); break; case EnumExpress.Pow: retValue = Math.Pow (Convert.ToDouble (strExpression), 2); break;} if (retValue == 0) return Convert.ToDouble (strExpression); return RetValue;} private int getNextPos (string strexpression) {int [] expressos = new int [4]; expressos [0] = strexpression.indexof (" "); EXPOS [1] = strexpression.indexof ("-"); EXPOS [2] = strexpression.indexof ("*"); Expos [3] = strexpression.indexof ("/"); int Tmpmin = strexpression.length; for (int count = 1; count <= evpos.length; count ) ) {I f (Tmpmin> EXPOS [count-1] && expressos [count-1]! = - 1) {TMPMIN = EXppos [count-1];}}}} Return TMPMIN;} private int getprivorpos (string strexpression) {int [] EXPOS = new int [4]; Expos [0] = strexpression.lastIndexof (" "); expressos [1] = strexpression.lastindexof ("-"); Expos [2] = strexpression.lastIndexof ("*"); EXPOS [3] = strexpression.lastIndexof ("/"); int Tmpmax = -1; for (int count = 1; count <= exppos.length; count ) {if (Tmpmax

EXppos [count-1];}}}}}} public string spiltexpression (string strexpression) {string straTemp = ""; string strexp = ""; while (strexpression.indexof (")! = - 1) {strtemp = strexpression.substring (") 1, strexpression.Length-strexpression.lastindexof (") - 1); strexp = strTemp.substring (0, strtemp.indexof (")"); Strexpression = strexpression.replace (" strexp ") ", CalculateExpress (strexp) .tostring ());} if (strexpression.indexof (" ")! = - 1 || strexpression.indexof (" - ") ! = - 1 || strexpression.indexof ("*")! = - 1 || strexpression.indexof ("/")! = - 1) {strexpression = CalculateExpress (strexpression) .tostring ();} Return Streoscinger;} private string GetExpType (string strExpression) {strExpression = strExpression.ToUpper (); if (! strExpression.IndexOf ( "SIN") = - 1) {return CalculateExExpress (strExpression.Substring (strExpression.IndexOf) ( "N" 1, Strexpression.Length-1-strexpression.indexof ("n")), enumexpress.sin) .tostr ING ();} IF ("COS")! = - 1) {Return CalculateExexpress (strexpression.indexof ("s") 1, strexpression.Length-1-strexpression.indexof (" S ")), enumexpress.cos) .tostring ();} if (strexpression.indexof (" tan ")! = - 1) {Return CalculateexExpress (strexpression.indexof" (strexpression.indexof ("n") 1, strexpression .Lexof ("n")), enumexpress.tan) .tostring ();} if (strexpression.indexof ("Atan")! =

转载请注明原文地址:https://www.9cbs.com/read-24635.html

New Post(0)