1.打开vs.net (2003\2005都可).
2.建立一个Windows应用程序
3.在项目下添加一个类命名为“计算器“
4.添加如下图WinForm.
以下是程序的实现代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 计算器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button16;
private System.Windows.Forms.Button button22;
private System.Windows.Forms.Button ce;
private System.Windows.Forms.Button c;
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_div;
private System.Windows.Forms.Button btn_sqr;
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_mul;
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_sub;
private System.Windows.Forms.Button btn_rev;
private System.Windows.Forms.Button btn_0;
private System.Windows.Forms.Button btn_sign;
private System.Windows.Forms.Button btn_dot;
private System.Windows.Forms.Button btn_add;
private System.Windows.Forms.Button btn_equ;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItemEdior;
private System.Windows.Forms.MenuItem menuItemCopy;
private System.Windows.Forms.MenuItem menuItemPaste;
private System.Windows.Forms.TextBox txtShow;
private System.Windows.Forms.Label lblText;
private System.Windows.Forms.MenuItem menuItemAbout;
private System.Windows.Forms.MenuItem menuItemHelp;
private System.Windows.Forms.MenuItem menuItemLink;
private System.Windows.Forms.Button buttonBack;
/// <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.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItemEdior = new System.Windows.Forms.MenuItem();
this.menuItemCopy = new System.Windows.Forms.MenuItem();
this.menuItemPaste = new System.Windows.Forms.MenuItem();
this.menuItemAbout = new System.Windows.Forms.MenuItem();
this.menuItemHelp = new System.Windows.Forms.MenuItem();
this.menuItemLink = new System.Windows.Forms.MenuItem();
this.lblText = new System.Windows.Forms.Label();
this.btn_add = new System.Windows.Forms.Button();
this.btn_dot = new System.Windows.Forms.Button();
this.btn_sign = new System.Windows.Forms.Button();
this.btn_0 = new System.Windows.Forms.Button();
this.button22 = new System.Windows.Forms.Button();
this.btn_rev = new System.Windows.Forms.Button();
this.btn_sub = new System.Windows.Forms.Button();
this.btn_3 = new System.Windows.Forms.Button();
this.btn_2 = new System.Windows.Forms.Button();
this.btn_1 = new System.Windows.Forms.Button();
this.button16 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.btn_mul = new System.Windows.Forms.Button();
this.btn_6 = new System.Windows.Forms.Button();
this.btn_5 = new System.Windows.Forms.Button();
this.btn_4 = new System.Windows.Forms.Button();
this.btn_equ = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.btn_sqr = new System.Windows.Forms.Button();
this.btn_div = new System.Windows.Forms.Button();
this.btn_9 = new System.Windows.Forms.Button();
this.btn_8 = new System.Windows.Forms.Button();
this.btn_7 = new System.Windows.Forms.Button();
this.txtShow = new System.Windows.Forms.TextBox();
this.button4 = new System.Windows.Forms.Button();
this.c = new System.Windows.Forms.Button();
this.ce = new System.Windows.Forms.Button();
this.buttonBack = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemEdior,
this.menuItemAbout});
//
// menuItemEdior
//
this.menuItemEdior.Index = 0;
this.menuItemEdior.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemCopy,
this.menuItemPaste});
this.menuItemEdior.Text = "编辑(&E)";
//
// menuItemCopy
//
this.menuItemCopy.Index = 0;
this.menuItemCopy.Text = "复制(&C)";
this.menuItemCopy.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItemPaste
//
this.menuItemPaste.Index = 1;
this.menuItemPaste.Text = "粘贴(&P)";
//
// menuItemAbout
//
this.menuItemAbout.Index = 1;
this.menuItemAbout.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemHelp,
this.menuItemLink});
this.menuItemAbout.Text = "关于(&A)";
//
// menuItemHelp
//
this.menuItemHelp.Index = 0;
this.menuItemHelp.Text = "帮助(&H)";
this.menuItemHelp.Click += new System.EventHandler(this.menuItemHelp_Click);
//
// menuItemLink
//
this.menuItemLink.Index = 1;
this.menuItemLink.Text = "关于(&L)";
this.menuItemLink.Click += new System.EventHandler(this.menuItemLink_Click);
//
// lblText
//
this.lblText.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblText.Location = new System.Drawing.Point(16, 43);
this.lblText.Name = "lblText";
this.lblText.Size = new System.Drawing.Size(30, 23);
this.lblText.TabIndex = 57;
this.lblText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btn_add
//
this.btn_add.Location = new System.Drawing.Point(173, 169);
this.btn_add.Name = "btn_add";
this.btn_add.Size = new System.Drawing.Size(33, 23);
this.btn_add.TabIndex = 55;
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(137, 169);
this.btn_dot.Name = "btn_dot";
this.btn_dot.Size = new System.Drawing.Size(33, 23);
this.btn_dot.TabIndex = 54;
this.btn_dot.Text = ".";
this.btn_dot.Click += new System.EventHandler(this.btn_dot_Click);
//
// btn_sign
//
this.btn_sign.Location = new System.Drawing.Point(101, 169);
this.btn_sign.Name = "btn_sign";
this.btn_sign.Size = new System.Drawing.Size(33, 23);
this.btn_sign.TabIndex = 53;
this.btn_sign.Text = "+/-";
this.btn_sign.Click += new System.EventHandler(this.btn_sign_Click);
//
// btn_0
//
this.btn_0.Location = new System.Drawing.Point(62, 169);
this.btn_0.Name = "btn_0";
this.btn_0.Size = new System.Drawing.Size(33, 23);
this.btn_0.TabIndex = 52;
this.btn_0.Tag = "0";
this.btn_0.Text = "0";
this.btn_0.Click += new System.EventHandler(this.btn_0_Click);
//
// button22
//
this.button22.Location = new System.Drawing.Point(20, 169);
this.button22.Name = "button22";
this.button22.Size = new System.Drawing.Size(30, 23);
this.button22.TabIndex = 51;
this.button22.Text = "M+";
this.button22.Click += new System.EventHandler(this.button22_Click);
//
// btn_rev
//
this.btn_rev.Location = new System.Drawing.Point(209, 139);
this.btn_rev.Name = "btn_rev";
this.btn_rev.Size = new System.Drawing.Size(39, 23);
this.btn_rev.TabIndex = 50;
this.btn_rev.Text = "1/x";
this.btn_rev.Click += new System.EventHandler(this.btn_rev_Click);
//
// btn_sub
//
this.btn_sub.Location = new System.Drawing.Point(173, 139);
this.btn_sub.Name = "btn_sub";
this.btn_sub.Size = new System.Drawing.Size(33, 23);
this.btn_sub.TabIndex = 49;
this.btn_sub.Text = "-";
this.btn_sub.Click += new System.EventHandler(this.btn_sub_Click);
//
// btn_3
//
this.btn_3.Location = new System.Drawing.Point(137, 139);
this.btn_3.Name = "btn_3";
this.btn_3.Size = new System.Drawing.Size(33, 23);
this.btn_3.TabIndex = 48;
this.btn_3.Tag = "3";
this.btn_3.Text = "3";
this.btn_3.Click += new System.EventHandler(this.btn_3_Click);
//
// btn_2
//
this.btn_2.Location = new System.Drawing.Point(101, 139);
this.btn_2.Name = "btn_2";
this.btn_2.Size = new System.Drawing.Size(33, 23);
this.btn_2.TabIndex = 47;
this.btn_2.Tag = "2";
this.btn_2.Text = "2";
this.btn_2.Click += new System.EventHandler(this.btn_2_Click);
//
// btn_1
//
this.btn_1.Location = new System.Drawing.Point(65, 139);
this.btn_1.Name = "btn_1";
this.btn_1.Size = new System.Drawing.Size(33, 23);
this.btn_1.TabIndex = 46;
this.btn_1.Tag = "1";
this.btn_1.Text = "1";
this.btn_1.Click += new System.EventHandler(this.btn_1_Click);
//
// button16
//
this.button16.Location = new System.Drawing.Point(20, 137);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(30, 23);
this.button16.TabIndex = 45;
this.button16.Text = "MS";
this.button16.Click += new System.EventHandler(this.button16_Click);
//
// button15
//
this.button15.Location = new System.Drawing.Point(209, 106);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(39, 23);
this.button15.TabIndex = 44;
this.button15.Text = "%";
this.button15.Click += new System.EventHandler(this.button15_Click);
//
// btn_mul
//
this.btn_mul.Location = new System.Drawing.Point(173, 106);
this.btn_mul.Name = "btn_mul";
this.btn_mul.Size = new System.Drawing.Size(33, 23);
this.btn_mul.TabIndex = 43;
this.btn_mul.Text = "*";
this.btn_mul.Click += new System.EventHandler(this.btn_mul_Click);
//
// btn_6
//
this.btn_6.Location = new System.Drawing.Point(137, 106);
this.btn_6.Name = "btn_6";
this.btn_6.Size = new System.Drawing.Size(33, 23);
this.btn_6.TabIndex = 42;
this.btn_6.Tag = "6";
this.btn_6.Text = "6";
this.btn_6.Click += new System.EventHandler(this.btn_6_Click);
//
// btn_5
//
this.btn_5.Location = new System.Drawing.Point(101, 106);
this.btn_5.Name = "btn_5";
this.btn_5.Size = new System.Drawing.Size(33, 23);
this.btn_5.TabIndex = 41;
this.btn_5.Tag = "5";
this.btn_5.Text = "5";
this.btn_5.Click += new System.EventHandler(this.btn_5_Click);
//
// btn_4
//
this.btn_4.Location = new System.Drawing.Point(62, 106);
this.btn_4.Name = "btn_4";
this.btn_4.Size = new System.Drawing.Size(33, 23);
this.btn_4.TabIndex = 40;
this.btn_4.Tag = "4";
this.btn_4.Text = "4";
this.btn_4.Click += new System.EventHandler(this.btn_4_Click);
//
// btn_equ
//
this.btn_equ.Location = new System.Drawing.Point(209, 169);
this.btn_equ.Name = "btn_equ";
this.btn_equ.Size = new System.Drawing.Size(39, 23);
this.btn_equ.TabIndex = 56;
this.btn_equ.Text = "=";
this.btn_equ.Click += new System.EventHandler(this.btn_equ_Click);
//
// button10
//
this.button10.Location = new System.Drawing.Point(20, 105);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(30, 23);
this.button10.TabIndex = 39;
this.button10.Text = "MR";
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// btn_sqr
//
this.btn_sqr.Location = new System.Drawing.Point(209, 73);
this.btn_sqr.Name = "btn_sqr";
this.btn_sqr.Size = new System.Drawing.Size(39, 23);
this.btn_sqr.TabIndex = 38;
this.btn_sqr.Text = "sqrt";
this.btn_sqr.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btn_sqr.Click += new System.EventHandler(this.btn_sqr_Click);
//
// btn_div
//
this.btn_div.Location = new System.Drawing.Point(173, 73);
this.btn_div.Name = "btn_div";
this.btn_div.Size = new System.Drawing.Size(33, 23);
this.btn_div.TabIndex = 37;
this.btn_div.Text = "/";
this.btn_div.Click += new System.EventHandler(this.btn_div_Click);
//
// btn_9
//
this.btn_9.Location = new System.Drawing.Point(137, 73);
this.btn_9.Name = "btn_9";
this.btn_9.Size = new System.Drawing.Size(33, 23);
this.btn_9.TabIndex = 36;
this.btn_9.Tag = "9";
this.btn_9.Text = "9";
this.btn_9.Click += new System.EventHandler(this.btn_9_Click);
//
// btn_8
//
this.btn_8.Location = new System.Drawing.Point(101, 73);
this.btn_8.Name = "btn_8";
this.btn_8.Size = new System.Drawing.Size(33, 23);
this.btn_8.TabIndex = 35;
this.btn_8.Tag = "8";
this.btn_8.Text = "8";
this.btn_8.Click += new System.EventHandler(this.btn_8_Click);
//
// btn_7
//
this.btn_7.Location = new System.Drawing.Point(62, 73);
this.btn_7.Name = "btn_7";
this.btn_7.Size = new System.Drawing.Size(33, 23);
this.btn_7.TabIndex = 34;
this.btn_7.Tag = "7";
this.btn_7.Text = "7";
this.btn_7.Click += new System.EventHandler(this.btn_7_Click);
//
// txtShow
//
this.txtShow.BackColor = System.Drawing.SystemColors.Control;
this.txtShow.Location = new System.Drawing.Point(8, 19);
this.txtShow.Name = "txtShow";
this.txtShow.Size = new System.Drawing.Size(240, 21);
this.txtShow.TabIndex = 29;
this.txtShow.Text = "0";
this.txtShow.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// button4
//
this.button4.Location = new System.Drawing.Point(20, 73);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(30, 23);
this.button4.TabIndex = 33;
this.button4.Text = "MC";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// c
//
this.c.Location = new System.Drawing.Point(209, 46);
this.c.Name = "c";
this.c.Size = new System.Drawing.Size(39, 23);
this.c.TabIndex = 32;
this.c.Text = "c";
this.c.Click += new System.EventHandler(this.c_Click);
//
// ce
//
this.ce.Location = new System.Drawing.Point(149, 46);
this.ce.Name = "ce";
this.ce.Size = new System.Drawing.Size(57, 23);
this.ce.TabIndex = 31;
this.ce.Text = "CE";
this.ce.Click += new System.EventHandler(this.ce_Click);
//
// buttonBack
//
this.buttonBack.Location = new System.Drawing.Point(64, 46);
this.buttonBack.Name = "buttonBack";
this.buttonBack.Size = new System.Drawing.Size(78, 23);
this.buttonBack.TabIndex = 58;
this.buttonBack.Text = "Backspace";
this.buttonBack.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(264, 211);
this.Controls.Add(this.buttonBack);
this.Controls.Add(this.btn_add);
this.Controls.Add(this.btn_dot);
this.Controls.Add(this.btn_sign);
this.Controls.Add(this.btn_0);
this.Controls.Add(this.button22);
this.Controls.Add(this.btn_rev);
this.Controls.Add(this.btn_sub);
this.Controls.Add(this.btn_3);
this.Controls.Add(this.btn_2);
this.Controls.Add(this.btn_1);
this.Controls.Add(this.button16);
this.Controls.Add(this.button15);
this.Controls.Add(this.btn_mul);
this.Controls.Add(this.btn_6);
this.Controls.Add(this.btn_5);
this.Controls.Add(this.btn_4);
this.Controls.Add(this.btn_equ);
this.Controls.Add(this.button10);
this.Controls.Add(this.btn_sqr);
this.Controls.Add(this.btn_div);
this.Controls.Add(this.btn_9);
this.Controls.Add(this.btn_8);
this.Controls.Add(this.btn_7);
this.Controls.Add(this.txtShow);
this.Controls.Add(this.button4);
this.Controls.Add(this.c);
this.Controls.Add(this.ce);
this.Controls.Add(this.lblText);
this.Menu = this.mainMenu1;
this.Name = "Form1";
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; // 定义是否点击了小数点,0 为没点
public const int HASDOT = 1;
private double res = 0; // 记录结果数
private double tmp = 0; // 当前输入的操作数
private int opt = NULL; // 记录操作码
private int dot = NODOT; // 记录是否点击了小数点,0为没有点
private int num = 0; // 记录输入操作数的个数
private int dotnum = 0; // 记录小数点部分的个数
string strOper; //获取操作符
private double ShowNum; //获取txtShow中的文本
string ShowNum1;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
if(txtShow.SelectionLength>0)
{
txtShow.Focus();
txtShow.SelectAll();
txtShow.Copy();
}
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
Help.ShowHelp(this,"C:\\WINDOWS\\Help\\calc.chm");
}
private void btn_1_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()); //取用户自定义的控件关联数,并转换成double型
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_2_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()); //取用户自定义的控件关联数,并转换成double型
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_3_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()); //取用户自定义的控件关联数,并转换成double型
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_4_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()); //取用户自定义的控件关联数,并转换成double型
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_5_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()); //取用户自定义的控件关联数,并转换成double型
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_6_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()); //取用户自定义的控件关联数,并转换成double型
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_7_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()); //取用户自定义的控件关联数,并转换成double型
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_8_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()); //取用户自定义的控件关联数,并转换成double型
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_9_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()); //取用户自定义的控件关联数,并转换成double型
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_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()); //取用户自定义的控件关联数,并转换成double型
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_Oper(Button tmp1)
{
strOper=tmp1.Text;
switch (strOper)
{
case "/": //除法运算
if(opt!=NULL&&opt!=DIV)
{
calc();
}
opt = DIV;
if (num != 0) //判断操作数的个数,如果两个就做二元运算
{
if (tmp != 0)
res = res / tmp;
}
else
res = tmp;
num++;
tmp = 0;
txtShow.Text = res.ToString();
dot = NODOT;
break;
case "*":
// 乘法运算
if(opt!=NULL&&opt!=MUL)
{
calc();
}
opt = MUL;
if (num!= 0 ) //判断操作数的个数,如果两个就做二元运算
{
if(tmp!=0)
res = res * tmp;
}
else
res = tmp;
num++;
tmp = 0;
txtShow.Text = res.ToString();
dot = NODOT;
break;
case "+": //加法运算
if(opt!=NULL&&opt!=ADD)
{
calc();
}
opt = ADD;
if (num != 0) //判断操作数的个数,如果两个就做二元运算
res = res + tmp;
else
res = tmp;
num++;
tmp = 0;
txtShow.Text = res.ToString();
dot = NODOT;
break;
case "-": //减法运算
if(opt!=NULL&&opt!=SUB)
{
calc();
}
opt = SUB;
if (num != 0) //判断操作数的个数,如果两个就做二元运算
res = res - tmp;
else
res = tmp;
num++;
tmp = 0;
txtShow.Text = res.ToString();
dot = NODOT;
break;
case "sqrt": //平方根运算
if(opt!=NULL)
{
calc();
}
//opt=SQRT;
if (tmp > 0) //要求操作数大于0
{
res = Math.Sqrt(tmp);
//res=tmp;
}
else if(res>0)
res= Math.Sqrt(res);
txtShow.Text =res.ToString();
num++;
tmp=0;
dot = NODOT;
break;
case "1/x": //倒数运算
if(opt!=NULL)
{
calc();
}
if (tmp != 0)
{
res = 1 / tmp;
//res=tmp;
}
else
res=1/res;
txtShow.Text = res.ToString();
tmp=0;
dot = NODOT;
break;
case ".":
// 点击了小数点
if(dot==HASDOT)
return;
else
{
dot = HASDOT;
dotnum = 0;
}
break;
case "+/-":
// 点击了符号运算
if(tmp!=0)
{
tmp = -tmp;
txtShow.Text = tmp.ToString();
}
else
{
res=-res;
//res=tmp;
txtShow.Text = res.ToString();
}
dot = NODOT;
break;
case "CE": //清除运算
res = 0; // 记录结果数
tmp = 0; // 当前输入的操作数
opt = NULL; // 记录操作码
dot = NODOT; // 记录是否点击了小数点
num = 0; // 记录输入操作数的个数
dotnum = 0; // 记录小数点部分的个数
txtShow.Text="";
break;
case "c": //清除运算
res = 0; // 记录结果数
tmp = 0; // 当前输入的操作数
opt = NULL; // 记录操作码
dot = NODOT; // 记录是否点击了小数点
num = 0; // 记录输入操作数的个数
dotnum = 0; // 记录小数点部分的个数
txtShow.Text="0";
break;
case "%":
if(tmp!=0)
{
if(opt == NULL)
{
tmp = tmp/100;
}
if(opt != NULL)
{
tmp = res*tmp/100;
}
txtShow.Text = tmp.ToString();
}
else
{
res = res/100;
txtShow.Text = res.ToString();
}
dot = NODOT;
break;
}
}
private void btn_sign_Click(object sender, System.EventArgs e)
{
btn_Oper(btn_sign);
}
private void btn_dot_Click(object sender, System.EventArgs e)
{
btn_Oper(btn_dot);
}
private void btn_add_Click(object sender, System.EventArgs e)
{
btn_Oper(btn_add);
}
private void btn_equ_Click(object sender, System.EventArgs e)
{
calc();
}
private void calc()
{
// 生成结果
if (num == 0 ) //是否有操作数,没有就返回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/100;
// break;
//平方根
case SQRT:
res = System.Math.Sqrt(tmp);
break;
default:
return;
}
txtShow.Text = res.ToString(); //结果输出到文本显示屏
opt = NULL; //运算符清空
tmp=0;
dot = NODOT;
res = 0;
num = 0;
}
private void btn_sub_Click(object sender, System.EventArgs e)
{
btn_Oper(btn_sub);
}
private void btn_mul_Click(object sender, System.EventArgs e)
{
btn_Oper(btn_mul);
}
private void btn_div_Click(object sender, System.EventArgs e)
{
btn_Oper(btn_div);
}
private void ce_Click(object sender, System.EventArgs e)
{
btn_Oper(ce);
}
private void c_Click(object sender, System.EventArgs e)
{
btn_Oper(c);
}
private void btn_sqr_Click(object sender, System.EventArgs e)
{
btn_Oper(btn_sqr);
}
private void button15_Click(object sender, System.EventArgs e)
{
btn_Oper(button15);
}
private void btn_rev_Click(object sender, System.EventArgs e)
{
btn_Oper(btn_rev);
}
private void button4_Click(object sender, System.EventArgs e)
{
lblText.Text = "";
ShowNum=0;
tmp = 0;
}
private void button10_Click(object sender, System.EventArgs e)
{
if(ShowNum!=0)
{
txtShow.Text = ShowNum.ToString();
}
tmp=0;
}
private void button16_Click(object sender, System.EventArgs e)
{
if(txtShow.Text!="0")
{
ShowNum = tmp;
lblText.Text = "M";
tmp = 0;
}
}
private void button22_Click(object sender, System.EventArgs e)
{
ShowNum=ShowNum+tmp;
txtShow.Text = ShowNum.ToString();
tmp=0;
}
private void menuItemLink_Click(object sender, System.EventArgs e)
{
MessageBox.Show("By - Looky.Tenfu","关于");
}
private void menuItemHelp_Click(object sender, System.EventArgs e)
{
Help.ShowHelp(this,"C:\\WINDOWS\\Help\\calc.chm");
}
private void button1_Click(object sender, System.EventArgs e)
{
if(txtShow.Text.Length > 0)
{
ShowNum1=txtShow.Text;
txtShow.Text = ShowNum1.Substring(0,ShowNum1.Length-1);
tmp=0;
}
if(txtShow.Text.Length == 0)
{
txtShow.Text="0";
tmp=0;
}
}
}
}
有任何问题,可在下面回复!呵呵......