示例#1
0
        private static void EvaluateInput(string input,out string output)
        {
            input = input.Trim();

            //
            // Use Regex to identify the input type and split into tokens.
            //

            var intOpRegex = @"^\d+\s*(\+|\-|\*|\/)\s*\d+$";
            var binOpRegex = @"^b[01]+\s*(\+|\-|\*|\/)\s*b[01]+$";
            var strOpRegex = @"^[A-Za-z0-9]+\s*(\+|\-|\*|\/)\s*[A-Za-z0-9]+$";
            string[] tokens = Regex.Split(input, @"(\+|\-|\*|\/)");

            if (Regex.IsMatch(input, intOpRegex))
            {
                ICalculator<int> intCalc = new IntegerCalculator();
                output = intCalc.eval(tokens[0], tokens[1], tokens[2].TrimStart()).ToString();

            }
            else if (Regex.IsMatch(input, binOpRegex))
            {
                ICalculator<Binary> binCalc = new BinaryCalculator();
                output = binCalc.eval(tokens[0].Substring(1, tokens[0].Length - 1).TrimEnd(), tokens[1], tokens[2].TrimStart().Substring(1)).ToString();
            }
            else if(Regex.IsMatch(input,strOpRegex))
            {
                ICalculator<string> strCalc = new StringCalculator();
                output = strCalc.eval(tokens[0].TrimEnd(), tokens[1], tokens[2].TrimStart());
            }
            else
            {
                output = "Operation cannot be performed.";
            }
        }
        public void TestAdd(string input, int expected)
        {
            // Arrange
            var calc = new StringCalculator();

            // Act
            var actual = calc.Add(input);

            // Assert
            Assert.AreEqual(expected, actual);
        }
示例#3
0
 public static void ShouldEqual(this string input, int expected)
 {
     var cal = new StringCalculator();
     Assert.Equal(expected, cal.Add(input));
 }
        public void NegativeNumberThrowsException(string input, string expectMessage)
        {
            // Arrange
            var calc = new StringCalculator();

            try
            {
                // Act
                calc.Add(input);
            }
            catch (Exception ex)
            {
                // Assert
                Assert.AreEqual(ex.Message, expectMessage);
                return;
            }

            Assert.Fail("Exception should be thrown");
        }
示例#5
0
 public void GivenIHaveACalculator()
 {
     calculator = new StringCalculator();
 }