Example #1
0
        static void Main(string[] args)
        {
            var mikeCalc = new OneCalculatorToRuleThemAll <MikesNumbers>();

            mikeCalc.Add(new MikesNumbers("Blah"), new MikesNumbers("Foo"));

            Console.ReadLine();

            IntCalculator c = new IntCalculator();

            c.Add(5, 8);
            c.Multiply(5, 8);
            c.Subtract(5, 8);
            c.Mod(5, 8);

            FloatCalculator c2     = new FloatCalculator();
            float           result = c2.Add(5.6f, 4.3f);

            c2.Subtract(5.6f, 4.3f);

            CustomNumberCalculator c3 = new CustomNumberCalculator();
            var          num1         = new CustomNumber("Daniel");
            var          num2         = new CustomNumber("Mike");
            CustomNumber num3         = c3.Add(num1, num2);

            c3.Multiply(num1, num2);
            c3.Subtract(num1, num2);
            c3.Mod(num1, num2);



            Console.ReadLine();
        }
Example #2
0
        public static void TestIntCalculator()
        {
            Console.Clear();
            ICalculator <int>      calc = new IntCalculator();
            INumberValidator <int> validator = new IntValidator();
            int value1, value2, result;

            while (true)
            {
                try
                {
                    Console.Write("Введите операцию (+ - * /): ");
                    ConsoleKeyInfo key = Console.ReadKey();
                    if (key.Key == ConsoleKey.Escape)
                    {
                        return;
                    }
                    char operation = key.KeyChar;
                    if (operations.IndexOf(operation) == -1)
                    {
                        Console.WriteLine("\r\nА вот и не правильно");
                        continue;
                    }
                    Console.Write("\r\nВведите 2 целых числа разделенных пробелом: ");
                    if (!validator.ValidateTwoNumbers(Console.ReadLine(), out value1, out value2))
                    {
                        Console.WriteLine("Неверный ввод. Придется повторить");
                        continue;
                    }
                    switch (operation)
                    {
                    case '+':
                        result = calc.Add(value1, value2);
                        break;

                    case '-':
                        result = calc.Add(value1, value2);
                        break;

                    case '*':
                        result = calc.Multiply(value1, value2);
                        break;

                    case '/':
                        result = calc.Divide(value1, value2);
                        break;

                    default:
                        continue;
                    }
                    Console.WriteLine($"{ value1}{ operation}{ value2}={ result}");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Exception {e.GetType().Name}: {e.Message}");
                }
            }
        }
        public void Add(int a, int b)
        {
            ICalculator <int> sut = new IntCalculator();

            sut.Add(a);
            sut.Add(b);

            sut.Value.Should().Be(a + b);
        }
Example #4
0
        public static IntCalculator Instance()
        {
            if (_instance == null)
            {
                _instance = new IntCalculator();
            }

            return(_instance);
        }
Example #5
0
        public void Ints()
        {
            var sut     = new IntCalculator();
            var fixture = new Fixture();

            sut.Subtract(fixture.Create <int>());

            Assert.True(sut.Value < 0);
        }
Example #6
0
        static void Main()
        {
            IntCalculator intCalculator = new IntCalculator(new MultiplyOperation());

            Console.WriteLine(intCalculator.Operate(5, 2));
            intCalculator.Operation = new PlusOperation();
            Console.WriteLine(intCalculator.Operate(5, 2));

            Console.ReadKey();
        }
Example #7
0
        public void Ints()
        {
            //arrange
            var sut     = new IntCalculator();
            var fixture = new Fixture();

            //act
            sut.Subtract(fixture.Create <int>());

            Assert.That(sut.Value, Is.LessThan(0));
        }
        public void Ints()
        {
            // arrange
            var sut = new IntCalculator();

            // act
            sut.Subtract(1);

            // assert
            Assert.True(sut.Value < 0);
        }
Example #9
0
        public void Ints()
        {
            var fixture = new Fixture();
            var sut     = new IntCalculator();

            int num = fixture.Create <int>();

            sut.Add(num);

            Assert.Equal(num, sut.Value);
        }
        public void Ints()
        {
            // arrange
            var     sut     = new IntCalculator();
            Fixture fixture = new Fixture();

            // act
            sut.Subtract(fixture.Create <int>());

            // assert
            Assert.True(sut.Value < 0);
        }
Example #11
0
        private void demoButton_Click(object sender, EventArgs e)
        {
            int           result;
            IntCalculator Sum      = (a, b) => a + b;
            IntCalculator Multiply = (a, b) => a * b;

            result = Sum(1, 2);
            MessageBox.Show("1 + 2 =  " + result.ToString());

            result = Multiply(2, 4);
            MessageBox.Show("2 * 4 =  " + result.ToString());
        }
        public void Ints()
        {
            // arrange
            var sut             = new IntCalculator();
            var anonymousNumber = 1;

            // act
            sut.Subtract(anonymousNumber);

            // assert
            Assert.True(sut.Value < 0);
        }
Example #13
0
        public void Manual_Anonymous_Data()
        {
            //arrange
            var sut             = new IntCalculator();
            var anonymousNumber = 394;

            //act
            sut.Subtract(anonymousNumber);

            //assert
            Assert.True(sut.Value < 0);
        }
        public void Ints()
        {
            //arrange
            IntCalculator sut     = new IntCalculator();
            Fixture       fixture = new Fixture();
            int           num     = fixture.Create <int>();

            //act
            sut.Add(num);

            //assert
            sut.Value.Should().Be(num);
        }
Example #15
0
        public void AutoFixture_Anonymous_Data()
        {
            //arrange
            var sut     = new IntCalculator();
            var fixture = new Fixture();

            //act
            sut.Subtract(fixture.Create <int>());    // creating an anonymous int
                                                     // int is passed as value to subtract

            //assert
            Assert.True(sut.Value < 0);
        }
        public void Integer()
        {
            //arrange
            var fixture = new Fixture();
            var sut     = new IntCalculator();
            int num     = fixture.Create <int>();

            //act

            sut.Add(num);
            //assert

            Assert.Equal(num, sut.value);
        }
Example #17
0
        private void demoButton_Click(object sender, EventArgs e)
        {
            int result;

            IntCalculator Sum = delegate(int a, int b)
            {
                return(a + b);
            };

            IntCalculator Multiply = delegate(int a, int b)
            {
                return(a * b);
            };

            result = Sum(1, 2);
            MessageBox.Show("1 + 2 = " + result.ToString());

            result = Multiply(2, 4);
            MessageBox.Show("2 * 4 = " + result.ToString());
        }
Example #18
0
 public int Multiply(int number1, int number2)
 {
     return(IntCalculator.Instance().Multiply(number1, number2));
 }
Example #19
0
 public int Divide(int number1, int number2)
 {
     return(IntCalculator.Instance().Divide(number1, number2));
 }
Example #20
0
 public int Substract(int number1, int number2)
 {
     return(IntCalculator.Instance().Subtract(number1, number2));
 }
Example #21
0
 public int Add(int number1, int number2)
 {
     return(IntCalculator.Instance().Add(number1, number2));
 }
Example #22
0
        /// <summary>
        /// Parse string expression to an arithmetic expression object
        /// </summary>
        /// <param name="s"></param>
        /// <param name="endIndex"></param>
        /// <param name="errMsg"></param>
        /// <param name="startIndex"></param>
        /// <param name="recurLevel"></param>
        /// <returns></returns>
        private static ArithmeticExpression <ICalculator> ParseExpression(string s, out int endIndex, out string errMsg, int startIndex = 0, int recurLevel = 0)
        {
            endIndex = startIndex;
            errMsg   = string.Empty;
            ArithmeticExpression <ICalculator> root = null;

            if (!string.IsNullOrWhiteSpace(s) && s.Length > startIndex)
            {
                int index = startIndex;
                ArithmeticExpression <ICalculator> prevExpression = null;
                ArithmeticExpression <ICalculator> curExpression  = null;
                ArithmeticOperand <ICalculator>    newOperand     = null;

                bool prevOperand  = false;
                bool prevOperator = false;

                while (index < s.Length)
                {
                    char c = s[index];
                    // Check operator
                    if (c == '+' || c == '-' || c == '*' || c == '/')
                    {
                        if (curExpression != null)
                        {
                            prevExpression = curExpression;
                        }
                        curExpression = ArithmeticExpressionFactory.CreateArithmeticExpression(c, index, ref prevOperator, ref prevOperand, ref errMsg);
                        if (curExpression == null)
                        {
                            break;
                        }
                        else if (prevExpression != null)
                        {
                            if (!ArithmeticExpressionFactory.MergeExpressions(prevExpression, ref curExpression, ref newOperand, ref errMsg))
                            {
                                break;
                            }
                        }
                        else
                        {
                            prevExpression = curExpression;
                            if (newOperand != null)
                            {
                                curExpression.AddOperand(newOperand);
                                newOperand = null;
                            }
                        }
                    }
                    // Check number
                    else if (c == '(')
                    {
                        int    childEndIndex;
                        string childErrMsg;
                        ArithmeticExpression <ICalculator> childExpression = ParseExpression(s, out childEndIndex, out childErrMsg, index + 1, recurLevel + 1);
                        if (!string.IsNullOrWhiteSpace(childErrMsg))
                        {
                            errMsg += childErrMsg;
                            break;
                        }
                        else if (childExpression == null)
                        {
                            errMsg += $"Expression missing in parentheses: [index: {index}].";
                            break;
                        }
                        else if (prevOperand)
                        {
                            errMsg += $"Operator is missing before character: ( [index: {index}].";
                            break;
                        }
                        else
                        {
                            index      = childEndIndex; // ')'
                            newOperand = new ArithmeticOperand <ICalculator>
                            {
                                Expression = childExpression
                            };
                            prevOperand  = true;
                            prevOperator = false;
                        }
                    }
                    else if (c == ')')
                    {
                        recurLevel--;
                        if (recurLevel < 0)
                        {
                            errMsg += $"Redundant right parenthesis: [index: {index}].";
                        }
                        break;
                    }
                    else if (c >= '0' && c <= '9')
                    {
                        IntCalculator intCalculator = ArithmeticExpressionFactory.CreateCalculator(s, ref index, ref prevOperator, ref prevOperand, ref errMsg) as IntCalculator;
                        if (intCalculator == null)
                        {
                            break;
                        }
                        else
                        {
                            newOperand = new ArithmeticOperand <ICalculator>
                            {
                                SimpleValue = intCalculator
                            };
                        }
                    }
                    // Support spaces
                    else if (c == ' ')
                    {
                    }
                    // Not support characters
                    else
                    {
                        errMsg += $"Illegal characters in the expression: {c} [index: {index}].";
                        break;
                    }

                    if (root == null && curExpression != null)
                    {
                        root = curExpression;
                    }

                    index++;
                }

                if (prevOperator)
                {
                    errMsg += $"Operand is missing at the end of the expressioin.";
                }

                if (index == s.Length && recurLevel > 0)
                {
                    errMsg += $"Missing {recurLevel} right parenthesis.";
                }
                else if (newOperand != null && curExpression != null)
                {
                    curExpression.AddOperand(newOperand);
                    newOperand = null;
                }

                endIndex = index;
            }

            if (!string.IsNullOrEmpty(errMsg))
            {
                root = null;
            }

            if (root != null)
            {
                while (root.ParentExpression != null)
                {
                    root = root.ParentExpression;
                }
            }
            return(root);
        }