一、设计思路
用C#写的比较基础的Windows Form 程序,该计算器实现了基础的数学运算,如加,减,乘,除等任务.主要是通过该程序学习VS.net的
编程环境,以及windows Form程序.主要针对初学者
我们分两部份来实现程序,
第一部份.程序界面
1,以下控件表
控件类型 Name Text
form calcForm 计算器
button button1 0
.....
button10 9
bDot .(小数点) 小数点按钮
bPlus +(加号) 加号按钮
bSub -(减号) 减号按钮
bMul *(乘号) 乘号按钮
bDiv /(除号) 除号按钮
bEqu =(等号) 等号按钮
bClr AC 清除按钮
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;
}
程序中所涉及到的一些问题,都给解决了,现在我们动手吧!操上我的利器,去完成任务吧!
源程序
//基本的计算器
//蚕蛹 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);