namespace 计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// bool iskey = false;
// 定义一个字符接收 符号
char type;
double temp, num;
// 1
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
// 2
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text +="2";
}
//3
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
//4
private void button12_Click(object sender, EventArgs e)
{
textBox1.Text +="4";
}
//5
private void button13_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
//6
private void button14_Click(object sender, EventArgs e)
{
textBox1.Text +="6";
}
//7
private void button15_Click(object sender, EventArgs e)
{
textBox1.Text +="7";
}
//8
private void button16_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
//9
private void button17_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
//0
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
//+
private void button2_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
type= '+';
textBox1.Text = null;
}
//-
private void button3_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
type = '-';
textBox1.Text = null;
}
//*
private void button4_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
type = '*';
textBox1.Text = null;
}
// ÷
private void button5_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
type = '/';
textBox1.Text = null;
}
// %
private void button6_Click(object sender, EventArgs e)
{
num = double.Parse(textBox1.Text);
type = '%';
textBox1.Text = null;
}
// =
private void button7_Click(object sender, EventArgs e)
{ //.Parse()方法只能将 数字类型转换为.前面的类型 只能将数字
temp = double.Parse(textBox1.Text);
switch (type)
{
case '+':
num += temp;
break;
case '-':
num -= temp;
break;
case '*':
num *= temp;
break;
case '/':
num += temp;
break;
case '%':
num %= temp;
break;
}
textBox1.Text = num.ToString();
type = ' ';
num = 0;
temp = 0;
}
// .
private void button18_Click(object sender, EventArgs e)
{
textBox1.Text += ".";
}
// 清零 CE
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
// 删除 C
private void button19_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
}
}
}
C# 按钮计算器的实现
最新推荐文章于 2023-01-30 19:06:39 发布