Example #1
0
        public static bool Validate(List <string> expression, bool debugMode)
        {
            foreach (var t in expression.Where(t => t.Length != 1))
            {
                if (debugMode)
                {
                    Console.Write(" | ");
                }
                if (debugMode)
                {
                    Console.Write(t);
                }

                var invalidMatrixFound = false;
                var processedMatrix    = MatrixProcessor.ProcessMatrix(t);
                for (var j = 1; j < processedMatrix.Count && !invalidMatrixFound; j++)
                {
                    if (processedMatrix[j].Count != processedMatrix[j - 1].Count)
                    {
                        invalidMatrixFound = true;
                    }
                }

                if (!invalidMatrixFound)
                {
                    TextProc.WriteColor($" | (matrix definition) OK\n", ConsoleColor.Green);
                    continue;
                }

                TextProc.WriteColor($" <-- not allowed matrix definition\n", ConsoleColor.Red);
                return(false);
            }

            return(true);
        }
Example #2
0
        public static bool Validate(Enum expressionType, string expression, bool debugMode)
        {
            var allowedChars = new HashSet <char>();

            switch (expressionType)
            {
            case ExpressionType.Reg:
                allowedChars = new HashSet <char>
                {
                    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
                    '-', '+', '/', '*', ')', '(', 'P', 'E', 'S', 'C', 'T', ','
                };
                break;

            case ExpressionType.Bool:
                allowedChars = new HashSet <char>
                {
                    '1', '0', '|', '&', '(', ')', '!',
                };
                break;

            case ExpressionType.Matrix:
                allowedChars = new HashSet <char>
                {
                    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
                    ')', '(', '[', ']', '-', '+', '*', 'I', '.', ',', ';',
                };
                break;
            }

            if (debugMode)
            {
                Console.Write(" | ");
            }
            foreach (var _char in expression)
            {
                if (debugMode)
                {
                    Console.Write(_char);
                }
                if (!allowedChars.Add(_char))
                {
                    continue;
                }
                if (debugMode)
                {
                    TextProc.WriteColor(" <-- this character is not allowed\n", ConsoleColor.Red);
                }
                return(false);
            }

            if (debugMode)
            {
                TextProc.WriteColor(" | (chars) OK\n", ConsoleColor.Green);
            }
            return(true);
        }
Example #3
0
        public static List <string> ProcessBoolExpression(string expression, bool debugMode)
        {
            if (debugMode)
            {
                Console.Write(" | ");
            }
            if (!debugMode)
            {
                return(expression.Select(s => "" + s).ToList());
            }
            Console.Write(expression);
            TextProc.WriteColor(" | (expression separation) OK", ConsoleColor.Green);

            return(expression.Select(s => "" + s).ToList());
        }
Example #4
0
        public static List <List <string> > SeparateExpression(string expression, bool debugMode)
        {
            expression = expression.Replace(" ", "");
            var charCodes = Encoding.ASCII.GetBytes(expression);
            var segments  = new List <string>();

            if (debugMode)
            {
                Console.Write(" | ");
            }
            var lastRemoveIndex = 0;

            for (var i = 0; i < expression.Length; i++)
            {
                switch (expression[i])
                {
                case '+':
                case '-':
                case '*':
                case '/':
                case '(':
                case ')':
                    if (lastRemoveIndex != i)
                    {
                        segments.Add(expression.Substring(lastRemoveIndex, i - lastRemoveIndex));
                    }
                    segments.Add("" + expression[i]);
                    lastRemoveIndex = i + 1;
                    break;

                default:
                    if (i == expression.Length - 1)
                    {
                        segments.Add(expression.Substring(lastRemoveIndex, i - lastRemoveIndex + 1));
                    }
                    break;
                }
            }

            if (!debugMode)
            {
                PerformExpression(segments);
            }
            Console.Write(String.Join(" ", segments));
            TextProc.WriteColor(" | (expression separation) OK", ConsoleColor.Green);

            return(PerformExpression(segments));
        }
Example #5
0
        private static string _ResolveMicroExpression(string matrix1, string _operator, string matrix2)
        {
            var processedMatrix1 = ProcessMatrix(matrix1);
            var processedMatrix2 = ProcessMatrix(matrix2);

            switch (_operator)
            {
            case "+":
                var validAdd = MatrixOperationValidator.ValidateSum(processedMatrix1, processedMatrix2);
                Console.Write($" | {matrix1} + {matrix2}");
                TextProc.WriteColor(validAdd ? " | (matrix addition) OK\n" : " <- can not add this matrices",
                                    ConsoleColor.Green);
                if (!validAdd)
                {
                    return("error (matrix addition)");
                }

                return(_MatrixToString(_MatrixAddition(processedMatrix1, processedMatrix2)));

            case "-":
                var validSub = MatrixOperationValidator.ValidateSum(processedMatrix1, processedMatrix2);
                Console.Write($" | {matrix1} - {matrix2}");
                TextProc.WriteColor(validSub ? " | (matrix subtraction) OK\n" : " <- can not subtract this matrices",
                                    ConsoleColor.Green);
                if (!validSub)
                {
                    return("error (matrix subtraction)");
                }

                return(_MatrixToString(_MatrixSubtraction(processedMatrix1, processedMatrix2)));

            case "*":
                var validMul = MatrixOperationValidator.ValidateSum(processedMatrix1, processedMatrix2);
                Console.Write($" | {matrix1} * {matrix2}");
                TextProc.WriteColor(validMul ? " | (matrix multiplication) OK\n" : " <- can not multiple this matrices",
                                    ConsoleColor.Green);
                if (!validMul)
                {
                    return("error (matrix multiplication)");
                }

                return(_MatrixToString(_MatrixMultiplication(processedMatrix1, processedMatrix2)));

            default:
                return("");
            }
        }
Example #6
0
        public static bool Validate(char pointer, string expression, bool debugMode)
        {
            var pointersCount = 0;

            if (debugMode)
            {
                Console.Write(" | ");
            }

            foreach (var _char in expression)
            {
                if (debugMode)
                {
                    Console.Write(_char);
                }

                if (_char == pointer)
                {
                    pointersCount++;

                    if (pointersCount != 2)
                    {
                        continue;
                    }
                    if (!debugMode)
                    {
                        return(false);
                    }
                    TextProc.WriteColor($" <-- {pointer} not allowed pointers sequence\n", ConsoleColor.Red);
                    return(false);
                }

                if (_char >= 57 || _char <= 48)
                {
                    pointersCount = 0;
                }
            }

            if (!debugMode)
            {
                return(true);
            }
            TextProc.WriteColor($" | {pointer}pointers{pointer} OK\n", ConsoleColor.Green);
            return(true);
        }
Example #7
0
        public static List <string> GetExpressionSeparated(string expression, bool debugMode)
        {
            var charCodes = Encoding.ASCII.GetBytes(expression);
            var segments  = new List <string>();

            if (debugMode)
            {
                Console.Write(" | ");
            }
            var lastRemoveIndex = 0;

            for (var i = 0; i < expression.Length; i++)
            {
                if ((charCodes[i] >= 40 && charCodes[i] <= 43) ||
                    charCodes[i] == 45 || charCodes[i] == 47 ||
                    charCodes[i] == 83 || charCodes[i] == 67 || charCodes[i] == 84)
                {
                    if ((charCodes[lastRemoveIndex] >= 48 && charCodes[lastRemoveIndex] <= 57))
                    {
                        segments.Add(expression.Substring(lastRemoveIndex, i - lastRemoveIndex));
                    }

                    segments.Add(expression[i].ToString());
                    lastRemoveIndex = i + 1;
                }
                else if
                (
                    i == expression.Length - 1 &&
                    (charCodes[lastRemoveIndex] >= 48 && charCodes[lastRemoveIndex] <= 57) ||
                    charCodes[lastRemoveIndex] == 80 || charCodes[lastRemoveIndex] == 69
                )
                {
                    segments.Add(expression.Substring(lastRemoveIndex, i - lastRemoveIndex + 1));
                }
            }

            if (!debugMode)
            {
                return(segments);
            }
            Console.Write(String.Join("", segments));
            TextProc.WriteColor(" | (expression separation) OK", ConsoleColor.Green);

            return(segments);
        }
Example #8
0
        public static bool ValidateMatrixExpression(string expression, bool debugMode)
        {
            if (debugMode)
            {
                Console.Write(" | ");
            }

            for (var i = 0; i < expression.Length; i++)
            {
                if (debugMode)
                {
                    Console.Write(expression[i]);
                }

                if (IsNearbyMatrixSequenceOk
                    (
                        i - 1 < 0 ? ' ' : expression[i - 1],
                        expression[i],
                        i + 1 > expression.Length - 1 ? ' ' : expression[i + 1]
                    ))
                {
                    continue;
                }

                if (!debugMode)
                {
                    return(false);
                }
                Console.Write(i + 1 > expression.Length - 1 ? ' ' : expression[i + 1]);
                TextProc.WriteColor(" <-- not allowed sequence\n", ConsoleColor.Red);
                return(false);
            }

            if (!debugMode)
            {
                return(true);
            }
            TextProc.WriteColor(" | (sequence) OK\n", ConsoleColor.Green);
            return(true);
        }
Example #9
0
        public static bool Validate(Enum bracketsTypes, string expression, bool debugMode)
        {
            var openingBrackets = 0;
            var closingBrackets = 0;

            if (debugMode)
            {
                Console.Write(" | ");
            }

            char openingBracket;
            char closingBracket;

            switch (bracketsTypes)
            {
            case BracketsTypes.Round:
                openingBracket = '(';
                closingBracket = ')';
                break;

            case BracketsTypes.Square:
                openingBracket = '[';
                closingBracket = ']';
                break;

            default:
                if (debugMode)
                {
                    TextProc.WriteColor(" <-- not type of brackets\n", ConsoleColor.Red);
                }
                return(false);
            }

            foreach (var _char in expression)
            {
                if (debugMode)
                {
                    Console.Write(_char);
                }

                if (_char == openingBracket)
                {
                    openingBrackets++;
                }
                else if (_char == closingBracket)
                {
                    closingBrackets++;
                }

                if (closingBrackets <= openingBrackets)
                {
                    continue;
                }
                TextProc.WriteColor($" <-- {closingBracket} no brackets to close\n", ConsoleColor.Red);
                return(false);
            }

            if (closingBrackets < openingBrackets)
            {
                if (debugMode)
                {
                    TextProc.WriteColor($" <-- {closingBracket} bracket is not closed\n", ConsoleColor.Red);
                }
                return(false);
            }

            if (debugMode)
            {
                TextProc.WriteColor($" | {openingBracket}brackets{closingBracket} OK\n", ConsoleColor.Green);
            }
            return(true);
        }