/// <summary> /// Raises the number in the results box to the power of 1/3 /// </summary> private void btn_cubeRoot_Click(object sender, RoutedEventArgs e) { if (!thereWasError) { double numToRoot = Convert.ToDouble(outResults.Text); if (numToRoot < 0) { outResults.Text = "-" + Arithmetic.nthRoot(Math.Abs(numToRoot), 3).ToString(); } else { outResults.Text = Arithmetic.nthRoot(numToRoot, 3).ToString(); } } }
/// <summary> /// Makes the number in the results box the square root of the number originally in there. /// </summary> private void btn_sqrt_Click(object sender, RoutedEventArgs e) { if (!thereWasError) { if (Convert.ToDouble(outResults.Text) >= 0) { outResults.Text = Arithmetic.nthRoot(Convert.ToDouble(outResults.Text), 2).ToString(); } else //Square roots of negative numbers are impossible { outStatus.Text = Messages.error_InvNumber; outResults.Text = Messages.error_InvNumber; thereWasError = true; } } }
/// <summary> /// Performs the operation requested and stores the number in the lastResults variable. /// </summary> public void performOperation() { //For the most part, this is the same as the btn_calculate_Click, but not setting isStarting to true, // and storing the answer in the lastResults variable. if (!thereWasError) { try { switch (curOperation) { case "add": outResults.Text = (lastResults + Convert.ToDouble(outResults.Text)).ToString(); lastResults = Convert.ToDouble(outResults.Text); break; case "subtract": outResults.Text = (lastResults - Convert.ToDouble(outResults.Text)).ToString(); lastResults = Convert.ToDouble(outResults.Text); break; case "mult": outResults.Text = (lastResults * Convert.ToDouble(outResults.Text)).ToString(); lastResults = Convert.ToDouble(outResults.Text); break; case "divide": if (outResults.Text != "0") { outResults.Text = (lastResults / Convert.ToDouble(outResults.Text)).ToString(); lastResults = Convert.ToDouble(outResults.Text); } else { outStatus.Text = Messages.error_DivZero; outResults.Text = Messages.error_DivZero; thereWasError = true; } break; case "log": if (Convert.ToDouble(outResults.Text) > 0) { outResults.Text = Arithmetic.nthLogBase(lastResults, Convert.ToDouble(outResults.Text)).ToString(); lastResults = Convert.ToDouble(outResults.Text); } else { outResults.Text = Messages.error_InvNumber; outStatus.Text = Messages.error_log.Replace("%s", Messages.error_log_base); thereWasError = true; } break; case "yroot": double rootNum = Convert.ToDouble(outResults.Text); if (rootNum % 2.0 == 0) //No negatives allowed { if (lastResults < 0) { outResults.Text = Messages.error_InvNumber; outStatus.Text = Messages.error_InvNumber; thereWasError = true; } else { outResults.Text = Arithmetic.nthRoot(lastResults, rootNum).ToString(); lastResults = Convert.ToDouble(outResults.Text); } } else //Negatives allowed { if (lastResults < 0) { outResults.Text = "-" + Arithmetic.nthRoot(Math.Abs(lastResults), rootNum).ToString(); lastResults = Convert.ToDouble(outResults.Text); } else { outResults.Text = Arithmetic.nthRoot(lastResults, rootNum).ToString(); lastResults = Convert.ToDouble(outResults.Text); } } break; case "pow": outResults.Text = Arithmetic.nthPower(lastResults, Convert.ToDouble(outResults.Text)).ToString(); lastResults = Convert.ToDouble(outResults.Text); break; case "mod": if (outResults.Text != "0") { outResults.Text = (lastResults % Convert.ToDouble(outResults.Text)).ToString(); lastResults = Convert.ToDouble(outResults.Text); } else { outStatus.Text = Messages.error_DivZero; outResults.Text = Messages.error_DivZero; thereWasError = true; } break; } if (!thereWasError && (Double.IsInfinity(lastResults) || Double.IsInfinity(Convert.ToDouble(outResults.Text)))) { outResults.Text = Messages.error_overflow; outStatus.Text = Messages.error_overflow; thereWasError = true; } } //Also, catch those potential errors. catch (OverflowException) { outResults.Text = Messages.error_overflow; outStatus.Text = Messages.error_overflow; thereWasError = true; } } }
/// <summary> /// Calculates the final results from the numbers given and the operation(s) requested. /// </summary> private void btn_calculate_Click(object sender, RoutedEventArgs e) { if (!thereWasError) { if (!isStarting) //The potential constant is usually from the last number entered. { potentialConstant = Convert.ToDouble(outResults.Text); } switch (curOperation) { case "add": //Show the results from adding... if (!isStarting) { outResults.Text = (lastResults + Convert.ToDouble(outResults.Text)).ToString(); } else { outResults.Text = (Convert.ToDouble(outResults.Text) + potentialConstant).ToString(); } break; case "subtract": //...or subtracting... if (!isStarting) { outResults.Text = (lastResults - Convert.ToDouble(outResults.Text)).ToString(); } else { outResults.Text = (Convert.ToDouble(outResults.Text) - potentialConstant).ToString(); } break; case "mult": //...or multiplying... if (!isStarting) { outResults.Text = (lastResults * Convert.ToDouble(outResults.Text)).ToString(); } else { outResults.Text = (Convert.ToDouble(outResults.Text) * potentialConstant).ToString(); } break; case "divide": //...or dividing... if (!isStarting) { if (outResults.Text != "0") //Also if denominator is zero, throw an error. { outResults.Text = (lastResults / Convert.ToDouble(outResults.Text)).ToString(); } else { outStatus.Text = Messages.error_DivZero; outResults.Text = Messages.error_DivZero; thereWasError = true; } } else { outResults.Text = (Convert.ToDouble(outResults.Text) / potentialConstant).ToString(); } break; case "log": //...or logarithms... if (Convert.ToDouble(outResults.Text) > 0) { outResults.Text = Arithmetic.nthLogBase(lastResults, Convert.ToDouble(outResults.Text)).ToString(); } else //Logarithms can't have a negative number. { outResults.Text = Messages.error_InvNumber; outStatus.Text = Messages.error_log.Replace("%s", Messages.error_log_base); thereWasError = true; } break; case "yroot": //...or custom roots... double rootNum = Convert.ToDouble(outResults.Text); if (rootNum % 2.0 == 0) //Root is even. No negatives allowed. { if (lastResults < 0) { outResults.Text = Messages.error_InvNumber; outStatus.Text = Messages.error_InvNumber; thereWasError = true; } else { outResults.Text = Arithmetic.nthRoot(lastResults, rootNum).ToString(); } } else //Root is odd. Negatives are allowed. { if (lastResults < 0) { //For some reason, this won't accept negative numbers... at all. So just add a negative sign if it's negative. outResults.Text = "-" + Arithmetic.nthRoot(Math.Abs(lastResults), rootNum).ToString(); } else { outResults.Text = Arithmetic.nthRoot(lastResults, rootNum).ToString(); } } break; case "pow": //...or raising to powers... outResults.Text = Arithmetic.nthPower(lastResults, Convert.ToDouble(outResults.Text)).ToString(); break; case "mod": //...or the modulus operator. if (!isStarting) { if (outResults.Text != "0") { outResults.Text = (lastResults % Convert.ToDouble(outResults.Text)).ToString(); } else { outStatus.Text = Messages.error_DivZero; outResults.Text = Messages.error_DivZero; thereWasError = true; } } else { outResults.Text = (Convert.ToDouble(outResults.Text) % potentialConstant).ToString(); } break; } //Final result display if there was no error. if (!thereWasError) { outStatus.Text = Messages.equalNum; } if (!thereWasError && (Double.IsInfinity(lastResults) || Double.IsInfinity(Convert.ToDouble(outResults.Text)))) { outResults.Text = Messages.error_overflow; outStatus.Text = Messages.error_overflow; thereWasError = true; } if (!thereWasError && (Double.IsNaN(lastResults) || Double.IsNaN(Convert.ToDouble(outResults.Text)))) { outResults.Text = Messages.error_InvNumber; outStatus.Text = Messages.error_InvNumber; thereWasError = true; } //Also, since the equal button was pressed, this is the final answer, and therefore, we're starting a new equation. isStarting = true; } }