www.pudn.com > jisuanji.rar > Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace jisuanji
{
///
/// 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 bRev;
private System.Windows.Forms.Button bSqr;
private System.Windows.Forms.Button bSqrt;
private System.Windows.Forms.Button bSign;
private System.Windows.Forms.Button bLkuohao;
private System.Windows.Forms.Button bRkuohao;
private System.Windows.Forms.Button bSign1;
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;
Double dblStore; //暂存数值的变量
bool blnClear,blnFrstOpen;
String strOper;
String strStore; //暂存操作的变量
//以上是添加的代码
///
/// 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;
dblStore=0;//暂存数值的变量
blnFrstOpen=true;
blnClear=true;
strOper=new string('=',1);
strStore=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.bRev = new System.Windows.Forms.Button();
this.bSqr = new System.Windows.Forms.Button();
this.bSqrt = new System.Windows.Forms.Button();
this.bSign = new System.Windows.Forms.Button();
this.bLkuohao = new System.Windows.Forms.Button();
this.bRkuohao = new System.Windows.Forms.Button();
this.bSign1 = 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(176, 152);
this.bPlus.Name = "bPlus";
this.bPlus.Size = new System.Drawing.Size(40, 24);
this.bPlus.TabIndex = 1;
this.bPlus.Text = "+";
this.bPlus.Click += new System.EventHandler(this.btn_Oper);
//
// bMul
//
this.bMul.Location = new System.Drawing.Point(176, 80);
this.bMul.Name = "bMul";
this.bMul.Size = new System.Drawing.Size(40, 24);
this.bMul.TabIndex = 1;
this.bMul.Text = "*";
this.bMul.Click += new System.EventHandler(this.btn_Oper);
//
// bDot
//
this.bDot.ForeColor = System.Drawing.Color.Black;
this.bDot.Location = new System.Drawing.Point(120, 152);
this.bDot.Name = "bDot";
this.bDot.Size = new System.Drawing.Size(40, 24);
this.bDot.TabIndex = 0;
this.bDot.Text = ".";
this.bDot.Click += new System.EventHandler(this.btn_clk);
//
// txtCalc
//
this.txtCalc.Location = new System.Drawing.Point(8, 16);
this.txtCalc.Name = "txtCalc";
this.txtCalc.ReadOnly = true;
this.txtCalc.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.txtCalc.Size = new System.Drawing.Size(266, 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(232, 184);
this.bClr.Name = "bClr";
this.bClr.Size = new System.Drawing.Size(40, 24);
this.bClr.TabIndex = 0;
this.bClr.Text = "AC";
this.bClr.Click += new System.EventHandler(this.btn_clr);
//
// bDiv
//
this.bDiv.Location = new System.Drawing.Point(176, 48);
this.bDiv.Name = "bDiv";
this.bDiv.Size = new System.Drawing.Size(40, 24);
this.bDiv.TabIndex = 1;
this.bDiv.Text = "/";
this.bDiv.Click += new System.EventHandler(this.btn_Oper);
//
// bSub
//
this.bSub.Location = new System.Drawing.Point(176, 112);
this.bSub.Name = "bSub";
this.bSub.Size = new System.Drawing.Size(40, 24);
this.bSub.TabIndex = 1;
this.bSub.Text = "-";
this.bSub.Click += new System.EventHandler(this.btn_Oper);
//
// bRev
//
this.bRev.Location = new System.Drawing.Point(232, 112);
this.bRev.Name = "bRev";
this.bRev.Size = new System.Drawing.Size(40, 24);
this.bRev.TabIndex = 1;
this.bRev.Text = "1/x";
this.bRev.Click += new System.EventHandler(this.btn_Oper);
//
// bSqr
//
this.bSqr.Location = new System.Drawing.Point(232, 80);
this.bSqr.Name = "bSqr";
this.bSqr.Size = new System.Drawing.Size(40, 24);
this.bSqr.TabIndex = 1;
this.bSqr.Text = "Sqr";
this.bSqr.Click += new System.EventHandler(this.btn_Oper);
//
// bSqrt
//
this.bSqrt.Location = new System.Drawing.Point(232, 48);
this.bSqrt.Name = "bSqrt";
this.bSqrt.Size = new System.Drawing.Size(40, 24);
this.bSqrt.TabIndex = 1;
this.bSqrt.Text = "Sqrt";
this.bSqrt.Click += new System.EventHandler(this.btn_Oper);
//
// bSign
//
this.bSign.Location = new System.Drawing.Point(64, 152);
this.bSign.Name = "bSign";
this.bSign.Size = new System.Drawing.Size(40, 24);
this.bSign.TabIndex = 1;
this.bSign.Text = "+/-";
this.bSign.Click += new System.EventHandler(this.btn_sign);
//
// bLkuohao
//
this.bLkuohao.Location = new System.Drawing.Point(8, 184);
this.bLkuohao.Name = "bLkuohao";
this.bLkuohao.Size = new System.Drawing.Size(40, 24);
this.bLkuohao.TabIndex = 1;
this.bLkuohao.Text = "(";
this.bLkuohao.Click += new System.EventHandler(this.btn_lkh);
//
// bRkuohao
//
this.bRkuohao.Location = new System.Drawing.Point(64, 184);
this.bRkuohao.Name = "bRkuohao";
this.bRkuohao.Size = new System.Drawing.Size(40, 24);
this.bRkuohao.TabIndex = 1;
this.bRkuohao.Text = ")";
this.bRkuohao.Click += new System.EventHandler(this.btn_rkh);
//
// bSign1
//
this.bSign1.Location = new System.Drawing.Point(120, 184);
this.bSign1.Name = "bSign1";
this.bSign1.Size = new System.Drawing.Size(96, 24);
this.bSign1.TabIndex = 1;
this.bSign1.Text = "二进制";
this.bSign1.Click += new System.EventHandler(this.btn_itsc);
//
// button8
//
this.button8.Location = new System.Drawing.Point(8, 48);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(40, 24);
this.button8.TabIndex = 0;
this.button8.Text = "7";
this.button8.Click += new System.EventHandler(this.btn_clk);
//
// button9
//
this.button9.Location = new System.Drawing.Point(64, 48);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(40, 24);
this.button9.TabIndex = 0;
this.button9.Text = "8";
this.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(232, 152);
this.bEqu.Name = "bEqu";
this.bEqu.Size = new System.Drawing.Size(40, 24);
this.bEqu.TabIndex = 1;
this.bEqu.Text = "=";
this.bEqu.Click += new System.EventHandler(this.btn_equ);
//
// button10
//
this.button10.Location = new System.Drawing.Point(120, 48);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(40, 24);
this.button10.TabIndex = 0;
this.button10.Text = "9";
this.button10.Click += new System.EventHandler(this.btn_clk);
//
// button4
//
this.button4.Location = new System.Drawing.Point(120, 112);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(40, 24);
this.button4.TabIndex = 0;
this.button4.Text = "3";
this.button4.Click += new System.EventHandler(this.btn_clk);
//
// button5
//
this.button5.Location = new System.Drawing.Point(8, 80);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(40, 24);
this.button5.TabIndex = 0;
this.button5.Text = "4";
this.button5.Click += new System.EventHandler(this.btn_clk);
//
// button6
//
this.button6.Location = new System.Drawing.Point(64, 80);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(40, 24);
this.button6.TabIndex = 0;
this.button6.Text = "5";
this.button6.Click += new System.EventHandler(this.btn_clk);
//
// button7
//
this.button7.Location = new System.Drawing.Point(120, 80);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(40, 24);
this.button7.TabIndex = 0;
this.button7.Text = "6";
this.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(8, 152);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 24);
this.button1.TabIndex = 0;
this.button1.Text = "0";
this.button1.Click += new System.EventHandler(this.btn_clk);
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 112);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(40, 24);
this.button2.TabIndex = 0;
this.button2.Text = "1";
this.button2.Click += new System.EventHandler(this.btn_clk);
//
// button3
//
this.button3.Location = new System.Drawing.Point(64, 112);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(40, 24);
this.button3.TabIndex = 0;
this.button3.Text = "2";
this.button3.Click += new System.EventHandler(this.btn_clk);
//
// calcForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(280, 214);
this.Controls.Add(this.txtCalc);
this.Controls.Add(this.bEqu);
this.Controls.Add(this.bDiv);
this.Controls.Add(this.bMul);
this.Controls.Add(this.bSub);
this.Controls.Add(this.bPlus);
this.Controls.Add(this.bDot);
this.Controls.Add(this.bClr);
this.Controls.Add(this.bRev);
this.Controls.Add(this.bSqr);
this.Controls.Add(this.bSqrt);
this.Controls.Add(this.bSign);
this.Controls.Add(this.bSign1);
this.Controls.Add(this.bLkuohao);
this.Controls.Add(this.bRkuohao);
this.Controls.Add(this.button10);
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.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_sign(object obj,EventArgs ea)
{
dblSec=-dblSec;
txtCalc.Text=Convert.ToString(dblSec);
}
//十进制数转换成二进制数的实现方法
//二进制按钮的实现代码
private void btn_itsc(object obj,EventArgs ea)
{
int i=0,m=0;
int k=1;
int dblSec1=0; //整数部分存储变量
int intSec1=0;
int[] liuSec1=new int[20]; //
int[] liuSec2=new int[20]; //
Double dblSec2=0; //小数部分存储变量
Double n=0.1;
//先将输入的数值分成整数和小数两部分
dblSec1=(int)dblSec; //输入数的整数部分
dblSec2=dblSec-dblSec1; //输入数的小数部分
//将十进制数的整数部分转换成二进制数
//1.用2作除数连续求余
while(dblSec1>0)
{
intSec1=(int)(dblSec1/2);
liuSec1[i]=dblSec1-(intSec1*2);
i++;
dblSec1=intSec1;
}
//2.用求得的所有余数组成整数部分的二进制树
for(int j=0;j0)&&(m<5))
{
//当乘2恰好为1时
if((dblSec2*2)>=1)
{
liuSec2[m]=1;
dblSec2=(dblSec2*2)-1;
m++;
}
//乘2不为1时
else if((dblSec2*2)>0&&(dblSec2*2)<1)
{
liuSec2[m]=0;
dblSec2*=2;
m++;
}
}
//2.将所有取得的整数转换成二进制数
dblSec2=0;
for(int s=0;s