#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button0 = new System.Windows.Forms.Button();
this.addButton = new System.Windows.Forms.Button();
this.subButton = new System.Windows.Forms.Button();
this.mulButton = new System.Windows.Forms.Button();
this.divButton = new System.Windows.Forms.Button();
this.conButton = new System.Windows.Forms.Button();
this.doneButton = new System.Windows.Forms.Button();
this.clearButton = new System.Windows.Forms.Button();
this.operatorLabel = new System.Windows.Forms.Label();
this.txtOutput = new System.Windows.Forms.Label();
this.asButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(32, 24);
this.button1.TabIndex = 1;
this.button1.Text = "1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(64, 80);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(32, 24);
this.button2.TabIndex = 2;
this.button2.Text = "2";
//
// button3
//
this.button3.Location = new System.Drawing.Point(104, 80);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(32, 24);
this.button3.TabIndex = 3;
this.button3.Text = "3";
//
// button4
//
this.button4.Location = new System.Drawing.Point(24, 112);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(32, 24);
this.button4.TabIndex = 4;
this.button4.Text = "4";
//
// button5
//
this.button5.Location = new System.Drawing.Point(64, 112);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(32, 24);
this.button5.TabIndex = 5;
this.button5.Text = "5";
//
// button6
//
this.button6.Location = new System.Drawing.Point(104, 112);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(32, 24);
this.button6.TabIndex = 6;
this.button6.Text = "6";
//
// button7
//
this.button7.Location = new System.Drawing.Point(24, 144);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(32, 24);
this.button7.TabIndex = 7;
this.button7.Text = "7";
//
// button8
//
this.button8.Location = new System.Drawing.Point(64, 144);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(32, 24);
this.button8.TabIndex = 8;
this.button8.Text = "8";
//
// button9
//
this.button9.Location = new System.Drawing.Point(104, 144);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(32, 24);
this.button9.TabIndex = 9;
this.button9.Text = "9";
//
// button0
//
this.button0.Location = new System.Drawing.Point(24, 176);
this.button0.Name = "button0";
this.button0.Size = new System.Drawing.Size(32, 23);
this.button0.TabIndex = 10;
this.button0.Text = "0";
//
// addButton
//
this.addButton.Location = new System.Drawing.Point(144, 80);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(48, 24);
this.addButton.TabIndex = 11;
this.addButton.Text = "+";
//
// subButton
//
this.subButton.Location = new System.Drawing.Point(144, 112);
this.subButton.Name = "subButton";
this.subButton.Size = new System.Drawing.Size(48, 24);
this.subButton.TabIndex = 12;
this.subButton.Text = "-";
//
// mulButton
//
this.mulButton.Location = new System.Drawing.Point(144, 144);
this.mulButton.Name = "mulButton";
this.mulButton.Size = new System.Drawing.Size(48, 24);
this.mulButton.TabIndex = 13;
this.mulButton.Text = "*";
//
// divButton
//
this.divButton.Location = new System.Drawing.Point(144, 176);
this.divButton.Name = "divButton";
this.divButton.Size = new System.Drawing.Size(48, 24);
this.divButton.TabIndex = 14;
this.divButton.Text = "/";
//
// conButton
//
this.conButton.Location = new System.Drawing.Point(104, 176);
this.conButton.Name = "conButton";
this.conButton.Size = new System.Drawing.Size(32, 24);
this.conButton.TabIndex = 1;
this.conButton.Text = "=";
//
// doneButton
//
this.doneButton.Location = new System.Drawing.Point(64, 176);
this.doneButton.Name = "doneButton";
this.doneButton.Size = new System.Drawing.Size(32, 24);
this.doneButton.TabIndex = 16;
this.doneButton.Text = ".";
this.doneButton.Click += new System.EventHandler(this.doneButton_Click);
//
// clearButton
//
this.clearButton.ForeColor = System.Drawing.Color.Red;
this.clearButton.Location = new System.Drawing.Point(24, 208);
this.clearButton.Name = "clearButton";
this.clearButton.Size = new System.Drawing.Size(112, 23);
this.clearButton.TabIndex = 17;
this.clearButton.Text = "C";
this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
//
// operatorLabel
//
this.operatorLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.operatorLabel.Location = new System.Drawing.Point(24, 48);
this.operatorLabel.Name = "operatorLabel";
this.operatorLabel.Size = new System.Drawing.Size(32, 23);
this.operatorLabel.TabIndex = 18;
this.operatorLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtOutput
//
this.txtOutput.BackColor = System.Drawing.Color.White;
this.txtOutput.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.txtOutput.Location = new System.Drawing.Point(24, 16);
this.txtOutput.Name = "txtOutput";
this.txtOutput.Size = new System.Drawing.Size(168, 23);
this.txtOutput.TabIndex = 19;
this.txtOutput.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// asButton
//
this.asButton.Location = new System.Drawing.Point(144, 208);
this.asButton.Name = "asButton";
this.asButton.Size = new System.Drawing.Size(48, 23);
this.asButton.TabIndex = 20;
this.asButton.Text = "+/-";
this.asButton.Click += new System.EventHandler(this.asButton_Click);
//
// Calculate
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(216, 245);
this.Controls.Add(this.asButton);
this.Controls.Add(this.txtOutput);
this.Controls.Add(this.operatorLabel);
this.Controls.Add(this.clearButton);
this.Controls.Add(this.doneButton);
this.Controls.Add(this.conButton);
this.Controls.Add(this.divButton);
this.Controls.Add(this.mulButton);
this.Controls.Add(this.subButton);
this.Controls.Add(this.addButton);
this.Controls.Add(this.button0);
this.Controls.Add(this.button9);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Calculate";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "计算器";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Calculate());
}
//数字按钮事件方法
private void Number_Click(Object sender,EventArgs e)
{
double iCur,i; //用于记录文本框中的数字及用户点击的按钮(事件源)数字
if(txtOutput.Text=="Error")
txtOutput.Text="0";
if(bDone) //如果用户点击了小数点
iDone++; //记录小数点后的位数
try
{
iCur=double.Parse(txtOutput.Text); //得到文本框中的数字
i=double.Parse(((Button)sender).Text); //得到按钮(事件源)的标签
//如果是第一次点击数字
if(bNumBegins)
{
iCur=i; //存储用户点击的按钮(事件源)的标签
bNumBegins=false;
}
else
{
if(iCur>=0)
{
//如果小数点后位数为0,说明用户输入的是整数
if(iDone==0)
checked{iCur=(iCur*10)+i;}
else
{
long iC=1;
for(long u=1;u<=iDone;u++)
iC*=10; //得到小数点后的位数的倍数,即有几位则除几个10
checked{iCur=iCur+i/iC;}
}
}
else
{
InitNumber();
bNumBegins=false;
txtOutput.Text=i.ToString();
return;
}
}
txtOutput.Text=iCur.ToString();
}
catch{}
}
private void allMouse_Up(Object sender,MouseEventArgs e)
{
conButton.Focus();
}
//所有按钮的键盘事件
private void allKeys_Down(Object sender,KeyEventArgs e)
{
switch(e.KeyValue)
{
case 96:
Number_Click(button0,e);
break;
case 97:
Number_Click(button1,e);
break;
case 98:
Number_Click(button2,e);
break;
case 99:
Number_Click(button3,e);
break;
case 100:
Number_Click(button4,e);
break;
case 101:
Number_Click(button5,e);
break;
case 102:
Number_Click(button6,e);
break;
case 103:
Number_Click(button7,e);
break;
case 104:
Number_Click(button8,e);
break;
case 105:
Number_Click(button9,e);
break;
case 107:
Operators_Click(addButton,e);
break;
case 109:
Operators_Click(subButton,e);
break;
case 106:
Operators_Click(mulButton,e);
break;
case 111:
Operators_Click(divButton,e);
break;
case 110:
doneButton_Click(doneButton,e);
break;
case 13:
Operators_Click(conButton,e);
break;
case 67:
clearButton_Click(clearButton,e);
break;
}
}
//符号按钮的事件方法
private void Operators_Click(Object sender,EventArgs e)
{
double iCur,iResult; //得到文本框中的数字及计算结果
char op=((Button)sender).Text[0]; //得到按钮(事件源)的标签,由于标签为字符串,所以要得到该字符串的第一个字符Text[0]
try
{
iCur=double.Parse(txtOutput.Text);
}
catch
{
txtOutput.Text="Error";
InitNumber();
return;
}
try
{
//遍历符号
switch(cOperators)
{
case '+':
checked{iResult=dNum+iCur;}
break;
case '-':
checked{iResult=dNum-iCur;}
break;
case '*':
checked{iResult=dNum*iCur;}
break;
case '/':
checked{iResult=dNum/iCur;}
break;
//以上都不是,所以肯定是等于号
default:
iResult=iCur;
break;
}
txtOutput.Text=iResult.ToString();
dNum=iResult;
cOperators=op;
bNumBegins=true;
bDone=false;
iDone=0;
operatorLabel.Text=cOperators.ToString();
}
catch
{
txtOutput.Text="Error";
InitNumber();
return;
}
}
//初始化变量及控件
private void InitNumber()
{
dNum=0;
txtOutput.Text="0";
bNumBegins=true;
cOperators='=';
iDone=0;
bDone=false;
operatorLabel.Text="";
}
//小数点按钮的事件
private void doneButton_Click(object sender, System.EventArgs e)
{
//看文本框中的数字是否已有小数点,如果没有则执行
if(txtOutput.Text.IndexOf(".")==-1)
{
txtOutput.Text+=".";
bDone=true; //已点击小数点
bNumBegins=false; //第一个数设置为非
}
}
//清除按钮事件方法
private void clearButton_Click(object sender, System.EventArgs e)
{
InitNumber(); //回到初始状态
}
private void asButton_Click(object sender, System.EventArgs e)
{
try
{
double d=double.Parse(txtOutput.Text);
if(d!=0)
{
d=d*(-1);
}
txtOutput.Text=d.ToString();
}
catch
{
txtOutput.Text="Error";
InitNumber();
return;
}
}
}
}