public void test_numbers_parser() { var result = NumbersParser.Parse("1,5000,5"); Assert.AreEqual(result, new int[] { 1, 5 }); result = NumbersParser.Parse("4,tytyt"); Assert.AreEqual(result, new int[] { 4, 0 }); result = NumbersParser.Parse("1,2,3,4,5,6,7,8,9,10,11,12"); Assert.AreEqual(result, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }); // Test for using a `\n` delimiter. result = NumbersParser.Parse("3,4\n5,6\n9"); Assert.AreEqual(result, new int[] { 3, 4, 5, 6, 9 }); // Test for using a custom delimiters. result = NumbersParser.Parse("//*\n2*ff*100"); Assert.AreEqual(result, new int[] { 2, 0, 100 }); result = NumbersParser.Parse("//[***]\n2***5***13"); Assert.AreEqual(result, new int[] { 2, 5, 13 }); // Should throw exception on a negative number. Assert.Throws(Is.TypeOf <Exception>() .And.Message.EqualTo("Numbers can not be less than zero.\n[-4,-9]"), delegate { NumbersParser.Parse("3,-4,-9"); }); }
static void Main(string[] args) { // Usage/Example help blurb. Console.WriteLine("Step 09 (Stretch 1)\n"); Console.WriteLine("============================================="); Console.WriteLine("Usage: {operation} {n,n}"); Console.WriteLine("{operation:string = [add]} {n:integer(n > 0)}"); Console.WriteLine("Delimiters allowed: [',', '\\n']"); Console.WriteLine("Example: add 2,6\\n3\nResult: 11"); Console.WriteLine("=============================================\n"); try { // Create array of given arguments. var arguments = Console.ReadLine().Split(' '); // First position should be the `Operation` command. var operationCmd = OperationParser.Parse(arguments[0]); // Transform the `String` representation of all // integers in the list to `Int` and also apply // various cli rules during transformation. var numbers = NumbersParser.Parse(arguments[1]); Printer.PrintEquation(operationCmd, numbers); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("Input file required!"); return; } string filePath = args[0]; if (!File.Exists(filePath)) { Console.WriteLine("File does not exist!"); return; } Console.WriteLine("Processing file {0}.", args[0]); var _parser = new NumbersParser(); foreach (var line in File.ReadLines(filePath)) { var numbers = _parser.Parse(line); Console.WriteLine("A:{0:n0} B:{1:n0}", numbers[0], numbers[1]); foreach (var calculator in _calculators) { var result = calculator.Calculate(numbers[0], numbers[1]); Console.WriteLine("{0} - {1} numbers", calculator, result.Count); } } Console.WriteLine("Finished"); }
public void test_calculator_print_equation() { var result = _calculator.CalcAndPrintEquation(OperationEnum.Add, NumbersParser.Parse("//[@@][**]\n17@@8**3")); Assert.AreEqual(result, "17+8+3 = 28"); result = _calculator.CalcAndPrintEquation(OperationEnum.Add, NumbersParser.Parse("1,2,3,4,5,6,7,8,9,10,11,12")); Assert.AreEqual(result, "1+2+3+4+5+6+7+8+9+10+11+12 = 78"); }
public void test_numbers_parser() { var result = NumbersParser.Parse("1,5000"); Assert.AreEqual(result, new int[] { 1, 5000 }); result = NumbersParser.Parse("4,tytyt"); Assert.AreEqual(result, new int[] { 4, 0 }); result = NumbersParser.Parse("1,2,3,4,5,6,7,8,9,10,11,12"); Assert.AreEqual(result, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }); }
public void test_numbers_parser() { var result = NumbersParser.Parse("1,5000"); Assert.AreEqual(result, new int[] { 1, 5000 }); result = NumbersParser.Parse("4,tytyt"); Assert.AreEqual(result, new int[] { 4, 0 }); // Should throw exception b/c of max 2 number rule. Assert.Throws(Is.TypeOf <Exception>() .And.Message.EqualTo("More than two numbers were provided."), delegate { NumbersParser.Parse("6,3,7"); }); }
static void Main(string[] args) { // Usage/Example help blurb. Console.WriteLine("Step 03\n"); Console.WriteLine("============================================="); Console.WriteLine("Usage: {operation} {n,n}"); Console.WriteLine("{operation:string = [add]} {n:integer}"); Console.WriteLine("Delimiters allowed: [',', '\\n']"); Console.WriteLine("Example: add 2,6\\n3\nResult: 11"); Console.WriteLine("=============================================\n"); try { // Create array of given arguments. var arguments = Console.ReadLine().Split(' '); // First position should be the `Operation` command. var operationCmd = OperationParser.Parse(arguments[0]); // Transform the `String` representation of all // integers in the list to `Int` and also apply // various cli rules during transformation. var numbers = NumbersParser.Parse(arguments[1]); // Find which `Operation` to run and get result. var result = 0; switch (operationCmd) { case OperationEnum.Add: { Console.WriteLine("Adding . . ."); result = Calculator.Add(numbers); break; } default: break; } Console.WriteLine("{0}", result); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void test_numbers_parser() { var result = NumbersParser.Parse("1,5000"); Assert.AreEqual(result, new int[] { 1, 5000 }); result = NumbersParser.Parse("4,tytyt"); Assert.AreEqual(result, new int[] { 4, 0 }); result = NumbersParser.Parse("1,2,3,4,5,6,7,8,9,10,11,12"); Assert.AreEqual(result, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }); // Test for using a `\n` delimiter. result = NumbersParser.Parse("3,4\n5,6\n9"); Assert.AreEqual(result, new int[] { 3, 4, 5, 6, 9 }); }
public void Does_Parse_Numbers() { //Arrange var parser = new NumbersParser(); var delimiters = new List <string>() { ";", "$", "#" }; const string testString = "1;2$5#6"; //Act var numbers = parser.Parse(testString, delimiters); //Assert Assert.AreEqual(4, numbers.Count()); Assert.AreEqual(1, numbers.First()); Assert.AreEqual(6, numbers.Last()); }
static void Main(string[] args) { #region - Usage/Example help blurb. Console.WriteLine("Step 12 (Stretch 4)\n"); Console.WriteLine("========================================================"); Console.WriteLine("USAGE: {add} [//[DELIMITER][...]\\n]{n1[,...]} [OPTIONS]"); Console.WriteLine("OPTIONS:"); Console.WriteLine(" -delim:char – single char to override delimiter (defaults are `,`, `\\n`)"); Console.WriteLine(" -noneg – default is to allow negative integers"); Console.WriteLine(" -maxint:n – default is no upper bound"); Console.WriteLine("\nEXAMPLES:"); Console.WriteLine(" % add 2,6\\n3 –> 11"); Console.WriteLine(" % add 2^6^3 -delim:^ –> 11"); Console.WriteLine(" % add 2,-6,3 -noneg –> 'exception thrown'"); Console.WriteLine(" % add 2,11,3 -maxint:10 –> 5"); Console.WriteLine(" % add //[**]\\n2**6**3 –> 11"); Console.WriteLine(" % add //[**][!!]\\n2**6!!3 –> 11"); Console.WriteLine("========================================================\n"); //add //[@][!]\n55@33!22 -delim:% -maxint:50 -noneg #endregion var serviceProvider = new ServiceCollection() .AddSingleton <ICalculator, Calculator>() .BuildServiceProvider(); Console.CancelKeyPress += delegate { Console.WriteLine("Good bye!"); }; try { while (true) { var givenArgs = new Arguments(Console.ReadLine()); var operationCmd = OperationParser.Parse(givenArgs.GetOperationString()); var numbers = NumbersParser.Parse(givenArgs.GetNumbersString(), givenArgs.GetOptions()); serviceProvider.GetService <ICalculator>().CalcAndPrintEquation(operationCmd, numbers); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }