Beispiel #1
0
        //testovanie MathHead Krat
        public void Function_Ml()
        {
            MathHeadFunctiones mh = new MathHeadFunctiones();

            Assert.AreEqual(2, mh.Mul(2, 1));
            Assert.AreEqual(-81, mh.Mul(9, -9));
            Assert.AreEqual(60, mh.Mul(-6, -10));
            Assert.AreEqual(-42, mh.Mul(-42, 1));
            Assert.AreEqual(0, mh.Mul(0, 1987));
            Assert.AreEqual(400000000, mh.Mul(20000, 20000));
            Assert.AreEqual(-100000000, mh.Mul(-100000000, 1));
            Assert.AreEqual(44.1, mh.Mul(6.3, 7));
        }
Beispiel #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();
        }
Beispiel #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();
        }