#To create a calculator program using C#, you need to use Web Forms. Create buttons for 1-9, addition, subtraction, multiplication, etc. underneath it.
Let's look at the codes for addition, subtraction and multiplication. First, we declared two variables -
static float x, y;
Now, we will see how to set up the calculation code when a single button is clicked: Our result text box is a tbResult, since we are also using Windows Forms to display calculations Device-
protected void add_Click(object sender, EventArgs e) { x = Convert.ToInt32(tbResult.Text); tbResult.Text = ""; y = '+'; tbResult.Text += y; } protected void sub_Click(object sender, EventArgs e) { x = Convert.ToInt32(tbResult.Text); tbResult.Text = ""; y = '-'; tbResult.Text += y; } protected void mul_Click(object sender, EventArgs e) { x = Convert.ToInt32(tbResult.Text); tbResult.Text = ""; y = '*'; tbResult.Text += y; }
The following is the equal sign button code-
protected void eql_Click(object sender, EventArgs e) { z = Convert.ToInt32(tbResult.Text); tbResult.Text = ""; if (y == '/') { p = x / z; tbResult.Text += p; x = d; } else if (y == '+') { p = x + z; tbResult.Text += p; a = d; } else if (y == '-') { p = x - z; tbResult.Text += p; x = p; } else { p = x * z; tbResult.Text += p; x = p; } }
The above is the detailed content of Basic calculator program using C#. For more information, please follow other related articles on the PHP Chinese website!