/// <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(); }
//testovanie MathHead vypocet s exponentom public void Function_Exp() { MathHeadFunctiones mh = new MathHeadFunctiones(); Assert.AreEqual(9, mh.Exp(3, 2)); Assert.AreEqual(42875, mh.Exp(35, 3)); Assert.AreEqual(147008443, mh.Exp(43, 5)); Assert.AreEqual(25, mh.Exp(-5, 2)); Assert.AreEqual(-125, mh.Exp(-5, 3)); Assert.AreEqual(0.729, mh.Exp(0.9, 3), esp); Assert.AreEqual(3.652264, mh.Exp(1.54, 3)); Assert.AreNotEqual(745, mh.Exp(9, 5)); Assert.AreNotEqual(0.56, mh.Exp(1.26, 4)); Assert.AreNotEqual(654, mh.Exp(7, 6)); Assert.AreNotEqual(1231456, mh.Exp(53, 2)); }
/// <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(); }