Home  >  Article  >  Backend Development  >  Basic calculator program using C#

Basic calculator program using C#

WBOY
WBOYforward
2023-09-06 08:45:13828browse

使用 C# 的基本计算器程序

#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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete