Ejemplo n.º 1
0
        //testovanie MathHead ^2
        public void Function_SQRT()
        {
            MathHeadFunctiones mh = new MathHeadFunctiones();

            Assert.AreEqual(144, mh.Sqrt(12));
            Assert.AreEqual(64000000, mh.Sqrt(8000));
            Assert.AreEqual(4, mh.Sqrt(2));
            Assert.AreEqual(1, mh.Sqrt(1));

            Assert.AreNotEqual(-9, mh.Sqrt(-3));
            Assert.AreNotEqual(-64, mh.Sqrt(-8));
            Assert.AreNotEqual(-64000000, mh.Sqrt(-8000));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Metóda btn_sqrt_Click, ktorá je volaná pri stlačení tlačidla "x2". Metóda počíta druhú mocninu čísla pomocou metódy Sqrt() volanej z matematickej knižnice.
        /// Ošetruje chybové stavy, pridá funkciu do histórie a výsledok vypíše do textBoxu výsledku.
        /// </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_sqrt_Click(object sender, EventArgs e)
        {
            MathHead mh = new MathHead();

            if (enterPressed == true)
            {
                lbl_history.Text = "";
                enterPressed     = false;
            }

            try
            {
                Double.Parse(resultBox.Text);
            }
            catch (FormatException)
            {
                Error(1);
                return;
            }
            catch (OverflowException)
            {
                Error(3);
                return;
            }

            if (double.IsInfinity(historyValue) || double.IsInfinity(double.Parse(resultBox.Text)))
            {
                Error(3);
                return;
            }
            switch (operation)
            {
            case "+":
                lbl_history.Text = lbl_history.Text + "sqrt(" + Double.Parse(resultBox.Text) + ")";
                resultValue      = historyValue + mh.Sqrt(Double.Parse(resultBox.Text));
                resultBox.Text   = resultValue.ToString();
                historyValue     = resultValue;

                break;

            case "-":
                lbl_history.Text = lbl_history.Text + "sqrt(" + Double.Parse(resultBox.Text) + ")";
                resultValue      = historyValue - mh.Sqrt(Double.Parse(resultBox.Text));
                resultBox.Text   = resultValue.ToString();
                historyValue     = resultValue;

                break;

            case "*":
                lbl_history.Text = lbl_history.Text + "sqrt(" + Double.Parse(resultBox.Text) + ")";
                resultValue      = historyValue * mh.Sqrt(Double.Parse(resultBox.Text));
                resultBox.Text   = resultValue.ToString();
                historyValue     = resultValue;

                break;

            case "/":
                lbl_history.Text = lbl_history.Text + "sqrt(" + Double.Parse(resultBox.Text) + ")";
                resultValue      = historyValue / mh.Sqrt(Double.Parse(resultBox.Text));
                resultBox.Text   = resultValue.ToString();
                historyValue     = resultValue;

                break;

            default:
                if (funcPressed == true)
                {
                    lbl_history.Text = "sqrt(" + historyString + ")";
                }
                else
                {
                    lbl_history.Text = lbl_history.Text + "sqrt(" + Double.Parse(resultBox.Text) + ")";
                }

                resultValue    = mh.Sqrt(Double.Parse(resultBox.Text));
                resultBox.Text = resultValue.ToString();
                historyValue   = resultValue;
                break;
            }
            if (double.IsInfinity(resultValue))
            {
                Error(3);
                return;
            }
            historyString    = lbl_history.Text;
            operationPressed = false;
            operation        = "";
            funcPressed      = true;
            NewFont();
            maxChar = false;
            btn_enter.Focus();
        }