当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  用C#写计算器程序(二)

 用C#写计算器程序(二)

点击次数:16 次 发布日期:2008-11-26 14:42:14 作者:源代码网
源代码网推荐     
源代码网推荐  源程序
源代码网推荐  
源代码网推荐  //基本的计算器
源代码网推荐  //蚕蛹 2001.11.26
源代码网推荐  //Using C#
源代码网推荐  //E-mail:sillnet@hotmail.com
源代码网推荐  using System;
源代码网推荐  using System.Drawing;
源代码网推荐  using System.Collections;
源代码网推荐  using System.ComponentModel;
源代码网推荐  using System.Windows.Forms;
源代码网推荐  
源代码网推荐  namespace wincalc
源代码网推荐  {
源代码网推荐  ///
源代码网推荐  /// Summary description for calcForm.
源代码网推荐  ///
源代码网推荐  public class calcForm : System.Windows.Forms.Form
源代码网推荐  {
源代码网推荐  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 button10;
源代码网推荐  private System.Windows.Forms.Button bClr;
源代码网推荐  private System.Windows.Forms.Button bDot;
源代码网推荐  private System.Windows.Forms.Button bPlus;
源代码网推荐  private System.Windows.Forms.Button bSub;
源代码网推荐  private System.Windows.Forms.Button bMul;
源代码网推荐  private System.Windows.Forms.Button bDiv;
源代码网推荐  private System.Windows.Forms.Button bEqu;
源代码网推荐  private System.Windows.Forms.TextBox txtCalc;
源代码网推荐  
源代码网推荐  //以下是要添加的代码
源代码网推荐  //定义变量
源代码网推荐  Double dblAcc;
源代码网推荐  Double dblSec;
源代码网推荐  bool blnClear,blnFrstOpen;
源代码网推荐  String strOper;
源代码网推荐  //以上是添加的代码
源代码网推荐  ///
源代码网推荐  /// Required designer variable.
源代码网推荐  ///
源代码网推荐  private System.ComponentModel.Container components = null;
源代码网推荐  
源代码网推荐  public calcForm()
源代码网推荐  {
源代码网推荐  //
源代码网推荐  // Required for Windows Form Designer support
源代码网推荐  //
源代码网推荐  InitializeComponent();
源代码网推荐  
源代码网推荐  //
源代码网推荐  // TODO: Add any constructor code after InitializeComponent call
源代码网推荐  //
源代码网推荐  
源代码网推荐  //以下是要添加的代码
源代码网推荐  //初始化设量
源代码网推荐  dblAcc=0;
源代码网推荐  dblSec=0;
源代码网推荐  blnFrstOpen=true;
源代码网推荐  blnClear=true;
源代码网推荐  strOper=new string("=",1);
源代码网推荐  //以上是添加的代码
源代码网推荐  }
源代码网推荐  
源代码网推荐  ///
源代码网推荐  /// Clean up any resources being used.
源代码网推荐  ///
源代码网推荐  protected override void Dispose( bool disposing )
源代码网推荐  {
源代码网推荐  if( disposing )
源代码网推荐  {
源代码网推荐  if(components != null)
源代码网推荐  {
源代码网推荐  components.Dispose();
源代码网推荐  }
源代码网推荐  }
源代码网推荐  base.Dispose( disposing );
源代码网推荐  }
源代码网推荐  
源代码网推荐  #region Windows Form Designer generated code
源代码网推荐  ///
源代码网推荐  /// Required method for Designer support - do not modify
源代码网推荐  /// the contents of this method with the code editor.
源代码网推荐  ///
源代码网推荐  private void InitializeComponent()
源代码网推荐  {
源代码网推荐  this.bPlus = new System.Windows.Forms.Button();
源代码网推荐  this.bMul = new System.Windows.Forms.Button();
源代码网推荐  this.bDot = new System.Windows.Forms.Button();
源代码网推荐  this.txtCalc = new System.Windows.Forms.TextBox();
源代码网推荐  this.bClr = new System.Windows.Forms.Button();
源代码网推荐  this.bDiv = new System.Windows.Forms.Button();
源代码网推荐  this.bSub = new System.Windows.Forms.Button();
源代码网推荐  this.button8 = new System.Windows.Forms.Button();
源代码网推荐  this.button9 = new System.Windows.Forms.Button();
源代码网推荐  this.bEqu = new System.Windows.Forms.Button();
源代码网推荐  this.button10 = 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.button1 = new System.Windows.Forms.Button();
源代码网推荐  this.button2 = new System.Windows.Forms.Button();
源代码网推荐  this.button3 = new System.Windows.Forms.Button();
源代码网推荐  this.SuspendLayout();
源代码网推荐  //
源代码网推荐  // bPlus
源代码网推荐  //
源代码网推荐  this.bPlus.BackColor = System.Drawing.SystemColors.Control;
源代码网推荐  this.bPlus.ForeColor = System.Drawing.SystemColors.ControlText;
源代码网推荐  this.bPlus.Location = new System.Drawing.Point(208, 112);
源代码网推荐  this.bPlus.Name = "bPlus";
源代码网推荐  this.bPlus.Size = new System.Drawing.Size(32, 80);
源代码网推荐  this.bPlus.TabIndex = 1;
源代码网推荐  this.bPlus.Text = "+";
源代码网推荐  //以下是要添加的代码
源代码网推荐  bPlus.Click += new System.EventHandler(this.btn_Oper);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // bMul
源代码网推荐  //
源代码网推荐  this.bMul.Location = new System.Drawing.Point(160, 112);
源代码网推荐  this.bMul.Name = "bMul";
源代码网推荐  this.bMul.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.bMul.TabIndex = 1;
源代码网推荐  this.bMul.Text = "*";
源代码网推荐  //以下是要添加的代码
源代码网推荐  bMul.Click += new System.EventHandler(this.btn_Oper);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // bDot
源代码网推荐  //
源代码网推荐  this.bDot.ForeColor = System.Drawing.Color.Black;
源代码网推荐  this.bDot.Location = new System.Drawing.Point(112, 208);
源代码网推荐  this.bDot.Name = "bDot";
源代码网推荐  this.bDot.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.bDot.TabIndex = 0;
源代码网推荐  this.bDot.Text = ".";
源代码网推荐  //以下是要添加的代码
源代码网推荐  bDot.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // txtCalc
源代码网推荐  //
源代码网推荐  this.txtCalc.Location = new System.Drawing.Point(16, 24);
源代码网推荐  this.txtCalc.Name = "txtCalc";
源代码网推荐  this.txtCalc.ReadOnly = true;
源代码网推荐  this.txtCalc.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
源代码网推荐  this.txtCalc.Size = new System.Drawing.Size(224, 21);
源代码网推荐  this.txtCalc.TabIndex = 2;
源代码网推荐  this.txtCalc.Text = "";
源代码网推荐  //
源代码网推荐  // bClr
源代码网推荐  //
源代码网推荐  this.bClr.BackColor = System.Drawing.SystemColors.Control;
源代码网推荐  this.bClr.ForeColor = System.Drawing.SystemColors.ControlText;
源代码网推荐  this.bClr.Location = new System.Drawing.Point(208, 64);
源代码网推荐  this.bClr.Name = "bClr";
源代码网推荐  this.bClr.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.bClr.TabIndex = 0;
源代码网推荐  this.bClr.Text = "AC";
源代码网推荐  //以下是要添加的代码
源代码网推荐  bClr.Click += new System.EventHandler(this.btn_clr);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // bDiv
源代码网推荐  //
源代码网推荐  this.bDiv.Location = new System.Drawing.Point(160, 160);
源代码网推荐  this.bDiv.Name = "bDiv";
源代码网推荐  this.bDiv.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.bDiv.TabIndex = 1;
源代码网推荐  this.bDiv.Text = "/";
源代码网推荐  //以下是要添加的代码
源代码网推荐  bDiv.Click += new System.EventHandler(this.btn_Oper);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // bSub
源代码网推荐  //
源代码网推荐  this.bSub.Location = new System.Drawing.Point(160, 64);
源代码网推荐  this.bSub.Name = "bSub";
源代码网推荐  this.bSub.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.bSub.TabIndex = 1;
源代码网推荐  this.bSub.Text = "-";
源代码网推荐  //以下是要添加的代码
源代码网推荐  bSub.Click += new System.EventHandler(this.btn_Oper);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button8
源代码网推荐  //
源代码网推荐  this.button8.Location = new System.Drawing.Point(16, 64);
源代码网推荐  this.button8.Name = "button8";
源代码网推荐  this.button8.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.button8.TabIndex = 0;
源代码网推荐  this.button8.Text = "7";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button8.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button9
源代码网推荐  //
源代码网推荐  this.button9.Location = new System.Drawing.Point(64, 64);
源代码网推荐  this.button9.Name = "button9";
源代码网推荐  this.button9.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.button9.TabIndex = 0;
源代码网推荐  this.button9.Text = "8";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button9.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // bEqu
源代码网推荐  //
源代码网推荐  this.bEqu.BackColor = System.Drawing.SystemColors.Control;
源代码网推荐  this.bEqu.ForeColor = System.Drawing.SystemColors.ControlText;
源代码网推荐  this.bEqu.Location = new System.Drawing.Point(160, 208);
源代码网推荐  this.bEqu.Name = "bEqu";
源代码网推荐  this.bEqu.Size = new System.Drawing.Size(80, 32);
源代码网推荐  this.bEqu.TabIndex = 1;
源代码网推荐  this.bEqu.Text = "=";
源代码网推荐  //以下是要添加的代码
源代码网推荐  bEqu.Click += new System.EventHandler(this.btn_equ);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button10
源代码网推荐  //
源代码网推荐  this.button10.Location = new System.Drawing.Point(112, 64);
源代码网推荐  this.button10.Name = "button10";
源代码网推荐  this.button10.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.button10.TabIndex = 0;
源代码网推荐  this.button10.Text = "9";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button10.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button4
源代码网推荐  //
源代码网推荐  this.button4.Location = new System.Drawing.Point(112, 160);
源代码网推荐  this.button4.Name = "button4";
源代码网推荐  this.button4.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.button4.TabIndex = 0;
源代码网推荐  this.button4.Text = "3";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button4.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button5
源代码网推荐  //
源代码网推荐  this.button5.Location = new System.Drawing.Point(16, 112);
源代码网推荐  this.button5.Name = "button5";
源代码网推荐  this.button5.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.button5.TabIndex = 0;
源代码网推荐  this.button5.Text = "4";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button5.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button6
源代码网推荐  //
源代码网推荐  this.button6.Location = new System.Drawing.Point(64, 112);
源代码网推荐  this.button6.Name = "button6";
源代码网推荐  this.button6.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.button6.TabIndex = 0;
源代码网推荐  this.button6.Text = "5";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button6.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button7
源代码网推荐  //
源代码网推荐  this.button7.Location = new System.Drawing.Point(112, 112);
源代码网推荐  this.button7.Name = "button7";
源代码网推荐  this.button7.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.button7.TabIndex = 0;
源代码网推荐  this.button7.Text = "6";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button7.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button1
源代码网推荐  //
源代码网推荐  this.button1.BackColor = System.Drawing.SystemColors.Control;
源代码网推荐  this.button1.ForeColor = System.Drawing.Color.Black;
源代码网推荐  this.button1.Location = new System.Drawing.Point(16, 208);
源代码网推荐  this.button1.Name = "button1";
源代码网推荐  this.button1.Size = new System.Drawing.Size(80, 32);
源代码网推荐  this.button1.TabIndex = 0;
源代码网推荐  this.button1.Text = "0";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button1.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button2
源代码网推荐  //
源代码网推荐  this.button2.Location = new System.Drawing.Point(16, 160);
源代码网推荐  this.button2.Name = "button2";
源代码网推荐  this.button2.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.button2.TabIndex = 0;
源代码网推荐  this.button2.Text = "1";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button2.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // button3
源代码网推荐  //
源代码网推荐  this.button3.Location = new System.Drawing.Point(64, 160);
源代码网推荐  this.button3.Name = "button3";
源代码网推荐  this.button3.Size = new System.Drawing.Size(32, 32);
源代码网推荐  this.button3.TabIndex = 0;
源代码网推荐  this.button3.Text = "2";
源代码网推荐  //以下是要添加的代码
源代码网推荐  button3.Click += new System.EventHandler(this.btn_clk);
源代码网推荐  //以上是添加的代码
源代码网推荐  //
源代码网推荐  // calcForm
源代码网推荐  //
源代码网推荐  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
源代码网推荐  this.ClientSize = new System.Drawing.Size(256, 261);
源代码网推荐  this.Controls.AddRange(new System.Windows.Forms.Control[] {
源代码网推荐  this.txtCalc,
源代码网推荐  this.bEqu,
源代码网推荐  this.bDiv,
源代码网推荐  this.bMul,
源代码网推荐  this.bSub,
源代码网推荐  this.bPlus,
源代码网推荐  this.bDot,
源代码网推荐  this.bClr,
源代码网推荐  this.button10,
源代码网推荐  this.button9,
源代码网推荐  this.button8,
源代码网推荐  this.button7,
源代码网推荐  this.button6,
源代码网推荐  this.button5,
源代码网推荐  this.button4,
源代码网推荐  this.button3,
源代码网推荐  this.button2,
源代码网推荐  this.button1});
源代码网推荐  this.Name = "calcForm";
源代码网推荐  this.Text = "计算器";
源代码网推荐  this.ResumeLayout(false);
源代码网推荐  
源代码网推荐  }
源代码网推荐  #endregion
源代码网推荐  
源代码网推荐  //以下是要添加的代码
源代码网推荐  //小数点的操作
源代码网推荐  private void btn_clk(object obj,EventArgs ea){
源代码网推荐  if(blnClear)
源代码网推荐  txtCalc.Text="";
源代码网推荐  Button b3=(Button)obj;
源代码网推荐  txtCalc.Text+=b3.Text;
源代码网推荐  if(txtCalc.Text==".")
源代码网推荐  txtCalc.Text="0.";
源代码网推荐  dblSec=Convert.ToDouble(txtCalc.Text);
源代码网推荐  blnClear=false;
源代码网推荐  }
源代码网推荐  
源代码网推荐  //程序开始点
源代码网推荐  private static void Main(){
源代码网推荐  Application.Run(new calcForm());
源代码网推荐  }
源代码网推荐  
源代码网推荐  private void btn_Oper(object obj,EventArgs ea){
源代码网推荐  Button tmp=(Button)obj;
源代码网推荐  strOper=tmp.Text;
源代码网推荐  if(blnFrstOpen)
源代码网推荐  dblAcc=dblSec;
源代码网推荐  else
源代码网推荐  calc();
源代码网推荐  blnFrstOpen=false;
源代码网推荐  blnClear=true;
源代码网推荐  }
源代码网推荐  
源代码网推荐  //等号运算
源代码网推荐  private void btn_equ(object obj,EventArgs ea){
源代码网推荐  calc();
源代码网推荐  }
源代码网推荐  
源代码网推荐  //所有运算操作
源代码网推荐  private void calc(){
源代码网推荐  switch(strOper){
源代码网推荐  case "+":
源代码网推荐  dblAcc+=dblSec; //加号运算
源代码网推荐  break;
源代码网推荐  case "-":
源代码网推荐  dblAcc-=dblSec; //减号运算
源代码网推荐  break;
源代码网推荐  case "*":
源代码网推荐  dblAcc*=dblSec; //乘号运算
源代码网推荐  break;
源代码网推荐  case "/":
源代码网推荐  dblAcc/=dblSec; //除号运算
源代码网推荐  break;
源代码网推荐  }
源代码网推荐  strOper="="; //等号运算
源代码网推荐  blnFrstOpen=true;
源代码网推荐  txtCalc.Text=Convert.ToString(dblAcc);//将运算结果转换成字符类型,并输出
源代码网推荐  dblSec=dblAcc;//将运算数A的值放入运算数B中,以便后面运算
源代码网推荐  }
源代码网推荐  
源代码网推荐  //清除按钮
源代码网推荐  private void btn_clr(object obj,EventArgs ea){
源代码网推荐  clear();
源代码网推荐  }
源代码网推荐  
源代码网推荐  //清除按钮的操作
源代码网推荐  private void clear(){
源代码网推荐  dblAcc=0;
源代码网推荐  dblSec=0;
源代码网推荐  blnFrstOpen=true;
源代码网推荐  txtCalc.Text="";
源代码网推荐  txtCalc.Focus();//设置焦点为txtCalc
源代码网推荐  }
源代码网推荐  //以上是添加的代码
源代码网推荐  }
源代码网推荐  
源代码网推荐  }
源代码网推荐  
源代码网推荐    以上只是一个简单的用C#开发的Windows Form程序,在vs.nt bate2+windows 2000专业版编译通过.向正在学习VS.net网友们抛砖引玉,其功能上还有很多不完善的地方,欢迎网友们将其完善。
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华