winform计算器---C#
using System;
using System.Windows.Forms;
namespace _201_winform
{
public partial class Form1 : Form
{
private string strOutput="";
private double INumFormer=0;
private double INumTemp=0;
private double IResult=0;
private char cOperator;
private bool bDotClicked=false;//小数点的建是否按下
double lastDecimalNum = 1;//小数的最后以为1.0001
public Form1()
{
InitializeComponent();
EventHandler eh = new EventHandler(Numbers_Click);
button_num0.Click += eh;
button_num1.Click += eh;
button_num1.Click += eh;
button_num3.Click += eh;
button_num4.Click += eh;
button_num5.Click += eh;
button_num6.Click += eh;
button_num7.Click += eh;
button_num8.Click += eh;
button_num9.Click += eh;
EventHandler eh2 = new EventHandler(Operators_Click);
button_add.Click += eh2;
button_sub.Click += eh2;
button_mal.Click += eh2;
button_div.Click += eh2;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
INumFormer = 0;//前一个操作清空
INumTemp = 0;
IResult = 0;
cOperator = '\0';
strOutput = "";
bDotClicked = false;
lastDecimalNum = 1;
txtOutput.Text = "";//页面显示情kong
}
private void button_div_Click(object sender, EventArgs e)
{
//除法的实现,统一使用操作符实现
}
private void button_mal_Click(object sender, EventArgs e)
{
//乘法的实现,统一使用操作符
}
private void button_sub_Click(object sender, EventArgs e)
{
//减法的实现
}
private void button_add_Click(object sender, EventArgs e)
{
//加法的实现
}
private void button_num7_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num8_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num9_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num4_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num5_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num6_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num1_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num2_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_num3_Click(object sender, EventArgs e)
{
//数字功能的实现,统一使用数字操作函数实现
}
private void button_dot_Click(object sender, EventArgs e)
{
if(!bDotClicked)
{
strOutput += ".";
txtOutput.Text = strOutput;
bDotClicked = true;
}
}
private void button_enter_Click(object sender, EventArgs e)
{
try
{
switch (cOperator)
{
case '+':
checked
{
IResult = INumFormer + INumTemp;
}
break;
case '-':
checked
{
IResult = INumFormer - INumTemp;
}
break;
case '*':
checked
{
IResult = INumFormer * INumTemp;
}
break;
case '/':
checked
{
IResult = INumFormer / INumTemp;
}
break;
default:
break;
}
}
catch (Exception)
{
MessageBox.Show("计算结果溢出");
}
txtOutput.Text = IResult.ToString();
INumFormer = 0;
INumTemp = 0;
IResult = 0;
cOperator = '\0';
bDotClicked = false;
lastDecimalNum = 1;
}
//集中处理按钮点击事件
private void Numbers_Click(object senders,EventArgs e)
{
string strClickNum = ((Button)senders).Text;
try
{
if(bDotClicked)
{
lastDecimalNum *= 0.1;
//得出点击的小数数值
checked
{
INumTemp = INumTemp + long.Parse(strClickNum) * lastDecimalNum;
}
}
else
{
checked
{
INumTemp = INumTemp * 10 + long.Parse(strClickNum);
}
}
}
catch (Exception)
{
MessageBox.Show("数据溢出");
}
txtOutput.Text = INumTemp.ToString();
strOutput += strClickNum;
txtOutput.Text = strOutput;
}
//操作符按钮点击事件
private void Operators_Click(object senders, EventArgs e)
{
string strClickOp = ((Button)senders).Text;
cOperator = strClickOp[0];
INumFormer = INumTemp;
INumTemp = 0;
strOutput += cOperator.ToString();
txtOutput.Text = strOutput;
bDotClicked = false;
lastDecimalNum = 1;
}
protected override bool ProcessCmdKey(ref Message msg,Keys keyData)
{
switch (keyData)
{
case Keys.NumPad0:
button_num0.Focus();
button_num0.PerformClick();
return true;
case Keys.NumPad1:
button_num1.Focus();
button_num1.PerformClick();
return true;
case Keys.NumPad2:
button_num2.Focus();
button_num2.PerformClick();
return true;
case Keys.NumPad3:
button_num3.Focus();
button_num3.PerformClick();
return true;
case Keys.NumPad4:
button_num4.Focus();
button_num4.PerformClick();
return true;
case Keys.NumPad5:
button_num5.Focus();
button_num5.PerformClick();
return true;
case Keys.NumPad6:
button_num6.Focus();
button_num6.PerformClick();
return true;
case Keys.NumPad7:
button_num7.Focus();
button_num7.PerformClick();
return true;
case Keys.NumPad8:
button_num8.Focus();
button_num8.PerformClick();
return true;
case Keys.NumPad9:
button_num9.Focus();
button_num9.PerformClick();
return true;
case Keys.Add:
button_add.Focus();
button_add.PerformClick();
return true;
case Keys.Subtract:
button_sub.Focus();
button_sub.PerformClick();
return true;
case Keys.Multiply:
button_mal.Focus();
button_mal.PerformClick();
return true;
case Keys.Divide:
button_div.Focus();
button_div.PerformClick();
return true;
case Keys.Enter:
button_enter.Focus();
button_enter.PerformClick();
return true;
case Keys.Escape:
button_CE.Focus();
button_CE.PerformClick();
return true;
case Keys.Decimal:
button_dot.Focus();
button_dot.PerformClick();
return true;
default:
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
}