C#计算器 (要求:一个textbox接受输入一个计算按钮一个显示结果的textbox)
接受的输入是一个表达式(其中能包括+-/*以及小括号甚至大括号之类的运算操作)注:只在button按钮下写代码...
接受的输入是一个表达式(其中能包括+ - / *以及小括号甚至大括号之类的运算操作)
注:只在button按钮下写代码 展开
注:只在button按钮下写代码 展开
7个回答
展开全部
你参考一下,收集的一个计算器.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace user
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtExp;
private System.Windows.Forms.Button btn1;
private System.Windows.Forms.Button btnReverse;
private System.Windows.Forms.Button btnDot;
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btn9;
private System.Windows.Forms.Button btn8;
private System.Windows.Forms.Button btn7;
private System.Windows.Forms.Button btn6;
private System.Windows.Forms.Button btn5;
private System.Windows.Forms.Button btn4;
private System.Windows.Forms.Button btn3;
private System.Windows.Forms.Button btn2;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnMinus;
private System.Windows.Forms.Button btnMutiply;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnBak;
private System.Windows.Forms.Button btnRec;
private System.Windows.Forms.Button btnRemain;
private System.Windows.Forms.Button btnPower;
private System.Windows.Forms.Button btnSqrt;
private System.Windows.Forms.Button btnEqual;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtExp = new System.Windows.Forms.TextBox();
this.btn1 = new System.Windows.Forms.Button();
this.btnReverse = new System.Windows.Forms.Button();
this.btnDot = new System.Windows.Forms.Button();
this.btn0 = new System.Windows.Forms.Button();
this.btn9 = new System.Windows.Forms.Button();
this.btn8 = new System.Windows.Forms.Button();
this.btn7 = new System.Windows.Forms.Button();
this.btn6 = new System.Windows.Forms.Button();
this.btn5 = new System.Windows.Forms.Button();
this.btn4 = new System.Windows.Forms.Button();
this.btn3 = new System.Windows.Forms.Button();
this.btn2 = new System.Windows.Forms.Button();
this.btnReset = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnMinus = new System.Windows.Forms.Button();
this.btnRec = new System.Windows.Forms.Button();
this.btnMutiply = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnRemain = new System.Windows.Forms.Button();
this.btnPower = new System.Windows.Forms.Button();
this.btnSqrt = new System.Windows.Forms.Button();
this.btnEqual = new System.Windows.Forms.Button();
this.btnBak = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtExp
//
this.txtExp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtExp.Location = new System.Drawing.Point(8, 8);
this.txtExp.Name = "txtExp";
this.txtExp.Size = new System.Drawing.Size(224, 21);
this.txtExp.TabIndex = 0;
this.txtExp.Text = "";
this.txtExp.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.txtExp.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtExp_KeyPress);
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(8, 40);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(32, 23);
this.btn1.TabIndex = 1;
this.btn1.Text = "1";
this.btn1.Click += new System.EventHandler(this.btnOperand_Click);
//
// btnReverse
//
this.btnReverse.Location = new System.Drawing.Point(72, 112);
this.btnReverse.Name = "btnReverse";
this.btnReverse.Size = new System.Drawing.Size(32, 23);
this.btnReverse.TabIndex = 2;
this.btnReverse.Text = "+/-";
this.btnReverse.Click += new System.EventHandler(this.btnSingleOper_Click);
//
// btnDot
//
this.btnDot.Location = new System.Drawing.Point(40, 112);
this.btnDot.Name = "btnDot";
this.btnDot.Size = new System.Drawing.Size(32, 23);
this.btnDot.TabIndex = 3;
this.btnDot.Text = ".";
this.btnDot.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn0
//
this.btn0.Location = new System.Drawing.Point(8, 112);
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(32, 23);
this.btn0.TabIndex = 4;
this.btn0.Text = "0";
this.btn0.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn9
//
this.btn9.Location = new System.Drawing.Point(72, 88);
this.btn9.Name = "btn9";
this.btn9.Size = new System.Drawing.Size(32, 23);
this.btn9.TabIndex = 5;
this.btn9.Text = "9";
this.btn9.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn8
//
this.btn8.Location = new System.Drawing.Point(40, 88);
this.btn8.Name = "btn8";
this.btn8.Size = new System.Drawing.Size(32, 23);
this.btn8.TabIndex = 6;
this.btn8.Text = "8";
this.btn8.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn7
//
this.btn7.Location = new System.Drawing.Point(8, 88);
this.btn7.Name = "btn7";
this.btn7.Size = new System.Drawing.Size(32, 23);
this.btn7.TabIndex = 7;
this.btn7.Text = "7";
this.btn7.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn6
//
this.btn6.Location = new System.Drawing.Point(72, 64);
this.btn6.Name = "btn6";
this.btn6.Size = new System.Drawing.Size(32, 23);
this.btn6.TabIndex = 8;
this.btn6.Text = "6";
this.btn6.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn5
//
this.btn5.Location = new System.Drawing.Point(40, 64);
this.btn5.Name = "btn5";
this.btn5.Size = new System.Drawing.Size(32, 23);
this.btn5.TabIndex = 9;
this.btn5.Text = "5";
this.btn5.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn4
//
this.btn4.Location = new System.Drawing.Point(8, 64);
this.btn4.Name = "btn4";
this.btn4.Size = new System.Drawing.Size(32, 23);
this.btn4.TabIndex = 10;
this.btn4.Text = "4";
this.btn4.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn3
//
this.btn3.Location = new System.Drawing.Point(72, 40);
this.btn3.Name = "btn3";
this.btn3.Size = new System.Drawing.Size(32, 23);
this.btn3.TabIndex = 11;
this.btn3.Text = "3";
this.btn3.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn2
//
this.btn2.Location = new System.Drawing.Point(40, 40);
this.btn2.Name = "btn2";
this.btn2.Size = new System.Drawing.Size(32, 23);
this.btn2.TabIndex = 12;
this.btn2.Text = "2";
this.btn2.Click += new System.EventHandler(this.btnOperand_Click);
//
// btnReset
//
this.btnReset.Location = new System.Drawing.Point(192, 40);
this.btnReset.Name = "btnReset";
this.btnReset.Size = new System.Drawing.Size(40, 23);
this.btnReset.TabIndex = 23;
this.btnReset.Text = "C";
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(112, 64);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(40, 23);
this.btnAdd.TabIndex = 22;
this.btnAdd.Text = "+";
this.btnAdd.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnMinus
//
this.btnMinus.Location = new System.Drawing.Point(152, 64);
this.btnMinus.Name = "btnMinus";
this.btnMinus.Size = new System.Drawing.Size(40, 23);
this.btnMinus.TabIndex = 21;
this.btnMinus.Text = "-";
this.btnMinus.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnRec
//
this.btnRec.Location = new System.Drawing.Point(192, 64);
this.btnRec.Name = "btnRec";
this.btnRec.Size = new System.Drawing.Size(40, 23);
this.btnRec.TabIndex = 20;
this.btnRec.Text = "1/x";
this.btnRec.Click += new System.EventHandler(this.btnSingleOper_Click);
//
// btnMutiply
//
this.btnMutiply.Location = new System.Drawing.Point(112, 88);
this.btnMutiply.Name = "btnMutiply";
this.btnMutiply.Size = new System.Drawing.Size(40, 23);
this.btnMutiply.TabIndex = 19;
this.btnMutiply.Text = "*";
this.btnMutiply.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnDivide
//
this.btnDivide.Location = new System.Drawing.Point(152, 88);
this.btnDivide.Name = "btnDivide";
this.btnDivide.Size = new System.Drawing.Size(40, 23);
this.btnDivide.TabIndex = 18;
this.btnDivide.Text = "/";
this.btnDivide.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnRemain
//
this.btnRemain.Location = new System.Drawing.Point(192, 88);
this.btnRemain.Name = "btnRemain";
this.btnRemain.Size = new System.Drawing.Size(40, 23);
this.btnRemain.TabIndex = 17;
this.btnRemain.Text = "%";
this.btnRemain.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnPower
//
this.btnPower.Location = new System.Drawing.Point(112, 112);
this.btnPower.Name = "btnPower";
this.btnPower.Size = new System.Drawing.Size(40, 23);
this.btnPower.TabIndex = 16;
this.btnPower.Text = "pow";
this.btnPower.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnSqrt
//
this.btnSqrt.Location = new System.Drawing.Point(152, 112);
this.btnSqrt.Name = "btnSqrt";
this.btnSqrt.Size = new System.Drawing.Size(40, 23);
this.btnSqrt.TabIndex = 15;
this.btnSqrt.Text = "sqrt";
this.btnSqrt.Click += new System.EventHandler(this.btnSingleOper_Click);
//
// btnEqual
//
this.btnEqual.Location = new System.Drawing.Point(192, 112);
this.btnEqual.Name = "btnEqual";
this.btnEqual.Size = new System.Drawing.Size(40, 23);
this.btnEqual.TabIndex = 14;
this.btnEqual.Text = "=";
this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
//
// btnBak
//
this.btnBak.Location = new System.Drawing.Point(112, 40);
this.btnBak.Name = "btnBak";
this.btnBak.Size = new System.Drawing.Size(80, 23);
this.btnBak.TabIndex = 13;
this.btnBak.Text = "Backspace";
this.btnBak.Click += new System.EventHandler(this.btnBak_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(242, 144);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnMinus);
this.Controls.Add(this.btnRec);
this.Controls.Add(this.btnMutiply);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnRemain);
this.Controls.Add(this.btnPower);
this.Controls.Add(this.btnSqrt);
this.Controls.Add(this.btnEqual);
this.Controls.Add(this.btnBak);
this.Controls.Add(this.btn2);
this.Controls.Add(this.btn3);
this.Controls.Add(this.btn4);
this.Controls.Add(this.btn5);
this.Controls.Add(this.btn6);
this.Controls.Add(this.btn7);
this.Controls.Add(this.btn8);
this.Controls.Add(this.btn9);
this.Controls.Add(this.btn0);
this.Controls.Add(this.btnDot);
this.Controls.Add(this.btnReverse);
this.Controls.Add(this.btn1);
this.Controls.Add(this.txtExp);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "计算器";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
int dotCount = 0;
private void btnOperand_Click(object sender, System.EventArgs e)
{
if(this.ActiveControl==this.btnDot && ++this.dotCount>1)
{
return;
}
this.txtExp.Text += this.ActiveControl.Text;
}
double operand = 0;
string sign = "";
private void btnOperator_Click(object sender, System.EventArgs e)
{
if(this.txtExp.Text!="")
{
this.operand = double.Parse(this.txtExp.Text);
}
this.sign = this.ActiveControl.Text;
this.txtExp.Clear();
}
private void btnEqual_Click(object sender, System.EventArgs e)
{
double result = 0;
switch(this.sign)
{
case "+":
result = this.operand + double.Parse(this.txtExp.Text);
break;
case "-":
result = this.operand - double.Parse(this.txtExp.Text);
break;
case "*":
result = this.operand * double.Parse(this.txtExp.Text);
break;
case "/":
result = this.operand / double.Parse(this.txtExp.Text);
break;
case "%":
result = this.operand % double.Parse(this.txtExp.Text);
break;
case "pow":
result = Math.Pow(this.operand, double.Parse(this.txtExp.Text));
break;
default:
MessageBox.Show("未知运算", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
this.txtExp.Text = result.ToString();
}
private void btnSingleOper_Click(object sender, System.EventArgs e)
{
if(this.ActiveControl==this.btnReverse)
{
this.txtExp.Text = (-double.Parse(this.txtExp.Text)).ToString();
}
else if(this.ActiveControl==this.btnRec)
{
this.txtExp.Text = (1/double.Parse(this.txtExp.Text)).ToString();
}
else if(this.ActiveControl==this.btnSqrt)
{
this.txtExp.Text = Convert.ToString(Math.Sqrt(double.Parse(this.txtExp.Text)));
}
}
private void btnBak_Click(object sender, System.EventArgs e)
{
this.txtExp.Text = this.txtExp.Text.Substring(0, this.txtExp.Text.Length-1);
}
private void btnReset_Click(object sender, System.EventArgs e)
{
this.operand = 0;
this.sign = "";
this.dotCount = 0;
this.txtExp.Clear();
}
private void txtExp_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar<'0' || e.KeyChar>'9')
{
e.Handled = true;
}
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace user
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtExp;
private System.Windows.Forms.Button btn1;
private System.Windows.Forms.Button btnReverse;
private System.Windows.Forms.Button btnDot;
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btn9;
private System.Windows.Forms.Button btn8;
private System.Windows.Forms.Button btn7;
private System.Windows.Forms.Button btn6;
private System.Windows.Forms.Button btn5;
private System.Windows.Forms.Button btn4;
private System.Windows.Forms.Button btn3;
private System.Windows.Forms.Button btn2;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnMinus;
private System.Windows.Forms.Button btnMutiply;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnBak;
private System.Windows.Forms.Button btnRec;
private System.Windows.Forms.Button btnRemain;
private System.Windows.Forms.Button btnPower;
private System.Windows.Forms.Button btnSqrt;
private System.Windows.Forms.Button btnEqual;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtExp = new System.Windows.Forms.TextBox();
this.btn1 = new System.Windows.Forms.Button();
this.btnReverse = new System.Windows.Forms.Button();
this.btnDot = new System.Windows.Forms.Button();
this.btn0 = new System.Windows.Forms.Button();
this.btn9 = new System.Windows.Forms.Button();
this.btn8 = new System.Windows.Forms.Button();
this.btn7 = new System.Windows.Forms.Button();
this.btn6 = new System.Windows.Forms.Button();
this.btn5 = new System.Windows.Forms.Button();
this.btn4 = new System.Windows.Forms.Button();
this.btn3 = new System.Windows.Forms.Button();
this.btn2 = new System.Windows.Forms.Button();
this.btnReset = new System.Windows.Forms.Button();
this.btnAdd = new System.Windows.Forms.Button();
this.btnMinus = new System.Windows.Forms.Button();
this.btnRec = new System.Windows.Forms.Button();
this.btnMutiply = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnRemain = new System.Windows.Forms.Button();
this.btnPower = new System.Windows.Forms.Button();
this.btnSqrt = new System.Windows.Forms.Button();
this.btnEqual = new System.Windows.Forms.Button();
this.btnBak = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtExp
//
this.txtExp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtExp.Location = new System.Drawing.Point(8, 8);
this.txtExp.Name = "txtExp";
this.txtExp.Size = new System.Drawing.Size(224, 21);
this.txtExp.TabIndex = 0;
this.txtExp.Text = "";
this.txtExp.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.txtExp.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtExp_KeyPress);
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(8, 40);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(32, 23);
this.btn1.TabIndex = 1;
this.btn1.Text = "1";
this.btn1.Click += new System.EventHandler(this.btnOperand_Click);
//
// btnReverse
//
this.btnReverse.Location = new System.Drawing.Point(72, 112);
this.btnReverse.Name = "btnReverse";
this.btnReverse.Size = new System.Drawing.Size(32, 23);
this.btnReverse.TabIndex = 2;
this.btnReverse.Text = "+/-";
this.btnReverse.Click += new System.EventHandler(this.btnSingleOper_Click);
//
// btnDot
//
this.btnDot.Location = new System.Drawing.Point(40, 112);
this.btnDot.Name = "btnDot";
this.btnDot.Size = new System.Drawing.Size(32, 23);
this.btnDot.TabIndex = 3;
this.btnDot.Text = ".";
this.btnDot.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn0
//
this.btn0.Location = new System.Drawing.Point(8, 112);
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(32, 23);
this.btn0.TabIndex = 4;
this.btn0.Text = "0";
this.btn0.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn9
//
this.btn9.Location = new System.Drawing.Point(72, 88);
this.btn9.Name = "btn9";
this.btn9.Size = new System.Drawing.Size(32, 23);
this.btn9.TabIndex = 5;
this.btn9.Text = "9";
this.btn9.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn8
//
this.btn8.Location = new System.Drawing.Point(40, 88);
this.btn8.Name = "btn8";
this.btn8.Size = new System.Drawing.Size(32, 23);
this.btn8.TabIndex = 6;
this.btn8.Text = "8";
this.btn8.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn7
//
this.btn7.Location = new System.Drawing.Point(8, 88);
this.btn7.Name = "btn7";
this.btn7.Size = new System.Drawing.Size(32, 23);
this.btn7.TabIndex = 7;
this.btn7.Text = "7";
this.btn7.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn6
//
this.btn6.Location = new System.Drawing.Point(72, 64);
this.btn6.Name = "btn6";
this.btn6.Size = new System.Drawing.Size(32, 23);
this.btn6.TabIndex = 8;
this.btn6.Text = "6";
this.btn6.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn5
//
this.btn5.Location = new System.Drawing.Point(40, 64);
this.btn5.Name = "btn5";
this.btn5.Size = new System.Drawing.Size(32, 23);
this.btn5.TabIndex = 9;
this.btn5.Text = "5";
this.btn5.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn4
//
this.btn4.Location = new System.Drawing.Point(8, 64);
this.btn4.Name = "btn4";
this.btn4.Size = new System.Drawing.Size(32, 23);
this.btn4.TabIndex = 10;
this.btn4.Text = "4";
this.btn4.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn3
//
this.btn3.Location = new System.Drawing.Point(72, 40);
this.btn3.Name = "btn3";
this.btn3.Size = new System.Drawing.Size(32, 23);
this.btn3.TabIndex = 11;
this.btn3.Text = "3";
this.btn3.Click += new System.EventHandler(this.btnOperand_Click);
//
// btn2
//
this.btn2.Location = new System.Drawing.Point(40, 40);
this.btn2.Name = "btn2";
this.btn2.Size = new System.Drawing.Size(32, 23);
this.btn2.TabIndex = 12;
this.btn2.Text = "2";
this.btn2.Click += new System.EventHandler(this.btnOperand_Click);
//
// btnReset
//
this.btnReset.Location = new System.Drawing.Point(192, 40);
this.btnReset.Name = "btnReset";
this.btnReset.Size = new System.Drawing.Size(40, 23);
this.btnReset.TabIndex = 23;
this.btnReset.Text = "C";
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(112, 64);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(40, 23);
this.btnAdd.TabIndex = 22;
this.btnAdd.Text = "+";
this.btnAdd.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnMinus
//
this.btnMinus.Location = new System.Drawing.Point(152, 64);
this.btnMinus.Name = "btnMinus";
this.btnMinus.Size = new System.Drawing.Size(40, 23);
this.btnMinus.TabIndex = 21;
this.btnMinus.Text = "-";
this.btnMinus.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnRec
//
this.btnRec.Location = new System.Drawing.Point(192, 64);
this.btnRec.Name = "btnRec";
this.btnRec.Size = new System.Drawing.Size(40, 23);
this.btnRec.TabIndex = 20;
this.btnRec.Text = "1/x";
this.btnRec.Click += new System.EventHandler(this.btnSingleOper_Click);
//
// btnMutiply
//
this.btnMutiply.Location = new System.Drawing.Point(112, 88);
this.btnMutiply.Name = "btnMutiply";
this.btnMutiply.Size = new System.Drawing.Size(40, 23);
this.btnMutiply.TabIndex = 19;
this.btnMutiply.Text = "*";
this.btnMutiply.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnDivide
//
this.btnDivide.Location = new System.Drawing.Point(152, 88);
this.btnDivide.Name = "btnDivide";
this.btnDivide.Size = new System.Drawing.Size(40, 23);
this.btnDivide.TabIndex = 18;
this.btnDivide.Text = "/";
this.btnDivide.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnRemain
//
this.btnRemain.Location = new System.Drawing.Point(192, 88);
this.btnRemain.Name = "btnRemain";
this.btnRemain.Size = new System.Drawing.Size(40, 23);
this.btnRemain.TabIndex = 17;
this.btnRemain.Text = "%";
this.btnRemain.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnPower
//
this.btnPower.Location = new System.Drawing.Point(112, 112);
this.btnPower.Name = "btnPower";
this.btnPower.Size = new System.Drawing.Size(40, 23);
this.btnPower.TabIndex = 16;
this.btnPower.Text = "pow";
this.btnPower.Click += new System.EventHandler(this.btnOperator_Click);
//
// btnSqrt
//
this.btnSqrt.Location = new System.Drawing.Point(152, 112);
this.btnSqrt.Name = "btnSqrt";
this.btnSqrt.Size = new System.Drawing.Size(40, 23);
this.btnSqrt.TabIndex = 15;
this.btnSqrt.Text = "sqrt";
this.btnSqrt.Click += new System.EventHandler(this.btnSingleOper_Click);
//
// btnEqual
//
this.btnEqual.Location = new System.Drawing.Point(192, 112);
this.btnEqual.Name = "btnEqual";
this.btnEqual.Size = new System.Drawing.Size(40, 23);
this.btnEqual.TabIndex = 14;
this.btnEqual.Text = "=";
this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
//
// btnBak
//
this.btnBak.Location = new System.Drawing.Point(112, 40);
this.btnBak.Name = "btnBak";
this.btnBak.Size = new System.Drawing.Size(80, 23);
this.btnBak.TabIndex = 13;
this.btnBak.Text = "Backspace";
this.btnBak.Click += new System.EventHandler(this.btnBak_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(242, 144);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.btnMinus);
this.Controls.Add(this.btnRec);
this.Controls.Add(this.btnMutiply);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnRemain);
this.Controls.Add(this.btnPower);
this.Controls.Add(this.btnSqrt);
this.Controls.Add(this.btnEqual);
this.Controls.Add(this.btnBak);
this.Controls.Add(this.btn2);
this.Controls.Add(this.btn3);
this.Controls.Add(this.btn4);
this.Controls.Add(this.btn5);
this.Controls.Add(this.btn6);
this.Controls.Add(this.btn7);
this.Controls.Add(this.btn8);
this.Controls.Add(this.btn9);
this.Controls.Add(this.btn0);
this.Controls.Add(this.btnDot);
this.Controls.Add(this.btnReverse);
this.Controls.Add(this.btn1);
this.Controls.Add(this.txtExp);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "计算器";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
int dotCount = 0;
private void btnOperand_Click(object sender, System.EventArgs e)
{
if(this.ActiveControl==this.btnDot && ++this.dotCount>1)
{
return;
}
this.txtExp.Text += this.ActiveControl.Text;
}
double operand = 0;
string sign = "";
private void btnOperator_Click(object sender, System.EventArgs e)
{
if(this.txtExp.Text!="")
{
this.operand = double.Parse(this.txtExp.Text);
}
this.sign = this.ActiveControl.Text;
this.txtExp.Clear();
}
private void btnEqual_Click(object sender, System.EventArgs e)
{
double result = 0;
switch(this.sign)
{
case "+":
result = this.operand + double.Parse(this.txtExp.Text);
break;
case "-":
result = this.operand - double.Parse(this.txtExp.Text);
break;
case "*":
result = this.operand * double.Parse(this.txtExp.Text);
break;
case "/":
result = this.operand / double.Parse(this.txtExp.Text);
break;
case "%":
result = this.operand % double.Parse(this.txtExp.Text);
break;
case "pow":
result = Math.Pow(this.operand, double.Parse(this.txtExp.Text));
break;
default:
MessageBox.Show("未知运算", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
this.txtExp.Text = result.ToString();
}
private void btnSingleOper_Click(object sender, System.EventArgs e)
{
if(this.ActiveControl==this.btnReverse)
{
this.txtExp.Text = (-double.Parse(this.txtExp.Text)).ToString();
}
else if(this.ActiveControl==this.btnRec)
{
this.txtExp.Text = (1/double.Parse(this.txtExp.Text)).ToString();
}
else if(this.ActiveControl==this.btnSqrt)
{
this.txtExp.Text = Convert.ToString(Math.Sqrt(double.Parse(this.txtExp.Text)));
}
}
private void btnBak_Click(object sender, System.EventArgs e)
{
this.txtExp.Text = this.txtExp.Text.Substring(0, this.txtExp.Text.Length-1);
}
private void btnReset_Click(object sender, System.EventArgs e)
{
this.operand = 0;
this.sign = "";
this.dotCount = 0;
this.txtExp.Clear();
}
private void txtExp_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar<'0' || e.KeyChar>'9')
{
e.Handled = true;
}
}
}
}
展开全部
如果你用MSScriptControl,就要在项目中引用---->系统盘:\WINDOWS\system32\msscript.ocx ,如果没有就需要下载msscript.ocx ,它是一个ActiveX(R) 控件。下载后,新建一个C#的Windows应用程序项目,在解决方案资源管理器中选中引用节点,右键点击选择添加引用菜单,弹出添加引用对话框,单击浏览找到msscript.ocx,选取文件确定。那么在引用节点下会增加一个MSScriptControl组件.
在C#的.cs里首先写上using MSScriptControl,然后就可以象下面的例子那么用拉:
private string totalresult(string str) //计算四则混合运算
{
try
{
ScriptControlClass comobj = new ScriptControlClass(); //MSScriptControl组件的 ScriptControlClass类
comobj.Language = "JavaScript"; //该.cs代码引用的Script语言
return comobj.Eval(str).ToString(); //计算表达式并返回结果
}
catch
{
return null;
}
}
或者你自己利用逆波兰思想写算法。楼上有位仁兄已经给出来了。其思想可参见
http://www-128.ibm.com/developerworks/cn/java/j-w3eva/
在C#的.cs里首先写上using MSScriptControl,然后就可以象下面的例子那么用拉:
private string totalresult(string str) //计算四则混合运算
{
try
{
ScriptControlClass comobj = new ScriptControlClass(); //MSScriptControl组件的 ScriptControlClass类
comobj.Language = "JavaScript"; //该.cs代码引用的Script语言
return comobj.Eval(str).ToString(); //计算表达式并返回结果
}
catch
{
return null;
}
}
或者你自己利用逆波兰思想写算法。楼上有位仁兄已经给出来了。其思想可参见
http://www-128.ibm.com/developerworks/cn/java/j-w3eva/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
很简单的
使用StringBuilder 这个去添加单击按钮的字符 如 按 1 就添加 1 进去
private void button1_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
StringBuilder builder = new StringBuilder();
builder.Append(btnSender.Text);
textBox1.Text=builder.ToString();
}
在把所有你要实现功能的按钮的Click事件弄成这个就行了
这样就可以实现了
使用StringBuilder 这个去添加单击按钮的字符 如 按 1 就添加 1 进去
private void button1_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
StringBuilder builder = new StringBuilder();
builder.Append(btnSender.Text);
textBox1.Text=builder.ToString();
}
在把所有你要实现功能的按钮的Click事件弄成这个就行了
这样就可以实现了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
So Easy
textBox1.Text=(结果就行了)
textBox1.Text=(结果就行了)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码写了一大堆 都是笨人用的方法
Windows已经集成了MSScriptControl
首先引用%SystemRoot%/System32下面的 %SystemRoot%\System32\msscript.ocx
假定你的TextBox名为Exp Button名Calc Button按下的时候激活的方法为Calc_Click
简单代码如下:
protected void Calc_Click(object sender, EventArgs args)
{
ScriptControlClass sc = new ScriptControlClass();
sc.Language = "javascript";
sc.UseSafeSubset = true;
sc.AllowUI = false;
sc.Timeout = 60;
try
{
object result = sc.Eval(Exp.Text);
if (result != null)
{
string resultStr= result.ToString();
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
//resultStr就是你要的结果
不管加减乘除大括号小括号多复杂 只要表达式正确 都不在话下
Windows已经集成了MSScriptControl
首先引用%SystemRoot%/System32下面的 %SystemRoot%\System32\msscript.ocx
假定你的TextBox名为Exp Button名Calc Button按下的时候激活的方法为Calc_Click
简单代码如下:
protected void Calc_Click(object sender, EventArgs args)
{
ScriptControlClass sc = new ScriptControlClass();
sc.Language = "javascript";
sc.UseSafeSubset = true;
sc.AllowUI = false;
sc.Timeout = 60;
try
{
object result = sc.Eval(Exp.Text);
if (result != null)
{
string resultStr= result.ToString();
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
//resultStr就是你要的结果
不管加减乘除大括号小括号多复杂 只要表达式正确 都不在话下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询