Example #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.";
            }
        }
Example #2
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 CalculateTest(double firstAgrument, double secondArgument, double expectedResult)
        {
            var calculator   = new IntegerCalculator();
            var actualResult = calculator.Calculate(firstAgrument, secondArgument);

            Assert.AreEqual(expectedResult, actualResult, 0.001);
        }
        public void CalculateTest()

        {
            ITwoArgumentsCalculator calculator = new IntegerCalculator();
            double result = calculator.Calculate(1, 2);

            Assert.AreEqual(0, result);
        }
Example #5
0
        static void Main(string[] args)
        {
            IntegerCalculator intCalc = new IntegerCalculator();

            Console.WriteLine(intCalc.Type);


            string a = "2.5d";

            string b = "2.5";

            double c = 0;

            try
            {
                intCalc.Addition(Int32.Parse(a), Int32.Parse(b));

                if (!(b is double))
                {
                    throw new InvalidArgumentException();
                }
            }
            catch (InvalidArgumentException e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Message);
            }
            catch (GenericException e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Message);
            }
        }
        public void ExceptionTest()
        {
            var calculator = new IntegerCalculator();

            Assert.Throws <DivideByZeroException>(() => calculator.Calculate(1, 0));
        }