示例#1
0
        public static void Main(string[] args)
        {
            decimal       output = 0;
            DataContainer dc     = new DataContainer();

            Console.WriteLine("Write your number");
            dc.FirstInput = Input.createInput();
            Console.WriteLine("Write your number");
            dc.SecondInput = Input.createInput();

            if (InputValidation.CheckAll(dc.FirstInput) && InputValidation.CheckAll(dc.SecondInput))
            {
                dc.FirstNumber  = dc.ConvertToDec(dc.FirstInput);
                dc.SecondNumber = dc.ConvertToDec(dc.SecondInput);
                Console.WriteLine("Write your operator [+,-,*,/,%]");
                dc.Command = Input.createInput();
            }
            switch (dc.Command)
            {
            case "+":
                output = MathOperations.Add(dc.FirstNumber, dc.SecondNumber);
                break;

            case "-":
                output = MathOperations.Substract(dc.FirstNumber, dc.SecondNumber);
                break;

            case "*":
                output = MathOperations.Multiply(dc.FirstNumber, dc.SecondNumber);
                break;

            case "/":
                output = MathOperations.Divide(dc.FirstNumber, dc.SecondNumber);
                break;

            case "%":
                output = MathOperations.Modulo(dc.FirstNumber, dc.SecondNumber);
                break;
            }
            Console.WriteLine("Your result equals " + output);
            Console.ReadKey();
        }
示例#2
0
        static void Main(string[] args)
        {
            //variables declaration
            string firstNumberTest;
            string secondNumberTest;
            double firstNumber;
            double secondNumber;
            double result;
            bool   firstNumberValid;
            bool   secondNumberValid;
            bool   OperationValid = false;


            //Let user input two numbers and store them into variables.
            //For each number loop until input is correct to prevent errors
            do
            {
                Console.Write("Please enter first number: ");
                firstNumberTest  = Console.ReadLine();
                firstNumberValid = Double.TryParse(firstNumberTest, out firstNumber);
            } while (firstNumberValid == false);


            do
            {
                Console.Write("Please enter second number: ");
                secondNumberTest  = Console.ReadLine();
                secondNumberValid = Double.TryParse(secondNumberTest, out secondNumber);
            } while (secondNumberValid == false);



            //Inform user about available math operations
            Console.WriteLine("Below are available operations to select:");
            Console.WriteLine("1: Addition");
            Console.WriteLine("2: Substraction");
            Console.WriteLine("3: Multiplication");
            Console.WriteLine("4: Division");

            //Let user select one of the operations above and store it into variable
            do
            {
                Console.Write("Enter selected operation: ");
                int selectedOperation = Convert.ToInt32(Console.ReadLine());

                //Perform selected math operation
                switch (selectedOperation)
                {
                case 1:
                    result = MathOperations.Addition(firstNumber, secondNumber);
                    Console.WriteLine(firstNumber + " + " + secondNumber + " = " + result);
                    OperationValid = true;
                    break;

                case 2:
                    result = MathOperations.Substraction(firstNumber, secondNumber);
                    Console.WriteLine(firstNumber + " - " + secondNumber + " = " + result);
                    OperationValid = true;
                    break;

                case 3:
                    result = MathOperations.Multiplication(firstNumber, secondNumber);
                    Console.WriteLine(firstNumber + " * " + secondNumber + " = " + result);
                    OperationValid = true;
                    break;

                case 4:
                    result = MathOperations.Division(firstNumber, secondNumber);
                    Console.WriteLine(firstNumber + " / " + secondNumber + " = " + result);
                    OperationValid = true;
                    break;

                default:
                    Console.WriteLine("Invalid number! Please select one of the following:");
                    Console.WriteLine("1: Addition");
                    Console.WriteLine("2: Substraction");
                    Console.WriteLine("3: Multiplication");
                    Console.WriteLine("4: Division");
                    break;
                }
            } while (OperationValid == false);


            //Pause the script
            Console.ReadLine();
        }
示例#3
0
 public Form1()
 {
     InitializeComponent();
     textBox1.Text = "";
     math          = new MathOperations();
 }
示例#4
0
 public MathLibrary(MathOperations sum, MathOperations subtract, MathOperations multiply, MathOperations divide)
 {
     m_Sum           = sum;
     m_Subtract      = subtract;
     m_Multiply      = multiply;
     m_Divide        = divide;
     m_Commands['+'] = m_Sum;
     m_Commands['-'] = m_Subtract;
     m_Commands['*'] = m_Multiply;
     m_Commands['/'] = m_Divide;
 }