static void Main(string[] args) { Console.WriteLine("Example: "); MathTree tree = new MathTree(new MathTreeNode(new MathSymbol(MathSymbol.Operator.Add))); tree.root.right = new MathTreeNode(new MathSymbol(MathSymbol.Operator.Add)); tree.root.right.right = new MathTreeNode(new MathSymbol(MathSymbol.Operator.Multiply)); tree.root.right.right.right = new MathTreeNode(new MathSymbol(4)); tree.root.right.right.left = new MathTreeNode(new MathSymbol(3)); tree.root.right.left = new MathTreeNode(new MathSymbol(2)); tree.root.left = new MathTreeNode(new MathSymbol(1)); Console.WriteLine(": " + String.Join(" ", tree.ToPreOrderString().ToCharArray())); Console.WriteLine("=> " + tree.Evaluate()); while (true) { Console.Write(": "); string input = Console.ReadLine(); try { tree = MathTree.FromString(input); Console.WriteLine("=> " + tree.Evaluate()); } catch (InvalidOperationException e) { Console.WriteLine("Error: " + e.Message); } } }
/// <summary> /// 数式ツリーがデータ型の違反をしていないか判定します。 /// </summary> /// <param name="tree">数式ツリー。</param> /// <param name="variableMap">変数の連想配列。キーは変数名。</param> /// <param name="functionMap">関数の連想配列。キーは関数名。</param> /// <returns>この数式ツリーが返す評価値のデータ型を返却します。 /// 文法エラーの場合、Noneが返却されます。</returns> /// <returns></returns> public static DataType checkDataType(MathTree tree, Dictionary <string, Variable> variableMap, Dictionary <string, Function> functionMap) { Dictionary <string, DataType> variableDataTypeMap = variableMap.ToDictionary(x => x.Key, x => x.Value.type); Dictionary <string, DataType> functionDataTypeMap = functionMap.ToDictionary(x => x.Key, x => x.Value.returnDataType); return(checkDataType(tree, variableDataTypeMap, functionDataTypeMap)); }
public InputInfoInteger(MathTree min, MathTree max, String minStr, String maxStr, string divisor) { this.min = min; this.max = max; this.minStr = minStr; this.maxStr = maxStr; this.divisor = divisor; }
public InputInfoLoopStart(MathTree loopMin, MathTree loopMax, String loopMinStr, String loopMaxStr, string divisorInter, string divisorLast) { this.loopMin = loopMin; this.loopMax = loopMax; this.loopMinStr = loopMinStr; this.loopmaxStr = loopMaxStr; this.divisorInter = divisorInter; this.divisorLast = divisorLast; }
/// <summary> /// 品詞のリストを解析して、数式ツリーを作成する。 /// </summary> /// <param name="lexicalList">品詞のリスト。</param> /// <returns>数式ツリー。</returns> public static MathTree makeMathTree(List <Lexical> lexicalList) { if (lexicalList.Count == 0) { throw new ArgumentException("品詞のリストが空です。"); } var tree = new MathTree(); tree.root = makeMathTreeNode(lexicalList, ref tree); return(tree); }
/// <summary> /// 逆ポーランド記法表現の文字列から、数式ツリーを復元する。 /// ただし、演算子品詞のparenthesisDepthは正しく設定されないので注意。 /// (逆ポーランド記法表現では再現不能。) /// </summary> /// <param name="rPolish">逆ポーランド記法表現</param> /// <returns>復元した数式ツリー。</returns> public static MathTree analyzeRPolish(string rPolish) { string[] separatingStrings = { DIVIDE_CHAR_R_POLISH }; string[] lexicalStrList = rPolish.Split(separatingStrings, StringSplitOptions.RemoveEmptyEntries); var tree = new MathTree(); var queue = new Queue <MathTreeNode>(); foreach (string lexicalStr in lexicalStrList) { Lexical lexical = LexicalFactory.createFromRPolish(lexicalStr); if (lexical is Literal) { var node = new MathTreeNode(); node.lex = lexical; node.master = tree; queue.Enqueue(node); } else if (lexical is UnaryOperator) { MathTreeNode operand = queue.Dequeue(); var node = new MathTreeNode(); node.lex = lexical; node.master = tree; node.right = operand; queue.Enqueue(node); } else if (lexical is BinaryOperator) { if (queue.Count < 2) { throw new ArgumentException("2つのリテラルを接続していない2項演算子があります。"); } MathTreeNode rightOperand = queue.Dequeue(); MathTreeNode leftOperand = queue.Dequeue(); var node = new MathTreeNode(); node.lex = lexical; node.master = tree; node.left = leftOperand; node.right = rightOperand; queue.Enqueue(node); } else { throw new ArgumentException("評価できない品詞が存在します。対象文字列:" + lexicalStr); } } if (queue.Count != 1) { throw new ArgumentException("最終評価が複数のリテラルとなります。"); } tree.root = queue.Dequeue(); return(tree); }
public static MathTreeNodeValue eval(MathTree tree, Dictionary <string, Variable> variableMap = null, Dictionary <string, Function> functionMap = null) { if (variableMap == null) { variableMap = new Dictionary <string, Variable>(); } if (functionMap == null) { functionMap = new Dictionary <string, Function>(); } return(evalNode(tree.root, variableMap, functionMap)); }
/// <summary> /// 文字列から数式ツリーを作成する。 /// </summary> /// <param name="expr"></param> /// <returns></returns> public static MathTree getMathTreeFromString(String expr) { // 終端記号のリストに変換する。 List <TerminalSymbol> terminalSymbolList = convertTerminalSymbolList(expr); // 品詞のリストに変換する。 // ()はここで取り除かれ、operatorの優先度として割り当てる。 List <Lexical> lexicalList = convertLexicalList(terminalSymbolList); // 数式構造を割り当てる。 MathTree tree = makeMathTree(lexicalList); return(tree); }
/// <summary> /// 数式ツリーがデータ型の違反をしていないか判定します。 /// </summary> /// <param name="tree">数式ツリー。</param> /// <param name="variableDataTypeMap">変数のデータ型の連想配列。</param> /// <param name="functionDataTypeMap">関数の戻り値のデータ型の連想配列。</param> /// <returns>この数式ツリーが返す評価値のデータ型を返却します。 /// 文法エラーの場合、Noneが返却されます。</returns> public static DataType checkDataType(MathTree tree, Dictionary <string, DataType> variableDataTypeMap = null, Dictionary <string, DataType> functionDataTypeMap = null) { if (variableDataTypeMap == null) { variableDataTypeMap = new Dictionary <string, DataType>(); } if (functionDataTypeMap == null) { functionDataTypeMap = new Dictionary <string, DataType>(); } return(checkNodeDataType(tree.root, variableDataTypeMap, functionDataTypeMap)); }
public void TestPattern3() { string expr = "1.3+-2.4-3.5*-4.6/5.7%6.8^-7.9"; List <TerminalSymbol> terminalSymbolList = MathExpressionAnalysisLogic.convertTerminalSymbolList(expr); Assert.AreEqual(16, terminalSymbolList.Count); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[0].type); Assert.AreEqual("1.3", terminalSymbolList[0].value); Assert.AreEqual(TerminalSymbolType.OpAdd, terminalSymbolList[1].type); Assert.AreEqual("+", terminalSymbolList[1].value); Assert.AreEqual(TerminalSymbolType.OpNeg, terminalSymbolList[2].type); Assert.AreEqual("-", terminalSymbolList[2].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[3].type); Assert.AreEqual("2.4", terminalSymbolList[3].value); Assert.AreEqual(TerminalSymbolType.OpDiff, terminalSymbolList[4].type); Assert.AreEqual("-", terminalSymbolList[4].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[5].type); Assert.AreEqual("3.5", terminalSymbolList[5].value); Assert.AreEqual(TerminalSymbolType.OpProd, terminalSymbolList[6].type); Assert.AreEqual("*", terminalSymbolList[6].value); Assert.AreEqual(TerminalSymbolType.OpNeg, terminalSymbolList[7].type); Assert.AreEqual("-", terminalSymbolList[7].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[8].type); Assert.AreEqual("4.6", terminalSymbolList[8].value); Assert.AreEqual(TerminalSymbolType.OpDivide, terminalSymbolList[9].type); Assert.AreEqual("/", terminalSymbolList[9].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[10].type); Assert.AreEqual("5.7", terminalSymbolList[10].value); Assert.AreEqual(TerminalSymbolType.OpMod, terminalSymbolList[11].type); Assert.AreEqual("%", terminalSymbolList[11].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[12].type); Assert.AreEqual("6.8", terminalSymbolList[12].value); Assert.AreEqual(TerminalSymbolType.OpPow, terminalSymbolList[13].type); Assert.AreEqual("^", terminalSymbolList[13].value); Assert.AreEqual(TerminalSymbolType.OpNeg, terminalSymbolList[14].type); Assert.AreEqual("-", terminalSymbolList[14].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[15].type); Assert.AreEqual("7.9", terminalSymbolList[15].value); List <Lexical> lexicalList = MathExpressionAnalysisLogic.convertLexicalList(terminalSymbolList); Assert.AreEqual(16, lexicalList.Count); Assert.IsTrue(lexicalList[0].GetType() == typeof(LiteralDecimal)); Assert.IsTrue(lexicalList[1].GetType() == typeof(BinaryOperatorAdd)); Assert.IsTrue(lexicalList[2].GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(lexicalList[3].GetType() == typeof(LiteralDecimal)); Assert.IsTrue(lexicalList[4].GetType() == typeof(BinaryOperatorDiff)); Assert.IsTrue(lexicalList[5].GetType() == typeof(LiteralDecimal)); Assert.IsTrue(lexicalList[6].GetType() == typeof(BinaryOperatorProd)); Assert.IsTrue(lexicalList[7].GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(lexicalList[8].GetType() == typeof(LiteralDecimal)); Assert.IsTrue(lexicalList[9].GetType() == typeof(BinaryOperatorDivide)); Assert.IsTrue(lexicalList[10].GetType() == typeof(LiteralDecimal)); Assert.IsTrue(lexicalList[11].GetType() == typeof(BinaryOperatorMod)); Assert.IsTrue(lexicalList[12].GetType() == typeof(LiteralDecimal)); Assert.IsTrue(lexicalList[13].GetType() == typeof(BinaryOperatorPow)); Assert.IsTrue(lexicalList[14].GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(lexicalList[15].GetType() == typeof(LiteralDecimal)); LiteralDecimal l0 = (LiteralDecimal)lexicalList[0]; Assert.AreEqual("1.3", l0.value); LiteralDecimal l3 = (LiteralDecimal)lexicalList[3]; Assert.AreEqual("2.4", l3.value); LiteralDecimal l5 = (LiteralDecimal)lexicalList[5]; Assert.AreEqual("3.5", l5.value); LiteralDecimal l8 = (LiteralDecimal)lexicalList[8]; Assert.AreEqual("4.6", l8.value); LiteralDecimal l10 = (LiteralDecimal)lexicalList[10]; Assert.AreEqual("5.7", l10.value); LiteralDecimal l12 = (LiteralDecimal)lexicalList[12]; Assert.AreEqual("6.8", l12.value); LiteralDecimal l15 = (LiteralDecimal)lexicalList[15]; Assert.AreEqual("7.9", l15.value); Operator op1 = (Operator)lexicalList[1]; Assert.AreEqual(5, op1.getPriority()); Operator op2 = (Operator)lexicalList[2]; Assert.AreEqual(8, op2.getPriority()); Operator op4 = (Operator)lexicalList[4]; Assert.AreEqual(5, op4.getPriority()); Operator op6 = (Operator)lexicalList[6]; Assert.AreEqual(6, op6.getPriority()); Operator op7 = (Operator)lexicalList[7]; Assert.AreEqual(8, op7.getPriority()); Operator op9 = (Operator)lexicalList[9]; Assert.AreEqual(6, op9.getPriority()); Operator op11 = (Operator)lexicalList[11]; Assert.AreEqual(6, op11.getPriority()); Operator op13 = (Operator)lexicalList[13]; Assert.AreEqual(7, op13.getPriority()); Operator op14 = (Operator)lexicalList[14]; Assert.AreEqual(8, op14.getPriority()); MathTree tree = MathExpressionAnalysisLogic.makeMathTree(lexicalList); Assert.IsTrue(tree.root.left.left.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal0 = (LiteralDecimal)tree.root.left.left.lex; Assert.AreEqual("1.3", literal0.value); Assert.IsTrue(tree.root.left.lex.GetType() == typeof(BinaryOperatorAdd)); Assert.IsTrue(tree.root.left.right.lex.GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(tree.root.left.right.right.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal3 = (LiteralDecimal)tree.root.left.right.right.lex; Assert.AreEqual("2.4", literal3.value); Assert.IsTrue(tree.root.lex.GetType() == typeof(BinaryOperatorDiff)); Assert.IsTrue(tree.root.right.left.left.left.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal5 = (LiteralDecimal)tree.root.right.left.left.left.lex; Assert.AreEqual("3.5", literal5.value); Assert.IsTrue(tree.root.right.left.left.lex.GetType() == typeof(BinaryOperatorProd)); Assert.IsTrue(tree.root.right.left.left.right.lex.GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(tree.root.right.left.left.right.right.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal8 = (LiteralDecimal)tree.root.right.left.left.right.right.lex; Assert.AreEqual("4.6", literal8.value); Assert.IsTrue(tree.root.right.left.lex.GetType() == typeof(BinaryOperatorDivide)); Assert.IsTrue(tree.root.right.left.right.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal10 = (LiteralDecimal)tree.root.right.left.right.lex; Assert.AreEqual("5.7", literal10.value); Assert.IsTrue(tree.root.right.lex.GetType() == typeof(BinaryOperatorMod)); Assert.IsTrue(tree.root.right.right.left.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal12 = (LiteralDecimal)tree.root.right.right.left.lex; Assert.AreEqual("6.8", literal12.value); Assert.IsTrue(tree.root.right.right.lex.GetType() == typeof(BinaryOperatorPow)); Assert.IsTrue(tree.root.right.right.right.lex.GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(tree.root.right.right.right.right.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal15 = (LiteralDecimal)tree.root.right.right.right.right.lex; Assert.AreEqual("7.9", literal15.value); DataType dataType = MathExpressionAnalysisLogic.checkDataType(tree); Assert.AreEqual(dataType, DataType.Decimal); MathTreeNodeValue value = MathExpressionAnalysisLogic.eval(tree); Assert.AreEqual(DataType.Decimal, value.type); Assert.AreEqual(1.3 + -2.4 - 3.5 * -4.6 / 5.7 % Math.Pow(6.8, -7.9), value.valueDecimal); }
/// <summary> /// 数式の評価結果を確認する。 /// </summary> /// <param name="tree">評価対象の数式</param> /// <param name="dataType">期待する評価結果のデータ型</param> /// <returns>期待するデータ型になる場合はtrueを返す。</returns> public static bool validateMathTreeDataType(MathTree tree, DataType dataType, Dictionary <string, DataType> variableMap) { return(tree.checkDataType(variableMap, null) == dataType); }
/// <summary> /// 品詞のリストを解析し、数式ツリーの1ノードを作成する。 /// </summary> /// <param name="lexicalList">品詞のリスト。</param> /// <param name="master">数式ツリーのマスター。</param> /// <returns>数式ツリーのノード。</returns> private static MathTreeNode makeMathTreeNode(List <Lexical> lexicalList, ref MathTree master) { if (lexicalList.Count == 0) { throw new ApplicationException("無効な式が指定されました。"); } var node = new MathTreeNode(); node.master = master; if (lexicalList.Count == 1) { Lexical lex = lexicalList[0]; if (!(lex is Literal)) { throw new ArgumentException("最終評価が演算子となる品詞が存在します。"); } node.lex = lex; return(node); } else { int operatorIndex = leastPrioritizedRightOperatorIndex(lexicalList); if (operatorIndex == -1) // 2つ以上項があるのに、演算子が見つからない場合 { throw new ArgumentException("複数のリテラルからなる式が評価されました。"); } Operator op = (Operator)lexicalList[operatorIndex]; var leftLexicalList = new List <Lexical>(); for (int i = 0; i < operatorIndex; i++) { leftLexicalList.Add(lexicalList[i]); } var rightLexicalList = new List <Lexical>(); for (int i = operatorIndex + 1; i < lexicalList.Count; i++) { rightLexicalList.Add(lexicalList[i]); } if (op is UnaryOperator) { if (leftLexicalList.Count != 0) { throw new ArgumentException("単項演算子の左側にオペランドを持つことはできません。"); } else if (rightLexicalList.Count == 0) { throw new ArgumentException("単項演算子の右側にオペランドが存在しません。"); } node.lex = op; node.left = null; node.right = makeMathTreeNode(rightLexicalList, ref master); return(node); } else if (op is BinaryOperator) { if (leftLexicalList.Count == 0) { throw new ArgumentException("2項演算子の左側にオペランドが存在しません。"); } else if (rightLexicalList.Count == 0) { throw new ArgumentException("2項演算子の右側にオペランドが存在しません。"); } node.lex = op; node.left = makeMathTreeNode(leftLexicalList, ref master); node.right = makeMathTreeNode(rightLexicalList, ref master); return(node); } else { throw new ApplicationException("単項演算子、2項演算子以外の品詞が、演算子として評価されました。"); } } }
public void TestPattern2() { string expr = "!true || 1 > 5 != 2 >= 6.3"; // 終端記号化 List <TerminalSymbol> terminalSymbolList = MathExpressionAnalysisLogic.convertTerminalSymbolList(expr); Assert.AreEqual(10, terminalSymbolList.Count); Assert.AreEqual(TerminalSymbolType.OpNot, terminalSymbolList[0].type); Assert.AreEqual("!", terminalSymbolList[0].value); Assert.AreEqual(TerminalSymbolType.LogicTrue, terminalSymbolList[1].type); Assert.AreEqual("true", terminalSymbolList[1].value); Assert.AreEqual(TerminalSymbolType.OpOr, terminalSymbolList[2].type); Assert.AreEqual("||", terminalSymbolList[2].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[3].type); Assert.AreEqual("1", terminalSymbolList[3].value); Assert.AreEqual(TerminalSymbolType.OpGt, terminalSymbolList[4].type); Assert.AreEqual(">", terminalSymbolList[4].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[5].type); Assert.AreEqual("5", terminalSymbolList[5].value); Assert.AreEqual(TerminalSymbolType.OpNotEq, terminalSymbolList[6].type); Assert.AreEqual("!=", terminalSymbolList[6].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[7].type); Assert.AreEqual("2", terminalSymbolList[7].value); Assert.AreEqual(TerminalSymbolType.OpGtEq, terminalSymbolList[8].type); Assert.AreEqual(">=", terminalSymbolList[8].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[9].type); Assert.AreEqual("6.3", terminalSymbolList[9].value); // 品詞化 List <Lexical> lexicalList = MathExpressionAnalysisLogic.convertLexicalList(terminalSymbolList); Assert.AreEqual(10, lexicalList.Count); Assert.IsTrue(lexicalList[0].GetType() == typeof(UnaryOperatorNot)); Assert.IsTrue(lexicalList[1].GetType() == typeof(LiteralTrue)); Assert.IsTrue(lexicalList[2].GetType() == typeof(BinaryOperatorOr)); Assert.IsTrue(lexicalList[3].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[4].GetType() == typeof(BinaryOperatorGt)); Assert.IsTrue(lexicalList[5].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[6].GetType() == typeof(BinaryOperatorNotEqual)); Assert.IsTrue(lexicalList[7].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[8].GetType() == typeof(BinaryOperatorGtEq)); Assert.IsTrue(lexicalList[9].GetType() == typeof(LiteralDecimal)); LiteralTrue l1 = (LiteralTrue)lexicalList[1]; Assert.AreEqual("true", l1.value); LiteralInteger l3 = (LiteralInteger)lexicalList[3]; Assert.AreEqual("1", l3.value); LiteralInteger l5 = (LiteralInteger)lexicalList[5]; Assert.AreEqual("5", l5.value); LiteralInteger l7 = (LiteralInteger)lexicalList[7]; Assert.AreEqual("2", l7.value); LiteralDecimal l9 = (LiteralDecimal)lexicalList[9]; Assert.AreEqual("6.3", l9.value); Operator op0 = (Operator)lexicalList[0]; Assert.AreEqual(8, op0.getPriority()); Operator op2 = (Operator)lexicalList[2]; Assert.AreEqual(2, op2.getPriority()); Operator op4 = (Operator)lexicalList[4]; Assert.AreEqual(4, op4.getPriority()); Operator op6 = (Operator)lexicalList[6]; Assert.AreEqual(3, op6.getPriority()); Operator op8 = (Operator)lexicalList[8]; Assert.AreEqual(4, op8.getPriority()); // 数式ツリー化 MathTree tree = MathExpressionAnalysisLogic.makeMathTree(lexicalList); Assert.IsTrue(tree.root.left.lex.GetType() == typeof(UnaryOperatorNot)); Assert.IsTrue(tree.root.left.right.lex.GetType() == typeof(LiteralTrue)); LiteralTrue literal1 = (LiteralTrue)tree.root.left.right.lex; Assert.AreEqual("true", literal1.value); Assert.IsTrue(tree.root.lex.GetType() == typeof(BinaryOperatorOr)); Assert.IsTrue(tree.root.right.left.left.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal3 = (LiteralInteger)tree.root.right.left.left.lex; Assert.AreEqual("1", literal3.value); Assert.IsTrue(tree.root.right.left.lex.GetType() == typeof(BinaryOperatorGt)); Assert.IsTrue(tree.root.right.left.right.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal5 = (LiteralInteger)tree.root.right.left.right.lex; Assert.AreEqual("5", literal5.value); Assert.IsTrue(tree.root.right.lex.GetType() == typeof(BinaryOperatorNotEqual)); Assert.IsTrue(tree.root.right.right.left.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal7 = (LiteralInteger)tree.root.right.right.left.lex; Assert.AreEqual("2", literal7.value); Assert.IsTrue(tree.root.right.right.lex.GetType() == typeof(BinaryOperatorGtEq)); Assert.IsTrue(tree.root.right.right.right.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal9 = (LiteralDecimal)tree.root.right.right.right.lex; Assert.AreEqual("6.3", literal9.value); // データ型評価 DataType dataType = MathExpressionAnalysisLogic.checkDataType(tree); Assert.AreEqual(dataType, DataType.Boolean); // 評価値評価 MathTreeNodeValue value = MathExpressionAnalysisLogic.eval(tree); Assert.AreEqual(value.type, DataType.Boolean); Assert.AreEqual(value.valueBool, false); }
public void TestPattern1() { string expr = "true && 1 < 5 == 2 <= 6.3"; // 終端記号化 List <TerminalSymbol> terminalSymbolList = MathExpressionAnalysisLogic.convertTerminalSymbolList(expr); Assert.AreEqual(9, terminalSymbolList.Count); Assert.AreEqual(TerminalSymbolType.LogicTrue, terminalSymbolList[0].type); Assert.AreEqual("true", terminalSymbolList[0].value); Assert.AreEqual(TerminalSymbolType.OpAnd, terminalSymbolList[1].type); Assert.AreEqual("&&", terminalSymbolList[1].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[2].type); Assert.AreEqual("1", terminalSymbolList[2].value); Assert.AreEqual(TerminalSymbolType.OpLt, terminalSymbolList[3].type); Assert.AreEqual("<", terminalSymbolList[3].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[4].type); Assert.AreEqual("5", terminalSymbolList[4].value); Assert.AreEqual(TerminalSymbolType.OpEq, terminalSymbolList[5].type); Assert.AreEqual("==", terminalSymbolList[5].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[6].type); Assert.AreEqual("2", terminalSymbolList[6].value); Assert.AreEqual(TerminalSymbolType.OpLtEq, terminalSymbolList[7].type); Assert.AreEqual("<=", terminalSymbolList[7].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[8].type); Assert.AreEqual("6.3", terminalSymbolList[8].value); // 品詞化 List <Lexical> lexicalList = MathExpressionAnalysisLogic.convertLexicalList(terminalSymbolList); Assert.AreEqual(9, lexicalList.Count); Assert.IsTrue(lexicalList[0].GetType() == typeof(LiteralTrue)); Assert.IsTrue(lexicalList[1].GetType() == typeof(BinaryOperatorAnd)); Assert.IsTrue(lexicalList[2].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[3].GetType() == typeof(BinaryOperatorLt)); Assert.IsTrue(lexicalList[4].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[5].GetType() == typeof(BinaryOperatorEqual)); Assert.IsTrue(lexicalList[6].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[7].GetType() == typeof(BinaryOperatorLtEq)); Assert.IsTrue(lexicalList[8].GetType() == typeof(LiteralDecimal)); LiteralTrue l0 = (LiteralTrue)lexicalList[0]; Assert.AreEqual("true", l0.value); LiteralInteger l2 = (LiteralInteger)lexicalList[2]; Assert.AreEqual("1", l2.value); LiteralInteger l4 = (LiteralInteger)lexicalList[4]; Assert.AreEqual("5", l4.value); LiteralInteger l6 = (LiteralInteger)lexicalList[6]; Assert.AreEqual("2", l6.value); LiteralDecimal l8 = (LiteralDecimal)lexicalList[8]; Assert.AreEqual("6.3", l8.value); Operator op1 = (Operator)lexicalList[1]; Assert.AreEqual(2, op1.getPriority()); Operator op3 = (Operator)lexicalList[3]; Assert.AreEqual(4, op3.getPriority()); Operator op5 = (Operator)lexicalList[5]; Assert.AreEqual(3, op5.getPriority()); Operator op7 = (Operator)lexicalList[7]; Assert.AreEqual(4, op7.getPriority()); // 数式ツリー化 MathTree tree = MathExpressionAnalysisLogic.makeMathTree(lexicalList); Assert.IsTrue(tree.root.left.lex.GetType() == typeof(LiteralTrue)); LiteralTrue literal0 = (LiteralTrue)tree.root.left.lex; Assert.AreEqual("true", literal0.value); Assert.IsTrue(tree.root.lex.GetType() == typeof(BinaryOperatorAnd)); Assert.IsTrue(tree.root.right.left.left.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal2 = (LiteralInteger)tree.root.right.left.left.lex; Assert.AreEqual("1", literal2.value); Assert.IsTrue(tree.root.right.left.lex.GetType() == typeof(BinaryOperatorLt)); Assert.IsTrue(tree.root.right.left.right.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal4 = (LiteralInteger)tree.root.right.left.right.lex; Assert.AreEqual("5", literal4.value); Assert.IsTrue(tree.root.right.lex.GetType() == typeof(BinaryOperatorEqual)); Assert.IsTrue(tree.root.right.right.left.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal6 = (LiteralInteger)tree.root.right.right.left.lex; Assert.AreEqual("2", literal6.value); Assert.IsTrue(tree.root.right.right.lex.GetType() == typeof(BinaryOperatorLtEq)); Assert.IsTrue(tree.root.right.right.right.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal8 = (LiteralDecimal)tree.root.right.right.right.lex; Assert.AreEqual("6.3", literal8.value); // データ型評価 DataType dataType = MathExpressionAnalysisLogic.checkDataType(tree); Assert.AreEqual(dataType, DataType.Boolean); // 評価値評価 MathTreeNodeValue value = MathExpressionAnalysisLogic.eval(tree); Assert.AreEqual(value.type, DataType.Boolean); Assert.AreEqual(value.valueBool, true); }
/// <summary> /// 数式ツリーの逆ポーランド記法表現を取得する。 /// </summary> /// <param name="tree">数式ツリー。</param> /// <returns>数式ツリーの逆ポーランド記法表現の文字列。</returns> public static string createRPolish(MathTree tree) { return(createRPolishNode(tree.root)); }
public void TestMethodIntegerVariable() { string expr = "var1 + var2 * 5"; Variable var1 = new Variable(13); Variable var2 = new Variable(29); var variableMap = new Dictionary <string, Variable>(); variableMap.Add("var1", var1); variableMap.Add("var2", var2); var functionMap = new Dictionary <string, Function>(); // 終端記号化 List <TerminalSymbol> terminalSymbolList = MathExpressionAnalysisLogic.convertTerminalSymbolList(expr); Assert.AreEqual(5, terminalSymbolList.Count); Assert.AreEqual(TerminalSymbolType.Variable, terminalSymbolList[0].type); Assert.AreEqual("var1", terminalSymbolList[0].value); Assert.AreEqual(TerminalSymbolType.OpAdd, terminalSymbolList[1].type); Assert.AreEqual("+", terminalSymbolList[1].value); Assert.AreEqual(TerminalSymbolType.Variable, terminalSymbolList[2].type); Assert.AreEqual("var2", terminalSymbolList[2].value); Assert.AreEqual(TerminalSymbolType.OpProd, terminalSymbolList[3].type); Assert.AreEqual("*", terminalSymbolList[3].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[4].type); Assert.AreEqual("5", terminalSymbolList[4].value); // 品詞化 List <Lexical> lexicalList = MathExpressionAnalysisLogic.convertLexicalList(terminalSymbolList); Assert.AreEqual(5, lexicalList.Count); Assert.IsTrue(lexicalList[0].GetType() == typeof(LiteralVariable)); Assert.IsTrue(lexicalList[1].GetType() == typeof(BinaryOperatorAdd)); Assert.IsTrue(lexicalList[2].GetType() == typeof(LiteralVariable)); Assert.IsTrue(lexicalList[3].GetType() == typeof(BinaryOperatorProd)); Assert.IsTrue(lexicalList[4].GetType() == typeof(LiteralInteger)); LiteralVariable l0 = (LiteralVariable)lexicalList[0]; Assert.AreEqual("var1", l0.value); LiteralVariable l2 = (LiteralVariable)lexicalList[2]; Assert.AreEqual("var2", l2.value); LiteralInteger l4 = (LiteralInteger)lexicalList[4]; Assert.AreEqual("5", l4.value); Operator op1 = (Operator)lexicalList[1]; Assert.AreEqual(5, op1.getPriority()); Operator op3 = (Operator)lexicalList[3]; Assert.AreEqual(6, op3.getPriority()); // 数式ツリー化 MathTree tree = MathExpressionAnalysisLogic.makeMathTree(lexicalList); Assert.IsTrue(tree.root.left.lex.GetType() == typeof(LiteralVariable)); LiteralVariable literal0 = (LiteralVariable)tree.root.left.lex; Assert.AreEqual("var1", literal0.value); Assert.IsTrue(tree.root.lex.GetType() == typeof(BinaryOperatorAdd)); Assert.IsTrue(tree.root.right.left.lex.GetType() == typeof(LiteralVariable)); LiteralVariable literal2 = (LiteralVariable)tree.root.right.left.lex; Assert.AreEqual("var2", literal2.value); Assert.IsTrue(tree.root.right.lex.GetType() == typeof(BinaryOperatorProd)); Assert.IsTrue(tree.root.right.right.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal4 = (LiteralInteger)tree.root.right.right.lex; Assert.AreEqual("5", literal4.value); // データ型評価 DataType dataType = MathExpressionAnalysisLogic.checkDataType(tree, variableMap, functionMap); Assert.AreEqual(dataType, DataType.Integer); // 評価値評価 MathTreeNodeValue value = MathExpressionAnalysisLogic.eval(tree, variableMap, functionMap); Assert.AreEqual(value.type, DataType.Integer); Assert.AreEqual(value.valueInteger, 158); }
public void TestPattern2() { string expr = "1+-2-3*-4/5%6"; List <TerminalSymbol> terminalSymbolList = MathExpressionAnalysisLogic.convertTerminalSymbolList(expr); Assert.AreEqual(13, terminalSymbolList.Count); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[0].type); Assert.AreEqual("1", terminalSymbolList[0].value); Assert.AreEqual(TerminalSymbolType.OpAdd, terminalSymbolList[1].type); Assert.AreEqual("+", terminalSymbolList[1].value); Assert.AreEqual(TerminalSymbolType.OpNeg, terminalSymbolList[2].type); Assert.AreEqual("-", terminalSymbolList[2].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[3].type); Assert.AreEqual("2", terminalSymbolList[3].value); Assert.AreEqual(TerminalSymbolType.OpDiff, terminalSymbolList[4].type); Assert.AreEqual("-", terminalSymbolList[4].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[5].type); Assert.AreEqual("3", terminalSymbolList[5].value); Assert.AreEqual(TerminalSymbolType.OpProd, terminalSymbolList[6].type); Assert.AreEqual("*", terminalSymbolList[6].value); Assert.AreEqual(TerminalSymbolType.OpNeg, terminalSymbolList[7].type); Assert.AreEqual("-", terminalSymbolList[7].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[8].type); Assert.AreEqual("4", terminalSymbolList[8].value); Assert.AreEqual(TerminalSymbolType.OpDivide, terminalSymbolList[9].type); Assert.AreEqual("/", terminalSymbolList[9].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[10].type); Assert.AreEqual("5", terminalSymbolList[10].value); Assert.AreEqual(TerminalSymbolType.OpMod, terminalSymbolList[11].type); Assert.AreEqual("%", terminalSymbolList[11].value); Assert.AreEqual(TerminalSymbolType.Integer, terminalSymbolList[12].type); Assert.AreEqual("6", terminalSymbolList[12].value); List <Lexical> lexicalList = MathExpressionAnalysisLogic.convertLexicalList(terminalSymbolList); Assert.AreEqual(13, lexicalList.Count); Assert.IsTrue(lexicalList[0].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[1].GetType() == typeof(BinaryOperatorAdd)); Assert.IsTrue(lexicalList[2].GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(lexicalList[3].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[4].GetType() == typeof(BinaryOperatorDiff)); Assert.IsTrue(lexicalList[5].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[6].GetType() == typeof(BinaryOperatorProd)); Assert.IsTrue(lexicalList[7].GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(lexicalList[8].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[9].GetType() == typeof(BinaryOperatorDivide)); Assert.IsTrue(lexicalList[10].GetType() == typeof(LiteralInteger)); Assert.IsTrue(lexicalList[11].GetType() == typeof(BinaryOperatorMod)); Assert.IsTrue(lexicalList[12].GetType() == typeof(LiteralInteger)); LiteralInteger l0 = (LiteralInteger)lexicalList[0]; Assert.AreEqual("1", l0.value); LiteralInteger l3 = (LiteralInteger)lexicalList[3]; Assert.AreEqual("2", l3.value); LiteralInteger l5 = (LiteralInteger)lexicalList[5]; Assert.AreEqual("3", l5.value); LiteralInteger l8 = (LiteralInteger)lexicalList[8]; Assert.AreEqual("4", l8.value); LiteralInteger l10 = (LiteralInteger)lexicalList[10]; Assert.AreEqual("5", l10.value); LiteralInteger l12 = (LiteralInteger)lexicalList[12]; Assert.AreEqual("6", l12.value); Operator op1 = (Operator)lexicalList[1]; Assert.AreEqual(5, op1.getPriority()); Operator op2 = (Operator)lexicalList[2]; Assert.AreEqual(8, op2.getPriority()); Operator op4 = (Operator)lexicalList[4]; Assert.AreEqual(5, op4.getPriority()); Operator op6 = (Operator)lexicalList[6]; Assert.AreEqual(6, op6.getPriority()); Operator op7 = (Operator)lexicalList[7]; Assert.AreEqual(8, op7.getPriority()); Operator op9 = (Operator)lexicalList[9]; Assert.AreEqual(6, op9.getPriority()); Operator op11 = (Operator)lexicalList[11]; Assert.AreEqual(6, op11.getPriority()); MathTree tree = MathExpressionAnalysisLogic.makeMathTree(lexicalList); Assert.IsTrue(tree.root.left.left.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal0 = (LiteralInteger)tree.root.left.left.lex; Assert.AreEqual("1", literal0.value); Assert.IsTrue(tree.root.left.lex.GetType() == typeof(BinaryOperatorAdd)); Assert.IsTrue(tree.root.left.right.lex.GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(tree.root.left.right.right.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal3 = (LiteralInteger)tree.root.left.right.right.lex; Assert.AreEqual("2", literal3.value); Assert.IsTrue(tree.root.lex.GetType() == typeof(BinaryOperatorDiff)); Assert.IsTrue(tree.root.right.left.left.left.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal5 = (LiteralInteger)tree.root.right.left.left.left.lex; Assert.AreEqual("3", literal5.value); Assert.IsTrue(tree.root.right.left.left.lex.GetType() == typeof(BinaryOperatorProd)); Assert.IsTrue(tree.root.right.left.left.right.lex.GetType() == typeof(UnaryOperatorNegative)); Assert.IsTrue(tree.root.right.left.left.right.right.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal8 = (LiteralInteger)tree.root.right.left.left.right.right.lex; Assert.AreEqual("4", literal8.value); Assert.IsTrue(tree.root.right.left.lex.GetType() == typeof(BinaryOperatorDivide)); Assert.IsTrue(tree.root.right.left.right.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal10 = (LiteralInteger)tree.root.right.left.right.lex; Assert.AreEqual("5", literal10.value); Assert.IsTrue(tree.root.right.lex.GetType() == typeof(BinaryOperatorMod)); Assert.IsTrue(tree.root.right.right.lex.GetType() == typeof(LiteralInteger)); LiteralInteger literal12 = (LiteralInteger)tree.root.right.right.lex; Assert.AreEqual("6", literal12.value); DataType dataType = MathExpressionAnalysisLogic.checkDataType(tree); Assert.AreEqual(dataType, DataType.Integer); MathTreeNodeValue value = MathExpressionAnalysisLogic.eval(tree); Assert.AreEqual(value.type, DataType.Integer); Assert.AreEqual(value.valueInteger, 1); }
private void btnEnter_Click(object sender, EventArgs e) { // バリデーション string validateMessage = ""; // 数式をツリーに変換。 MathTree treeMin = new MathTree(); MathTree treeMax = new MathTree(); try { treeMin = MathExpressionAnalysisLogic.getMathTreeFromString(txtMin.Text); if (!InputInfoValidation.validateMathTreeDataType(treeMin, DataType.Integer, this.availableVariableMap)) { validateMessage += "最小値の評価結果が整数になりません。\r\n"; } } catch (ArgumentException argEx) { validateMessage += "最小値を数式に変換できませんでした。\r\n" + "エラーメッセージ:" + argEx.Message + "\r\n"; } try { treeMax = MathExpressionAnalysisLogic.getMathTreeFromString(txtMax.Text); if (!InputInfoValidation.validateMathTreeDataType(treeMax, DataType.Integer, this.availableVariableMap)) { validateMessage += "最大値の評価結果が整数になりません。\r\n"; } } catch (ArgumentException argEx) { validateMessage += "最大値を数式に変換できませんでした。\r\n" + "エラーメッセージ:" + argEx.Message + "\r\n"; } if (!rbDivisorNewLine.Checked && !rbDivisorSpace.Checked && !rbDivisorEmpty.Checked && !rbDivisorCustom.Checked) { validateMessage += "区切り文字を指定してください。\r\n"; } string name = txtName.Text; if (!InputInfoValidation.validateVariableName(name)) { validateMessage += "名前に使用できない文字列が含まれているか、または名前の先頭が数値です。\r\n"; } else if (variableNameList.Contains(name)) { validateMessage += "この変数名はすでに使用されています。\r\n"; } if (validateMessage.Length > 0) { MessageBox.Show(validateMessage); return; } // 変換 string divisor = InputInfoLogic.getDivisor(rbDivisorNewLine.Checked, rbDivisorSpace.Checked, rbDivisorEmpty.Checked, rbDivisorCustom.Checked, txtDivisorCustom.Text); // FormEditIntegerの戻り値設定 var inputinfoInteger = new InputInfoInteger(treeMin, treeMax, txtMin.Text, txtMax.Text, divisor); inputinfoInteger.name = name; this.inputInfo = inputinfoInteger; // DialogResultの設定 this.DialogResult = DialogResult.OK; // フォームを閉じる this.Close(); }
static void Main(string[] args) { // BigFloat configuration BigFloat.RoundingMode = BigFloat.RoundingModeType.TRIM; BigFloat.RoundingDigits = 4; BigFloat.DefaultPrecision = new Library.Numerics.PrecisionSpec(16, Library.Numerics.PrecisionSpec.BaseType.DEC); /* BigDecimal a = BigDecimal.Parse("1234567865757567"); BigDecimal b = BigDecimal.Parse("1233425678"); BigDecimal n = 15; BigDecimal x, y; x = b * BigDecimal.Log10(a); y = (BigDecimal.Pow(10, x - BigDecimal.Round(x) + n - 1)); Console.WriteLine("Result: {0}*10^{1}\n", BigDecimal.Round(y), BigDecimal.Round(x)); */ Stopwatch Time = new Stopwatch(); string input = "(6/(6x)) + a^(x)/a + 6x/3 + x(x-5)(x+3)(x-54x+54x)x^3 = -4x^5"; MathNode Node = new MathNode(input); Console.WriteLine("\n Equation: {0}", input); MathTree t = new MathTree(Node); // t.Print(); Time.Start(); Node.Simplify(); Time.Stop(); Node.SolveEqual(); Node.Simplify(); // Rearrange the terms Node.Rearrange(); Console.WriteLine("\n Expanded: {0}\n\n", Node.ToString()); /* if (Node.type == NodeTypes.Equal) { Tuple<List<BigFloat>, List<BigFloat>> Roots = Node.Roots(); if (!Object.ReferenceEquals(Roots.Item1, null)) { List<Tuple<BigFloat, BigFloat>> Roots2 = Roots.Item1.Zip(Roots.Item2, (x, y) => Tuple.Create(x, y)).ToList(); bool FirstReal = true; bool FirstImag = true; foreach (Tuple<BigFloat, BigFloat> Real in Roots2.Where(x => x.Item2.IsZero())) { if (FirstReal) { Console.WriteLine("\n\n Real Zero Values: \n"); FirstReal = false; } Console.WriteLine(" x = {0}", Real.Item1); } foreach (Tuple<BigFloat, BigFloat> Imaginary in Roots2.Where(x => !x.Item2.IsZero())) { if (FirstImag) { Console.WriteLine("\n\n Imaginary Zero Values: \n"); FirstImag = false; } Console.WriteLine(" x = {0} + {1}i", Imaginary.Item1, Imaginary.Item2); } Console.WriteLine(); } }*/ }
public void TestMethodDecimalVariable() { string expr = "var1 - var2 / 2.1"; Variable var1 = new Variable(2.3); Variable var2 = new Variable(12.6); var variableMap = new Dictionary <string, Variable>(); variableMap.Add("var1", var1); variableMap.Add("var2", var2); var functionMap = new Dictionary <string, Function>(); // 終端記号化 List <TerminalSymbol> terminalSymbolList = MathExpressionAnalysisLogic.convertTerminalSymbolList(expr); Assert.AreEqual(5, terminalSymbolList.Count); Assert.AreEqual(TerminalSymbolType.Variable, terminalSymbolList[0].type); Assert.AreEqual("var1", terminalSymbolList[0].value); Assert.AreEqual(TerminalSymbolType.OpDiff, terminalSymbolList[1].type); Assert.AreEqual("-", terminalSymbolList[1].value); Assert.AreEqual(TerminalSymbolType.Variable, terminalSymbolList[2].type); Assert.AreEqual("var2", terminalSymbolList[2].value); Assert.AreEqual(TerminalSymbolType.OpDivide, terminalSymbolList[3].type); Assert.AreEqual("/", terminalSymbolList[3].value); Assert.AreEqual(TerminalSymbolType.Decimal, terminalSymbolList[4].type); Assert.AreEqual("2.1", terminalSymbolList[4].value); // 品詞化 List <Lexical> lexicalList = MathExpressionAnalysisLogic.convertLexicalList(terminalSymbolList); Assert.AreEqual(5, lexicalList.Count); Assert.IsTrue(lexicalList[0].GetType() == typeof(LiteralVariable)); Assert.IsTrue(lexicalList[1].GetType() == typeof(BinaryOperatorDiff)); Assert.IsTrue(lexicalList[2].GetType() == typeof(LiteralVariable)); Assert.IsTrue(lexicalList[3].GetType() == typeof(BinaryOperatorDivide)); Assert.IsTrue(lexicalList[4].GetType() == typeof(LiteralDecimal)); LiteralVariable l0 = (LiteralVariable)lexicalList[0]; Assert.AreEqual("var1", l0.value); LiteralVariable l2 = (LiteralVariable)lexicalList[2]; Assert.AreEqual("var2", l2.value); LiteralDecimal l4 = (LiteralDecimal)lexicalList[4]; Assert.AreEqual("2.1", l4.value); Operator op1 = (Operator)lexicalList[1]; Assert.AreEqual(5, op1.getPriority()); Operator op3 = (Operator)lexicalList[3]; Assert.AreEqual(6, op3.getPriority()); // 数式ツリー化 MathTree tree = MathExpressionAnalysisLogic.makeMathTree(lexicalList); Assert.IsTrue(tree.root.left.lex.GetType() == typeof(LiteralVariable)); LiteralVariable literal0 = (LiteralVariable)tree.root.left.lex; Assert.AreEqual("var1", literal0.value); Assert.IsTrue(tree.root.lex.GetType() == typeof(BinaryOperatorDiff)); Assert.IsTrue(tree.root.right.left.lex.GetType() == typeof(LiteralVariable)); LiteralVariable literal2 = (LiteralVariable)tree.root.right.left.lex; Assert.AreEqual("var2", literal2.value); Assert.IsTrue(tree.root.right.lex.GetType() == typeof(BinaryOperatorDivide)); Assert.IsTrue(tree.root.right.right.lex.GetType() == typeof(LiteralDecimal)); LiteralDecimal literal4 = (LiteralDecimal)tree.root.right.right.lex; Assert.AreEqual("2.1", literal4.value); // データ型評価 DataType dataType = MathExpressionAnalysisLogic.checkDataType(tree, variableMap, functionMap); Assert.AreEqual(dataType, DataType.Decimal); // 評価値評価 MathTreeNodeValue value = MathExpressionAnalysisLogic.eval(tree, variableMap, functionMap); Assert.AreEqual(value.type, DataType.Decimal); Assert.AreEqual(value.valueDecimal, -3.7); }
/// <summary> /// Strip the parentheses from a list of elements /// </summary> /// <param name="Elements"></param> /// <returns></returns> private List<Tuple<string, string>> StripParentheses(List<Tuple<string, string>> Elements) { // ListOfTuples.ToString() string Result = Elements.Select(x => x.Item1).Aggregate((x, y) => x + y).ToString(); string val = ""; while (Result.Where(x => x == '(' || x == ')').Count() > 0 && val != Result) { val = Result; if (string.Format("({0})", Result.Substring(1, Result.Length - 2)) == Result && ParserHelper.isValidParentheses(Result.Substring(1, Result.Length - 2))) { Elements.RemoveAt(0); Elements.RemoveAt(Elements.Count - 1); } Result = Elements.Select(x => x.Item1).Aggregate((x, y) => x + y).ToString(); } return Elements; }