用C#实现简易计算器(Windows)

      通过对C#的学习,总想编个小程序试试,今天终于如愿所偿.刚开始想的时候是挺简单的,但到后来遇到的困难越来越大.关键是不知道怎么实现对数据的存储,因为只是用到一个textBox来输入数据,显示结果.
      曾想在网上找到源代码,不过都没找到,有过放弃的想法,但后来一想自己的第一个程序就这么快就放弃,以后怎么学习,到最后终于把它实现了.虽然很多不足,不过对于自己的第一个程序还是比较满意的.
      在本程序是用Windows应用程序实现的,用textBox,button来共同实现.
      具体界面如下:
     
       
       
       具体代码实现如下:
     
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SimpleCalculator
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Button button5;
  private System.Windows.Forms.Button button6;
  private System.Windows.Forms.Button button7;
  private System.Windows.Forms.Button button8;
  private System.Windows.Forms.Button button9;
  private System.Windows.Forms.Button button0;
  private System.Windows.Forms.Button buttonDot;
  private System.Windows.Forms.Button buttonEqu;
  private System.Windows.Forms.Button buttonPlus;
  private System.Windows.Forms.Button buttonSub;
  private System.Windows.Forms.Button buttonMul;
  private System.Windows.Forms.Button buttonDiv;
  private System.Windows.Forms.Button buttonce;
  private System.Windows.Forms.Button buttonSqr;
  private System.Windows.Forms.Button buttonRec;
  private System.Windows.Forms.Button buttonDel;
  private System.Windows.Forms.Button buttonOFF;
  private System.Windows.Forms.Button buttonLOG;
  private System.Windows.Forms.Button buttonEXP;
  private System.Windows.Forms.Button buttonABS;
  private System.Windows.Forms.Button buttonPlusAndMul;
  private System.Windows.Forms.Button buttonSquare;
  /// <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.textBox1 = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.button5 = new System.Windows.Forms.Button();
   this.button6 = new System.Windows.Forms.Button();
   this.button7 = new System.Windows.Forms.Button();
   this.button8 = new System.Windows.Forms.Button();
   this.button9 = new System.Windows.Forms.Button();
   this.button0 = new System.Windows.Forms.Button();
   this.buttonDot = new System.Windows.Forms.Button();
   this.buttonEqu = new System.Windows.Forms.Button();
   this.buttonPlus = new System.Windows.Forms.Button();
   this.buttonSub = new System.Windows.Forms.Button();
   this.buttonMul = new System.Windows.Forms.Button();
   this.buttonDiv = new System.Windows.Forms.Button();
   this.buttonce = new System.Windows.Forms.Button();
   this.buttonPlusAndMul = new System.Windows.Forms.Button();
   this.buttonSquare = new System.Windows.Forms.Button();
   this.buttonSqr = new System.Windows.Forms.Button();
   this.buttonRec = new System.Windows.Forms.Button();
   this.buttonDel = new System.Windows.Forms.Button();
   this.buttonOFF = new System.Windows.Forms.Button();
   this.buttonLOG = new System.Windows.Forms.Button();
   this.buttonEXP = new System.Windows.Forms.Button();
   this.buttonABS = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // textBox1
   //
   this.textBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
   this.textBox1.Location = new System.Drawing.Point(24, 16);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(176, 21);
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "";
   this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
   //
   // button1
   //
   this.button1.BackColor = System.Drawing.Color.Wheat;
   this.button1.Location = new System.Drawing.Point(48, 72);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(32, 24);
   this.button1.TabIndex = 1;
   this.button1.Text = "1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.BackColor = System.Drawing.Color.Wheat;
   this.button2.Location = new System.Drawing.Point(80, 72);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(32, 24);
   this.button2.TabIndex = 2;
   this.button2.Text = "2";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.BackColor = System.Drawing.Color.Wheat;
   this.button3.Location = new System.Drawing.Point(112, 72);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(32, 24);
   this.button3.TabIndex = 3;
   this.button3.Text = "3";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.BackColor = System.Drawing.Color.Wheat;
   this.button4.Location = new System.Drawing.Point(48, 96);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(32, 24);
   this.button4.TabIndex = 4;
   this.button4.Text = "4";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // button5
   //
   this.button5.BackColor = System.Drawing.Color.Wheat;
   this.button5.Location = new System.Drawing.Point(80, 96);
   this.button5.Name = "button5";
   this.button5.Size = new System.Drawing.Size(32, 24);
   this.button5.TabIndex = 5;
   this.button5.Text = "5";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // button6
   //
   this.button6.BackColor = System.Drawing.Color.Wheat;
   this.button6.Location = new System.Drawing.Point(112, 96);
   this.button6.Name = "button6";
   this.button6.Size = new System.Drawing.Size(32, 24);
   this.button6.TabIndex = 6;
   this.button6.Text = "6";
   this.button6.Click += new System.EventHandler(this.button6_Click);
   //
   // button7
   //
   this.button7.BackColor = System.Drawing.Color.Wheat;
   this.button7.Location = new System.Drawing.Point(48, 120);
   this.button7.Name = "button7";
   this.button7.Size = new System.Drawing.Size(32, 24);
   this.button7.TabIndex = 7;
   this.button7.Text = "7";
   this.button7.Click += new System.EventHandler(this.button7_Click);
   //
   // button8
   //
   this.button8.BackColor = System.Drawing.Color.Wheat;
   this.button8.Location = new System.Drawing.Point(80, 120);
   this.button8.Name = "button8";
   this.button8.Size = new System.Drawing.Size(32, 24);
   this.button8.TabIndex = 8;
   this.button8.Text = "8";
   this.button8.Click += new System.EventHandler(this.button8_Click);
   //
   // button9
   //
   this.button9.BackColor = System.Drawing.Color.Wheat;
   this.button9.Location = new System.Drawing.Point(112, 120);
   this.button9.Name = "button9";
   this.button9.Size = new System.Drawing.Size(32, 24);
   this.button9.TabIndex = 9;
   this.button9.Text = "9";
   this.button9.Click += new System.EventHandler(this.button9_Click);
   //
   // button0
   //
   this.button0.BackColor = System.Drawing.Color.Wheat;
   this.button0.Location = new System.Drawing.Point(48, 144);
   this.button0.Name = "button0";
   this.button0.Size = new System.Drawing.Size(32, 24);
   this.button0.TabIndex = 10;
   this.button0.Text = "0";
   this.button0.Click += new System.EventHandler(this.button0_Click);
   //
   // buttonDot
   //
   this.buttonDot.BackColor = System.Drawing.Color.Wheat;
   this.buttonDot.Location = new System.Drawing.Point(80, 144);
   this.buttonDot.Name = "buttonDot";
   this.buttonDot.Size = new System.Drawing.Size(32, 24);
   this.buttonDot.TabIndex = 11;
   this.buttonDot.Text = ".";
   this.buttonDot.Click += new System.EventHandler(this.buttonDot_Click);
   //
   // buttonEqu
   //
   this.buttonEqu.BackColor = System.Drawing.Color.Wheat;
   this.buttonEqu.Location = new System.Drawing.Point(176, 120);
   this.buttonEqu.Name = "buttonEqu";
   this.buttonEqu.Size = new System.Drawing.Size(32, 48);
   this.buttonEqu.TabIndex = 12;
   this.buttonEqu.Text = "=";
   this.buttonEqu.Click += new System.EventHandler(this.buttonEqu_Click);
   //
   // buttonPlus
   //
   this.buttonPlus.BackColor = System.Drawing.Color.Wheat;
   this.buttonPlus.Location = new System.Drawing.Point(144, 72);
   this.buttonPlus.Name = "buttonPlus";
   this.buttonPlus.Size = new System.Drawing.Size(32, 24);
   this.buttonPlus.TabIndex = 13;
   this.buttonPlus.Text = "+";
   this.buttonPlus.Click += new System.EventHandler(this.buttonPlus_Click);
   //
   // buttonSub
   //
   this.buttonSub.BackColor = System.Drawing.Color.Wheat;
   this.buttonSub.Location = new System.Drawing.Point(144, 96);
   this.buttonSub.Name = "buttonSub";
   this.buttonSub.Size = new System.Drawing.Size(32, 24);
   this.buttonSub.TabIndex = 14;
   this.buttonSub.Text = "-";
   this.buttonSub.Click += new System.EventHandler(this.buttonSub_Click);
   //
   // buttonMul
   //
   this.buttonMul.BackColor = System.Drawing.Color.Wheat;
   this.buttonMul.Location = new System.Drawing.Point(144, 120);
   this.buttonMul.Name = "buttonMul";
   this.buttonMul.Size = new System.Drawing.Size(32, 24);
   this.buttonMul.TabIndex = 15;
   this.buttonMul.Text = "*";
   this.buttonMul.Click += new System.EventHandler(this.buttonMul_Click);
   //
   // buttonDiv
   //
   this.buttonDiv.BackColor = System.Drawing.Color.Wheat;
   this.buttonDiv.Location = new System.Drawing.Point(144, 144);
   this.buttonDiv.Name = "buttonDiv";
   this.buttonDiv.Size = new System.Drawing.Size(32, 24);
   this.buttonDiv.TabIndex = 16;
   this.buttonDiv.Text = "/";
   this.buttonDiv.Click += new System.EventHandler(this.buttonDiv_Click);
   //
   // buttonce
   //
   this.buttonce.BackColor = System.Drawing.Color.Tomato;
   this.buttonce.Location = new System.Drawing.Point(176, 72);
   this.buttonce.Name = "buttonce";
   this.buttonce.Size = new System.Drawing.Size(32, 48);
   this.buttonce.TabIndex = 17;
   this.buttonce.Text = "ON/C";
   this.buttonce.Click += new System.EventHandler(this.buttonce_Click);
   //
   // buttonPlusAndMul
   //
   this.buttonPlusAndMul.BackColor = System.Drawing.Color.Wheat;
   this.buttonPlusAndMul.Location = new System.Drawing.Point(112, 144);
   this.buttonPlusAndMul.Name = "buttonPlusAndMul";
   this.buttonPlusAndMul.Size = new System.Drawing.Size(32, 24);
   this.buttonPlusAndMul.TabIndex = 18;
   this.buttonPlusAndMul.Text = "+/-";
   this.buttonPlusAndMul.Click += new System.EventHandler(this.buttonPlusAndMul_Click);
   //
   // buttonSquare
   //
   this.buttonSquare.BackColor = System.Drawing.Color.Wheat;
   this.buttonSquare.Location = new System.Drawing.Point(16, 72);
   this.buttonSquare.Name = "buttonSquare";
   this.buttonSquare.Size = new System.Drawing.Size(32, 24);
   this.buttonSquare.TabIndex = 19;
   this.buttonSquare.Text = "X*X";
   this.buttonSquare.Click += new System.EventHandler(this.buttonSquare_Click);
   //
   // buttonSqr
   //
   this.buttonSqr.BackColor = System.Drawing.Color.Wheat;
   this.buttonSqr.Location = new System.Drawing.Point(16, 96);
   this.buttonSqr.Name = "buttonSqr";
   this.buttonSqr.Size = new System.Drawing.Size(32, 24);
   this.buttonSqr.TabIndex = 20;
   this.buttonSqr.Text = "Sqrt";
   this.buttonSqr.Click += new System.EventHandler(this.buttonSqr_Click);
   //
   // buttonRec
   //
   this.buttonRec.BackColor = System.Drawing.Color.Wheat;
   this.buttonRec.Location = new System.Drawing.Point(16, 120);
   this.buttonRec.Name = "buttonRec";
   this.buttonRec.Size = new System.Drawing.Size(32, 24);
   this.buttonRec.TabIndex = 21;
   this.buttonRec.Text = "1/X";
   this.buttonRec.Click += new System.EventHandler(this.buttonRec_Click);
   //
   // buttonDel
   //
   this.buttonDel.BackColor = System.Drawing.Color.Wheat;
   this.buttonDel.Location = new System.Drawing.Point(16, 144);
   this.buttonDel.Name = "buttonDel";
   this.buttonDel.Size = new System.Drawing.Size(32, 24);
   this.buttonDel.TabIndex = 22;
   this.buttonDel.Text = "→";
   this.buttonDel.Click += new System.EventHandler(this.buttonDel_Click);
   //
   // buttonOFF
   //
   this.buttonOFF.BackColor = System.Drawing.Color.Tomato;
   this.buttonOFF.Location = new System.Drawing.Point(112, 48);
   this.buttonOFF.Name = "buttonOFF";
   this.buttonOFF.Size = new System.Drawing.Size(96, 24);
   this.buttonOFF.TabIndex = 27;
   this.buttonOFF.Text = "OFF";
   this.buttonOFF.Click += new System.EventHandler(this.buttonOFF_Click);
   //
   // buttonLOG
   //
   this.buttonLOG.BackColor = System.Drawing.Color.Wheat;
   this.buttonLOG.Location = new System.Drawing.Point(16, 48);
   this.buttonLOG.Name = "buttonLOG";
   this.buttonLOG.Size = new System.Drawing.Size(32, 24);
   this.buttonLOG.TabIndex = 28;
   this.buttonLOG.Text = "LOG";
   this.buttonLOG.Click += new System.EventHandler(this.buttonLOG_Click);
   //
   // buttonEXP
   //
   this.buttonEXP.BackColor = System.Drawing.Color.Wheat;
   this.buttonEXP.Location = new System.Drawing.Point(48, 48);
   this.buttonEXP.Name = "buttonEXP";
   this.buttonEXP.Size = new System.Drawing.Size(32, 24);
   this.buttonEXP.TabIndex = 29;
   this.buttonEXP.Text = "EXP";
   this.buttonEXP.Click += new System.EventHandler(this.buttonEXP_Click);
   //
   // buttonABS
   //
   this.buttonABS.BackColor = System.Drawing.Color.Wheat;
   this.buttonABS.Location = new System.Drawing.Point(80, 48);
   this.buttonABS.Name = "buttonABS";
   this.buttonABS.Size = new System.Drawing.Size(32, 24);
   this.buttonABS.TabIndex = 30;
   this.buttonABS.Text = "ABS";
   this.buttonABS.Click += new System.EventHandler(this.buttonABS_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.BackColor = System.Drawing.Color.Wheat;
   this.ClientSize = new System.Drawing.Size(224, 182);
   this.Controls.Add(this.buttonABS);
   this.Controls.Add(this.buttonEXP);
   this.Controls.Add(this.buttonLOG);
   this.Controls.Add(this.buttonOFF);
   this.Controls.Add(this.buttonDel);
   this.Controls.Add(this.buttonRec);
   this.Controls.Add(this.buttonSqr);
   this.Controls.Add(this.buttonSquare);
   this.Controls.Add(this.buttonPlusAndMul);
   this.Controls.Add(this.buttonce);
   this.Controls.Add(this.buttonDiv);
   this.Controls.Add(this.buttonMul);
   this.Controls.Add(this.buttonSub);
   this.Controls.Add(this.buttonPlus);
   this.Controls.Add(this.buttonEqu);
   this.Controls.Add(this.buttonDot);
   this.Controls.Add(this.button0);
   this.Controls.Add(this.button9);
   this.Controls.Add(this.button8);
   this.Controls.Add(this.button7);
   this.Controls.Add(this.button6);
   this.Controls.Add(this.button5);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.textBox1);
   this.Name = "Form1";
   this.Text = "简易计算器";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

 
  private double data1=0.0;
  private double data2=0.0;
  private bool flag=false;
  private bool flag1=false;
  private int Operator=0;
  private int Operator1=0;
  

  private void Form1_Load(object sender, System.EventArgs e)
  {
  }
  #region DataButtonClick
  private void DataButtonClick(int temp)
  {
   if(flag)
   {
    if(flag1)
    {
     textBox1.Text="";
     flag1=false;
    }
    data2=double.Parse(textBox1.Text+temp.ToString());
    textBox1.Text=data2.ToString();
   }
   else
   {
    data1=double.Parse(textBox1.Text+temp.ToString());
    textBox1.Text=data1.ToString();
   }

  }
  private void button0_Click(object sender, System.EventArgs e)
  {
   DataButtonClick(0);
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   
   DataButtonClick(1);
  
  }
  private void button2_Click(object sender, System.EventArgs e)
  {
   DataButtonClick(2);
  }
  private void button3_Click(object sender, System.EventArgs e)
  {
   DataButtonClick(3);
  }

  private void button4_Click(object sender, System.EventArgs e)
  {
   DataButtonClick(4);
  
  }

  private void button5_Click(object sender, System.EventArgs e)
  {
   
   DataButtonClick(5);
  }

  private void button6_Click(object sender, System.EventArgs e)
  {
   
   DataButtonClick(6);
  
  }

  private void button7_Click(object sender, System.EventArgs e)
  {
   DataButtonClick(7);
  }

  private void button8_Click(object sender, System.EventArgs e)
  {
   DataButtonClick(8);
  }

  private void button9_Click(object sender, System.EventArgs e)
  {
   DataButtonClick(9);
  }
  #endregion
  #region OperatorButtonClick
  private void OperatoButtonClick()
  {
   switch (Operator)
   {
    case 1:
     data1+=data2;
     break;
    case 2:
     data1-=data2;
     break;
    case 3:
     data1*=data2;
     break;
    case 4:
     if(data2==0)
     {
      textBox1.Text="!无法被0除";
     }
     data1/=data2;
     break;
    case 5:
     data1=data1;
     break;
    default:
     data1=double.Parse(textBox1.Text.ToString());
     break;
   }
   textBox1.Text=data1.ToString();
   flag=true;
   flag1=true;
   
  }
  //"+"的运算
  private void buttonPlus_Click(object sender, System.EventArgs e)
  {
   OperatoButtonClick();
   Operator=1;
   Operator1=1;
  }
   
  //"-"的运算
  private void buttonSub_Click(object sender, System.EventArgs e)
  {
   OperatoButtonClick();
   Operator=2;
   Operator1=2;    
  }
  //"*"的运算
  private void buttonMul_Click(object sender, System.EventArgs e)
  {
   OperatoButtonClick();
   Operator=3;
   Operator1=3;  
  }
  //"/"的运算
  private void buttonDiv_Click(object sender, System.EventArgs e)
  {
   OperatoButtonClick();
   Operator=4;
   Operator1=4;   
  }
  //"="的运算
  private void buttonEqu_Click(object sender, System.EventArgs e)
  {
   //连续两次输入"="
   if(Operator==5)
   {
    if(Operator1==1)
     data1+=data2;
    else if(Operator1==2)
     data1-=data2;
    else if(Operator1==3)
     data1*=data2;
    else if(Operator1==4)
     data1/=data2;
    textBox1.Text=data1.ToString();
    flag=true;
    flag1=true;    
   }
   else
    OperatoButtonClick();
   
   Operator=5;   
  }
  #endregion
  //复位
  private void buttonce_Click(object sender, System.EventArgs e)
  {
   textBox1.Text="";
   data1=data2=0.0;
   flag=false;
   flag1=false;
  }
  private void buttonDot_Click(object sender, System.EventArgs e)
  {
   char []  str=textBox1.Text.ToCharArray();
   for(int i=0;i<str.Length;i++)
    if(str[i]=='.')
    {
     textBox1.Text="#########";
     this.Close();
    }
   textBox1.Text=textBox1.Text+buttonDot.Text;
  }
  private void buttonSquare_Click(object sender, System.EventArgs e)
  {
   data1=data1*data1;
   textBox1.Text=data1.ToString();
   
  }
  private void buttonSqr_Click(object sender, System.EventArgs e)
  {
   if(data1<0)
    textBox1.Text="!负数不能求平方根";
   data1=Math.Sqrt(data1);
   textBox1.Text=data1.ToString();
  
  }
  private void buttonRec_Click(object sender, System.EventArgs e)
  {
   if(data1==0)
    textBox1.Text="0不可以求倒数";
   data1=1/data1;
   textBox1.Text=data1.ToString();      
  }
  private void buttonDel_Click(object sender, System.EventArgs e)
  {
   string str =textBox1.Text;
   textBox1.Text="";
   
   for(int i=0;i<str.Length-1;i++)
   {
    char ch=str[i];
    textBox1.Text =textBox1.Text+ch.ToString();
   }
  }  
  private void buttonPlusAndMul_Click(object sender, System.EventArgs e)
  {
   data1=0-data1;
   textBox1.Text=data1.ToString();
  }
  private void buttonOFF_Click(object sender, System.EventArgs e)
  {
   this.Close();
  }
  private void buttonLOG_Click(object sender, System.EventArgs e)
  {
   if(data1<0)
    textBox1.Text="!负数不能计算对数";
   textBox1.Text=Convert.ToString(Math.Log(data1));
  }
  private void buttonEXP_Click(object sender, System.EventArgs e)
  {
   textBox1.Text=Convert.ToString(Math.Exp(data1));
  
  }
  private void buttonABS_Click(object sender, System.EventArgs e)
  {
   textBox1.Text=Convert.ToString(Math.Abs(data1));
  }
 }
}




      本程序可以实现简单的加,减,乘,除 ,平方求倒,LOG,EXP等简单的操作,同样通过Math可以实现其他的.
    

     由于水平有限,本程序有不少的不足和缺点,望多多指教!


    

     下载用运行文件请到https://files.cnblogs.com/zhaozhan/SimpleCalculator1.rar
  
 
 
 

 

  
 

 

posted @ 2005-06-16 20:25  Asharp  阅读(4440)  评论(0编辑  收藏  举报