Пример #1
0
        //testovanie MathHead Odmocnina
        public void Function_Root()
        {
            MathHeadFunctiones mh = new MathHeadFunctiones();

            Assert.AreEqual(3, mh.Root(9, 2));
            Assert.AreEqual(9, mh.Root(81, 2));
            Assert.AreEqual(0, mh.Root(0, 5));
            Assert.AreEqual(60000000, mh.Root(60000000, 1));
            Assert.AreEqual(3.9, mh.Root(59.319, 3), esp);
            Assert.AreEqual(12, mh.Root(144, 2));
            Assert.AreEqual(1000, mh.Root(1000000, 2));

            Assert.AreNotEqual(45, mh.Root(456, 9));
            Assert.AreNotEqual(74, mh.Root(978, 2));
            // pridat osetrenie pre vstup mensi ako 0
        }
Пример #2
0
        /// <summary>
        /// Metóda btn_op_Click, ktorá je volaná pri stlačení tlačidla operácie. Metóda vykoná predchádzajúcu operáciu (pokiaľ existuje).
        /// Vypočíta operáciu pomocou metód volaných z matematickej knižnice. Ošetruje chybové stavy.
        /// Nastavuje nadchádzajúcu operáciu na základe aktuálne kliknutej operácie. Pokiaľ bola operácia stlačená bezprostredne pred akutálnou operáciou,
        /// stará operácie je prepísaná novou (aktuálne stlačenou operáciou).
        /// </summary>
        /// <param name="sender">Objekt, ktorý sa posiela do danej metódy</param>
        /// <param name="e">Dáta udalosti, ktoré sa posielajú do danej metódy</param>
        private void btn_op_Click(object sender, EventArgs e)
        {
            MathHead mh = new MathHead();

            Button button = (Button)sender;

            if (operationPressed == true && (operation == "+" || operation == "-" || operation == "*" || operation == "/" || operation == "^" || operation == "\u221A"))
            {
                operation        = button.Text;
                lbl_history.Text = lbl_history.Text.Remove(lbl_history.Text.Length - 1) + operation;
            }

            else
            {
                if (enterPressed == true)
                {
                    operation = button.Text;

                    lbl_history.Text = historyValue.ToString() + operation;
                    enterPressed     = false;
                }
                else
                {
                    try
                    {
                        Double.Parse(resultBox.Text);
                    }
                    catch (FormatException)
                    {
                        Error(0);
                        return;
                    }
                    historyStringValue = Double.Parse(resultBox.Text);

                    switch (operation)
                    {
                    case "+":
                        if (button.Text == "^" || button.Text == "\u221A" || button.Text == "*" || button.Text == "/")
                        {
                            betweenValue = historyValue;
                            historyValue = Double.Parse(resultBox.Text);
                            oldOperation = operation;
                            operation    = button.Text;
                        }

                        else
                        {
                            resultValue    = mh.Add(historyValue, Double.Parse(resultBox.Text));
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                        }
                        break;

                    case "^":
                        try
                        {
                            Int32.Parse(resultBox.Text);
                        }
                        catch (FormatException)
                        {
                            Error(2);
                            return;
                        }

                        if (Int32.Parse(resultBox.Text) < 0)
                        {
                            Error(2);
                            return;
                        }

                        switch (oldOperation)
                        {
                        case "+":
                            resultValue    = betweenValue + mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            betweenValue   = 0;
                            oldOperation   = "";
                            break;

                        case "-":
                            resultValue    = betweenValue - mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            betweenValue   = 0;
                            oldOperation   = "";
                            break;

                        case "*":
                            resultValue    = betweenValue * mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            betweenValue   = 0;
                            oldOperation   = "";
                            break;

                        case "/":
                            resultValue    = betweenValue / mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            betweenValue   = 0;
                            oldOperation   = "";
                            break;

                        default:
                            resultValue    = mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            oldOperation   = "";
                            break;
                        }

                        break;

                    case "\u221A":
                        if (historyValue == 0)
                        {
                            Error(4);
                            return;
                        }
                        if (Double.Parse(resultBox.Text) < 0)
                        {
                            Error(1);
                            return;
                        }
                        switch (oldOperation)
                        {
                        case "+":
                            resultValue    = betweenValue + mh.Root(Double.Parse(resultBox.Text), historyValue);
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            betweenValue   = 0;
                            oldOperation   = "";
                            break;

                        case "-":
                            resultValue    = betweenValue - mh.Root(Double.Parse(resultBox.Text), historyValue);
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            betweenValue   = 0;
                            oldOperation   = "";
                            break;

                        case "*":
                            resultValue    = betweenValue * mh.Root(Double.Parse(resultBox.Text), historyValue);
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            betweenValue   = 0;
                            oldOperation   = "";
                            break;

                        case "/":
                            resultValue    = betweenValue / mh.Root(Double.Parse(resultBox.Text), historyValue);
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            betweenValue   = 0;
                            oldOperation   = "";
                            break;

                        default:
                            resultValue    = mh.Root(Double.Parse(resultBox.Text), historyValue);
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                            oldOperation   = "";
                            break;
                        }

                        break;

                    case "-":
                        if (button.Text == "^" || button.Text == "\u221A" || button.Text == "*" || button.Text == "/")
                        {
                            betweenValue = historyValue;
                            historyValue = Double.Parse(resultBox.Text);
                            oldOperation = operation;
                            operation    = button.Text;
                        }

                        else
                        {
                            resultValue    = mh.Sub(historyValue, Double.Parse(resultBox.Text));
                            resultBox.Text = resultValue.ToString();
                            historyValue   = resultValue;
                        }

                        break;

                    case "*":

                        if (button.Text == "^" || button.Text == "\u221A" || button.Text == "/")
                        {
                            betweenValue = historyValue;
                            historyValue = Double.Parse(resultBox.Text);
                            oldOperation = operation;
                            operation    = button.Text;
                        }
                        else
                        {
                            switch (oldOperation)
                            {
                            case "+":
                                resultValue    = betweenValue + mh.Mul(historyValue, Double.Parse(resultBox.Text));
                                resultBox.Text = resultValue.ToString();
                                historyValue   = resultValue;
                                betweenValue   = 0;
                                oldOperation   = "";
                                break;

                            case "-":
                                resultValue    = betweenValue - mh.Mul(historyValue, Double.Parse(resultBox.Text));
                                resultBox.Text = resultValue.ToString();
                                historyValue   = resultValue;
                                betweenValue   = 0;
                                oldOperation   = "";
                                break;

                            case "/":
                                resultValue    = betweenValue / mh.Mul(historyValue, Double.Parse(resultBox.Text));
                                resultBox.Text = resultValue.ToString();
                                historyValue   = resultValue;
                                betweenValue   = 0;
                                oldOperation   = "";
                                break;

                            default:
                                if (historyValue == 0)
                                {
                                    historyValue = 1;
                                }
                                resultValue    = mh.Mul(historyValue, Double.Parse(resultBox.Text));
                                resultBox.Text = resultValue.ToString();
                                historyValue   = resultValue;
                                oldOperation   = "";
                                break;
                            }
                        }


                        break;

                    case "/":
                        if (button.Text == "^" || button.Text == "\u221A" || button.Text == "*")
                        {
                            betweenValue = historyValue;
                            historyValue = Double.Parse(resultBox.Text);
                            oldOperation = operation;
                            operation    = button.Text;
                        }
                        else
                        {
                            if (Double.Parse(resultBox.Text) == 0)
                            {
                                Error(4);
                                return;
                            }
                            switch (oldOperation)
                            {
                            case "+":
                                resultValue    = betweenValue + mh.Div(historyValue, Double.Parse(resultBox.Text));
                                resultBox.Text = resultValue.ToString();
                                historyValue   = resultValue;
                                betweenValue   = 0;
                                oldOperation   = "";
                                break;

                            case "-":
                                resultValue    = betweenValue - mh.Div(historyValue, Double.Parse(resultBox.Text));
                                resultBox.Text = resultValue.ToString();
                                historyValue   = resultValue;
                                betweenValue   = 0;
                                oldOperation   = "";
                                break;

                            case "*":
                                resultValue    = betweenValue * mh.Div(historyValue, Double.Parse(resultBox.Text));
                                resultBox.Text = resultValue.ToString();
                                historyValue   = resultValue;
                                betweenValue   = 0;
                                oldOperation   = "";
                                break;

                            default:
                                resultValue    = mh.Div(historyValue, Double.Parse(resultBox.Text));
                                resultBox.Text = resultValue.ToString();
                                historyValue   = resultValue;
                                oldOperation   = "";
                                break;
                            }
                        }

                        break;

                    default:
                        historyValue = historyStringValue;
                        break;
                    }
                    if (funcPressed == true)
                    {
                        lbl_history.Text = historyStringValue.ToString() + button.Text;
                    }
                    else
                    {
                        lbl_history.Text = lbl_history.Text + historyStringValue.ToString() + button.Text;
                    }

                    operation = button.Text;
                }
            }
            funcPressed      = false;
            enterPressed     = false;
            operationPressed = true;
            NewFont();
            maxChar = false;
            btn_enter.Focus();
        }
Пример #3
0
        /// <summary>
        /// Metóda btn_enter_Click, ktorá je volaná pri stlačení tlačidla "rovná sa". Má za úlohu zistiť naposledy kliknutú operáciu
        /// a na základe nej vypočítať výsledok a ukončiť sekvenciu operácií. Tento výsledok sa následne vypíše do textBox výsledku a do histórie sa vypíše história operácií
        /// zakončená posledným číslom a znaminekom rovnosti. Ošetruje aj chybové stavy.
        /// </summary>
        /// <param name="sender">Objekt, ktorý sa posiela do danej metódy</param>
        /// <param name="e">Dáta udalosti, ktoré sa posielajú do danej metódy</param>
        private void btn_enter_Click(object sender, EventArgs e)
        {
            MathHead mh = new MathHead();

            try
            {
                Double.Parse(resultBox.Text);
            }
            catch (FormatException)
            {
                Error(0);
                return;
            }

            historyStringValue = Double.Parse(resultBox.Text);

            switch (operation)
            {
            case "+":
                resultValue      = mh.Add(historyValue, Double.Parse(resultBox.Text));
                resultBox.Text   = resultValue.ToString();
                historyValue     = resultValue;
                lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                break;

            case "-":
                resultValue      = mh.Sub(historyValue, Double.Parse(resultBox.Text));
                resultBox.Text   = resultValue.ToString();
                historyValue     = resultValue;
                lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                break;

            case "*":
                switch (oldOperation)
                {
                case "+":
                    resultValue      = betweenValue + mh.Mul(historyValue, Double.Parse(resultBox.Text));
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                case "-":
                    resultValue      = betweenValue - mh.Mul(historyValue, Double.Parse(resultBox.Text));
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                case "/":
                    resultValue      = betweenValue / mh.Mul(historyValue, Double.Parse(resultBox.Text));
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                default:
                    resultValue      = mh.Mul(historyValue, Double.Parse(resultBox.Text));
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    oldOperation     = "";
                    break;
                }
                break;

            case "^":
                try
                {
                    Int32.Parse(resultBox.Text);
                }
                catch (FormatException)
                {
                    Error(2);
                    return;
                }

                if (Int32.Parse(resultBox.Text) < 0)
                {
                    Error(2);
                    return;
                }

                switch (oldOperation)
                {
                case "+":
                    resultValue      = betweenValue + mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                case "-":
                    resultValue      = betweenValue - mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                case "*":
                    resultValue      = betweenValue * mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                case "/":
                    resultValue      = betweenValue / mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                default:
                    resultValue      = mh.Exp(historyValue, Int32.Parse(resultBox.Text));
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    oldOperation     = "";
                    break;
                }
                break;

            case "\u221A":

                if (historyValue == 0)
                {
                    Error(4);
                    return;
                }
                if (Double.Parse(resultBox.Text) < 0)
                {
                    Error(1);
                    return;
                }
                switch (oldOperation)
                {
                case "+":
                    resultValue      = betweenValue + mh.Root(Double.Parse(resultBox.Text), historyValue);
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                case "-":
                    resultValue      = betweenValue - mh.Root(Double.Parse(resultBox.Text), historyValue);
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                case "*":
                    resultValue      = betweenValue * mh.Root(Double.Parse(resultBox.Text), historyValue);
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                case "/":
                    resultValue      = betweenValue / mh.Root(Double.Parse(resultBox.Text), historyValue);
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    betweenValue     = 0;
                    oldOperation     = "";
                    break;

                default:
                    resultValue      = mh.Root(Double.Parse(resultBox.Text), historyValue);
                    resultBox.Text   = resultValue.ToString();
                    historyValue     = resultValue;
                    lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                    oldOperation     = "";
                    break;
                }
                break;

            case "/":
                if (Double.Parse(resultBox.Text) == 0)
                {
                    Error(4);
                    return;
                }
                else
                {
                    switch (oldOperation)
                    {
                    case "+":
                        resultValue      = betweenValue + mh.Div(historyValue, Double.Parse(resultBox.Text));
                        resultBox.Text   = resultValue.ToString();
                        historyValue     = resultValue;
                        lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                        betweenValue     = 0;
                        oldOperation     = "";
                        break;

                    case "-":
                        resultValue      = betweenValue - mh.Div(historyValue, Double.Parse(resultBox.Text));
                        resultBox.Text   = resultValue.ToString();
                        historyValue     = resultValue;
                        lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                        betweenValue     = 0;
                        oldOperation     = "";
                        break;

                    case "*":
                        resultValue      = betweenValue * mh.Div(historyValue, Double.Parse(resultBox.Text));
                        resultBox.Text   = resultValue.ToString();
                        historyValue     = resultValue;
                        lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                        betweenValue     = 0;
                        oldOperation     = "";
                        break;

                    default:
                        resultValue      = mh.Div(historyValue, Double.Parse(resultBox.Text));
                        resultBox.Text   = resultValue.ToString();
                        historyValue     = resultValue;
                        lbl_history.Text = lbl_history.Text + historyStringValue + "=";
                        oldOperation     = "";
                        break;
                    }
                }
                break;
            }
            if (double.IsInfinity(resultValue))
            {
                Error(2);
                return;
            }
            NewFont();
            enterPressed     = true;
            operationPressed = false;
            operation        = "";
            maxChar          = false;
            btn_enter.Focus();
        }