using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data; namespace calculator
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1: System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button17;
private System.Windows.Forms.Button button18;
private System.Windows.Forms.Button button19;
private System.Windows.Forms.Button button20;
private System.Windows.Forms.Button button21;
private System.Windows.Forms.Button button23;
private System.Windows.Forms.Button button24;
private System.Windows.Forms.Button button25;
private System.Windows.Forms.Button button26;
private System.Windows.Forms.Button button27; Double dblAcc;
Double dblSec;
bool blnClear,blnFrstOpen;
String strOper; //操作符字符串 /// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
dblAcc=0;
dblSec=0;
blnClear=true;
blnFrstOpen=true;
//strOper=new string("");
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = 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.button11 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.button18 = new System.Windows.Forms.Button();
this.button19 = new System.Windows.Forms.Button();
this.button20 = new System.Windows.Forms.Button();
this.button21 = new System.Windows.Forms.Button();
this.button23 = new System.Windows.Forms.Button();
this.button24 = new System.Windows.Forms.Button();
this.button25 = new System.Windows.Forms.Button();
this.button26 = new System.Windows.Forms.Button();
this.button27 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 16);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.textBox1.Size = new System.Drawing.Size(224, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 32);
this.button2.TabIndex = 2;
this.button2.Text = "CE";
//
// button3
//
this.button3.Location = new System.Drawing.Point(128, 48);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(104, 32);
this.button3.TabIndex = 3;
this.button3.Text = "C";
this.button3.Click += new System.EventHandler(this.btn_clr);
//
// button5
//
this.button5.Location = new System.Drawing.Point(8, 88);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(32, 24);
this.button5.TabIndex = 5;
this.button5.Text = "7";
this.button5.Click += new System.EventHandler(this.btn_clk);
//
// button6
//
this.button6.Location = new System.Drawing.Point(56, 88);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(32, 24);
this.button6.TabIndex = 6;
this.button6.Text = "8";
this.button6.Click += new System.EventHandler(this.btn_clk);
//
// button7
//
this.button7.Location = new System.Drawing.Point(104, 88);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(32, 24);
this.button7.TabIndex = 7;
this.button7.Text = "9";
this.button7.Click += new System.EventHandler(this.btn_clk);
//
// button8
//
this.button8.Location = new System.Drawing.Point(152, 88);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(32, 23);
this.button8.TabIndex = 8;
this.button8.Text = "/";
this.button8.Click += new System.EventHandler(this.btn_Oper);
//
// button9
//
this.button9.Location = new System.Drawing.Point(200, 88);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(32, 24);
this.button9.TabIndex = 9;
this.button9.Text = "sqr";
this.button9.Click += new System.EventHandler(this.btn_Oper);
//
// button11
//
this.button11.Location = new System.Drawing.Point(8, 120);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(32, 24);
this.button11.TabIndex = 11;
this.button11.Text = "4";
this.button11.Click += new System.EventHandler(this.btn_clk);
//
// button12
//
this.button12.Location = new System.Drawing.Point(56, 120);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(32, 24);
this.button12.TabIndex = 12;
this.button12.Text = "5";
this.button12.Click += new System.EventHandler(this.btn_clk);
//
// button13
//
this.button13.Location = new System.Drawing.Point(104, 120);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(32, 23);
this.button13.TabIndex = 13;
this.button13.Text = "6";
this.button13.Click += new System.EventHandler(this.btn_clk);
//
// button14
//
this.button14.Location = new System.Drawing.Point(152, 120);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(32, 24);
this.button14.TabIndex = 14;
this.button14.Text = "*";
this.button14.Click += new System.EventHandler(this.btn_Oper);
//
// button15
//
this.button15.Location = new System.Drawing.Point(200, 120);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(32, 24);
this.button15.TabIndex = 15;
this.button15.Text = "%";
//
// button17
//
this.button17.Location = new System.Drawing.Point(8, 152);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(32, 24);
this.button17.TabIndex = 17;
this.button17.Text = "1";
this.button17.Click += new System.EventHandler(this.btn_clk);
//
// button18
//
this.button18.Location = new System.Drawing.Point(56, 152);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(32, 24);
this.button18.TabIndex = 18;
this.button18.Text = "2";
this.button18.Click += new System.EventHandler(this.btn_clk);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
//判断字符的输入
private void btn_clk(object obj,EventArgs ea)
{
if(blnClear)textBox1.Text="";
Button b1=(Button)obj;
textBox1.Text+=b1.Text;
if(textBox1.Text==".")textBox1.Text="0.";
dblSec=Convert.ToDouble(textBox1.Text);
blnClear=false;
}
static void Main()
{
Application.Run(new Form1());
}
private void btn_Oper(object obj,EventArgs ea)
{
Button b2=(Button)obj;
strOper=b2.Text;
if(blnFrstOpen)dblAcc=dblSec;
else calc();
blnFrstOpen=false;
blnClear=true;
}
//等号运算
private void btn_equ(object obj,EventArgs ea)
{
calc();
}
//所有运算操作
private void calc()
{
switch(strOper)
{
case "+":
dblAcc+=dblSec; //加号运算
break;
case "-":
dblAcc-=dblSec; //减号运算
break;
case "*":
dblAcc*=dblSec; //乘号运算
break;
case "/":
dblAcc/=dblSec; //除号运算
break;
case "sqr":
dblAcc=Math.Sqrt(dblAcc); //开方运算
break;
case "%":
dblAcc%=dblSec; //余数运算
break;
case "+/-":
dblAcc=(-1)*dblAcc; //取负数运算
break;
case "1/x":
dblAcc=1/dblAcc; //余数运算
break;
}
strOper="="; //等号运算
blnFrstOpen=true;
textBox1.Text=Convert.ToString(dblAcc);//将运算结果转换成字符类型,并输出
dblSec=dblAcc;//将运算数A的值放入运算数B中,以便后面运算
}
//清除按钮
private void btn_clr(object obj,EventArgs ea)
{
clear();
}
//清除按钮的操作
private void clear()
{
dblAcc=0;
dblSec=0;
blnFrstOpen=true;
textBox1.Text="";
textBox1.Focus(); //设置焦点为txtCalc
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data; namespace calculator
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1: System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button17;
private System.Windows.Forms.Button button18;
private System.Windows.Forms.Button button19;
private System.Windows.Forms.Button button20;
private System.Windows.Forms.Button button21;
private System.Windows.Forms.Button button23;
private System.Windows.Forms.Button button24;
private System.Windows.Forms.Button button25;
private System.Windows.Forms.Button button26;
private System.Windows.Forms.Button button27; Double dblAcc;
Double dblSec;
bool blnClear,blnFrstOpen;
String strOper; //操作符字符串 /// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
dblAcc=0;
dblSec=0;
blnClear=true;
blnFrstOpen=true;
//strOper=new string("");
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = 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.button11 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.button18 = new System.Windows.Forms.Button();
this.button19 = new System.Windows.Forms.Button();
this.button20 = new System.Windows.Forms.Button();
this.button21 = new System.Windows.Forms.Button();
this.button23 = new System.Windows.Forms.Button();
this.button24 = new System.Windows.Forms.Button();
this.button25 = new System.Windows.Forms.Button();
this.button26 = new System.Windows.Forms.Button();
this.button27 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 16);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.textBox1.Size = new System.Drawing.Size(224, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 32);
this.button2.TabIndex = 2;
this.button2.Text = "CE";
//
// button3
//
this.button3.Location = new System.Drawing.Point(128, 48);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(104, 32);
this.button3.TabIndex = 3;
this.button3.Text = "C";
this.button3.Click += new System.EventHandler(this.btn_clr);
//
// button5
//
this.button5.Location = new System.Drawing.Point(8, 88);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(32, 24);
this.button5.TabIndex = 5;
this.button5.Text = "7";
this.button5.Click += new System.EventHandler(this.btn_clk);
//
// button6
//
this.button6.Location = new System.Drawing.Point(56, 88);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(32, 24);
this.button6.TabIndex = 6;
this.button6.Text = "8";
this.button6.Click += new System.EventHandler(this.btn_clk);
//
// button7
//
this.button7.Location = new System.Drawing.Point(104, 88);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(32, 24);
this.button7.TabIndex = 7;
this.button7.Text = "9";
this.button7.Click += new System.EventHandler(this.btn_clk);
//
// button8
//
this.button8.Location = new System.Drawing.Point(152, 88);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(32, 23);
this.button8.TabIndex = 8;
this.button8.Text = "/";
this.button8.Click += new System.EventHandler(this.btn_Oper);
//
// button9
//
this.button9.Location = new System.Drawing.Point(200, 88);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(32, 24);
this.button9.TabIndex = 9;
this.button9.Text = "sqr";
this.button9.Click += new System.EventHandler(this.btn_Oper);
//
// button11
//
this.button11.Location = new System.Drawing.Point(8, 120);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(32, 24);
this.button11.TabIndex = 11;
this.button11.Text = "4";
this.button11.Click += new System.EventHandler(this.btn_clk);
//
// button12
//
this.button12.Location = new System.Drawing.Point(56, 120);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(32, 24);
this.button12.TabIndex = 12;
this.button12.Text = "5";
this.button12.Click += new System.EventHandler(this.btn_clk);
//
// button13
//
this.button13.Location = new System.Drawing.Point(104, 120);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(32, 23);
this.button13.TabIndex = 13;
this.button13.Text = "6";
this.button13.Click += new System.EventHandler(this.btn_clk);
//
// button14
//
this.button14.Location = new System.Drawing.Point(152, 120);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(32, 24);
this.button14.TabIndex = 14;
this.button14.Text = "*";
this.button14.Click += new System.EventHandler(this.btn_Oper);
//
// button15
//
this.button15.Location = new System.Drawing.Point(200, 120);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(32, 24);
this.button15.TabIndex = 15;
this.button15.Text = "%";
//
// button17
//
this.button17.Location = new System.Drawing.Point(8, 152);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(32, 24);
this.button17.TabIndex = 17;
this.button17.Text = "1";
this.button17.Click += new System.EventHandler(this.btn_clk);
//
// button18
//
this.button18.Location = new System.Drawing.Point(56, 152);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(32, 24);
this.button18.TabIndex = 18;
this.button18.Text = "2";
this.button18.Click += new System.EventHandler(this.btn_clk);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
//判断字符的输入
private void btn_clk(object obj,EventArgs ea)
{
if(blnClear)textBox1.Text="";
Button b1=(Button)obj;
textBox1.Text+=b1.Text;
if(textBox1.Text==".")textBox1.Text="0.";
dblSec=Convert.ToDouble(textBox1.Text);
blnClear=false;
}
static void Main()
{
Application.Run(new Form1());
}
private void btn_Oper(object obj,EventArgs ea)
{
Button b2=(Button)obj;
strOper=b2.Text;
if(blnFrstOpen)dblAcc=dblSec;
else calc();
blnFrstOpen=false;
blnClear=true;
}
//等号运算
private void btn_equ(object obj,EventArgs ea)
{
calc();
}
//所有运算操作
private void calc()
{
switch(strOper)
{
case "+":
dblAcc+=dblSec; //加号运算
break;
case "-":
dblAcc-=dblSec; //减号运算
break;
case "*":
dblAcc*=dblSec; //乘号运算
break;
case "/":
dblAcc/=dblSec; //除号运算
break;
case "sqr":
dblAcc=Math.Sqrt(dblAcc); //开方运算
break;
case "%":
dblAcc%=dblSec; //余数运算
break;
case "+/-":
dblAcc=(-1)*dblAcc; //取负数运算
break;
case "1/x":
dblAcc=1/dblAcc; //余数运算
break;
}
strOper="="; //等号运算
blnFrstOpen=true;
textBox1.Text=Convert.ToString(dblAcc);//将运算结果转换成字符类型,并输出
dblSec=dblAcc;//将运算数A的值放入运算数B中,以便后面运算
}
//清除按钮
private void btn_clr(object obj,EventArgs ea)
{
clear();
}
//清除按钮的操作
private void clear()
{
dblAcc=0;
dblSec=0;
blnFrstOpen=true;
textBox1.Text="";
textBox1.Focus(); //设置焦点为txtCalc
}
}
}
// button19
//
this.button19.Location = new System.Drawing.Point(104, 152);
this.button19.Name = "button19";
this.button19.Size = new System.Drawing.Size(32, 24);
this.button19.TabIndex = 19;
this.button19.Text = "3";
this.button19.Click += new System.EventHandler(this.btn_clk);
//
// button20
//
this.button20.Location = new System.Drawing.Point(152, 152);
this.button20.Name = "button20";
this.button20.Size = new System.Drawing.Size(32, 24);
this.button20.TabIndex = 20;
this.button20.Text = "-";
this.button20.Click += new System.EventHandler(this.btn_Oper);
//
// button21
//
this.button21.Location = new System.Drawing.Point(200, 152);
this.button21.Name = "button21";
this.button21.Size = new System.Drawing.Size(32, 24);
this.button21.TabIndex = 21;
this.button21.Text = "1/x";
this.button21.Click += new System.EventHandler(this.btn_Oper);
//
// button23
//
this.button23.Location = new System.Drawing.Point(8, 184);
this.button23.Name = "button23";
this.button23.Size = new System.Drawing.Size(32, 24);
this.button23.TabIndex = 23;
this.button23.Text = "0";
this.button23.Click += new System.EventHandler(this.btn_clk);
//
// button24
//
this.button24.Location = new System.Drawing.Point(56, 184);
this.button24.Name = "button24";
this.button24.Size = new System.Drawing.Size(32, 24);
this.button24.TabIndex = 24;
this.button24.Text = "+/-";
this.button24.Click += new System.EventHandler(this.btn_Oper);
//
// button25
//
this.button25.Location = new System.Drawing.Point(104, 184);
this.button25.Name = "button25";
this.button25.Size = new System.Drawing.Size(32, 24);
this.button25.TabIndex = 25;
this.button25.Text = ".";
//
// button26
//
this.button26.Location = new System.Drawing.Point(152, 184);
this.button26.Name = "button26";
this.button26.Size = new System.Drawing.Size(32, 24);
this.button26.TabIndex = 26;
this.button26.Text = "+";
this.button26.Click += new System.EventHandler(this.btn_Oper);
//
// button27
//
this.button27.Location = new System.Drawing.Point(200, 184);
this.button27.Name = "button27";
this.button27.Size = new System.Drawing.Size(32, 24);
this.button27.TabIndex = 27;
this.button27.Text = "=";
this.button27.Click += new System.EventHandler(this.btn_equ);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(248, 213);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button27,
this.button26,
this.button25,
this.button24,
this.button23,
this.button21,
this.button20,
this.button19,
this.button18,
this.button17,
this.button15,
this.button14,
this.button13,
this.button12,
this.button11,
this.button9,
this.button8,
this.button7,
this.button6,
this.button5,
this.button3,
this.button2,
this.textBox1});
this.Name = "Form1";
this.Text = "计算器";
this.ResumeLayout(false); }
#endregion 这个接在 button18 那里....
百度上的..
http://zhidao.baidu.com/question/93382678.html