private void OperationButton_Click(object sender, RoutedEventArgs e)
        {
            // Storing the previous number to be able to start the computation now.
            if (double.TryParse(ResultLabel.Content.ToString(), out _lastNumber))
            {
                ResultLabel.Content = "0";  // Clearing it back to receive the next button.
            }

            if (sender == MultiplyButton)
            {
                _selectedOperation = CalculatorOperation.Multiplication;
            }
            if (sender == DivideButton)
            {
                _selectedOperation = CalculatorOperation.Division;
            }
            if (sender == AddButton)
            {
                _selectedOperation = CalculatorOperation.Addition;
            }
            if (sender == SubtractButton)
            {
                _selectedOperation = CalculatorOperation.Subtraction;
            }
        }
Beispiel #2
0
 public Calculator(CalculatorParser parser, CalculatorConverter converter,
                   CalculatorOperation calculatorOperation)
 {
     _parser              = parser;
     _converter           = converter;
     _calculatorOperation = calculatorOperation;
 }
Beispiel #3
0
 public void AddOperation(CalculatorOperation op)
 {
     if (this.op.HasValue)
     {
         Compute();
     }
     this.op = op;
     if (input.HasValue)
     {
         result = input;
         this.Clear();
     }
 }
Beispiel #4
0
        private static CalculatorOperation parseCalculatorOperation(string userInput)
        {
            string[] parts = userInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length == 0)
                throw new ArgumentException("Cannot parse user input - input string malformed!");

            int[] operands = new int[parts.Length - 1];
            for (int i = 1; i < parts.Length; i++)
                operands[i - 1] = Int16.Parse(parts[i]);

            var operation = new CalculatorOperation(parts[0], operands);
            return operation;
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Simple Calculator");
            Console.WriteLine("This application allows you to perform basic mathematics calculation\n 1. Addittion (+)\n 2. Subraction(-)\n 3. Multiplcation(*)\n Division()\n Exponential(**)\n 4. Modulus(%)");
            Console.Write("Please enter the first number: ");
            int num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Please select an operator: ");
            String ops = Console.ReadLine();

            Console.Write("Please enter the second number: ");
            int num2 = Convert.ToInt32(Console.ReadLine());
            int result;

            if (ops.Equals("+"))
            {
                result = CalculatorOperation.add(num1, num2);
                Console.WriteLine($"{num1} + {num2} = {result}");
            }
            else if (ops.Equals("-"))
            {
                result = CalculatorOperation.subract(num1, num2);
                Console.WriteLine($"{num1} - {num2} = {result}");
            }
            else if (ops.Equals("/"))
            {
                result = CalculatorOperation.divide(num1, num2);
                Console.WriteLine($"{num1} / {num2} = {result}");
            }
            else if (ops.Equals("*"))
            {
                result = CalculatorOperation.multiply(num1, num2);
                Console.WriteLine($"{num1} * {num2} = {result}");
            }
            else if (ops.Equals("%"))
            {
                result = CalculatorOperation.modu(num1, num2);
                Console.WriteLine($"{num1} % {num2} = {result}");
            }
            else
            {
                Console.WriteLine($"Invalid operator.");
            }
            Console.Read();
        }