As can be seen in the image, the calculator can perform the basic arithmetic operations along with evaluating trig functions, logs, etc. The first step I took in building this calculator was adding a bunch of buttons onto the Windows Forms App (and adjusting the font to something beautiful, like Times New Roman). After that I began coding all the numerical buttons to add a number into the text box when pressed, using the following if else statement:
private void zero_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "0"; } else textBox1.Text += "0"; }
private void dot_Click(object sender, EventArgs e) { if (!textBox1.Text.Contains(".")) { textBox1.Text += "."; } }
private void addition_Click(object sender, EventArgs e) { num1 = decimal.Parse(textBox1.Text); operation = "+"; textBox1.Text = "0"; }
private void sin_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Sin(double.Parse(textBox1.Text))); }
private void factorial_Click(object sender, EventArgs e) { long f = 1; for (long i = 1; i <= long.Parse(textBox1.Text); i++) { f = f * i; } textBox1.Text = Convert.ToString(f); }
private void evaluate_Click(object sender, EventArgs e) { num2 = decimal.Parse(textBox1.Text); switch (operation) { case "+": textBox1.Text = Convert.ToString(num1 + num2); break; case "-": textBox1.Text = Convert.ToString(num1 - num2); break; case "*": textBox1.Text = Convert.ToString(num1 * num2); break; case "/": if (num2 != 0) { textBox1.Text = Convert.ToString(num1 / num2); } else { textBox1.Text = "NaN"; } break; case "^": textBox1.Text = Convert.ToString(int.Parse(Convert.ToString(num1)) ^ int.Parse(Convert.ToString(num2))); if(num1 == 0 & num2 == 0) { textBox1.Text = "1"; } break; }
using System; using System.Windows.Forms; namespace calculator { public partial class Form1 : Form { private decimal num1; private decimal num2; string operation; public Form1() { InitializeComponent(); } private void zero_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "0"; } else textBox1.Text += "0"; } private void one_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "1"; } else textBox1.Text += "1"; } private void two_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "2"; } else textBox1.Text += "2"; } private void three_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "3"; } else textBox1.Text += "3"; } private void four_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "4"; } else textBox1.Text += "4"; } private void five_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "5"; } else textBox1.Text += "5"; } private void six_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "6"; } else textBox1.Text += "6"; } private void seven_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "7"; } else textBox1.Text += "7"; } private void eight_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "8"; } else textBox1.Text += "8"; } private void nine_Click(object sender, EventArgs e) { if (textBox1.Text == "0") { textBox1.Text = "9"; } else textBox1.Text += "9"; } private void dot_Click(object sender, EventArgs e) { if (!textBox1.Text.Contains(".")) { textBox1.Text += "."; } } private void addition_Click(object sender, EventArgs e) { num1 = decimal.Parse(textBox1.Text); operation = "+"; textBox1.Text = "0"; } private void subtraction_Click(object sender, EventArgs e) { num1 = decimal.Parse(textBox1.Text); operation = "-"; textBox1.Text = "0"; } private void multiplication_Click(object sender, EventArgs e) { num1 = decimal.Parse(textBox1.Text); operation = "*"; textBox1.Text = "0"; } private void division_Click(object sender, EventArgs e) { num1 = decimal.Parse(textBox1.Text); operation = "/"; textBox1.Text = "0"; } private void sin_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Sin(double.Parse(textBox1.Text))); } private void cos_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Cos(double.Parse(textBox1.Text))); } private void tan_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Tan(double.Parse(textBox1.Text))); } private void arcsin_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Asin(double.Parse(textBox1.Text))); } private void arccos_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Acos(double.Parse(textBox1.Text))); } private void arctan_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Atan(double.Parse(textBox1.Text))); } private void tenPower_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Pow(10, double.Parse(textBox1.Text))); } private void log_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Log10(double.Parse(textBox1.Text))); } private void pi_Click(object sender, EventArgs e) { double pi = Math.PI; textBox1.Text = Convert.ToString(pi); } private void ePower_Click(object sender, EventArgs e) { double E = Math.E; textBox1.Text = Convert.ToString(Math.Pow(E, double.Parse(textBox1.Text))); } private void lnx_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Log(double.Parse(textBox1.Text))); } private void factorial_Click(object sender, EventArgs e) { long f = 1; for (long i = 1; i <= long.Parse(textBox1.Text); i++) { f = f * i; } textBox1.Text = Convert.ToString(f); } private void xSquared_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Pow(double.Parse(textBox1.Text), 2)); } private void xPowerY_Click(object sender, EventArgs e) { num1 = decimal.Parse(textBox1.Text); operation = "^"; textBox1.Text = "0"; } private void sqrt_Click(object sender, EventArgs e) { textBox1.Text = Convert.ToString(Math.Sqrt(double.Parse(textBox1.Text))); } private void clear_Click(object sender, EventArgs e) { textBox1.Text = ""; operation = string.Empty; } private void evaluate_Click(object sender, EventArgs e) { num2 = decimal.Parse(textBox1.Text); switch (operation) { case "+": textBox1.Text = Convert.ToString(num1 + num2); break; case "-": textBox1.Text = Convert.ToString(num1 - num2); break; case "*": textBox1.Text = Convert.ToString(num1 * num2); break; case "/": if (num2 != 0) { textBox1.Text = Convert.ToString(num1 / num2); } else { textBox1.Text = "NaN"; } break; case "^": textBox1.Text = Convert.ToString(int.Parse(Convert.ToString(num1)) ^ int.Parse(Convert.ToString(num2))); if(num1 == 0 & num2 == 0) { textBox1.Text = "1"; } break; } } } }
No comments:
Post a Comment