Home > Backend Development > C#.Net Tutorial > Basic calculator program using C#

Basic calculator program using C#

WBOY
Release: 2023-09-06 08:45:13
forward
862 people have browsed it

使用 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;
Copy after login

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;
}
Copy after login

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;
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template