public int PerformCalculation(int firstNumber, int secondNumber, string operatorSymbol)
        {
            BinaryOperations execute = new BinaryOperations();

            if (operatorSymbol == OperatorConstants.AddSymbol.ToString())
            {
                secondNumber = execute.Addition(firstNumber, secondNumber);

                return(secondNumber);
            }
            else if (operatorSymbol == OperatorConstants.SubtractSymbol.ToString())
            {
                secondNumber = execute.Subtraction(firstNumber, secondNumber);

                return(secondNumber);
            }
            else if (operatorSymbol == OperatorConstants.DivideSymbol.ToString())
            {
                secondNumber = execute.Division(firstNumber, secondNumber);

                return(secondNumber);
            }
            else if (operatorSymbol == OperatorConstants.MultiplySymbol.ToString())
            {
                secondNumber = execute.Multiplication(firstNumber, secondNumber);

                return(secondNumber);
            }
            else
            {
                return(0);
            }
        }
Esempio n. 2
0
        public void AdditionInputArgumentReturnedValue(int leftValue, int rightValue, int expectedValue)
        {
            var leftNumber = new Number()
            {
                Value = leftValue
            };
            var rightNumber = new Number()
            {
                Value = rightValue
            };

            var result = BinaryOperations.Addition(leftNumber, rightNumber);

            Assert.AreEqual(expectedValue, result.Value);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            //keyBinary k1 = new keyBinary('1', KeyType.Number);
            //keyBinary k2 = new keyBinary('0', KeyType.Number);
            //keyBinary k3 = new keyBinary('1', KeyType.Number);
            //keyBinary k4 = new keyBinary('0', KeyType.Number);

            //BinaryNumber b1 = new BinaryNumber();
            //b1.Add(k1);
            //b1.Add(k2);
            //b1.Add(k3);
            //b1.Add(k4);

            //b1.convertToString();

            //BinaryConversion BC1 = new BinaryConversion(b1);
            //BC1.ToOctal();


            //keyOctal kO1 = new keyOctal('5', KeyType.Number);
            //keyOctal kO2 = new keyOctal('6', KeyType.Number);

            //OctalNumber O1 = new OctalNumber();
            //O1.Add(kO1);
            //O1.Add(kO2);
            //O1.convertToString();

            //OctalConversion OC1 = new OctalConversion(O1);
            //OC1.ToBinary();

            //keyBinary k5 = new keyBinary('1', KeyType.Number);
            //keyBinary k6 = new keyBinary('0', KeyType.Number);
            //keyBinary k7 = new keyBinary('0', KeyType.Number);
            //keyBinary k8 = new keyBinary('0', KeyType.Number);

            //BinaryNumber b2 = new BinaryNumber();
            //b2.Add(k5);
            //b2.Add(k6);
            //b2.Add(k7);
            //b2.Add(k8);

            //b2.convertToString();

            //BinaryOperations BO1 = new BinaryOperations(b1, b2);
            //BO1.Subtraction();

            Screen      Screen1 = new Screen();
            keyOperator KeyOp   = Screen1.PrintOperators();
            NumberType  NumType = Screen1.PrintNumberType();

            Console.WriteLine("{0}", KeyOp.Key);


            switch (KeyOp.keyType)
            {
            case KeyType.Conversion:
            {
                switch (NumType)
                {
                case NumberType.Binary:

                    Console.WriteLine("Enter Binary Number. (press Enter after each digit. Enter 'X' to stop)");

                    BinaryNumber BN = new BinaryNumber();
                    BN.GetNumber();
                    BinaryConversion BC = new BinaryConversion(BN);
                    if (KeyOp.Key == 'M')
                    {
                        BC.ToDecimal();
                    }
                    else if (KeyOp.Key == 'I')
                    {
                        Console.WriteLine("cannot convert binary to binary");
                    }
                    else if (KeyOp.Key == 'O')
                    {
                        BC.ToOctal();
                    }
                    else if (KeyOp.Key == 'H')
                    {
                        BC.ToHexaDecimal();
                    }
                    break;

                case NumberType.Octal:

                    Console.WriteLine("Enter Octal Number. (press Enter after each digit. Enter 'X' to stop)");

                    OctalNumber ON = new OctalNumber();
                    ON.GetNumber();
                    OctalConversion OC = new OctalConversion(ON);
                    if (KeyOp.Key == 'M')
                    {
                        OC.ToDecimal();
                    }
                    else if (KeyOp.Key == 'I')
                    {
                        OC.ToBinary();
                    }
                    else if (KeyOp.Key == 'O')
                    {
                        OC.ToOctal();
                    }
                    else if (KeyOp.Key == 'H')
                    {
                        OC.ToHexaDecimal();
                    }
                    break;
                }
                break;
            }

            case KeyType.Operator:
            {
                switch (NumType)
                {
                case NumberType.Binary:

                    Console.WriteLine("Enter 1st Binary Number. (press Enter after each digit. Enter 'X' to stop");

                    BinaryNumber BN1 = new BinaryNumber();
                    BN1.GetNumber();


                    Console.WriteLine("Enter 2nd Binary Number (press Enter after each digit. Enter 'X' to stop");
                    BinaryNumber BN2 = new BinaryNumber();
                    BN2.GetNumber();
                    BinaryOperations BO = new BinaryOperations(BN1, BN2);
                    //Console.WriteLine("{0}", BN1.number);
                    //Console.WriteLine("{0}", BN2.number);
                    if (KeyOp.Key == '+')
                    {
                        BO.Addition();
                    }
                    else if (KeyOp.Key == '-')
                    {
                        BO.Subtraction();
                    }
                    else if (KeyOp.Key == '*')
                    {
                        Console.WriteLine("Not implemented");
                    }
                    else if (KeyOp.Key == '/')
                    {
                        Console.WriteLine("Not implemented");
                    }


                    break;

                default:
                    break;
                }
                break;
            }
            }
        }
        /// <summary>
        /// The calculation of the converted expression.
        /// </summary>
        /// <param name="inputExpression"> Input the converted expression. </param>
        /// <param name="firstNumber"> The first number. </param>
        /// <param name="secondNumber"> The second number. </param>
        /// <returns> Result of calculating the expression. </returns>
        public static Number Counting(string inputExpression, Number firstNumber, Number secondNumber)
        {
            if (string.IsNullOrWhiteSpace(inputExpression))
            {
                throw new ArgumentNullException("The input expression cannot be empty.", nameof(inputExpression));
            }

            double result = 0;
            var    temp   = new Stack <double>();

            for (int i = 0; i < inputExpression.Length; i++)
            {
                if (char.IsDigit(inputExpression[i]))
                {
                    string a = string.Empty;

                    while (!IsDelimeter(inputExpression[i]) && !IsOperator(inputExpression[i]))
                    {
                        a += inputExpression[i];
                        i++;
                        if (i == inputExpression.Length)
                        {
                            break;
                        }
                    }

                    temp.Push(double.Parse(a, CultureInfo.InvariantCulture));
                    i--;
                }
                else if (IsOperator(inputExpression[i]))
                {
                    if (inputExpression[i] == '!')
                    {
                        firstNumber = new Number {
                            Value = temp.Pop()
                        }
                    }
                    ;
                    else
                    {
                        firstNumber = new Number {
                            Value = temp.Pop()
                        };
                        secondNumber = new Number {
                            Value = temp.Pop()
                        };
                    }

                    switch (inputExpression[i])
                    {
                    case '+': result = BinaryOperations.Addition(secondNumber, firstNumber).Value; break;

                    case '-': result = BinaryOperations.Subsctraction(secondNumber, firstNumber).Value; break;

                    case '*': result = BinaryOperations.Multiplication(secondNumber, firstNumber).Value; break;

                    case '/': result = BinaryOperations.Division(secondNumber, firstNumber).Value; break;

                    case '%': result = BinaryOperations.DivisionReaminder(secondNumber, firstNumber).Value; break;

                    case '^': result = UnaryOperations.Exponentiation(secondNumber, firstNumber).Value; break;

                    case '!': result = UnaryOperations.Factorial(firstNumber).Value; break;
                    }

                    temp.Push(result);
                }
            }

            return(new Number {
                Value = temp.Peek()
            });
        }