/// <summary> /// Create XML helper method. /// </summary> /// <param name="expressions">The infix expressions to be analyzed and printed to the xml file.</param> public static void CreateXMLFile(List <InfixExpression> expressions) { CompareExpressions compareResults = new CompareExpressions(); try { FileStream fs = new FileStream(FILE_PATH, FileMode.Create);//creates new file if none exists, overwrites file where file present using (StreamWriter s = new StreamWriter(fs, Encoding.UTF8)) { //pass StreamWriter into the extension methods to build XML document WriteDocumentStart(s); WriteStartRootElement(s); foreach (InfixExpression exp in expressions) { string preConvertedExpression = InfixToPrefixConverter.Convert(exp.Expression); string preEvaluatedResult = ExpressionEvaluation.EvaluatePreFix(preConvertedExpression); string postConvertedExpression = InfixToPostfixConverter.Convert(exp.Expression); string postEvaluatedResult = ExpressionEvaluation.EvaluatePostFix(postConvertedExpression); WriteStartElement(s, "element", 1); WriteAttribute(s, exp.SNO.ToString(), "sno", 2); WriteAttribute(s, exp.Expression, "infix", 2); WriteAttribute(s, preConvertedExpression, "prefix", 2); WriteAttribute(s, postConvertedExpression, "postfix", 2); WriteAttribute(s, preEvaluatedResult, "evaluation", 2); WriteAttribute(s, compareResults.Compare(Convert.ToDouble(preEvaluatedResult), Convert.ToDouble(postEvaluatedResult)) == 0 ? "true" : "false", "comparison", 2); WriteEndElement(s, "element", 1); } WriteEndRootElement(s); s.Close(); fs.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
static void Main(string[] args) { // Set up console width for formatting Console.WindowWidth = 116; string underline = new string('═', Console.WindowWidth); // Parse expressions from CSV file List <InfixExpression> expressions = CSVFile.CSVDeserialize("../../../Data/Project 2_Info_5101.csv"); /* Prefix */ // Print converted Postfix expressions Console.WriteLine(underline); Console.WriteLine("Prefix Expressions:"); Console.WriteLine($"{underline}\n"); foreach (InfixExpression exp in expressions) { string convertedExpression = InfixToPrefixConverter.Convert(exp.Expression); Console.WriteLine($"{exp.SNO}:\t{convertedExpression}"); } // Print expression results Console.WriteLine($"\n{underline}"); Console.WriteLine("Prefix Results:"); Console.WriteLine($"{underline}\n"); foreach (InfixExpression exp in expressions) { string convertedExpression = InfixToPrefixConverter.Convert(exp.Expression); string evaluatedResult = ExpressionEvaluation.EvaluatePreFix(convertedExpression); Console.WriteLine($"{exp.SNO}:\t{evaluatedResult}"); } /* Postfix */ // Print converted Postfix expressions Console.WriteLine($"\n{underline}"); Console.WriteLine("Postfix Expressions:"); Console.WriteLine($"{underline}\n"); foreach (InfixExpression exp in expressions) { string convertedExpression = InfixToPostfixConverter.Convert(exp.Expression); Console.WriteLine($"{exp.SNO}:\t{convertedExpression}"); } // Print expression results Console.WriteLine($"\n{underline}"); Console.WriteLine("Postfix Results:"); Console.WriteLine($"{underline}\n"); foreach (InfixExpression exp in expressions) { string convertedExpression = InfixToPostfixConverter.Convert(exp.Expression); string evaluatedResult = ExpressionEvaluation.EvaluatePostFix(convertedExpression); Console.WriteLine($"{exp.SNO}:\t{evaluatedResult}"); } /* Summary Report*/ // Header string title = "Summary Report"; Console.WriteLine($"\n{underline}"); Console.WriteLine($"{new string(' ', (Console.WindowWidth-title.Length) / 2)}{title}"); Console.WriteLine($"{underline}\n"); Console.WriteLine("╔═════╦════════════════════════╦══════════════════════╦══════════════════════╦═══════════════╦═══════════════╦═════╗"); Console.WriteLine("║{0,-5}║{1,-24}║{2,-22}║{3,-22}║{4,-15}║{5,-15}║{6,-5}║", "Sno", "Infix", "Prefix", "Postfix", "Prefix Result", "Postfix Result", "Match"); Console.WriteLine("╠═════╬════════════════════════╬══════════════════════╬══════════════════════╬═══════════════╬═══════════════╬═════╣"); //Body CompareExpressions compareResults = new CompareExpressions(); foreach (InfixExpression exp in expressions) { string preConvertedExpression = InfixToPrefixConverter.Convert(exp.Expression); string preEvaluatedResult = ExpressionEvaluation.EvaluatePreFix(preConvertedExpression); string postConvertedExpression = InfixToPostfixConverter.Convert(exp.Expression); string postEvaluatedResult = ExpressionEvaluation.EvaluatePostFix(postConvertedExpression); Console.WriteLine("║{0,-5}║{1,-24}║{2,-22}║{3,-22}║{4,-15}║{5,-15}║{6,-5}║", exp.SNO, exp.Expression, preConvertedExpression, postConvertedExpression, preEvaluatedResult, postEvaluatedResult, compareResults.Compare(Convert.ToDouble(preEvaluatedResult), Convert.ToDouble(postEvaluatedResult)) == 0 ? "true" : "false"); } Console.WriteLine("╚═════╩════════════════════════╩══════════════════════╩══════════════════════╩═══════════════╩═══════════════╩═════╝"); //Create XML file XMLExtension.CreateXMLFile(expressions); bool done = false; do { Console.Write("\nWould you like to lauch web browser to view XML output? (y/n): "); string selection = Console.ReadLine().ToLower(); switch (selection) { case ("y"): XMLExtension.OpenXMLInBrowser(); done = true; break; case ("n"): done = true; break; default: Console.Write("\nInvalid selection. "); break; } } while (!done); Console.WriteLine("\nThank you, have a nice day!"); }