/// <summary> /// Changes the number in the results box to radians. Also, trig functions will now all use radians. /// </summary> private void chk_radMode_Checked(object sender, RoutedEventArgs e) { if (!thereWasError) { if (isInDegMode) { outStatus.Text = Messages.nowRadMode; isInDegMode = false; outResults.Text = Arithmetic.toRadians(Convert.ToDouble(outResults.Text)).ToString(); } } }
/// <summary> /// Cubes the number in the results box. /// </summary> private void btn_cube_Click(object sender, RoutedEventArgs e) { if (!thereWasError) { outResults.Text = Arithmetic.nthPower(Convert.ToDouble(outResults.Text), 3).ToString(); //If it's too much, just say so. if (outResults.Text == "Infinity") { outResults.Text = Messages.error_overflow; outStatus.Text = Messages.error_overflow; thereWasError = true; } } }
/// <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> /// Reciprocates the number in the results box. /// For example: The number "2" becomes the results of "1/2", "0.5" /// </summary> private void btn_recip_Click(object sender, RoutedEventArgs e) { if (!thereWasError) { if (outResults.Text != "0") //Putting to the power of -1 acts as reciprocating the number. { outResults.Text = Arithmetic.nthPower(Convert.ToDouble(outResults.Text), -1).ToString(); } else //And throw an error if the number is zero { outResults.Text = Messages.error_DivZero; outStatus.Text = Messages.error_DivZero; thereWasError = true; } } }
/// <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> /// Makes the number in the results box the loagrithm (base 10) of the number originally in there. /// </summary> private void btn_log10_Click(object sender, RoutedEventArgs e) { if (!thereWasError) { if (Convert.ToDouble(outResults.Text) > 0) { outResults.Text = Arithmetic.nthLogBase(Convert.ToDouble(outResults.Text), 10).ToString(); } else //Still can't take logs of negative numbers. { outResults.Text = Messages.error_InvNumber; outStatus.Text = Messages.error_log.Replace("%s", Messages.error_log_subject); thereWasError = true; } } }
/// <summary> /// Performs the tangent function on the number in the result box. The result depends on if the calculator is in degree or radian mode. /// It also depends on whether or not it is currently showing inverse functions or not. /// </summary> private void btn_tangent_Click(object sender, RoutedEventArgs e) { if (!thereWasError) { double numTangent = Convert.ToDouble(outResults.Text); if (!showingInvFns) { if (isInDegMode) { outResults.Text = Arithmetic.degTangent(Convert.ToDouble(outResults.Text)).ToString(); } else { outResults.Text = Arithmetic.radTangent(Convert.ToDouble(outResults.Text)).ToString(); } } else { if (isInDegMode) { outResults.Text = Arithmetic.degInvTangent(Convert.ToDouble(outResults.Text)).ToString(); } else { outResults.Text = Arithmetic.radInvTangent(Convert.ToDouble(outResults.Text)).ToString(); } } //If anything here causes this, it'll be tangent. if (outResults.Text == "NaN" || outResults.Text == "Infinity") { outStatus.Text = Messages.error_tangentAsymptotes.Replace("%s", numTangent.ToString()); outResults.Text = Messages.error_InvNumber; thereWasError = true; } } }
/// <summary> /// Performs the cosine function on the number in the result box. The result depends on if the calculator is in degree or radian mode. /// It also depends on whether or not it is currently showing inverse functions or not. /// </summary> private void btn_cosine_Click(object sender, RoutedEventArgs e) { if (!thereWasError) { if (!showingInvFns) { if (isInDegMode) { outResults.Text = Arithmetic.degCosine(Convert.ToDouble(outResults.Text)).ToString(); } else { outResults.Text = Arithmetic.radCosine(Convert.ToDouble(outResults.Text)).ToString(); } } else { if (isInDegMode) { outResults.Text = Arithmetic.degInvCosine(Convert.ToDouble(outResults.Text)).ToString(); } else { outResults.Text = Arithmetic.radInvCosine(Convert.ToDouble(outResults.Text)).ToString(); } } //If anything here causes this, it'll be inverse cosine. if (outResults.Text == "NaN") { outResults.Text = Messages.error_InvNumber; outStatus.Text = Messages.error_invCosine; 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; } }