今天用C#做出来了一个图形化界面的Calculator ,+ -* /的基本功能都可以实现, 呵呵。
现在分享一下源代码:因为实在VS 2010中编辑的,
//Form1.Designer.cs
namespace Calculator
{
partial class CalcForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.TextBox txtCalc;
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.Button bSub;
private System.Windows.Forms.Button btnClr;
private System.Windows.Forms.Button btn_4;
private System.Windows.Forms.Button btn_5;
private System.Windows.Forms.Button btn_6;
private System.Windows.Forms.Button bMul;
private System.Windows.Forms.Button bPlus;
private System.Windows.Forms.Button btn_1;
private System.Windows.Forms.Button btn_2;
private System.Windows.Forms.Button btn_3;
private System.Windows.Forms.Button bDiv;
private System.Windows.Forms.Button btn_0;
private System.Windows.Forms.Button bDot;
private System.Windows.Forms.Button bEqu;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtCalc = new System.Windows.Forms.TextBox();
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.bSub = new System.Windows.Forms.Button();
this.btnClr = new System.Windows.Forms.Button();
this.btn_4 = new System.Windows.Forms.Button();
this.btn_5 = new System.Windows.Forms.Button();
this.btn_6 = new System.Windows.Forms.Button();
this.bMul = new System.Windows.Forms.Button();
this.bPlus = new System.Windows.Forms.Button();
this.btn_1 = new System.Windows.Forms.Button();
this.btn_2 = new System.Windows.Forms.Button();
this.btn_3 = new System.Windows.Forms.Button();
this.bDiv = new System.Windows.Forms.Button();
this.btn_0 = new System.Windows.Forms.Button();
this.bDot = new System.Windows.Forms.Button();
this.bEqu = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtCalc
//
this.txtCalc.Location = new System.Drawing.Point(12, 22);
this.txtCalc.Name = "txtCalc";
this.txtCalc.Size = new System.Drawing.Size(240, 21);
this.txtCalc.TabIndex = 0;
this.txtCalc.Text = "";
//
// btn_7
//
this.btn_7.Location = new System.Drawing.Point(13, 59);
this.btn_7.Name = "btn_7";
this.btn_7.Size = new System.Drawing.Size(40, 40);
this.btn_7.TabIndex = 1;
this.btn_7.Text = "7";
this.btn_7.UseVisualStyleBackColor = true;
btn_7.Click += new System.EventHandler(this.btn_clk);
//
// btn_8
//
this.btn_8.Location = new System.Drawing.Point(63, 59);
this.btn_8.Name = "btn_8";
this.btn_8.Size = new System.Drawing.Size(40, 40);
this.btn_8.TabIndex = 2;
this.btn_8.Text = "8";
this.btn_8.UseVisualStyleBackColor = true;
btn_8.Click += new System.EventHandler(this.btn_clk);
//
// btn_9
//
this.btn_9.Location = new System.Drawing.Point(113, 60);
this.btn_9.Name = "btn_9";
this.btn_9.Size = new System.Drawing.Size(40, 40);
this.btn_9.TabIndex = 3;
this.btn_9.Text = "9";
this.btn_9.UseVisualStyleBackColor = true;
btn_9.Click += new System.EventHandler(this.btn_clk);
//
// bSub
//
this.bSub.Location = new System.Drawing.Point(163, 61);
this.bSub.Name = "bSub";
this.bSub.Size = new System.Drawing.Size(40, 40);
this.bSub.TabIndex = 4;
this.bSub.Text = "-";
this.bSub.UseVisualStyleBackColor = true;
bSub.Click += new System.EventHandler(this.btn_Oper);
//
// btnClr
//
this.btnClr.Location = new System.Drawing.Point(213, 62);
this.btnClr.Name = "btnClr";
this.btnClr.Size = new System.Drawing.Size(40, 38);
this.btnClr.TabIndex = 5;
this.btnClr.Text = "AC";
this.btnClr.UseVisualStyleBackColor = true;
btnClr.Click+=new System.EventHandler(this.btn_clr);
//
// btn_4
//
this.btn_4.Location = new System.Drawing.Point(13, 119);
this.btn_4.Name = "btn_4";
this.btn_4.Size = new System.Drawing.Size(40, 40);
this.btn_4.TabIndex = 6;
this.btn_4.Text = "4";
this.btn_4.UseVisualStyleBackColor = true;
btn_4.Click += new System.EventHandler(this.btn_clk);
//
// btn_5
//
this.btn_5.Location = new System.Drawing.Point(63, 119);
this.btn_5.Name = "btn_5";
this.btn_5.Size = new System.Drawing.Size(40, 40);
this.btn_5.TabIndex = 7;
this.btn_5.Text = "5";
this.btn_5.UseVisualStyleBackColor = true;
btn_5.Click += new System.EventHandler(this.btn_clk);
//
// btn_6
//
this.btn_6.Location = new System.Drawing.Point(113, 119);
this.btn_6.Name = "btn_6";
this.btn_6.Size = new System.Drawing.Size(40, 40);
this.btn_6.TabIndex = 8;
this.btn_6.Text = "6";
this.btn_6.UseVisualStyleBackColor = true;
btn_6.Click += new System.EventHandler(this.btn_clk);
//
// bMul
//
this.bMul.Location = new System.Drawing.Point(163, 119);
this.bMul.Name = "bMul";
this.bMul.Size = new System.Drawing.Size(40, 40);
this.bMul.TabIndex = 9;
this.bMul.Text = "*";
this.bMul.UseVisualStyleBackColor = true;
bMul.Click+=new System.EventHandler(this.btn_Oper);
//
// bPlus
//
this.bPlus.Location = new System.Drawing.Point(213, 119);
this.bPlus.Name = "bPlus";
this.bPlus.Size = new System.Drawing.Size(40, 100);
this.bPlus.TabIndex = 10;
this.bPlus.Text = "+";
this.bPlus.UseVisualStyleBackColor = true;
bPlus.Click+=new System.EventHandler(this.btn_Oper);
//
// btn_1
//
this.btn_1.Location = new System.Drawing.Point(13, 179);
this.btn_1.Name = "btn_1";
this.btn_1.Size = new System.Drawing.Size(40, 40);
this.btn_1.TabIndex = 11;
this.btn_1.Text = "1";
this.btn_1.UseVisualStyleBackColor = true;
btn_1.Click += new System.EventHandler(this.btn_clk);
//
// btn_2
//
this.btn_2.Location = new System.Drawing.Point(63, 179);
this.btn_2.Name = "btn_2";
this.btn_2.Size = new System.Drawing.Size(40, 40);
this.btn_2.TabIndex = 12;
this.btn_2.Text = "2";
this.btn_2.UseVisualStyleBackColor = true;
btn_2.Click += new System.EventHandler(this.btn_clk);
//
// btn_3
//
this.btn_3.Location = new System.Drawing.Point(113, 179);
this.btn_3.Name = "btn_3";
this.btn_3.Size = new System.Drawing.Size(40, 40);
this.btn_3.TabIndex = 13;
this.btn_3.Text = "3";
this.btn_3.UseVisualStyleBackColor = true;
btn_3.Click += new System.EventHandler(this.btn_clk);
//
// bDiv
//
this.bDiv.Location = new System.Drawing.Point(163, 179);
this.bDiv.Name = "bDiv";
this.bDiv.Size = new System.Drawing.Size(40, 40);
this.bDiv.TabIndex = 14;
this.bDiv.Text = "/";
this.bDiv.UseVisualStyleBackColor = true;
bDiv.Click+=new System.EventHandler(this.btn_Oper);
//
// btn_0
//
this.btn_0.Location = new System.Drawing.Point(13, 239);
this.btn_0.Name = "btn_0";
this.btn_0.Size = new System.Drawing.Size(90, 40);
this.btn_0.TabIndex = 15;
this.btn_0.Text = "0";
this.btn_0.UseVisualStyleBackColor = true;
btn_0.Click+=new System.EventHandler(this.btn_clk);
//
// bDot
//
this.bDot.Location = new System.Drawing.Point(113, 239);
this.bDot.Name = "bDot";
this.bDot.Size = new System.Drawing.Size(40, 40);
this.bDot.TabIndex = 16;
this.bDot.Text = ".";
this.bDot.UseVisualStyleBackColor = true;
bDot.Click+=new System.EventHandler(this.btn_clk);
//
// bEqu
//
this.bEqu.Location = new System.Drawing.Point(163, 239);
this.bEqu.Name = "bEqu";
this.bEqu.Size = new System.Drawing.Size(90, 40);
this.bEqu.TabIndex = 17;
this.bEqu.Text = "=";
this.bEqu.UseVisualStyleBackColor = true;
bEqu.Click+=new System.EventHandler(this.btn_equ);
//
// CalcForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(285, 288);
this.Controls.Add(this.bEqu);
this.Controls.Add(this.bDot);
this.Controls.Add(this.btn_0);
this.Controls.Add(this.bDiv);
this.Controls.Add(this.btn_3);
this.Controls.Add(this.btn_2);
this.Controls.Add(this.btn_1);
this.Controls.Add(this.bPlus);
this.Controls.Add(this.bMul);
this.Controls.Add(this.btn_6);
this.Controls.Add(this.btn_5);
this.Controls.Add(this.btn_4);
this.Controls.Add(this.btnClr);
this.Controls.Add(this.bSub);
this.Controls.Add(this.btn_9);
this.Controls.Add(this.btn_8);
this.Controls.Add(this.btn_7);
this.Controls.Add(this.txtCalc);
this.Name = "CalcForm";
this.Text = "Calculater";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
}
}
//Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Calculator
{
public partial class CalcForm : Form
{
//定义变量
double dblAcc;
double dblSec;
bool blnClear, blnFirstOpen;
string strOper;
public CalcForm()
{
InitializeComponent();
//初始化变量
dblAcc = 0;
dblSec = 0;
blnFirstOpen = true;
blnClear = true;
strOper = new string('=', 1);
}
//小数点的操作
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;
blnFirstOpen = false;
}
private void btn_Oper(object obj, EventArgs ea)
{
Button tmp = (Button)obj;
strOper = tmp.Text;
if (!blnFirstOpen)
dblAcc = dblSec;
else
calc();
blnFirstOpen = false;
blnClear = true;
}
//private void btn_Oper(object obj, EventArgs ea)
//{
// Button tmp = (Button)obj;
// strOper = tmp.Text;
// if (blnFirstOpen)
// dblAcc = dblSec;
// else
// {
// dblAcc = dblSec;
// calc();
// }
// blnFirstOpen = false;
// blnClear = true;
//}
//等号运算
private void btn_equ(object obj, EventArgs ea)
{
Button tmp = (Button)obj;
if (blnFirstOpen)
strOper = tmp.Text;
else
calc();
blnFirstOpen = false;
blnClear = true;
}
//所有运算操作
private void calc()
{
switch(strOper){
case "+":
dblAcc+=dblSec; //加号运算
break;
case "-":
dblAcc-=dblSec; //减号运算
break;
case "*":
dblAcc*=dblSec; //乘号运算
break;
case "/":
dblAcc/=dblSec; //除号运算
break;
}
strOper="="; //等号运算
blnFirstOpen=false;
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;
txtCalc.Text = "";
}
}
}