Example #1
0
        public static void Main(string[] args)
        {
            Addition     add = new Addition();
            substraction sub = new substraction();

            Console.WriteLine("Enter Your Choice");
            Console.WriteLine("\n 1 - For Addition \n 2 - For Substraction \n 3 - For Multiplication \n 4 - For Division");
            int a = Convert.ToInt32(Console.ReadLine());

            if (a == 1)
            {
                int c = add.Add();
                Console.WriteLine("The Sum Value is {0}", c);
            }
            if (a == 2)
            {
                int c = sub.Sub();
                Console.WriteLine("The Difference is {0}", c);
            }

            Console.ReadKey();
        }
Example #2
0
        //private int result = 0;

        public int Add(int a, int b)
        {
            return(Addition.Add(a, b));

            //return result;
        }
        /// <summary>
        /// CalculateResult is the bread and butter of this calculator. It takes all the parts of the input field and evaluates it.
        /// If a result is undefined or imaginary an error is "thrown" and also tells the user what kind of illegal action it took.
        /// Aside from this the method calculates the corresponding operations based on the input provided from the user.
        /// While the calculator cannot handle chain additon or chain multiplication (as that requires the shunting yard
        /// algorithm for more complex operations, eg. 3+2*6), it uses ANS as the next value if the user needs to use the result for something else.
        /// In that sense you CAN do chain addition or chain multiplication, but you cannot execute several different operations at the same time.
        /// </summary>
        private void CalculateResult()
        {
            double result  = 0;
            var    number1 = model.CalcNumbers[0];

            if (model.SpecialOperatorPicked == true)
            {
                switch (model.Operator)
                {
                case '√':
                    Boolean isNumberNegative = number1 < 0;
                    if (isNumberNegative)
                    {
                        result = 0;     //As result becomes an imaginary double
                        MessageBox.Show("NaN", "Error");
                    }
                    else
                    {
                        result = _sqrt.PowerOf(number1, 0.5);
                    }
                    break;

                default:
                    break;
                }
            }
            else if (model.OperatorPicked)
            {
                var number2 = model.CalcNumbers[1];

                switch (model.Operator)
                {
                case '+':
                    result = _add.Add(number1, number2);
                    break;

                case '−':
                    result = _remove.Add(number1, number2);
                    break;

                case '×':
                    result = _multiplicate.Multiplicate(number1, number2);
                    break;

                case '÷':
                    if (number2 == 0)
                    {
                        result = 0;     //Result becomes an undefined answer
                        MessageBox.Show("Undefined", "Error");
                    }
                    else
                    {
                        result = _divide.Multiplicate(number1, number2);
                    }
                    break;

                case '^':
                    result = _powerOf.PowerOf(number1, number2);
                    break;

                default:
                    break;
                }
            }
            DisplayResult(result);
        }