コード例 #1
0
        public static string Builder(string firstNum, string secondNum, string expression)
        {
            _expression = expression;
            switch (expression)
            {
            case "addition":
                return(Calculations.Addition(num: Convert.ToDouble(firstNum), num2: Convert.ToDouble(secondNum)).ToString());

            case "subtraction":
                return(Calculations.Subtraction(num: Convert.ToDouble(firstNum), num2: Convert.ToDouble(secondNum)).ToString());

            case "multiplication":
                return(Calculations.Multiplication(num: Convert.ToDouble(firstNum), num2: Convert.ToDouble(secondNum)).ToString());

            case "division":
                return(Calculations.Division(num: Convert.ToDouble(firstNum), num2: Convert.ToDouble(secondNum)).ToString());
            }


            return("Null");
        }
コード例 #2
0
        static void Main(string[] args)
        {
            double a         = EnterNumber1();
            char   operation = EnterOperator();
            double b         = EnterNumber2();

            Calculations calc = new Calculations();

            switch (operation)
            {
            case '+':
                PrdoubleResult(calc.Sum(a, b).ToString());
                break;

            case '-':
                PrdoubleResult(calc.Diff(a, b).ToString());
                break;

            case '*':
                PrdoubleResult(calc.Multiply(a, b).ToString());
                break;

            case '/':
                try
                {
                    PrdoubleResult(calc.Division(a, b).ToString());
                }
                catch (DivideByZeroException)
                {
                    Console.WriteLine("Divide by zero");
                }
                break;

            case '%':
                PrdoubleResult(calc.Mod(a, b).ToString());
                break;
            }
            Console.ReadKey();
        }
コード例 #3
0
        private void btnResult_Click(object sender, EventArgs e)
        {
            try
            {
                switch (cc.Calculation)
                {
                case "+":
                    lblResult.Text = Calculations.Addition(cc.Value, double.Parse(lblResult.Text)).ToString();
                    break;

                case "-":
                    lblResult.Text = Calculations.Substraction(cc.Value, double.Parse(lblResult.Text)).ToString();
                    break;

                case "×":
                    lblResult.Text = Calculations.Multiplication(cc.Value, double.Parse(lblResult.Text)).ToString();
                    break;

                case "÷":
                    lblResult.Text = Calculations.Division(cc.Value, double.Parse(lblResult.Text)).ToString();
                    break;

                case "√":
                    if (cc.Value >= 0)
                    {
                        lblResult.Text = Calculations.SquareRoot(cc.Value).ToString();
                    }
                    else
                    {
                        MessageBox.Show("You cannot find the square root of a negative number!");
                    }
                    break;

                case "1/x":
                    if (cc.Value != 0)
                    {
                        lblResult.Text = Calculations.Reciprocal(cc.Value).ToString();
                    }
                    else
                    {
                        MessageBox.Show("The devisor cannot be 0!");
                    }
                    break;

                case "x²":
                    lblResult.Text = Calculations.MathPow(cc.Value, 2).ToString();
                    break;

                case "x³":
                    lblResult.Text = Calculations.MathPow(cc.Value, 3).ToString();
                    break;

                case "±":
                    if (cc.Value != 0)
                    {
                        lblResult.Text = Calculations.Opposite(cc.Value).ToString();
                    }
                    else
                    {
                        MessageBox.Show("Try again!");
                    }
                    break;

                case "%":
                    lblResult.Text = Calculations.Percent(cc.Value).ToString();
                    break;

                default:
                    break;
                }
                cc.Operation      = true;
                cc.IsDecimalPoint = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #4
0
        // Button /
        private void BtnDivision_Click(object sender, RoutedEventArgs e)
        {
            if (TbInputNumbers.Text != "0")
            {
                //Progress of the calculation's text
                TbCalculationProgress.Text += TbInputNumbers.Text;

                if (NewCalculation == true)
                {
                    //Converting the Current number which is inside the TbInputNumbers TextBlock
                    //Adding the Current number to the Memory array, incrementing the Memory array's index counter by one
                    CurrentNumber = Convert.ToDouble(TbInputNumbers.Text);
                    Calculations.AddToMemory(CurrentNumber);
                    Calculations.IncrementMemoryIndexByOne();


                    UserCanDeleteLastNumber = true;
                }


                //if this is inside the program, then the multiple calculation part does not work
                //Replacing the CurrentNumber part works more effectively and it is easire to understand
                //Calculations.IncrementMemoryIndexByOne();


                //Checking if there is a new calculation in progress or not
                //Converting the Current number which is inside the TbInputNumbers TextBlock
                //Adding the Current number to the Memory array, incrementing the Memory array's index counter by one
                //Storing the calculation in the result variable (Rounding the result)
                //Showing the user the given result in the TbResultmemory textBlock
                //Adding the result to the Memory array, incrementing the Memory array's index counter by one
                if (NewCalculation == false)
                {
                    CurrentNumber = Convert.ToDouble(TbInputNumbers.Text);
                    Calculations.AddToMemory(CurrentNumber);

                    Result = Calculations.Division();
                    Math.Round(Result, 10);

                    TbResultMemory.Text = Result.ToString();

                    Calculations.AddToMemory(Result);
                    Calculations.IncrementMemoryIndexByOne();

                    NewCalculation          = true;
                    UserCanDeleteLastNumber = false;
                }
            }
            else if (TbInputNumbers.Text == "0")
            {
                double MostRecentlyAddedResult = Calculations.GiveBackMostRecentValueOfResultMemory();


                //Checking whether the memory has been cleared or not
                //Progress of the calculation's text
                if (MemoryHasBeenCleared == true)
                {
                    TbCalculationProgress.Text += MostRecentlyAddedResult.ToString();
                    MemoryHasBeenCleared        = false;
                }


                //Increment the index by one as we are not calculating but waiting for a new input
                //(if this is not here then the result STEP of the Memory array will always change to the new input)
                Calculations.IncrementMemoryIndexByOne();
            }

            //We add the button's text to the TbCalculationProgress textblock
            TbCalculationProgress.Text += "/";

            //After the calculation this variable will always be one
            CalculationIsOnGoing = 1;

            //Setting the TbInputNumbers Textblock's text to default after every calculation
            TbInputNumbers.Text = "0";
        }