windows计算器
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
using System.Data;
namespace comput
{
/// <summary>
/// 这是一个计算器的简单实现。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
#region 控件声明
private System.Windows.Forms.TextBox txtShow;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btn_rev;
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.Button btn_sign;
private System.Windows.Forms.Button btn_sub;
private System.Windows.Forms.Button btn_mul;
private System.Windows.Forms.Button btn_0;
private System.Windows.Forms.Button btn_3;
private System.Windows.Forms.Button btn_2;
private System.Windows.Forms.Button btn_1;
private System.Windows.Forms.Button btn_6;
private System.Windows.Forms.Button btn_5;
private System.Windows.Forms.Button btn_4;
private System.Windows.Forms.Button btn_sqrt;
private System.Windows.Forms.Button btn_div;
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.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.Button btn_sqr;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.Button c;
private System.Windows.Forms.Button ce;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;
/// <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 );
}
#endregion
#region 各控件的属性的方法Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.txtShow = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.c = new System.Windows.Forms.Button();
this.ce = new System.Windows.Forms.Button();
this.btn_rev = new System.Windows.Forms.Button();
this.btn_dot = new System.Windows.Forms.Button();
this.btn_add = new System.Windows.Forms.Button();
this.btn_equ = new System.Windows.Forms.Button();
this.btn_sign = new System.Windows.Forms.Button();
this.btn_sub = new System.Windows.Forms.Button();
this.btn_mul = new System.Windows.Forms.Button();
this.btn_0 = 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.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_sqrt = new System.Windows.Forms.Button();
this.btn_div = new System.Windows.Forms.Button();
this.btn_7 = new System.Windows.Forms.Button();
this.btn_8 = new System.Windows.Forms.Button();
this.btn_9 = new System.Windows.Forms.Button();
this.btn_sqr = new System.Windows.Forms.Button();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// txtShow
//
this.txtShow.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.txtShow.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txtShow.ForeColor = System.Drawing.Color.Purple;
this.txtShow.Location = new System.Drawing.Point(25, 8);
this.txtShow.Name = "txtShow";
this.txtShow.Size = new System.Drawing.Size(228, 23);
this.txtShow.TabIndex = 1;
this.txtShow.Text = "0.";
this.txtShow.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.c);
this.groupBox1.Controls.Add(this.ce);
this.groupBox1.Controls.Add(this.btn_rev);
this.groupBox1.Controls.Add(this.btn_dot);
this.groupBox1.Controls.Add(this.btn_add);
this.groupBox1.Controls.Add(this.btn_equ);
this.groupBox1.Controls.Add(this.btn_sign);
this.groupBox1.Controls.Add(this.btn_sub);
this.groupBox1.Controls.Add(this.btn_mul);
this.groupBox1.Controls.Add(this.btn_0);
this.groupBox1.Controls.Add(this.btn_3);
this.groupBox1.Controls.Add(this.btn_2);
this.groupBox1.Controls.Add(this.btn_1);
this.groupBox1.Controls.Add(this.btn_6);
this.groupBox1.Controls.Add(this.btn_5);
this.groupBox1.Controls.Add(this.btn_4);
this.groupBox1.Controls.Add(this.btn_sqrt);
this.groupBox1.Controls.Add(this.btn_div);
this.groupBox1.Controls.Add(this.btn_7);
this.groupBox1.Controls.Add(this.btn_8);
this.groupBox1.Controls.Add(this.btn_9);
this.groupBox1.Controls.Add(this.btn_sqr);
this.groupBox1.Location = new System.Drawing.Point(24, 40);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(232, 184);
this.groupBox1.TabIndex = 21;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "计算机区";
//
// c
//
this.c.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.c.ForeColor = System.Drawing.Color.Red;
this.c.Location = new System.Drawing.Point(178, 48);
this.c.Name = "c";
this.c.Size = new System.Drawing.Size(36, 61);
this.c.TabIndex = 41;
this.c.Text = "C";
this.c.Click += new System.EventHandler(this.btn_Oper);
//
// ce
//
this.ce.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.ce.ForeColor = System.Drawing.Color.Red;
this.ce.Location = new System.Drawing.Point(138, 16);
this.ce.Name = "ce";
this.ce.Size = new System.Drawing.Size(76, 29);
this.ce.TabIndex = 40;
this.ce.Text = "CE";
this.ce.Click += new System.EventHandler(this.btn_Oper);
//
// btn_rev
//
this.btn_rev.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_rev.ForeColor = System.Drawing.Color.Blue;
this.btn_rev.Location = new System.Drawing.Point(178, 112);
this.btn_rev.Name = "btn_rev";
this.btn_rev.Size = new System.Drawing.Size(36, 29);
this.btn_rev.TabIndex = 39;
this.btn_rev.Text = "1/x";
this.btn_rev.Click += new System.EventHandler(this.btn_Oper);
//
// btn_dot
//
this.btn_dot.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_dot.Location = new System.Drawing.Point(98, 144);
this.btn_dot.Name = "btn_dot";
this.btn_dot.Size = new System.Drawing.Size(36, 29);
this.btn_dot.TabIndex = 38;
this.btn_dot.Tag = "0";
this.btn_dot.Text = ".";
this.btn_dot.Click += new System.EventHandler(this.btn_Oper);
//
// btn_add
//
this.btn_add.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_add.ForeColor = System.Drawing.Color.Red;
this.btn_add.Location = new System.Drawing.Point(138, 144);
this.btn_add.Name = "btn_add";
this.btn_add.Size = new System.Drawing.Size(36, 29);
this.btn_add.TabIndex = 37;
this.btn_add.Text = "+";
this.btn_add.Click += new System.EventHandler(this.btn_Oper);
//
// btn_equ
//
this.btn_equ.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_equ.ForeColor = System.Drawing.Color.Red;
this.btn_equ.Location = new System.Drawing.Point(178, 144);
this.btn_equ.Name = "btn_equ";
this.btn_equ.Size = new System.Drawing.Size(36, 29);
this.btn_equ.TabIndex = 36;
this.btn_equ.Text = "=";
this.btn_equ.Click += new System.EventHandler(this.btn_equ_Click);
//
// btn_sign
//
this.btn_sign.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_sign.ForeColor = System.Drawing.Color.Blue;
this.btn_sign.Location = new System.Drawing.Point(58, 144);
this.btn_sign.Name = "btn_sign";
this.btn_sign.Size = new System.Drawing.Size(36, 29);
this.btn_sign.TabIndex = 35;
this.btn_sign.Text = "+/-";
this.btn_sign.Click += new System.EventHandler(this.btn_Oper);
//
// btn_sub
//
this.btn_sub.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_sub.ForeColor = System.Drawing.Color.Red;
this.btn_sub.Location = new System.Drawing.Point(138, 112);
this.btn_sub.Name = "btn_sub";
this.btn_sub.Size = new System.Drawing.Size(36, 29);
this.btn_sub.TabIndex = 34;
this.btn_sub.Text = "-";
this.btn_sub.Click += new System.EventHandler(this.btn_Oper);
//
// btn_mul
//
this.btn_mul.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_mul.ForeColor = System.Drawing.Color.Red;
this.btn_mul.Location = new System.Drawing.Point(138, 80);
this.btn_mul.Name = "btn_mul";
this.btn_mul.Size = new System.Drawing.Size(36, 29);
this.btn_mul.TabIndex = 33;
this.btn_mul.Text = "*";
this.btn_mul.Click += new System.EventHandler(this.btn_Oper);
//
// btn_0
//
this.btn_0.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_0.ForeColor = System.Drawing.Color.Blue;
this.btn_0.Location = new System.Drawing.Point(18, 144);
this.btn_0.Name = "btn_0";
this.btn_0.Size = new System.Drawing.Size(36, 29);
this.btn_0.TabIndex = 32;
this.btn_0.Tag = "0";
this.btn_0.Text = "0";
this.btn_0.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_3
//
this.btn_3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_3.ForeColor = System.Drawing.Color.Blue;
this.btn_3.Location = new System.Drawing.Point(98, 112);
this.btn_3.Name = "btn_3";
this.btn_3.Size = new System.Drawing.Size(36, 29);
this.btn_3.TabIndex = 31;
this.btn_3.Tag = "3";
this.btn_3.Text = "3";
this.btn_3.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_2
//
this.btn_2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_2.ForeColor = System.Drawing.Color.Blue;
this.btn_2.Location = new System.Drawing.Point(58, 112);
this.btn_2.Name = "btn_2";
this.btn_2.Size = new System.Drawing.Size(36, 29);
this.btn_2.TabIndex = 30;
this.btn_2.Tag = "2";
this.btn_2.Text = "2";
this.btn_2.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_1
//
this.btn_1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_1.ForeColor = System.Drawing.Color.Blue;
this.btn_1.Location = new System.Drawing.Point(18, 112);
this.btn_1.Name = "btn_1";
this.btn_1.Size = new System.Drawing.Size(36, 29);
this.btn_1.TabIndex = 29;
this.btn_1.Tag = "1";
this.btn_1.Text = "1";
this.btn_1.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_6
//
this.btn_6.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_6.ForeColor = System.Drawing.Color.Blue;
this.btn_6.Location = new System.Drawing.Point(98, 80);
this.btn_6.Name = "btn_6";
this.btn_6.Size = new System.Drawing.Size(36, 29);
this.btn_6.TabIndex = 28;
this.btn_6.Tag = "6";
this.btn_6.Text = "6";
this.btn_6.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_5
//
this.btn_5.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_5.ForeColor = System.Drawing.Color.Blue;
this.btn_5.Location = new System.Drawing.Point(58, 80);
this.btn_5.Name = "btn_5";
this.btn_5.Size = new System.Drawing.Size(36, 29);
this.btn_5.TabIndex = 27;
this.btn_5.Tag = "5";
this.btn_5.Text = "5";
this.btn_5.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_4
//
this.btn_4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_4.ForeColor = System.Drawing.Color.Blue;
this.btn_4.Location = new System.Drawing.Point(18, 80);
this.btn_4.Name = "btn_4";
this.btn_4.Size = new System.Drawing.Size(36, 29);
this.btn_4.TabIndex = 26;
this.btn_4.Tag = "4";
this.btn_4.Text = "4";
this.btn_4.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_sqrt
//
this.btn_sqrt.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_sqrt.ForeColor = System.Drawing.Color.Blue;
this.btn_sqrt.Location = new System.Drawing.Point(18, 16);
this.btn_sqrt.Name = "btn_sqrt";
this.btn_sqrt.Size = new System.Drawing.Size(76, 29);
this.btn_sqrt.TabIndex = 25;
this.btn_sqrt.Text = "sqrt";
this.btn_sqrt.Click += new System.EventHandler(this.btn_Oper);
//
// btn_div
//
this.btn_div.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_div.ForeColor = System.Drawing.Color.Red;
this.btn_div.Location = new System.Drawing.Point(138, 48);
this.btn_div.Name = "btn_div";
this.btn_div.Size = new System.Drawing.Size(36, 29);
this.btn_div.TabIndex = 24;
this.btn_div.Text = "/";
this.btn_div.Click += new System.EventHandler(this.btn_Oper);
//
// btn_7
//
this.btn_7.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_7.ForeColor = System.Drawing.Color.Blue;
this.btn_7.Location = new System.Drawing.Point(18, 48);
this.btn_7.Name = "btn_7";
this.btn_7.Size = new System.Drawing.Size(36, 29);
this.btn_7.TabIndex = 23;
this.btn_7.Tag = "7";
this.btn_7.Text = "7";
this.btn_7.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_8
//
this.btn_8.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_8.ForeColor = System.Drawing.Color.Blue;
this.btn_8.Location = new System.Drawing.Point(58, 48);
this.btn_8.Name = "btn_8";
this.btn_8.Size = new System.Drawing.Size(36, 29);
this.btn_8.TabIndex = 22;
this.btn_8.Tag = "8";
this.btn_8.Text = "8";
this.btn_8.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_9
//
this.btn_9.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_9.ForeColor = System.Drawing.Color.Blue;
this.btn_9.Location = new System.Drawing.Point(98, 48);
this.btn_9.Name = "btn_9";
this.btn_9.Size = new System.Drawing.Size(36, 29);
this.btn_9.TabIndex = 21;
this.btn_9.Tag = "9";
this.btn_9.Text = "9";
this.btn_9.Click += new System.EventHandler(this.btn_0_Click);
//
// btn_sqr
//
this.btn_sqr.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btn_sqr.ForeColor = System.Drawing.Color.Blue;
this.btn_sqr.Location = new System.Drawing.Point(98, 16);
this.btn_sqr.Name = "btn_sqr";
this.btn_sqr.Size = new System.Drawing.Size(36, 29);
this.btn_sqr.TabIndex = 19;
this.btn_sqr.Text = "sqr";
this.btn_sqr.Click += new System.EventHandler(this.btn_Oper);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem8,
this.menuItem1,
this.menuItem4});
//
// menuItem1
//
this.menuItem1.Index = 1;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.menuItem1.Text = "编辑(&E)";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "复制(&C)";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "粘贴(&P)";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5,
this.menuItem6});
this.menuItem4.Text = "帮助(&H)";
//
// menuItem5
//
this.menuItem5.Index = 0;
this.menuItem5.Text = "帮助主题(&H)";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.Text = "关于(&A)";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem8
//
this.menuItem8.Index = 0;
this.menuItem8.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem9,
this.menuItem10});
this.menuItem8.Text = "文件(&F)";
//
// menuItem9
//
this.menuItem9.Index = 0;
this.menuItem9.Text = "打开windows计算器(&O)";
this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
//
// menuItem10
//
this.menuItem10.Index = 1;
this.menuItem10.Text = "退出(&Q)";
this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(278, 239);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.txtShow);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "计算器";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
#region 各变量和常数的声明
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; //获取操作符
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
#region 获取操作数事件
//获取操作数事件
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();
}
}
}
#endregion
#region 等于事件和等于运算方法
//等于事件
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 * tmp;
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;
}
#endregion
#region 获取操作符运事件
//获取操作符运事件
private void btn_Oper(object obj,EventArgs ea)
{
Button tmp1=(Button)obj;
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();
}
/*if(opt==ADD)
{
res=res+tmp;
tmp=0;
}*/
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 "sqr":
// 平方运算
if(opt!=NULL)
{
calc();
}
//opt=SQR;
if (tmp != 0)
{
res = tmp * tmp;
//res=tmp;
}
else
res=res*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="";
break;
}
}
#endregion
#region 主菜单事件
//打开关于主题---调用windows xp中计算器的帮助
private void menuItem5_Click(object sender, System.EventArgs e)
{
Help.ShowHelp(this,"C://WINDOWS//Help//calc.chm");
}
//打开于我们
private void menuItem6_Click(object sender, System.EventArgs e)
{
Form2 fm=new Form2();
fm.Show();
}
//复制
private void menuItem2_Click(object sender, System.EventArgs e)
{
if(txtShow.SelectionLength>0)
{
txtShow.Copy();
}
}
//
//粘贴
private void menuItem3_Click(object sender, System.EventArgs e)
{
txtShow.Paste();
}
//调用windows xp中的计算器
private void menuItem9_Click(object sender, System.EventArgs e)
{
Help.ShowHelp(this,"C://WINDOWS//system32//calc.exe");
}
//退出
private void menuItem10_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
#endregion
}
}
相关文章:

哈夫曼树的构造
[转载于网易博客,具体地址不详] 构造哈夫曼树的过程是这样的 一、构成初始集合 对给定的n个权值{W1,W2,W3,...,Wi,...,Wn}构成n棵二叉树的初始集合F{T1,T2,T3,...,Ti,...,Tn},其中每棵二叉树Ti中只有一个权值为Wi的根结点,它的左右子树均为空…

物联网时代全面降临
从智能建筑到零售,英特尔物联网解决方案可以说是华丽丽地惊艳着大家的大脑和眼球,一切的不可能似乎都在朝着可能的方向努力着。在2015 MWC上,英特尔再次用各种神奇的物联网设备告诉大家:物联网时代已经来临。 “半边天”的力量&am…

Linux C++/Java/Web/OC Socket网络编程
一,Linux C Socket网络编程 1.什么是TCP/IP、UDP? TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,是一个工业标准的协议集,它是为广域网(WANs)设…

ASP.NET抓取其他网页代码
在.Net 平台下,创建一个ASP.Net的程序 1、引用两个NAMESPACE using System.Text //因为用了Encoding类 using System.Net //因为用了WebClient 类 2、整个程序用了三个控件 txtUrl //输入你要获取的网页地址 TEXTBOX控件 txtBody //得到你要获取的网…

特斯拉遇上 CPU:程序员的心思你别猜
作者 | 码农的荒岛求生来源 | 码农的荒岛求生图源 | 视觉中国18世纪流水线的诞生带来了制造技术的变革,人类当今拥有琳琅满目物美价廉的商品和流水线技术的发明密不可分,因此当你喝着可乐、吹着空调、坐在特斯拉里拿着智能手机刷这篇文章时需要感谢流水线…

《算法技术手册》一2.4.6 二次方的算法性能
2.4.6 二次方的算法性能 现在考虑一个类似的问题:两个n位的整数相乘。例2-4展示了使用小学课堂上学过的算法实现的乘法运算,其中n位数字的表示方法与之前的加法一样。 例2-4:mult乘法的Java实现 public static void mult (int[] n1, int[] n2…

如何使用 OpenCV 实现图像均衡?
来源 | 小白视觉志头图 | 下载于视觉中国我们已经练习了很多图像处理——操作图像(精确地说是图像矩阵)。为此,我们探索了图像的均衡方法,以便在一定程度上增强对比度,以使被处理的图像看起来比原始图像更好࿰…

《中国人工智能学会通讯》——1.42 理解情感
1.42 理解情感 安德鲁摩尔认为,人工智能能“感受”人类情感是人工智能研究领域最重要、也最先进的一个方向。扬波利斯基认为,计算机能够理解语言的能力最终会向人和计算机“无缝沟通”的方向发展。 越来越精准的图像、声音和面部识别系统能让计算机更好探…

matlab中help所有函数功能的英文翻译
doc funname 在帮助浏览器中打开帮助文档help funname 在命令窗口打开帮助文档helpbrowser 直接打开帮助浏览器lookfor funname 搜索某个关键字相关函数demo 打开视频教程 转http://blog.renren.com/share/239121107/690877048 里面有些不全的,自己用到的已添加…

C# 静态构造函数
(1)用于对静态字段、只读字段等的初始化。 (2)添加static关键字,不能添加访问修饰符,因为静态构造函数都是私有的。 (3)类的静态构造函数在给定应用程序域中…

破解数据流通痛点,华控清交的隐私计算之道
从无序中寻找踪迹,从眼前事探索未来。 正值 IT 黄金十年新开端, CSDN 欲以中立技术社区专业、客观的角度,深度探讨中国前沿 IT 技术演进,现在推出年度重磅企划栏目——「拟合」,通过对话企业高管大咖,跟踪报…

mac系统添加VSCode到右键菜单(转)
转自:https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/001470969077294a6455fc9cd1f48b69f82cd05e7fa9b40000 在Mac系统上,Finder选中一个目录,右键菜单并没有“通过Code打开”这个操作。不过我们可以…

在 C# 中通过 P/Invoke 调用Win32 DLL
,.NET Framework 1.0 或 1.1 版类库中存在任何 Windows 所没有的功能限制都不足为怪。毕竟,32 位的 Windows(不管何种版本)是一个成熟的操作系统,为广大客户服务了十多年。相比之下,.NET Framework 却是一个…

xp/2003开关3389指令
开启3389: echo offtitle 开启3389clsrem 开启3389reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 00000000 /f >nulecho.echo 提示你:3389已经开启 关闭3389&…

TIOBE 新榜单:Python 超越 Java 重回第二,Rust 崛起
作者 | 苏宓出品 | CSDN(ID:CSDNnews)TIOBE 官方最新发布了 5 月的编程语言榜单,不妨一起来看一下本月榜单中又有哪些最新的变化呢?Python 重回第二和 4 月相比,本月榜单的 TOP 10 中变化最大的非 Python 与…

Docker编排工具Fig介绍
本文讲的是Docker编排工具Fig介绍,【编者的话】Fig是一个基于Docker的用于快速搭建开发环境的工具,目前Fig团队已经加入Docker公司。Fig通过一个配置文件来管理多个Docker容器,非常适合组合使用多个容器进行开发的场景。Fig可以和Docker一起来…

java调用ffmpeg,mencoder进行视频转换,读取时长等
2019独角兽企业重金招聘Python工程师标准>>> 以前做的一个基于ffmpeg的视频格式转换的程序,现在抽空整理一下,很多地方都是从别的大神那借鉴的,只是把自己的觉得有用的,对别人有帮助的拿出来分享分享,下面是…

数字人民币实现可控匿名交易?产业升级离不开安全可信的“数字底座”
自央行进行数字人民币试点测试工作以来,人们讨论最多的可能是它的便捷性、匿名性。不过,它的意义远不止于人类个体层面。 作为一种面向未来的货币形式,在未来数字经济时代,央行数字人民币的普及无疑将加速全球资产数字化和身份数…

apache+tomcat 搭建负载均衡系统
apachetomcatmod_jk 搭建负载均衡系统。0.os系统采用centos6.8 x64 2.6.32-642.el6.x86_641.首先安装好jdk环境本次采用jdk-8u111-linux-x64.gz jdk和jre的安装目录要不同,否则的话lib目录下没有dt.jar 和tools.jar 要配置好环境变量如下 vi /etc/profile #ad…

从普本到北大:我的跨校跨专业考研经验
首先做一个我考研情况的简介。 经历了2013年考研的混战,据说是史上考研人数顶峰的年份,因为2014改革,不再有自费生之后,人民群众对于所谓学术硕士的需求量激减,继 而投奔价格费用相当,读书年份较少的专业硕…

C#中使用DirectX编程
我感觉声音的播放比较简单。我们从播放声音开始。为什么我这么觉得?我也不知道。这里是展示最最最最最简单的DirectX播放声音的例子,我尽量省略了无关的代码。最后的代码只有19行,够简单了吧? 准备工作:1.安装了Direc…

40+场面试,100%通过率,我想分享的14条经验
来源 | 陈同学在搬砖头图 | 下载于视觉中国大家好,我是陈同学,首先来一个简单的自我介绍和个人的经历分享。我的本科和硕士均就读于哈工大,在研究生期1年时间内自学操作系统、计算机网络、C、数据结构等,累计学习30本书、500博客文…

云端卫士架构师讲DDoS攻击的智能防御之道
DDoS即分布式拒绝服务攻击,这是一场关乎资源的较量,攻击者通过自己控制的大量僵尸主机,向目标设施(服务器、运营商网络和基础架构等)发起洪水猛兽般的流量型攻击,或是连绵不绝的应用型攻击。 如果将受害者比…

C#中方法参数的四种类型
C#中方法的参数有四种类型:-值参数:不含任何修饰符。方法中的形参是实参的一份拷贝,形参的改变不会影响到内存中实参的的值,实参是安全的。-引用参数:以ref修饰符声明。传递的参数实…

赠书 | 算力时代,用 Python 来快速解决复杂问题
Python作为一种编程语言,拥有简洁、高效的表达能力。与此同时,Python语言环境中还配备各种软件库,即模块。结合实际问题,选择适当的模块,便可生成简单、快速、正确的程序。书中列举了一些数值计算的简单例题࿰…

用for实现Go的while和do...while
Go的while和do...while实现 Go语言没有while和do...while语法,我们可以通过for实现:即break在业务代码执行前相当与while,break在业务代码执行后相当do...while while for {if condition {break}xxxxxxxx } do...while for {xxxxxxxxif cond…

DTCC:数据库安全重点在数据拷贝过程中
本文讲的是DTCC:数据库安全重点在数据拷贝过程中,2017年5月11日-13日,2017中国数据库技术大会于北京国际会议中心盛大开幕。作为国内最受关注的数据库技术大会,本届大会以“数据驱动价值发现”为主题,汇集多个领域的百…

Log4J配置方式Java工程测试
2019独角兽企业重金招聘Python工程师标准>>> Log4J配置方式 1、 导入jar包 Commons-logging .jarLog4j-1.2.17.jar2、 编写log4j.properties 文件 ############## ############################## 优先级 INFO ,输出到console_log 和filelog 两个位置 log4j.root…

C#“装箱”(boxing)与“拆箱”(unboxing)
装箱和拆箱:任何值类型、引用类型可以和object(对象)类型之间进行转换。装箱转换是指将一个值类型隐式或显式地转换成一个object类型,或者把这个值类型转换成一个被该值类型应用的接口类型(interface-type)…

无人机、IoT 设备都有漏洞?专访以色列老牌安全企业Check Point | 拟合
从无序中寻找踪迹,从眼前事探索未来。2021 年正值黄金十年新开端,CSDN 以中立技术社区专业、客观的角度,深度探讨中国前沿 IT 技术演进,推出年度重磅企划栏目——「拟合」,通过对话企业技术高管大咖,跟踪报…