程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> 用C#寫計算器程序

用C#寫計算器程序

編輯:C#基礎知識

  一、設計思路

  用C#寫的比較基礎的windows Form 程序,該計算器實現了基礎的數學運算,如加,減,乘,除等任務.主要是通過該程序學習vs.net的

  編程環境,以及windows Form程序.主要針對初學者

  我們分兩部份來實現程序,

  第一部份.程序界面

  1,以下控件表

  控件類型NameTextformcalcForm計算器buttonbutton10 .....   button109 bDot.(小數點) 小數點按鈕 bPlus+(加號) 加號按鈕 bSub -(減號) 減號按鈕 bMul*(乘號) 乘號按鈕 bDiv/(除號) 除號按鈕 bEqu =(等號) 等號按鈕 bClrAC 清除按鈕 textBox txtCalc (空值) 用來顯示輸入及輸出結果

  第二部份,程序結構

  1,定義以下變量

  

Double dblAcc; //運算數A
Double dblSec; //運算數B
bool blnClear,blnFrstOpen;//布爾類型用來判斷清除與否,以及第一個顯示字符
String strOper;//通過獲取strOper的值來決定運算+,-,*,/,=

  2,用以下方法來實現按鈕的動作

  例: bDot.click+=net EventHandler(btn_clk);//EventHandler類是事件代表類,用來注冊事件的處理方法.

  //第一個參數是object類型,指向發出事件的對象;

  //第二個參數是EventArgs類型,包含了關於這個事件的數據

  3,用以下方法來判斷運算以及運算操作

  

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;
}

  4,小數點運算

  

//先判斷是否已經按了小數點按鈕,如果按了,最0.x來代替運算變量,並且將轉換成Double數型數值
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;
}

  程序中所涉及到的一些問題,都給解決了,現在我們動手吧!操上我的利器,去完成任務吧!

  二、開發過程

  下面跟我們一步步學習吧.

  先啟動vs.net,建一個空的C#項目,如下(pic1.jpg)

  名稱用wincalc,位置就自己設定好了。確定後,從vs.net開發環境中右邊的工具箱中加入控件表的控件,,注意修改txtCalc的如屬性ReadOnly=True,RightTOLeft=Yes(從右至左輸入),最後畫面如何(pic2)

  添加代碼如源程序所示:

  最後大功告成,按Ctrl+F5吧,看看結果.如圖(pic3.jpg)

三、源程序

  

//基本的計算器
//蠶蛹 2001.11.26
//Using C#
//E-mail:[email protected]
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網友們拋磚引玉,其功能上還有很多不完善的地方,歡迎網友們將其完善。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved