C#实现简易版的计算器数
界面设计:textbook 和18个button按钮代码部分:
private void Form1_Load(object sender, EventArgs e)
{
//计算计算器窗口居中
int left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
int top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
this.Location = new Point(left, top);
}
double num1 = 0;
double num2 = 0;
bool iskey = false;
//1
private void Button7_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "1";
}
//2
private void Button6_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "2";
}
//3
private void Button5_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "3";
}
//4
private void Button11_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "4";
}
//5
private void Button10_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "5";
}
//6
private void Button9_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "6";
}
//7
private void Button15_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "7";
}
//8
private void Button14_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "8";
}
//9
private void Button12_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "9";
}
//0
private void Button18_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "0";
}
//小数点.
private void Button17_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += ".";
}
//CE
private void Button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
string type = "";
//=
private void Button2_Click(object sender, EventArgs e)
{
if (iskey)
{
return;
}
iskey = true;
if (textBox1.Text != "")
{
num2 = Convert.ToDouble(textBox1.Text);
}
switch (type)
{
case "+":
textBox1.Text = (num1 + num2).ToString();
break;
case "-":
textBox1.Text = (num1 - num2).ToString();
break;
case "×":
textBox1.Text = (num1 * num2).ToString();
break;
case "/":
textBox1.Text = (num1 / num2).ToString();
break;
case "%":
textBox1.Text = (num1 % num2).ToString();
break;
default:
break;
}
}
//-
private void Button3_Click(object sender, EventArgs e)
{
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
type = "-";
iskey = true;
}
//×
private void Button4_Click(object sender, EventArgs e)
{
type = "×";
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
iskey = true;
}
//除
private void Button8_Click(object sender, EventArgs e)
{
type = "/";
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
iskey = true;
}
//+
private void Button13_Click(object sender, EventArgs e)
{
type = "+";
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
iskey = true;
}
//取余%
private void Button16_Click(object sender, EventArgs e)
{
type = "%";
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
iskey = true;
}
}
}
还没有评论,来说两句吧...