public override MathTreeNodeValue eval(MathTreeNodeValue operand, Dictionary <string, Function> functionMap) { var arguments = new List <double>(); if (operand.type == DataType.Integer) { arguments.Add(operand.valueInteger); } else if (operand.type == DataType.Decimal) { arguments.Add(operand.valueDecimal); } else if (operand.type == DataType.DecimalList) { arguments.AddRange(operand.valueDecimalList); } else { throw new ArgumentException("関数の引数は整数、小数、小数リストのいずれかである必要があります。"); } if (!functionMap.ContainsKey(this.functionName)) { throw new ArgumentException("関数" + this.functionName + "が見つかりません。"); } Function function = functionMap[this.functionName]; return(function.eval(arguments)); }
public override MathTreeNodeValue eval(MathTreeNodeValue leftOperand, MathTreeNodeValue rightOperand) { double leftValue; if (leftOperand.type == DataType.Integer) { leftValue = leftOperand.valueInteger; } else if (leftOperand.type == DataType.Decimal) { leftValue = leftOperand.valueDecimal; } else { throw new ArgumentException("指数演算子は整数または小数に対してのみ演算できます。"); } double rightValue; if (rightOperand.type == DataType.Integer) { rightValue = rightOperand.valueInteger; } else if (rightOperand.type == DataType.Decimal) { rightValue = rightOperand.valueDecimal; } else { throw new ArgumentException("指数演算子は整数または小数に対してのみ演算できます。"); } return(new MathTreeNodeValue(Math.Pow(leftValue, rightValue))); }
public override MathTreeNodeValue eval(MathTreeNodeValue leftOperand, MathTreeNodeValue rightOperand) { if (leftOperand.type == DataType.Integer) { if (rightOperand.type == DataType.Integer) { return(new MathTreeNodeValue(leftOperand.valueInteger <= rightOperand.valueInteger)); } else if (rightOperand.type == DataType.Decimal) { return(new MathTreeNodeValue(leftOperand.valueInteger <= rightOperand.valueDecimal)); } } else if (leftOperand.type == DataType.Decimal) { if (rightOperand.type == DataType.Integer) { return(new MathTreeNodeValue(leftOperand.valueDecimal <= rightOperand.valueInteger)); } else if (rightOperand.type == DataType.Decimal) { return(new MathTreeNodeValue(leftOperand.valueDecimal <= rightOperand.valueDecimal)); } } throw new ArgumentException("小なりイコール演算子は整数または小数に対してのみ演算できます。"); }
public override MathTreeNodeValue eval(MathTreeNodeValue leftOperand, MathTreeNodeValue rightOperand) { if (leftOperand.type == DataType.Boolean && rightOperand.type == DataType.Boolean) { return(new MathTreeNodeValue(leftOperand.valueBool && rightOperand.valueBool)); } throw new ArgumentException("AND演算子は論理値に対してのみ演算できます。"); }
public override MathTreeNodeValue eval(MathTreeNodeValue operand, Dictionary <string, Function> functionMap) { if (operand.type == DataType.Boolean) { return(new MathTreeNodeValue(!operand.valueBool)); } else { throw new ArgumentException("否定演算子は論理値に対してのみ演算できます。"); } }
public override MathTreeNodeValue eval(MathTreeNodeValue leftOperand, MathTreeNodeValue rightOperand) { if (leftOperand.type == DataType.Integer && rightOperand.type == DataType.Integer) { return(new MathTreeNodeValue(leftOperand.valueInteger == rightOperand.valueInteger)); } else if (leftOperand.type == DataType.Boolean && rightOperand.type == DataType.Boolean) { return(new MathTreeNodeValue(leftOperand.valueBool == rightOperand.valueBool)); } throw new ArgumentException("等価演算子は整数と整数または論理値と論理値に対してのみ演算できます。"); }
public override MathTreeNodeValue eval(MathTreeNodeValue operand, Dictionary<string, Function> functionMap) { if (operand.type == DataType.Integer) { return new MathTreeNodeValue(-operand.valueInteger); } else if (operand.type == DataType.Decimal) { return new MathTreeNodeValue(-operand.valueDecimal); } else { throw new ArgumentException("負数演算子は整数または小数に対してのみ演算できます。"); } }
private static MathTreeNodeValue evalNode(MathTreeNode node, Dictionary <string, Variable> variableMap, Dictionary <string, Function> functionMap) { if (node.lex is Literal literal) { return(literal.eval(variableMap)); } else if (node.lex is UnaryOperator unaryOperator) { MathTreeNodeValue operand = evalNode(node.right, variableMap, functionMap); return(unaryOperator.eval(operand, functionMap)); } else if (node.lex is BinaryOperator binaryOperator) { MathTreeNodeValue leftOperand = evalNode(node.left, variableMap, functionMap); MathTreeNodeValue rightOperand = evalNode(node.right, variableMap, functionMap); return(binaryOperator.eval(leftOperand, rightOperand)); } else { throw new ApplicationException("リテラル、単項演算子、2項演算子以外の品詞が指定されました。"); } }
public override MathTreeNodeValue eval(MathTreeNodeValue leftOperand, MathTreeNodeValue rightOperand) { var list = new List <double>(); if (leftOperand.type == DataType.Integer) { list.Add(leftOperand.valueInteger); } else if (leftOperand.type == DataType.Decimal) { list.Add(leftOperand.valueDecimal); } else if (leftOperand.type == DataType.DecimalList) { list.AddRange(leftOperand.valueDecimalList); } else { throw new ArgumentException("カンマ演算子は整数、小数、小数リストに対してのみ演算できます。"); } if (rightOperand.type == DataType.Integer) { list.Add(rightOperand.valueInteger); } else if (rightOperand.type == DataType.Decimal) { list.Add(rightOperand.valueDecimal); } else if (rightOperand.type == DataType.DecimalList) { list.AddRange(rightOperand.valueDecimalList); } else { throw new ArgumentException("カンマ演算子は整数、小数、小数リストに対してのみ演算できます。"); } return(new MathTreeNodeValue(list)); }
public static void AssertMathTreeNodeValue(double expected, MathTreeNodeValue actual) { Assert.AreEqual(DataType.Decimal, actual.type); Assert.AreEqual(expected, actual.valueDecimal); }
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="operand">オペランドの値</param> /// <param name="functionMap">関数の連想配列。</param> /// <returns>演算後の評価値。</returns> public abstract MathTreeNodeValue eval(MathTreeNodeValue operand, Dictionary <string, Function> functionMap);
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); }
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); }
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 static void AssertMathTreeNodeValue(long expected, MathTreeNodeValue actual) { Assert.AreEqual(DataType.Integer, actual.type); Assert.AreEqual(expected, actual.valueInteger); }
public static void AssertMathTreeNodeValue(bool expected, MathTreeNodeValue actual) { Assert.AreEqual(DataType.Boolean, actual.type); Assert.AreEqual(expected, actual.valueBool); }
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); }
public static void AssertMathTreeNodeValue(List <double> expected, MathTreeNodeValue actual) { Assert.AreEqual(DataType.DecimalList, actual.type); CollectionAssert.AreEqual(expected, actual.valueDecimalList); }