C#计算器
程序员文章站
2022-07-16 19:17:23
...
效果图如下
代码如下
double num1, num2;
//定义第一次输入的数字与第二次输入的数字
string ysf;
//定义运算符
//第一次输入的数字都显示在文本框中
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += button7.Text;
textBox2.Text += button7.Text;
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += button8.Text;
textBox2.Text += button8.Text;
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += button9.Text;
textBox2.Text += button9.Text;
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text += button4.Text;
textBox2.Text += button4.Text;
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += button5.Text;
textBox2.Text += button5.Text;
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += button6.Text;
textBox2.Text += button6.Text;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += button1.Text;
textBox2.Text += button1.Text;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += button2.Text;
textBox2.Text += button2.Text;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += button3.Text;
textBox2.Text += button3.Text;
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += button11.Text;
textBox2.Text += button11.Text;
}
//+ 判断运算符为+
private void button17_Click(object sender, EventArgs e)
{
textBox2.Text += button17.Text;
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
ysf = "+";
}
//- 判断运算符为-
private void button15_Click(object sender, EventArgs e)
{
textBox2.Text += button15.Text;
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
ysf = "-";
}
//X 判断运算符为X
private void button14_Click(object sender, EventArgs e)
{
textBox2.Text += button14.Text;
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
ysf = "X";
}
// 判断运算符为/
private void button16_Click(object sender, EventArgs e)
{
textBox2.Text += button16.Text;
num1 = double.Parse(textBox1.Text);
textBox1.Text = "";
ysf = "/";
}
//=
private void button13_Click(object sender, EventArgs e)
{
num2 = double.Parse(textBox1.Text);
//判断运算符为+-X/时的算法
switch (ysf)
{
case ("+"):
textBox1.Text = (num1 + num2).ToString();
textBox2.Text += "=" + textBox1.Text;
break;
case ("-"):
textBox1.Text = (num1 - num2).ToString();
textBox2.Text += "=" + textBox1.Text;
break;
case ("X"):
textBox1.Text = (num1 * num2).ToString();
textBox2.Text += "=" + textBox1.Text;
break;
case ("/"):
textBox1.Text = (num1 / num2).ToString();
textBox2.Text += "=" + textBox1.Text;
break;
}
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += button10.Text;
textBox2.Text += button10.Text;
}
}
}
上一篇: Grpc C# 入门
下一篇: C#多功能计算器