在visual studio环境下用C#实现一个简单计算器.docx
《在visual studio环境下用C#实现一个简单计算器.docx》由会员分享,可在线阅读,更多相关《在visual studio环境下用C#实现一个简单计算器.docx(13页珍藏版)》请在冰豆网上搜索。
在visualstudio环境下用C#实现一个简单计算器
在visualstudio环境下用C#实现一个简单计算器
resource部分的代码:
//程序功能:
实现一个简单的计算器
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespacejisuanqi
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
//上一次值
}
//不相连
else
{
//直接显示按下的数字
txtShow.Text=strNum;
//设置连接标志为true,将后边所按下的数字进行连接
bAppend=true;
}
}
//“清除”按钮,将所有的信息清空
privatevoidbtn_clear_Click(objectsender,EventArgse)
{
txtShow.Text="";
bAppend=true;
iPrevValue=0;
//操作符
strPrevOpt="";
}
//操作符
privatevoidOpt_Click(objectsender,System.EventArgse)
{
//如果显示不为空
if(txtShow.Text!
="")
{
//得到当前显示的值
intiCurValue=int.Parse(txtShow.Text);
//判断上次按下的操作符
switch(strPrevOpt)
{
case"+":
iCurValue+=iPrevValue;
break;
case"-":
iCurValue=iPrevValue-iCurValue;
break;
case"*":
iCurValue*=iPrevValue;
break;
case"/":
iCurValue=iPrevValue/iCurValue;
break;
default:
break;
}
//取得上次按下的操做符
strPrevOpt=((Button)sender).Text;
//设置为按下的数字不和上次进行连接
bAppend=false;
//显示值
txtShow.Text=iCurValue.ToString();
//上次的值等于当前值
iPrevValue=iCurValue;
}
}
}
}
Design部分的代码:
namespacejisuanqi
{
partialclassForm1
{
///
///必需的设计器变量。
///
privateSystem.ComponentModel.IContainercomponents=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,为true;否则为false。
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!
=null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
///
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的内容。
///
privatevoidInitializeComponent()
{
this.button1=newSystem.Windows.Forms.Button();
this.txtShow=newSystem.Windows.Forms.TextBox();
this.button2=newSystem.Windows.Forms.Button();
this.button3=newSystem.Windows.Forms.Button();
this.button4=newSystem.Windows.Forms.Button();
this.button5=newSystem.Windows.Forms.Button();
this.button6=newSystem.Windows.Forms.Button();
this.button7=newSystem.Windows.Forms.Button();
this.button8=newSystem.Windows.Forms.Button();
this.button9=newSystem.Windows.Forms.Button();
this.button10=newSystem.Windows.Forms.Button();
this.button11=newSystem.Windows.Forms.Button();
this.button12=newSystem.Windows.Forms.Button();
this.button13=newSystem.Windows.Forms.Button();
this.button14=newSystem.Windows.Forms.Button();
this.button15=newSystem.Windows.Forms.Button();
this.button16=newSystem.Windows.Forms.Button();
this.SuspendLayout();
//
//button1
//
this.button1.Location=newSystem.Drawing.Point(27,49);
this.button1.Name="button1";
this.button1.Size=newSystem.Drawing.Size(46,23);
this.button1.TabIndex=0;
this.button1.Text="1";
this.button1.UseVisualStyleBackColor=true;
this.button1.Click+=newSystem.EventHandler(this.Num_Click);
//
//txtShow
//
this.txtShow.Location=newSystem.Drawing.Point(27,12);
this.txtShow.Name="txtShow";
this.txtShow.Size=newSystem.Drawing.Size(234,21);
this.txtShow.TabIndex=1;
//
//button2
//
this.button2.Location=newSystem.Drawing.Point(88,49);
this.button2.Name="button2";
this.button2.Size=newSystem.Drawing.Size(45,23);
this.button2.TabIndex=2;
this.button2.Text="2";
this.button2.UseVisualStyleBackColor=true;
this.button2.Click+=newSystem.EventHandler(this.Num_Click);
//
//button3
//
this.button3.Location=newSystem.Drawing.Point(153,49);
this.button3.Name="button3";
this.button3.Size=newSystem.Drawing.Size(47,23);
this.button3.TabIndex=3;
this.button3.Text="3";
this.button3.UseVisualStyleBackColor=true;
this.button3.Click+=newSystem.EventHandler(this.Num_Click);
//
//button4
//
this.button4.Location=newSystem.Drawing.Point(215,49);
this.button4.Name="button4";
this.button4.Size=newSystem.Drawing.Size(46,23);
this.button4.TabIndex=4;
this.button4.Text="Clear";
this.button4.UseVisualStyleBackColor=true;
this.button4.Click+=newSystem.EventHandler(this.btn_clear_Click);
//
//button5
//
this.button5.Location=newSystem.Drawing.Point(27,93);
this.button5.Name="button5";
this.button5.Size=newSystem.Drawing.Size(46,23);
this.button5.TabIndex=5;
this.button5.Text="4";
this.button5.UseVisualStyleBackColor=true;
this.button5.Click+=newSystem.EventHandler(this.Num_Click);
//
//button6
//
this.button6.Location=newSystem.Drawing.Point(88,93);
this.button6.Name="button6";
this.button6.Size=newSystem.Drawing.Size(45,23);
this.button6.TabIndex=6;
this.button6.Text="5";
this.button6.UseVisualStyleBackColor=true;
this.button6.Click+=newSystem.EventHandler(this.Num_Click);
//
//button7
//
this.button7.Location=newSystem.Drawing.Point(153,93);
this.button7.Name="button7";
this.button7.Size=newSystem.Drawing.Size(47,23);
this.button7.TabIndex=7;
this.button7.Text="6";
this.button7.UseVisualStyleBackColor=true;
this.button7.Click+=newSystem.EventHandler(this.Num_Click);
//
//button8
//
this.button8.Location=newSystem.Drawing.Point(215,92);
this.button8.Name="button8";
this.button8.Size=newSystem.Drawing.Size(46,23);
this.button8.TabIndex=8;
this.button8.Text="/";
this.button8.UseVisualStyleBackColor=true;
this.button8.Click+=newSystem.EventHandler(this.Opt_Click);
//
//button9
//
this.button9.Location=newSystem.Drawing.Point(27,133);
this.button9.Name="button9";
this.button9.Size=newSystem.Drawing.Size(46,23);
this.button9.TabIndex=9;
this.button9.Text="7";
this.button9.UseVisualStyleBackColor=true;
this.button9.Click+=newSystem.EventHandler(this.Num_Click);
//
//button10
//
this.button10.Location=newSystem.Drawing.Point(88,133);
this.button10.Name="button10";
this.button10.Size=newSystem.Drawing.Size(44,23);
this.button10.TabIndex=10;
this.button10.Text="8";
this.button10.UseVisualStyleBackColor=true;
this.button10.Click+=newSystem.EventHandler(this.Num_Click);
//
//button11
//
this.button11.Location=newSystem.Drawing.Point(154,133);
this.button11.Name="button11";
this.button11.Size=newSystem.Drawing.Size(46,23);
this.button11.TabIndex=11;
this.button11.Text="9";
this.button11.UseVisualStyleBackColor=true;
this.button11.Click+=newSystem.EventHandler(this.Num_Click);
//
//button12
//
this.button12.Location=newSystem.Drawing.Point(216,133);
this.button12.Name="button12";
this.button12.Size=newSystem.Drawing.Size(45,23);
this.button12.TabIndex=12;
this.button12.Text="*";
this.button12.UseVisualStyleBackColor=true;
this.button12.Click+=newSystem.EventHandler(this.Opt_Click);
//
//button13
//
this.button13.Location=newSystem.Drawing.Point(27,177);
this.button13.Name="button13";
this.button13.Size=newSystem.Drawing.Size(46,23);
this.button13.TabIndex=13;
this.button13.Text="0";
this.button13.UseVisualStyleBackColor=true;
this.button13.Click+=newSystem.EventHandler(this.Num_Click);
//
//button14
//
this.button14.Location=newSystem.Drawing.Point(87,177);
this.button14.Name="button14";
this.button14.Size=newSystem.Drawing.Size(45,23);
this.button14.TabIndex=14;
this.button14.Text="=";
this.button14.UseVisualStyleBackColor=true;
this.button14.Click+=newSystem.EventHandler(this.Opt_Click);
//
//button15
//
this.button15.Location=newSystem.Drawing.Point(153,177);
this.button15.Name="button15";
this.button15.Size=newSystem.Drawing.Size(46,23);
this.button15.TabIndex=15;
this.button15.Text="+";
this.button15.UseVisualStyleBackColor=true;
this.button15.Click+=newSystem.EventHandler(this.Opt_Click);
//
//button16
//
this.button16.Location=newSystem.Drawing.Point(215,177);
this.button16.Name="button16";
this.button16.Size=newSystem.Drawing.Size(45,23);
this.button16.TabIndex=16;
this.button16.Text="-";
this.button16.UseVisualStyleBackColor=true;
this.button16.Click+=newSystem.EventHandler(this.Opt_Click);
//
//Form1
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=newSystem.Drawing.Size(284,240);
this.Controls.Add(this.button16);
this.Controls.Add(this.button15);
this.Controls.Add(this.button14);
this.Controls.Add(this.button13);
this.Controls.Add(this.button12);
this.Controls.Add(this.button11);
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.txtShow);
this.Controls.Add(this.button1);
this.Name="Form1";
this.Text="一个简单的计算器";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
privateSystem.Windows.Forms.Buttonbutton1;
privateSystem.Windows.Forms.TextBoxtxtShow;
privateSystem.Windows.Forms.Buttonbutton2;
privateSystem.Windows.Forms.Buttonbutton3;
privateSystem.Windows.Forms.Buttonbutton4;
privateSystem.Windows.Forms.Buttonbutton5;
privateSystem.Windows.Forms.Buttonbutton6;
privateSystem.Windows.Forms.Buttonbutton7;
privateSystem.Windows.Forms.Buttonbutton8;
privateSystem.Windows.F