using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Calc
{
/// <summary>
/// 这是一个计算器的简单实现。
/// </summary>
public class CalcForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btn_0;
private System.Windows.Forms.Button btn_1;
private System.Windows.Forms.Button btn_2;
private System.Windows.Forms.Button btn_3;
private System.Windows.Forms.Button btn_4;
private System.Windows.Forms.Button btn_5;
private System.Windows.Forms.Button btn_6;
private System.Windows.Forms.Button btn_7;
private System.Windows.Forms.Button btn_8;
private System.Windows.Forms.Button btn_9;
private System.Windows.Forms.Button btn_add;
private System.Windows.Forms.Button btn_sub;
private System.Windows.Forms.Button btn_mul;
private System.Windows.Forms.Button btn_div;
private System.Windows.Forms.Button btn_sqrt;
private System.Windows.Forms.Button btn_sign;
private System.Windows.Forms.Button btn_equ;
private System.Windows.Forms.Button btn_dot;
private System.Windows.Forms.Button btn_rev;
private System.Windows.Forms.TextBox txtShow;
private System.Windows.Forms.Button btn_sqr;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public CalcForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Calc
{
/// <summary>
/// 这是一个计算器的简单实现。
/// </summary>
public class CalcForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btn_0;
private System.Windows.Forms.Button btn_1;
private System.Windows.Forms.Button btn_2;
private System.Windows.Forms.Button btn_3;
private System.Windows.Forms.Button btn_4;
private System.Windows.Forms.Button btn_5;
private System.Windows.Forms.Button btn_6;
private System.Windows.Forms.Button btn_7;
private System.Windows.Forms.Button btn_8;
private System.Windows.Forms.Button btn_9;
private System.Windows.Forms.Button btn_add;
private System.Windows.Forms.Button btn_sub;
private System.Windows.Forms.Button btn_mul;
private System.Windows.Forms.Button btn_div;
private System.Windows.Forms.Button btn_sqrt;
private System.Windows.Forms.Button btn_sign;
private System.Windows.Forms.Button btn_equ;
private System.Windows.Forms.Button btn_dot;
private System.Windows.Forms.Button btn_rev;
private System.Windows.Forms.TextBox txtShow;
private System.Windows.Forms.Button btn_sqr;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public CalcForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn_9 = new System.Windows.Forms.Button();
this.txtShow = new System.Windows.Forms.TextBox();
this.btn_8 = new System.Windows.Forms.Button();
this.btn_7 = new System.Windows.Forms.Button();
this.btn_div = new System.Windows.Forms.Button();
this.btn_sqrt = new System.Windows.Forms.Button();
this.btn_4 = new System.Windows.Forms.Button();
this.btn_5 = new System.Windows.Forms.Button();
this.btn_6 = new System.Windows.Forms.Button();
this.btn_1 = new System.Windows.Forms.Button();
this.btn_2 = new System.Windows.Forms.Button();
this.btn_3 = new System.Windows.Forms.Button();
this.btn_0 = new System.Windows.Forms.Button();
this.btn_mul = new System.Windows.Forms.Button();
this.btn_sub = new System.Windows.Forms.Button();
this.btn_sign = new System.Windows.Forms.Button();
this.btn_equ = new System.Windows.Forms.Button();
this.btn_add = new System.Windows.Forms.Button();
this.btn_dot = new System.Windows.Forms.Button();
this.btn_sqr = new System.Windows.Forms.Button();
this.btn_rev = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btn_9
//
this.btn_9.Location = new System.Drawing.Point(136, 48);
this.btn_9.Name = "btn_9";
this.btn_9.Size = new System.Drawing.Size(48, 23);
this.btn_9.TabIndex = 0;
this.btn_9.Tag = "9";
this.btn_9.Text = "9";
this.btn_9.Click += new System.EventHandler(this.btn_0_Click);
//
// txtShow
//
this.txtShow.Location = new System.Drawing.Point(8, 8);
this.txtShow.Name = "txtShow";
this.txtShow.ReadOnly = true;
this.txtShow.Size = new System.Drawing.Size(304, 25);
this.txtShow.TabIndex = 1;
this.txtShow.Text = "";
this.txtShow.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// btn_8
//
this.btn_8.Location = new System.Drawing.Point(72, 48);
this.btn_8.Name = "btn_8";
this.btn_8.Size = new System.Drawing.Size(48, 23);
this.btn_8.TabIndex = 2;
this.btn_8.Tag = "8";
this.btn_8.Text = "8";
this.btn_8.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_7
//
this.btn_7.Location = new System.Drawing.Point(8, 48);
this.btn_7.Name = "btn_7";
this.btn_7.Size = new System.Drawing.Size(48, 23);
this.btn_7.TabIndex = 3;
this.btn_7.Tag = "7";
this.btn_7.Text = "7";
this.btn_7.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_div
//
this.btn_div.Location = new System.Drawing.Point(200, 48);
this.btn_div.Name = "btn_div";
this.btn_div.Size = new System.Drawing.Size(48, 23);
this.btn_div.TabIndex = 4;
this.btn_div.Text = "/";
this.btn_div.Click += new System.EventHandler(this.btn_div_Click);
//
// btn_sqrt
//
this.btn_sqrt.Location = new System.Drawing.Point(264, 48);
this.btn_sqrt.Name = "btn_sqrt";
this.btn_sqrt.Size = new System.Drawing.Size(48, 23);
this.btn_sqrt.TabIndex = 5;
this.btn_sqrt.Text = "sqrt";
this.btn_sqrt.Click += new System.EventHandler(this.btn_sqrt_Click);
// btn_4
//
this.btn_4.Location = new System.Drawing.Point(8, 88);
this.btn_4.Name = "btn_4";
this.btn_4.Size = new System.Drawing.Size(48, 23);
this.btn_4.TabIndex = 6;
this.btn_4.Tag = "4";
this.btn_4.Text = "4";
this.btn_4.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_5
//
this.btn_5.Location = new System.Drawing.Point(72, 88);
this.btn_5.Name = "btn_5";
this.btn_5.Size = new System.Drawing.Size(48, 23);
this.btn_5.TabIndex = 7;
this.btn_5.Tag = "5";
this.btn_5.Text = "5";
this.btn_5.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_6
//
this.btn_6.Location = new System.Drawing.Point(136, 88);
this.btn_6.Name = "btn_6";
this.btn_6.Size = new System.Drawing.Size(48, 23);
this.btn_6.TabIndex = 8;
this.btn_6.Tag = "6";
this.btn_6.Text = "6";
this.btn_6.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_1
//
this.btn_1.Location = new System.Drawing.Point(8, 128);
this.btn_1.Name = "btn_1";
this.btn_1.Size = new System.Drawing.Size(48, 23);
this.btn_1.TabIndex = 9;
this.btn_1.Tag = "1";
this.btn_1.Text = "1";
this.btn_1.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_2
//
this.btn_2.Location = new System.Drawing.Point(72, 128);
this.btn_2.Name = "btn_2";
this.btn_2.Size = new System.Drawing.Size(48, 23);
this.btn_2.TabIndex = 10;
this.btn_2.Tag = "2";
this.btn_2.Text = "2";
this.btn_2.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_3
//
this.btn_3.Location = new System.Drawing.Point(136, 128);
this.btn_3.Name = "btn_3";
this.btn_3.Size = new System.Drawing.Size(48, 23);
this.btn_3.TabIndex = 11;
this.btn_3.Tag = "3";
this.btn_3.Text = "3";
this.btn_3.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_0
//
this.btn_0.Location = new System.Drawing.Point(8, 168);
this.btn_0.Name = "btn_0";
this.btn_0.Size = new System.Drawing.Size(48, 23);
this.btn_0.TabIndex = 12;
this.btn_0.Tag = "0";
this.btn_0.Text = "0";
this.btn_0.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_mul
//
this.btn_mul.Location = new System.Drawing.Point(200, 88);
this.btn_mul.Name = "btn_mul";
this.btn_mul.Size = new System.Drawing.Size(48, 23);
this.btn_mul.TabIndex = 13;
this.btn_mul.Text = "*";
this.btn_mul.Click += new System.EventHandler(this.btn_mul_Click);
//
// btn_sub
//
this.btn_sub.Location = new System.Drawing.Point(200, 128);
this.btn_sub.Name = "btn_sub";
this.btn_sub.Size = new System.Drawing.Size(48, 23);
this.btn_sub.TabIndex = 14;
this.btn_sub.Text = "-";
this.btn_sub.Click += new System.EventHandler(this.btn_sub_Click);
//
// btn_sign
//
this.btn_sign.Location = new System.Drawing.Point(72, 168);
this.btn_sign.Name = "btn_sign";
this.btn_sign.Size = new System.Drawing.Size(48, 23);
this.btn_sign.TabIndex = 15;
this.btn_sign.Text = "+/-";
this.btn_sign.Click += new System.EventHandler(this.btn_sign_Click);
//
//
this.btn_equ.Location = new System.Drawing.Point(264, 168);
this.btn_equ.Name = "btn_equ";
this.btn_equ.Size = new System.Drawing.Size(48, 23);
this.btn_equ.TabIndex = 16;
this.btn_equ.Text = "=";
this.btn_equ.Click += new System.EventHandler(this.btn_equ_Click);
//
// btn_add
//
this.btn_add.Location = new System.Drawing.Point(200, 168);
this.btn_add.Name = "btn_add";
this.btn_add.Size = new System.Drawing.Size(48, 23);
this.btn_add.TabIndex = 17;
this.btn_add.Text = "+";
this.btn_add.Click += new System.EventHandler(this.btn_add_Click);
//
// btn_dot
//
this.btn_dot.Location = new System.Drawing.Point(136, 168);
this.btn_dot.Name = "btn_dot";
this.btn_dot.Size = new System.Drawing.Size(48, 23);
this.btn_dot.TabIndex = 18;
this.btn_dot.Text = ".";
this.btn_dot.Click += new System.EventHandler(this.btn_dot_Click);
//
// btn_sqr
//
this.btn_sqr.Location = new System.Drawing.Point(264, 88);
this.btn_sqr.Name = "btn_sqr";
this.btn_sqr.Size = new System.Drawing.Size(48, 23);
this.btn_sqr.TabIndex = 19;
this.btn_sqr.Text = "sqr";
this.btn_sqr.Click += new System.EventHandler(this.btn_sqr_Click);
//
// btn_rev
//
this.btn_rev.Location = new System.Drawing.Point(264, 128);
this.btn_rev.Name = "btn_rev";
this.btn_rev.Size = new System.Drawing.Size(48, 23);
this.btn_rev.TabIndex = 20;
this.btn_rev.Text = "1/x";
this.btn_rev.Click += new System.EventHandler(this.btn_rev_Click);
//
// CalcForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(322, 202);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btn_rev,
this.btn_sqr,
this.btn_dot,
this.btn_add,
this.btn_equ,
this.btn_sign,
this.btn_sub,
this.btn_mul,
this.btn_0,
this.btn_3,
this.btn_2,
this.btn_1,
this.btn_6,
this.btn_5,
this.btn_4,
this.btn_sqrt,
this.btn_div,
this.btn_7,
this.btn_8,
this.txtShow,
this.btn_9});
this.MaximizeBox = false;
this.Name = "CalcForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "计算器";
this.ResumeLayout(false); }
#endregion public const int NULL = 0; // 定义操作码
public const int ADD = 1;
public const int SUB = 2;
public const int MUL = 3;
public const int DIV = 4;
public const int SQR = 5;
public const int SQRT = 6;
public const int NODOT = 0; // 定义是否点击了小数点
public const int HASDOT = 1;
private double res = 0; // 记录结果数
private double tmp = 0; // 当前输入的操作数
private int opt = NULL; // 记录操作码
private int dot = NODOT; // 记录是否点击了小数点
private int num = 0; // 记录输入操作数的个数
private int dotnum = 0; // 记录小数点部分的个数
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new CalcForm());
} /// <summary>
/// 当点击一个数字按钮的时候,需要进行判断,如果没有点击
/// 小数点,那么就把原来的数值扩大10倍后再加上当前的数值,
/// 如果点击了小数点,那么就将当前的数值除以一个权数,再
/// 加上原来的数值,得到新的数值。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_0_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.Button btnTmp;
double i; btnTmp = sender as System.Windows.Forms.Button;
if (btnTmp != null)
{
if (dot == NODOT)
{
// 没有点击小数点
i = double.Parse(btnTmp.Tag.ToString());
tmp = tmp * 10 + i;
txtShow.Text = tmp.ToString();
}
else
{
// 点击了小数点
dotnum++;
// 生成小数部分的新的数值
i = double.Parse(btnTmp.Tag.ToString()) / System.Math.Pow(10,dotnum);
tmp = tmp + i;
txtShow.Text = tmp.ToString();
}
}
} private void btn_equ_Click(object sender, System.EventArgs e)
{
// 生成结果
if (num == 0)
{
res = 0;
tmp = 0;
txtShow.Text = res.ToString();
return;
}
switch (opt)
{
// 加法
case ADD:
res = res + tmp;
break;
// 减法
case SUB:
res = res - tmp;
break;
// 乘法
case MUL:
res = res * tmp;
break;
// 除法
case DIV:
res = res / tmp;
break;
// 平方
case SQR:
res = tmp * tmp;
break;
// 平方根
case SQRT:
res = System.Math.Sqrt(tmp);
break;
default:
return;
}
txtShow.Text = res.ToString();
opt = NULL;
res = 0;
num = 0;
} private void btn_div_Click(object sender, System.EventArgs e)
{
// 除法运算
opt = DIV;
if (num != 0)
{
if (tmp != 0)
res = res / tmp;
}
else
res = tmp;
num++;
tmp = 0;
txtShow.Text = res.ToString();
} private void btn_mul_Click(object sender, System.EventArgs e)
{
// 乘法运算
opt = MUL;
if (num != 0)
res = res * tmp;
else
res = tmp;
num++;
tmp = 0;
txtShow.Text = res.ToString();
} private void btn_sub_Click(object sender, System.EventArgs e)
{
// 减法运算
opt = SUB;
if (num != 0)
res = res - tmp;
else
res = tmp;
num++;
tmp = 0;
txtShow.Text = res.ToString();
} private void btn_add_Click(object sender, System.EventArgs e)
{
// 加法运算
opt = ADD;
if (num != 0)
res = res + tmp;
else
res = tmp;
num++;
tmp = 0;
txtShow.Text = res.ToString();
}
{
// 开方运算
if (tmp > 0)
{
tmp = Math.Sqrt(tmp);
txtShow.Text = tmp.ToString();
}
} private void btn_sqr_Click(object sender, System.EventArgs e)
{
// 平方运算
tmp = tmp * tmp;
txtShow.Text = tmp.ToString();
} private void btn_rev_Click(object sender, System.EventArgs e)
{
// 求倒数运算
tmp = 1 / tmp;
txtShow.Text = tmp.ToString();
} private void btn_dot_Click(object sender, System.EventArgs e)
{
// 点击了小数点
dot = HASDOT;
dotnum = 0;
} private void btn_sign_Click(object sender, System.EventArgs e)
{
// 点击了符号运算
tmp = -tmp;
txtShow.Text = tmp.ToString();
}
}
}总算发完了.
就是分太少,真累呀.