Exemplo n.º 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");
        }
Exemplo n.º 2
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);
            }
        }
        //button +
        private void BtnAddition_Click(object sender, RoutedEventArgs e)
        {
            if (TbInputNumbers.Text != "0")
            {
                //Progress of the calculation's text
                TbCalculationProgress.Text += TbInputNumbers.Text;


                //Converting the Current number which is inside the TbInputNumbers TextBlock
                CurrentNumber = Convert.ToDouble(TbInputNumbers.Text);


                //Adding the Current number to the Memory array, incrementing the Memory array's index counter by one
                Calculations.AddToMemory(CurrentNumber);


                //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();

                //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
                //Setting the UserCanDelete variable to 0

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

                TbResultMemory.Text = Result.ToString();

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

                //Setting this variable to false as the user can use division or multiplication, because the Memory array is not empty
                NewCalculation = false;

                NoCalculationsYet = false;
            }
            if (TbInputNumbers.Text == "0")
            {
                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";
        }