private static void GenerateExpressionsFromFormat() { if (_formats.Count == 0) { IO.WriteLineError("There are no formats specified!"); return; } int resultLower, resultUpper, numberOfExpressions; string outFileName; while (!GetDesiredResult(out resultLower, out resultUpper)) { ; } while (!GetNumberOfExpressionsToGenerate(out numberOfExpressions)) { ; } while (!GetOutputFile(out outFileName)) { ; } IO.RedirectOutputToFile(outFileName, FileMode.Create); for (int i = 0; i < numberOfExpressions; i++) { int goal = ExpressionGenerator.Random.Next(resultLower, resultUpper + 1); var format = _formats.GetRandomElement(); IO.WriteLine(_generator.FromFormat(format, goal)); } IO.RedirectOutputToConsole(); }
private static bool GetInputFile(out string inputFileName) { inputFileName = IO.ReadLine("Input file: "); if (inputFileName == string.Empty) { IO.WriteLineError("No input file provided."); return(false); } try { new FileInfo(inputFileName); } catch (ArgumentException) { IO.WriteLineError("Invalid file name!"); return(false); } catch (Exception e) { IO.WriteLineError(e.Message); return(false); } return(true); }
private static bool GetNumberOfExpressionsToGenerate(out int number) { string input = IO.ReadLine("Number of expressions to generate: "); if (!int.TryParse(input, out number)) { IO.WriteLineError("You must enter a number!"); return(false); } return(true); }
private static void ParseInput() { string input = IO.ReadLine("Your choice: "); Action action; if (!_actions.TryGetValue(input, out action)) { IO.WriteLineError("Invalid input!"); return; } action(); }
private static bool GetNumberOfOperands(out int lower, out int upper) { lower = upper = -1; string input = IO.ReadLine("Desired number of operands in each expression (one number for exact number of operands, two numbers for a range): ", removeWhiteSpace: false); var args = input.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (args.Length == 0) { IO.WriteLineError("You must enter at least one number!"); return(false); } else if (args.Length == 1) { int result; if (!int.TryParse(args[0], out result)) { IO.WriteLineError("You must enter a number!"); return(false); } if (result < 2) { IO.WriteLineError("There must be at least two operands!"); return(false); } lower = upper = result; } else if (args.Length == 2) { if (!int.TryParse(args[0], out lower) || !int.TryParse(args[1], out upper)) { IO.WriteLineError("You must enter a number!"); return(false); } if (upper < lower) { MathHelper.Swap(ref lower, ref upper); } if (lower < 2 || upper < 2) { IO.WriteLineError("There must be at least two operands!"); return(false); } } else { IO.WriteLineError("Too many numbers provided!"); return(false); } return(true); }
private static bool AddNewFormat(string format) { format = Regex.Replace(format, @"\s+", ""); if (format.Length < 3 || !_formatRegex.IsMatch(format)) { IO.WriteLineError("Format '{0}' is invalid!", format); return(false); } if (!_formats.Add(new ExpressionFormat(format))) { IO.WriteLineError("Format '{0}' already exists!", format); return(false); } return(true); }
private static bool GetOutputFile(out string outFileName) { outFileName = IO.ReadLine("Output file (leave blank if you want console output): "); if (outFileName != "") { try { new FileInfo(outFileName); } catch (ArgumentException) { IO.WriteLineError("Invalid file name!"); return(false); } catch (Exception e) { IO.WriteLineError(e.Message); return(false); } } return(true); }
private static void RemoveFormat() { string input = IO.ReadLine("Id of format to remove: "); int id; if (!int.TryParse(input, out id)) { IO.WriteLineError("You must enter a number!"); return; } int removed = _formats.RemoveWhere(x => x.Index == id); //[[12]] if (removed == 0) { IO.WriteLineError("No formats with the specified ID found!"); } else { IO.WriteLineSuccess("Format removed successfully."); } }
private static void LoadFormatsFromFile() { string fileName; while (!GetInputFile(out fileName)) { ; } if (!File.Exists(fileName)) { IO.WriteLineError("The specified file could not be found."); return; } string[] lines = File.ReadAllLines(fileName).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray(); foreach (string line in lines) { if (!AddNewFormat(line)) { return; } } IO.WriteLineSuccess("All formats added successfully."); }