Beispiel #1
0
        private void divButton_Click(object sender, EventArgs e)
        {
            bool flag1 = Regex.IsMatch(number1TextBox.Text, "[0-9.]{1,}$");
            bool flag2 = Regex.IsMatch(number2TextBox.Text, "[0-9.]{1,}$");

            if (flag1 == true && flag2 == true)
            {
                double num1 = Convert.ToDouble(number1TextBox.Text);
                double num2 = Convert.ToDouble(number2TextBox.Text);
                resultTextBox.Text = Convert.ToString(calculator.Div(num1, num2));
            }
            else
            {
                MessageBox.Show("Enter all TextField properly.");
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Calculator calc = new Calculator();
            double     a = 4.5, b = 2.5;

            double result = calc.Add(a, b);

            Console.WriteLine($"result = {result}");
            double result2 = calc.Subtract(a, b);

            Console.WriteLine($"result2 = {result2}");
            double result3 = calc.Mult(a, b);

            Console.WriteLine($"result3 = {result3}");
            double result4 = calc.Div(a, b);

            Console.WriteLine($"result4 = {result4}");
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            bool operation = true;

            while (operation)
            {
                Console.Clear();
                Console.WriteLine("Calculator App By: 3 mands gruppen.");

                Console.WriteLine("Choose an action. Follwed by Enter.\n");
                Console.WriteLine("Add          : 1 \n"
                                  + "Subtract     : 2\n"
                                  + "Multiply     : 3\n"
                                  + "Divide       : 4\n"
                                  + "Power        : 5\n"
                                  + "Factorial    : 6 \n"
                                  + "Exit program : 7");
                string act = Console.ReadLine();
                Console.Clear();


                int value1 = 0;
                int value2 = 0;


                switch (act)
                {
                case "1":

                    try
                    {
                        Console.WriteLine("Enter your first value: followed by Enter.");
                        value1 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Enter your second value: followed by Enter.");
                        value2 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("The solution is :" + Calculator.Add(value1, value2));
                    }
                    catch
                    {
                        Console.WriteLine("Not a valid value!");
                        Console.ReadKey();
                    }
                    break;

                case "2":
                    try
                    {
                        Console.WriteLine("Enter your first value: followed by Enter.");
                        value1 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Enter your second value: followed by Enter.");
                        value2 = Convert.ToInt32(Console.ReadLine());

                        Console.WriteLine("The solution is :" + Calculator.Subtract(value1, value2));
                        Console.ReadKey();
                    }
                    catch
                    {
                        Console.WriteLine("Not a valid value!");
                        Console.ReadKey();
                    }

                    break;

                case "3":
                    try
                    {
                        Console.WriteLine("Enter your first value: followed by Enter.");
                        value1 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Enter your second value: followed by Enter.");
                        value2 = Convert.ToInt32(Console.ReadLine());

                        Console.WriteLine("The solution is :" + Calculator.Multy(value1, value2));
                        Console.ReadKey();
                    }
                    catch
                    {
                        Console.WriteLine("Not a valid value!");
                        Console.ReadKey();
                    }

                    break;

                case "4":
                    try
                    {
                        Console.WriteLine("Enter your first value: followed by Enter.");
                        value1 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Enter your second value: followed by Enter.");
                        value2 = Convert.ToInt32(Console.ReadLine());

                        Console.WriteLine("The solution is :" + Calculator.Div(value1, value2));
                        Console.ReadKey();
                    }
                    catch
                    {
                        Console.WriteLine("Not a valid value!");
                        Console.ReadKey();
                    }

                    break;


                case "5":
                    try
                    {
                        Console.WriteLine("Enter your first value: followed by Enter.");
                        value1 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Enter your second value: followed by Enter.");
                        value2 = Convert.ToInt32(Console.ReadLine());


                        Console.WriteLine("The solution is :" + Calculator.Power(value1, value2));
                        Console.ReadKey();
                    }
                    catch
                    {
                        Console.WriteLine("Not a valid value!");
                        Console.ReadKey();
                    }

                    break;

                case "6":
                    try
                    {
                        Console.WriteLine("Enter your value: followed by Enter.");
                        value1 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("The solution is :" + Calculator.Fac(value1));
                        Console.ReadKey();
                    }
                    catch
                    {
                        Console.WriteLine("Not a valid value!");
                        Console.ReadKey();
                    }

                    break;

                case "7":
                    try
                    {
                        Console.WriteLine("End of operation");
                        operation = false;
                        Console.ReadKey();
                    }
                    catch
                    {
                    }
                    break;


                default:
                    Console.WriteLine("You have to enter an invalid option!");
                    Console.ReadKey();
                    break;
                }
            }
        }
Beispiel #4
0
 private void divisionButton_Click(object sender, EventArgs e)
 {
     resultTextBox.Text = aCalculator.Div(Convert.ToDouble(firstNumberTextBox.Text), Convert.ToDouble(secondNumberTextBox.Text)).ToString();
 }