示例#1
0
        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);
        }
示例#2
0
        private static void GenerateRandomExpression()
        {
            int    resultLower, resultUpper, numberOfExpressions, numberOfOperandsLower, numberOfOperandsUpper;
            string outFileName;

            while (!GetDesiredResult(out resultLower, out resultUpper))
            {
                ;
            }
            while (!GetNumberOfOperands(out numberOfOperandsLower, out numberOfOperandsUpper))
            {
                ;
            }
            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);
                int numberOfOperands = ExpressionGenerator.Random.Next(numberOfOperandsLower, numberOfOperandsUpper + 1);
                IO.WriteLine(_generator.BuildExpression(goal, numberOfOperands));
            }

            IO.RedirectOutputToConsole();
        }
示例#3
0
        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();
        }
示例#4
0
 private static void ShowMenu()
 {
     foreach (var item in _menuItems)
     {
         IO.WriteLine("`{0}`. {1}", item.Id, item.Description);
     }
     IO.WriteLine("");
 }
示例#5
0
        private static void AddNewFormat()
        {
            string format = IO.ReadLine("Enter a format: ", removeWhiteSpace: false); // [[5]]

            if (AddNewFormat(format))
            {
                IO.WriteLineSuccess("Format added successfully.");
            }
        }
示例#6
0
        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);
        }
示例#7
0
        private static void ParseInput()
        {
            string input = IO.ReadLine("Your choice: ");
            Action action;

            if (!_actions.TryGetValue(input, out action))
            {
                IO.WriteLineError("Invalid input!");
                return;
            }
            action();
        }
示例#8
0
        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);
        }
示例#9
0
        private static void ShowExistingFormats()
        {
            if (_formats.Count == 0)
            {
                IO.WriteLineWarning("No formats found.");
                return;
            }

            foreach (var format in _formats.OrderBy(x => x.Index))
            {
                IO.WriteLine($"{format.Index}: {format.Format}");
            }
        }
示例#10
0
        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);
        }
示例#11
0
 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);
 }
示例#12
0
        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.");
            }
        }
示例#13
0
        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.");
        }