當我們拿到一個字串比如:20+31*(100+1)
的時候用口算就能算出結果為3151
,因為這是中綴表示式
對於人類的思維很簡單,但是對於計算機就比較複雜了。相對的字尾表示式
適合計算機進行計算。
我們就從簡單到複雜,逐步實現對公式的解析(下述的程式碼沒有經過嚴格驗證,可能會存在極端情況的BUG,作為一種思路僅供參考,商用環境還需細細修改)。
實現簡單的數字的加減乘除
我們從實現簡單的數字的加減乘除開始主要是提供一個思路有需要可以自己修改擴充套件比如增加函式、字串、陣列等(推薦一個專案寫的感覺就不錯https://github.com/KovtunV/NoStringEvaluating),那麼我們只需要關注加減乘除等運算子、左右括號和運算元(整數、小數和負數),所以我們先建立三個列舉類BracketEnum
、NodeTypeEnum
和OperatorEnum
如下:
BracketEnum
是括號列舉,也就是左右括號"()"
public enum BracketEnum { /// <summary> /// Undefined /// </summary> Undefined = 0, /// <summary> /// 左括號 /// </summary> Open, /// <summary> /// 右括號 /// </summary> Close }
NodeTypeEnum
是節點型別列舉,就簡單分為運算子、運算元和括號
public enum NodeTypeEnum { /// <summary> /// Null /// </summary> Null = 0, /// <summary> /// 運算元 /// </summary> Number, /// <summary> /// 運算子 /// </summary> Operator, /// <summary> /// 括號 /// </summary> Bracket, }
OperatorEnum
是運算子列舉,主要就是加減乘除這些簡單的
public enum OperatorEnum { /// <summary> /// Undefined /// </summary> Undefined = 0, /// <summary> /// + /// </summary> Plus, /// <summary> /// - /// </summary> Minus, /// <summary> /// * /// </summary> Multiply, /// <summary> /// / /// </summary> Divide, /// <summary> /// ^ /// </summary> Power, }
然後我們需要做以下三步:
- 解析公式將字元轉化為便於操作的節點資訊
- 進行解析為字尾表示式
- 進行計算
1、解析公式轉為節點資訊
根據我們的NodeTypeEnum
節點型別列舉我們需要三個不同的節點資訊類方便我們的操作,我們先建立基類BaseNode
以後的節點類都繼承它
public class BaseNode { public BaseNode(NodeTypeEnum nodeType) { NodeType = nodeType; } /// <summary> /// 節點型別 /// </summary> public NodeTypeEnum NodeType { get; set; } }
然後我們分別建立BracketNode
、NumberNode
和OperatorNode
類,分別是括號節點資訊、運算元節點新和運算子節點資訊,它們各有自己的具體實現,如下:
public class BracketNode : BaseNode { /// <summary> /// 括號值 /// </summary> public BracketEnum Bracket { get; } /// <summary> /// 公式括號節點 /// </summary> public BracketNode(BracketEnum bracket) : base(NodeTypeEnum.Bracket) { Bracket = bracket; } }
public class NumberNode : BaseNode { /// <summary> /// 數字值 /// </summary> public double Number { get; } public NumberNode(double number) : base(NodeTypeEnum.Number) { Number = number; } }
public class OperatorNode : BaseNode { /// <summary> /// 操作字串列舉 /// </summary> public OperatorEnum OperatorKey { get; } /// <summary> /// 優先順序 /// </summary> public int Priority { get; } public OperatorNode(OperatorEnum operatorKey) : base(NodeTypeEnum.Operator) { OperatorKey = operatorKey; Priority = GetPriority(); } private int GetPriority() { var priority = OperatorKey switch { OperatorEnum.Power => 6, OperatorEnum.Multiply => 5, OperatorEnum.Divide => 5, OperatorEnum.Plus => 4, OperatorEnum.Minus => 4, _ => 0 }; return priority; } }
有了節點資訊類,那我們肯定還要有對應的解析類分別是BracketReader(括號解析)
、NumberReader(運算元解析)
和OperatorReader(運算子解析)
,解析類就是為了將公式字串解析為對應的節點資訊具體如下:
public static class BracketReader { /// <summary> /// 左右括號字元 /// </summary> private const char OPEN_BRACKET_CHAR = '('; private const char CLOSE_BRACKET_CHAR = ')'; /// <summary> /// 嘗試獲取左括號 /// </summary> /// <param name="nodes">公式節點資訊</param> /// <param name="formula">公式字元</param> /// <param name="index">公式讀取的下標</param> /// <returns></returns> public static bool TryProceedOpenBracket(List<BaseNode> nodes, ReadOnlySpan<char> formula, ref int index) { if (formula[index].Equals(OPEN_BRACKET_CHAR)) { nodes.Add(new BracketNode(BracketEnum.Open)); return true; } return false; } /// <summary> /// 嘗試獲取右括號 /// </summary> /// <param name="nodes">公式節點資訊</param> /// <param name="formula">公式字元</param> /// <param name="index">公式讀取的下標</param> /// <returns></returns> public static bool TryProceedCloseBracket(List<BaseNode> nodes, ReadOnlySpan<char> formula, ref int index) { if (formula[index].Equals(CLOSE_BRACKET_CHAR)) { nodes.Add(new BracketNode(BracketEnum.Close)); return true; } return false; } }
public static class NumberReader { /// <summary> /// 嘗試讀取數字 /// </summary> public static bool TryProceedNumber(List<BaseNode> nodes, ReadOnlySpan<char> formula, ref int index) { double value = 0; var isTry = false;//是否轉換成功 var isNegative = formula[index] == '-';//是否是負數 var localIndex = isNegative ? index + 1 : index; //迴圈判斷數字 for (int i = localIndex; i < formula.Length; i++) { var ch = formula[i]; var isLastChar = i + 1 == formula.Length; if (IsFloatingNumber(ch)) { //如果最後一個並且成功 if (isLastChar && double.TryParse(formula.Slice(index, formula.Length - index), out value)) { index = i; isTry = true; break; } } else if(double.TryParse(formula.Slice(index, i - index), out value)) { //如果不是數字比如是字母,則直接判斷之前的數字 index = i - 1; isTry = true; break; } else { break; } } if (isTry) { nodes.Add(new NumberNode(value)); } return isTry; } /// <summary> /// 判斷是不是數字或者. /// </summary> /// <param name="ch">字元</param> /// <returns></returns> private static bool IsFloatingNumber(char ch) { //是不是十進位制數 var isDigit = char.IsDigit(ch); return isDigit || ch == '.'; } }
/// <summary> /// 運算子解讀 /// </summary> public static class OperatorReader { private static readonly string[] _operators = new[] { "+", "-", "*", "/", "^" }; /// <summary> /// 嘗試獲取運算子 /// </summary> public static bool TryProceedOperator(List<BaseNode> nodes, ReadOnlySpan<char> formula, ref int index) { if (_operators.Contains(formula[index].ToString())) { nodes.Add(new OperatorNode(GetOperatorKey(formula[index].ToString()))); return true; } return false; } /// <summary> /// 獲取對應列舉 /// </summary> /// <param name="name"></param> /// <returns></returns> private static OperatorEnum GetOperatorKey(string name) { return name switch { "+" => OperatorEnum.Plus, "-" => OperatorEnum.Minus, "*" => OperatorEnum.Multiply, "/" => OperatorEnum.Divide, "^" => OperatorEnum.Power, _ => OperatorEnum.Undefined }; } }
有了以上的準備,我們就可以將公式轉為我們的節點資訊瞭如下
/// <summary> /// 解析公式為節點 /// </summary> /// <param name="formula">公式字串</param> /// <returns></returns> public static List<BaseNode> AnalysisFormulaToNodes(string formula) { var nodes = new List<BaseNode>(); for(var index = 0;index< formula.Length; index++) { if (NumberReader.TryProceedNumber(nodes, formula.AsSpan(), ref index)) continue; if (OperatorReader.TryProceedOperator(nodes, formula.AsSpan(), ref index)) continue; if (BracketReader.TryProceedOpenBracket(nodes, formula.AsSpan(), ref index)) continue; if (BracketReader.TryProceedCloseBracket(nodes, formula.AsSpan(), ref index)) continue; } return nodes; }
2、轉為字尾表示式
轉為字尾表示式需要執行以下條件:
/// <summary> /// 轉為字尾表示式 /// </summary> /// <param name="nodes"></param> /// <returns></returns> public static List<BaseNode> GetRPN(List<BaseNode> nodes) { var rpnNodes = new List<BaseNode>(); var tempNodes = new Stack<BaseNode>(); foreach(var t in nodes) { //1、如果是運算元直接入棧 if(t.NodeType == NodeTypeEnum.Number) { rpnNodes.Add(t); continue; } //2、若取出的字元是運算子,則迴圈比較S1棧頂的運算子(包括左括號)優先順序,如果棧頂的運算子優先順序大於等於該運算子的優先順序,則S1棧頂運算子彈出加入到S2中直至不滿足條件為止,最後將該運算子送入S1中。 if (t.NodeType == NodeTypeEnum.Operator) { while (tempNodes.Count > 0) { var peekOperatorNode = tempNodes.Peek() as OperatorNode; if (peekOperatorNode != null && peekOperatorNode.Priority >= (t as OperatorNode).Priority) { rpnNodes.Add(tempNodes.Pop()); } else { break; } } tempNodes.Push(t); continue; } //3、若取出的字元是“(”,則直接送入S1棧頂 if(t.NodeType == NodeTypeEnum.Bracket) { if((t as BracketNode).Bracket == BracketEnum.Open) { tempNodes.Push(t); continue; } } //4、若取出的字元是“)”,則將距離S1棧棧頂最近的“(”之間的運算子,逐個出棧,依次送入S2棧,此時拋棄“(”。 if (t.NodeType == NodeTypeEnum.Bracket) { if ((t as BracketNode).Bracket == BracketEnum.Close) { while (tempNodes.Count > 0) { var peekBracketNode = tempNodes.Peek() as BracketNode; if (tempNodes.Peek().NodeType == NodeTypeEnum.Bracket && peekBracketNode != null && peekBracketNode.Bracket == BracketEnum.Open) { break; } else { rpnNodes.Add(tempNodes.Pop()); } } tempNodes.Pop(); continue; } } //5、重複上述步驟 } if(tempNodes.Count > 0) { rpnNodes.Add(tempNodes.Pop()); } return rpnNodes; }
3、計算字尾表示式
/// <summary> /// 計算字尾表示式 /// </summary> /// <param name="nodes"></param> /// <returns></returns> public static double CalculationRPN(List<BaseNode> nodes) { double result = 0; Stack<BaseNode> stack = new Stack<BaseNode>(); foreach(var t in nodes) { if(t.NodeType == NodeTypeEnum.Number) { //運算元直接入棧 stack.Push(t); } else if(t.NodeType == NodeTypeEnum.Operator) { //運算子彈出棧頂兩個進行計算 var a = stack.Pop(); var b = stack.Pop(); var operate = t as OperatorNode; var value = operate.OperatorKey switch { // 數學運算子 OperatorEnum.Multiply => OperatorService.Multiply(a, b), OperatorEnum.Divide => OperatorService.Divide(a, b), OperatorEnum.Plus => OperatorService.Plus(a, b), OperatorEnum.Minus => OperatorService.Minus(a, b), OperatorEnum.Power => OperatorService.Power(a, b), }; stack.Push(new NumberNode(value)); } } result = (stack.Pop() as NumberNode).Number; return result; }
數學運算子執行程式碼如下主要為了進行加減乘除簡單的計算:
/// <summary> /// 運算子服務 /// </summary> public static class OperatorService { #region Math public static double Multiply(in BaseNode a, in BaseNode b) { var (result, _a, _b) = IsNumber(a, b); if (result) { return _a * _b; } return default; } public static double Divide(in BaseNode a, in BaseNode b) { var (result, _a, _b) = IsNumber(a, b); if (result) { return _a / _b; } return default; } public static double Plus(in BaseNode a, in BaseNode b) { var (result, _a, _b) = IsNumber(a, b); if (result) { return _a + _b; } return default; } public static double Minus(in BaseNode a, in BaseNode b) { var (result, _a, _b) = IsNumber(a, b); if (result) { return _a - _b; } return default; } public static double Power(in BaseNode a, in BaseNode b) { var (result, _a, _b) = IsNumber(a, b); if (result) { return Math.Pow(_a, _b); } return default; } /// <summary> /// 判斷是不是數字型別,並返回數字 /// </summary> /// <param name="a"></param> /// <returns></returns> private static (bool,double,double) IsNumber(BaseNode a, in BaseNode b) { if(a.NodeType == NodeTypeEnum.Number && b.NodeType == NodeTypeEnum.Number) { var _a = a as NumberNode; var _b = b as NumberNode; return (true, _a.Number, _b.Number); } return (false, default, default); } #endregion }
最後串在一起就能得到結果啦,就像下面這樣
/// <summary> /// 計算 /// </summary> /// <param name="formula">公式字串</param> /// <returns></returns> public static double Calculation(string formula) { //1、獲取公式節點 var nodes = AnalysisFormulaToNodes(formula); //2、轉字尾表示式 var rpnNodes = GetRPN(nodes); //3、計算對字尾表示式求值 var result = CalculationRPN(rpnNodes); return result; }