diff --git a/LazyMathInstructor/Program.cs b/LazyMathInstructor/Program.cs index 403792f..64a2ca0 100644 --- a/LazyMathInstructor/Program.cs +++ b/LazyMathInstructor/Program.cs @@ -210,6 +210,35 @@ private Term(Variable variable) }; } + /// + /// Helper method: Output string to STDOUT with console color , + /// restoring the original console color afterwards. + /// + private static void PrintColored(string s, ConsoleColor c) + { + ConsoleColor prevColor = Console.ForegroundColor; + Console.ForegroundColor = c; + Console.Write(s); + Console.ForegroundColor = prevColor; + } + + /// + /// Helper method: Output information about a term calculation to STDOUT. + /// = , + /// where is the symbol (+, -, *) of the performed . + /// + private static void PrintOperation(Term first, Term second, Term result, string operation, char symbol) + { + Console.Write(operation); + PrintColored("(", ConsoleColor.Red); + Console.Write(first.ToString()); + PrintColored($") {symbol} (", ConsoleColor.Red); + Console.Write(second.ToString()); + PrintColored(") = ", ConsoleColor.Red); + PrintColored(result.ToString(), ConsoleColor.Green); + Console.WriteLine(); + } + /// /// Parses the string to a normalized term that can be compared to other terms. /// Parsing is done recursively for sub-terms in brackets, and terms are added/subtracted/multiplied @@ -255,12 +284,15 @@ public static Term Parse(string s) return lastTerm; + Term leftTerm = Term.Parse(s[..(restIndex - 1)]); + char operation = s[restIndex - 1]; + // Recursively parse the rest of the term to the left, *then* apply the correct operation between both subterms - return s[restIndex - 1] switch + return operation switch { - '+' => Term.Parse(s[..(restIndex - 1)]) + lastTerm, - '-' => Term.Parse(s[..(restIndex - 1)]) - lastTerm, - '*' => Term.Parse(s[..(restIndex - 1)]) * lastTerm, + '+' => leftTerm + lastTerm, + '-' => leftTerm - lastTerm, + '*' => leftTerm * lastTerm, _ => throw new ArgumentException("Malformed term: expected operator after sub-term", nameof(s)), }; } @@ -280,6 +312,9 @@ public static Term Parse(string s) result.Coefficients.Add(varCombo, coefficient); } + if (Program.Verbose) + PrintOperation(first, second, result, "Add: ", '+'); + return result; } @@ -298,6 +333,9 @@ public static Term Parse(string s) result.Coefficients.Add(varCombo, coefficient); } + if (Program.Verbose) + PrintOperation(first, second, result, "Sub: ", '-'); + return result; } @@ -327,6 +365,10 @@ public static Term Parse(string s) } } } + + if (Program.Verbose) + PrintOperation(first, second, result, "Mult: ", '*'); + return result; } @@ -367,8 +409,14 @@ public override string ToString() } } - internal class Program + public class Program { + /// + /// Indicates if the program was started with the --verbose flag + /// and should output (intermediate) parsing results. + /// + public static bool Verbose { get; private set; } + /// /// Reads the input from STDIN, parses the terms and returns them in a list of tuples /// of two terms each, that are to be checked for equivalence. @@ -381,7 +429,9 @@ internal class Program for (int i = 0; i < n; i++) { Term first = Term.Parse(Console.ReadLine()!.Replace(" ", "").Replace("\t", "")); + if (Program.Verbose) Console.WriteLine(); Term second = Term.Parse(Console.ReadLine()!.Replace(" ", "").Replace("\t", "")); + if (Program.Verbose) Console.WriteLine(); terms.Add((first, second)); } @@ -399,7 +449,7 @@ static void PrintHelpMessage() "Terminate each input with a newline.\n" + "\n" + "Options:\n" + - " -v, --verbose: Output parsed terms\n" + + " -v, --verbose: Output parsed terms step-by-step\n" + " -h, --help: Display this message"); } @@ -417,13 +467,13 @@ static void Main(string[] args) Console.WriteLine("LazyMathInstructor by Ludwig Kolesch\nReading input from STDIN now.\n"); - bool verbose = args.Contains("-v") || args.Contains("--verbose"); + Verbose = args.Contains("-v") || args.Contains("--verbose"); var terms = GetInputTerms(); foreach (var (first, second) in terms) { Console.WriteLine(first.Equals(second) ? "YES" : "NO"); - if (verbose) + if (Verbose) { Console.WriteLine("First: " + first.ToString()); Console.WriteLine("Second: " + second.ToString());