Node_M PlusMinus() { Node_M left = Mult(); while (true) { //Identify the operation Operation op = Operation.Null; if (splitter.current_sign == Sign.Plus) { op = Operation.Addition; } else if (splitter.current_sign == Sign.Minus) { op = Operation.Substraction; } //If no plus or minus sign is found if (op == Operation.Null) { return(left); } splitter.Move_sign(); //move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); } Node_M right = Mult(); //Create a node with a correspondent binary operation left = new NodeMatrBinary(left, right, op); } }
Node_M Mult() { //Check whether the current node is a unary operator Node_M left = Unary(); while (true) { //Identify the operation Operation op = Operation.Null; if (splitter.current_sign == Sign.Multiply) { op = Operation.Multiplication; } //If no multiply or divide sign is found if (op == Operation.Null) { return(left); } splitter.Move_sign(); //move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); } //Check whether the current node is a unary operator Node_M right = Unary(); //Create a node with a correspondent binary operation left = new NodeMatrBinary(left, right, op); } }
//Method that parses the whole expression, which calls other methods one by one //in order to build a correct binary tree of expression public Node_M CheckExpression() { Node_M node = PlusMinus(); if (splitter.current_sign != Sign.EOF) { Console.WriteLine("Unexpected characters at end of expression"); Environment.Exit(0); } return(node); }
Node_M Part() { /*[ 2 0 ; -1 3 ] * [ 2 0 ; -1 3 ]*/ //Matr if (splitter.current_sign == Sign.MatrOpen) { Node_M part = PartMatrOpen(); return(part); } //inv if (splitter.current_sign == Sign.Inv) { Node_M part = PartInv(); return(part); } //Parenthesis if (splitter.current_sign == Sign.Open) { splitter.Move_sign(); //move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); } //Parse sequentially all the expression after the ( Node_M part = PlusMinus(); // Find the ) if (splitter.current_sign != Sign.Close) //if there's no ) { Console.WriteLine("Missing close parenthesis"); Environment.Exit(0); } splitter.Move_sign(); //move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); } return(part); } //If the sign is not classified Console.WriteLine($"Unknown sign: {splitter.current_sign}"); throw new SystemException($"Unknown sign: {splitter.current_sign}"); }
Node_M Unary() { while (true) { //In case of unary plus //Unary plus doesn't need any operation if (splitter.current_sign == Sign.Plus) { splitter.Move_sign(); //Move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); } continue; } //In case of unary minus else if (splitter.current_sign == Sign.Minus) { splitter.Move_sign(); //Move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); } //Recursive call to find the sequence of unary operators (if exists) //eg -+--10 = -10 Node_M a = Unary(); //Node for unary operator return(new NodeMatrUnary(a, Operation.UnaryMinus)); } //Just return number if no unary operator is found return(Part()); } }
Node_M PartInv() { //inv([ 2 5 7 ; 6 3 4 ; 5 -2 -3]) splitter.Move_sign(); //move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); //skip space } if (splitter.current_sign == Sign.Open) { splitter.Move_sign(); if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); //skip space } //Parse sequentially all the expression after the ( Node_M inside = PlusMinus(); // Find the ) if (splitter.current_sign != Sign.Close) //if there's no ) { Console.WriteLine("Missing closing parenthesis"); Environment.Exit(0); } else if (splitter.current_sign == Sign.Close) { int r = inside.Calculate().GetLength(0); int c = inside.Calculate().GetLength(1); double[,] matr = new double[r, c]; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { matr[i, j] = inside.Calculate()[i, j]; } } if (r == c) { int n = r; int t = 0; double[,] A = new double[r, c * 2]; //Create a copy of array for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A[i, j] = matr[i, j]; } } double[,] inv = new double[n, n]; if (MatrInv(A, inv, n)) { Node_M part = new NodeMatr(inv); //create the node with value of matrix splitter.Move_sign(); //move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); } return(part); } else { Console.WriteLine("Impossible to MatrInv"); Environment.Exit(0); } } else { Console.WriteLine("Impossible to MatrInv a non-square matrix"); Environment.Exit(0); } } } else { Console.WriteLine("Missing opening parenthesis"); Environment.Exit(0); } return(null); }