public static void Main(string[] args) { while (true) { try { var inputString = ConsoleInput(); Calculator calculator = new Calculator(); CalcDataTransfer calcDataTransfer = calculator.Run(inputString); if (calcDataTransfer.ErrorMessage == null) { Console.WriteLine(calcDataTransfer.NumberOutput); Console.WriteLine(calcDataTransfer.Formula); } else { Console.WriteLine(calcDataTransfer.ErrorMessage); } } catch (ArgumentOutOfRangeException ex) { Console.WriteLine(ex.Message); } catch (Exception) { Console.WriteLine("An Exception occured."); } Console.WriteLine(); } }
// Seperate the input string using delimiters and return the number values public CalcDataTransfer SeperateValues(string inputString) { CalcDataTransfer calcDataTransfer = new CalcDataTransfer(); calcDataTransfer.InputString = inputString; var customDelimiterSearchText = FindCustomDelimiterSearchText(calcDataTransfer); List <string> delimiterInitial = new List <string> { ",", "\n" }; foreach (var item in customDelimiterSearchText.Delimiters) { delimiterInitial.Add(item); } var delimiter = delimiterInitial.ToArray(); // Separate the input numbers based on a delimiter var strList = customDelimiterSearchText.SearchString.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); // Reports error if more than 2 values entered. //if (strList.Length > 2) //{ // calcDataTransfer.ErrorMessage = "Error: Only 2 values can be entered."; // return calcDataTransfer; //} IEnumerable <int> numberList = ValidateNumberList(strList); calcDataTransfer.NumbersInput = numberList; return(calcDataTransfer); }
// Finds custom delimiters and returns them and the remaining search string. public CalcDataTransfer FindCustomDelimiterSearchText(CalcDataTransfer calcDataTransfer) { // Pattern for a custom defined delimiter: //{delimiter}\n{numbers} Regex rx = new Regex(@"\/\/(.+)\n"); var text = calcDataTransfer.InputString; var searchText = calcDataTransfer.InputString; // Find matches. MatchCollection matches = rx.Matches(text); string match = ""; List <string> delimiters = new List <string>(); // A custom delimiter exists if (matches.Count > 0) { // Groups[1] contains matched text only match = matches[0].Groups[1].Value; // Single char delimiter if (match.Length == 1) { delimiters.Add(match); // get text after custom delimiter searchText = text.Substring(match.Length + 3); } else { // Determine if multiple character custom delimiters exists rx = new Regex(@"\[(.*?)\]", RegexOptions.Compiled | RegexOptions.IgnoreCase); matches = rx.Matches(match); if (matches.Count > 0) { foreach (Match item in matches) { delimiters.Add(item.Groups[1].Value); } searchText = text.Substring(match.Length + 3); } } } calcDataTransfer.Delimiters = delimiters; calcDataTransfer.SearchString = searchText; return(calcDataTransfer); }
public int MathOperation(CalcDataTransfer calcDataTransfer) { int result = 0; // loop through list and perform mathematical operation foreach (int s in calcDataTransfer.NumbersInput) { result += s; } return(result); }
public CalcDataTransfer Run(string inputString) { InputProcessor inputProcessing = new InputProcessor(); CalcDataTransfer calcDataTransfer = inputProcessing.SeperateValues(inputString); if (calcDataTransfer.ErrorMessage == null) { calcDataTransfer.NumberOutput = MathOperation(calcDataTransfer); calcDataTransfer.Formula = CreateFormula(calcDataTransfer); } return(calcDataTransfer); }
public string CreateFormula(CalcDataTransfer calcDataTransfer) { string formula = ""; var numbersList = (List <int>)(calcDataTransfer.NumbersInput); for (int i = 0; i < numbersList.Count; i++) { formula += numbersList[i]; if (i < numbersList.Count - 1) { formula += "+"; } } formula += " = " + calcDataTransfer.NumberOutput; return(formula); }