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); }
private static string ProcessBoolExpression(string expression, bool debugMode) { if (debugMode) { TextProc.WriteProcess("preprocessing"); } expression = ConstantsValidator.ValidateBoolExpression(expression); if (expression.Length == 0) { return("error (length)"); } if (!CharValidator.Validate(CharValidator.ExpressionType.Bool, expression, debugMode)) { return("error (chars)"); } if (!BracketsValidator.Validate(BracketsValidator.BracketsTypes.Round, expression, debugMode)) { return("error (brackets)"); } if (!SequenceValidator.ValidateBoolExpression(expression, debugMode)) { return("error (sequence)"); } if (debugMode) { TextProc.WriteProcess("processing"); } return("" + BoolProcessor.GetBoolExpressionResolve( BoolProcessor.ProcessBoolExpression(expression, debugMode), debugMode)); }
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); }
private static string ProcessMatrixExpression(string expression, bool debugMode) { if (debugMode) { TextProc.WriteProcess("preprocessing"); } expression = ConstantsValidator.ValidateMatrixExpression(expression); if (expression.Length == 0) { return("error (length)"); } if (!CharValidator.Validate(CharValidator.ExpressionType.Matrix, expression, debugMode)) { return("error (chars)"); } if (!BracketsValidator.Validate(BracketsValidator.BracketsTypes.Round, expression, debugMode)) { return("error (brackets)"); } if (!BracketsValidator.Validate(BracketsValidator.BracketsTypes.Square, expression, debugMode)) { return("error (brackets)"); } if (!PointersValidator.Validate('.', expression, debugMode)) { return("error (pointers)"); } if (!SequenceValidator.ValidateMatrixExpression(expression, debugMode)) { return("error (sequence)"); } if (debugMode) { TextProc.WriteProcess("processing"); } var separatedExpression = MatrixProcessor.GetExpressionSeparated(expression, debugMode); if (!MatrixValidator.Validate(separatedExpression, debugMode)) { return("error (matrix)"); } if (debugMode) { TextProc.WriteProcess("calculating"); } return(MatrixProcessor.ResolveExpression(separatedExpression)); }
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()); }
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)); }
private static void Help() { TextProc.WriteBar("expression_types"); TextProc.WriteInfo("--bool = bool expression", ":", ConsoleColor.Magenta); TextProc.WriteInfo("--reg = regular expression", ":", ConsoleColor.Magenta); TextProc.WriteInfo("--mat = matrix expression", ":", ConsoleColor.Magenta); TextProc.WriteInfo("--pov = polynomial of one variable", ":", ConsoleColor.Magenta); TextProc.WriteBar("variables"); TextProc.WriteInfo("--vars", ":", ConsoleColor.Magenta); TextProc.WriteBar("debug_mode"); TextProc.WriteInfo("--debug", ":", ConsoleColor.Magenta); Console.WriteLine(); }
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(""); } }
public void TestMethod1() { TextProc tp = new TextProc(); string s = @".OnE aWo don't2oneone one !Unone-ones+awOED AWOing done doned slip funny fun slipped"; tp.Run(s); tp.DelStart("un"); tp.DelEnd("s"); tp.DelEnd("ed"); tp.ReplEnd("ed", "e"); tp.DelEnd("ing"); tp.ReplEnd2("y"); tp.ReplEnd2("ed"); tp.Sorting(); Assert.AreNotEqual(tp.lenText, 0, "lenText"); Assert.AreEqual(tp.lenText, s.Length, "lenText"); Assert.AreEqual(68, tp.allChars, "allChars"); Assert.AreEqual(15, tp.allWord, "allWord"); Assert.AreEqual(14, tp.uniqWord, "uniqWord"); Assert.AreEqual(7, tp.uniqMiddle, "uniqMiddle"); int nord = -1; WordCnt p; p = tp.At(++nord); Assert.AreEqual(p.word, "one", "Sort word " + nord); Assert.AreEqual(p.cnt, 4, "Sort cnt " + nord); p = tp.At(++nord); Assert.AreEqual(p.word, "awo", "Sort word " + nord); Assert.AreEqual(p.cnt, 3, "Sort cnt " + nord); p = tp.At(++nord); Assert.AreEqual(p.word, "done", "Sort word " + nord); Assert.AreEqual(p.cnt, 2, "Sort cnt " + nord); p = tp.At(++nord); Assert.AreEqual(p.word, "fun", "Sort word " + nord); Assert.AreEqual(p.cnt, 2, "Sort cnt " + nord); p = tp.At(++nord); Assert.AreEqual(p.word, "slip", "Sort word " + nord); Assert.AreEqual(p.cnt, 2, "Sort cnt " + nord); p = tp.At(++nord); Assert.AreEqual(p.word, "don't", "Sort word " + nord); Assert.AreEqual(p.cnt, 1, "Sort cnt " + nord); p = tp.At(++nord); Assert.AreEqual(p.word, "oneone", "Sort word " + nord); Assert.AreEqual(p.cnt, 1, "Sort cnt " + nord); } // //////////////////////////////////////////////////////////////////////
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); }
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); }
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); }
private static string ProcessRegularExpression(string expression, bool debugMode) { if (debugMode) { TextProc.WriteProcess("preprocessing"); } expression = ConstantsValidator.ValidateRegExpression(expression); if (expression.Length == 0) { return("error (length)"); } if (!CharValidator.Validate(CharValidator.ExpressionType.Reg, expression, debugMode)) { return("error (chars)"); } if (!BracketsValidator.Validate(BracketsValidator.BracketsTypes.Round, expression, debugMode)) { return("error (brackets)"); } if (!SequenceValidator.ValidateRegExpression(expression, debugMode)) { return("error (sequence)"); } if (!PointersValidator.Validate(',', expression, debugMode)) { return("error (pointers)"); } if (debugMode) { TextProc.WriteProcess("processing"); } return("" + Processor.ResolveExpression( Processor.GetExpressionSeparated(expression, debugMode))); }
private static void Main(string[] args) { var debugMode = false; var useVars = false; var expressionType = ""; var solution = ""; var vars = new Dictionary <string, string>(); foreach (var arg in args) { if (arg == "--bool" || arg == "--reg" || arg == "--mat" || arg == "--pov") { expressionType = arg; } else if (arg == "--debug") { debugMode = true; } else if (arg == "--vars") { useVars = true; } } if (expressionType == "") { Help(); } else { if (useVars) { TextProc.WriteBar("vars"); Console.Write(" : "); vars = VarsProcessor.GetVarDictionary(TextProc.ReadColor(ConsoleColor.Blue)); } TextProc.WriteBar("expression"); Console.Write(" : "); var expression = TextProc.ReadColor(ConsoleColor.Blue); if (useVars && VarsProcessor.ValidateVars(vars)) { expression = VarsProcessor.ReplaceVars(expression, vars); } switch (expressionType) { case "--bool": solution = ProcessBoolExpression(expression, debugMode); break; case "--reg": solution = ProcessRegularExpression(expression, debugMode); break; case "--mat": solution = ProcessMatrixExpression(expression, debugMode); break; case "--pov": solution = ProcessPovExpression(expression, debugMode); break; default: Help(); break; } if (debugMode) { Console.WriteLine(); } TextProc.WriteBar("solution"); TextProc.WriteInfo(solution, "=", solution.IndexOf("error", StringComparison.Ordinal) == -1 ? ConsoleColor.Green : ConsoleColor.Red); Console.WriteLine(); } }
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); }