C编写简易计算器附源代码超详细.docx
《C编写简易计算器附源代码超详细.docx》由会员分享,可在线阅读,更多相关《C编写简易计算器附源代码超详细.docx(31页珍藏版)》请在冰点文库上搜索。
![C编写简易计算器附源代码超详细.docx](https://file1.bingdoc.com/fileroot1/2023-4/29/477ebca0-d92e-4e9c-a547-d8c6be1568b1/477ebca0-d92e-4e9c-a547-d8c6be1568b11.gif)
C编写简易计算器附源代码超详细
超详细
因为计算器设计的控件太多,不便使用控制台应用程序完成,所以这里使用Windows窗体应用程序,并命名为Calc,如下图所示:
向窗体中拖入需要的控件,如下图所示:
结果显示区(作者博客左边的文本框)是TextBox控件,并修改其name为
txtShow,按键0~9为Button控件,并将其name分别修改为btn_O、btn_1、btn_2、btn_3、btn_4、btn_5、btn_6、btn_7、btn_8、btn_9;按键【负数】的name值修改为btn_sign,按键【.】的name修改为btn_dot,按键【+-*/】的name值分别修改为btn_add>btn_subbtn_mul、btn_div,按键【=】的name值修改为btn_equ,按键【倒数】的name值修改为btn_rev,按键【平方】的name值修改为btn_sqr,按键【开方】的name值修改为btn_sqrt。
右边的计算器图片空间是PictureBox,作者博客控件是LinkLabel,可以不添加,以上所有控件均可按照需求添加,只保留自己需要的按钮控件和textbox控件即可。
三、代码部分(含解释),采用switch多分支语句编写
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
using
usingSystem.Data;
namespaceCalc
{
///
///温柔一刀C#简易计算器的实现
///
publicclassCalcForm:
Form
{
privateButtonbtn_0;privateButtonbtn_1;privateButtonbtn_2;privateButtonbtn_3;
privateButtonbtn_4;
privateButtonbtn_5;
privateButtonbtn_6;
privateButtonbtn_7;
privateButtonbtn_8;
privateButtonbtn_9;
privateButtonbtn_add;
privateButtonbtn_sub;
privateButtonbtn_mul;
privateButtonbtn_div;
privateButtonbtn_sqrt;
privateButtonbtn_sign;
privateButtonbtn_equ;
privateButtonbtn_dot;
privateButtonbtn_rev;
privateTextBoxtxtShow;
privateButtonbtn_sqr;
privatePictureBoxpictureBox1;
privateLinkLabellinkLabel1;
///
///必需的设计器变量。
///
privateSystem.ComponentModel.Containercomponents=null;publicCalcForm()
{
//
//Windows窗体设计器支持所必需的
//InitializeComponent();
//
//TODO:
在InitializeComponent调用后添加任何构造函数代码//
}
///
///清理所有正在使用的资源。
///protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!
=null)
{
components.Dispose();
}base.Dispose(disposing);
}
#regionWindowsFormDesignergeneratedcode///
///设计器支持所需的方法-不要使用代码编辑器修改///此方法的内容。
///privatevoidInitializeComponent()
{
System.ComponentModel.ComponentResourceManagerresources=newSystem.ComponentModel.ComponentResourceManager(typeof(CalcForm));
this
.btn_9=newButton();
this
.txtShow=newTextBox();
this
.btn_8=newButton();
this
.btn_7=newButton();
this
.btn_div=newButton();
this
.btn_sqrt=newButton();
this
.btn_4=newButton();
this
.btn_5=newButton();
this
.btn_6=newButton();
this
.btn_1=newButton();
this
.btn_2=newButton();
this
.btn_3=newButton();
this
.btn_0=newButton();
this
.btn_mul=newButton();
this
.btn_sub=newButton();
this
.btn_sign=newButton();
this
.btn_equ=newButton();
this
.btn_add=newButton();
this
.btn_dot=newButton();
this
.btn_sqr=newButton();
this
.btn_rev=newButton();
this
.pictureBox1=newPictureBox();
this
.linkLabel1=newLinkLabel();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();this.SuspendLayout();
//
//btn_9
//
this
.btn_9.BackColor=System.Drawing.SystemColors.ActiveBorder;
this
.btn_9.BackgroundImageLayout=ImageLayout.Center;
this
=System.Drawing.SystemColors.ButtonFace;
this
=System.Drawing.Color.WhiteSmoke;
this.btn_9.Font=newSystem.Drawing.Font("黑体
",10.5F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((this.btn_9.ForeColor=System.Drawing.Color.Black;
this.btn_9.Location=newSystem.Drawing.Point(126,37);
this.btn_9.Name="btn_9";
this.btn_9.Size=newSystem.Drawing.Size(59,31);
this.btn_9.TabIndex=0;
this.btn_9.Tag="9";
this.btn_9.Text="9";
this.btn_9.UseVisualStyleBackColor=false;
this.btn_9.Click+=newSystem.EventHandler(this.btn_0_Click);
//
//txtShow
//
this.txtShow.Location=newSystem.Drawing.Point(4,6);
this.txtShow.Name="txtShow";
this.txtShow.ReadOnly=true;
this.txtShow.Size=newSystem.Drawing.Size(242,21);
this.txtShow.TabIndex=1;
this.txtShow.TextAlign=HorizontalAlignment.Right;
//
//btn_8
//
this.btn_8.BackColor=System.Drawing.SystemColors.ActiveBorder;
this.btn_8.BackgroundImageLayout=ImageLayout.Center;
.ButtonFace;
byte)(134)));
this.btn_8.FlatAppearance.MouseDownBackColor=System.Drawing.SystemColors
this=System.Drawing.Color.WhiteSmoke;
this.btn_8.Font=newSystem.Drawing.Font("黑体
",10.5F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((this.btn_8.ForeColor=System.Drawing.Color.Black;
this.btn_8.Location=newSystem.Drawing.Point(66,37);
this.btn_8.Name="btn_8";
this.btn_8.Size=newSystem.Drawing.Size(59,31);
this.btn_8.TabIndex=2;
this.btn_8.Tag="8";
this.btn_8.Text="8";
this.btn_8.UseVisualStyleBackColor=false;
this.btn_8.Click+=newSystem.EventHandler(this.btn_0_Click);
//
//btn_7
//
this.btn_7.BackColor=System.Drawing.SystemColors.ActiveBorder;
this.btn_7.BackgroundImageLayout=ImageLayout.Center;
this=System.Drawing.SystemColors.ButtonFace;
this=System.Drawing.Color.WhiteSmoke;
this.btn_7.Font=newSystem.Drawing.Font("黑体
",10.5F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((this.btn_7.ForeColor=System.Drawing.Color.Black;
this.btn_7.Location=newSystem.Drawing.Point(4,37);
this.btn_7.Name="btn_7";
this.btn_7.Size=newSystem.Drawing.Size(59,31);
this.btn_7.TabIndex=3;
this.btn_7.Tag="7";
this.btn_7.Text="7";
this.btn_7.UseVisualStyleBackColor=false;
this.btn_7.Click+=newSystem.EventHandler(this.btn_0_Click);
//
//btn_div
//
this.btn_div.BackColor=System.Drawing.SystemColors.ActiveBorder;
this.btn_div.BackgroundImageLayout=ImageLayout.Center;
this=System.Drawing.SystemColors.ButtonFace;
this=System.Drawing.Color.WhiteSmoke;
this.btn_div.Font=newSystem.Drawing.Font("黑体
byte)(134)));
",10.5F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((this.btn_div.ForeColor=System.Drawing.Color.Red;
this.btn_div.Location=newSystem.Drawing.Point(187,37);
this.btn_div.Name="btn_div";
this.btn_div.Size=newSystem.Drawing.Size(59,31);
this.btn_div.TabIndex=4;
this.btn_div.Text="/";
this.btn_div.UseVisualStyleBackColor=false;
this.btn_div.Click+=newSystem.EventHandler(this.btn_div_Click);
//
//btn_sqrt
//
this.btn_sqrt.BackColor=System.Drawing.SystemColors.ActiveBorder;
this.btn_sqrt.BackgroundImageLayout=ImageLayout.Center;
this=System.Drawing.SystemColors.ButtonFace;
this=System.Drawing.Color.WhiteSmoke;
this.btn_sqrt.Font=newSystem.Drawing.Font("黑体
",10.5F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((this.btn_sqrt.ForeColor=System.Drawing.Color.Black;
this.btn_sqrt.Location=newSystem.Drawing.Point(253,37);
this.btn_sqrt.Name="btn_sqrt";
this.btn_sqrt.Size=newSystem.Drawing.Size(59,31);
this.btn_sqrt.TabIndex=5;
this.btn_sqrt.Text="开方";
byte)(134)));
byte)(134)));
this.btn_sqrt.UseVisualStyleBackColor=false;
this.btn_sqrt.Click+=newSystem.EventHandler(this.btn_sqrt_Click);
//
//btn_4
//
this.btn_4.BackColor=System.Drawing.SystemColors.ActiveBorder;
this.btn_4.BackgroundImageLayout=ImageLayout.Center;
this=System.Drawing.SystemColors.ButtonFace;
this=System.Drawing.Color.WhiteSmoke;
this.btn_4.Font=newSystem.Drawing.Font("黑体
",10.5F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((this.btn_4.ForeColor=System.Drawing.Color.Black;
this.btn_4.Location=newSystem.Drawing.Point(4,72);
this.btn_4.Name="btn_4";
this.btn_4.Size=newSystem.Drawing.Size(59,31);
this.btn_4.TabIndex=6;
this.btn_4.Tag="4";
this.btn_4.Text="4";
this.btn_4.UseVisualStyleBackColor=false;
this.btn_4.Click+=newSystem.EventHandler(this.btn_0_Click);
//
//btn_5
//
this.btn_5.BackColor=System.Drawing.SystemColors.ActiveBorder;
this.btn_5.BackgroundImageLayout=ImageLayout.Center;
this=System.Drawing.SystemColors.ButtonFace;
this=System.Drawing.Color.WhiteSmoke;
this.btn_5.Font=newSystem.Drawing.Font("黑体
",10.5F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((this.btn_5.ForeColor=System.Drawing.Color.Black;
this.btn_5.Location=newSystem.Drawing.Point(66,72);
this.btn_5.Name="btn_5";
this.btn_5.Size=newSystem.Drawing.Size(59,31);
this.btn_5.TabIndex=7;
this.btn_5.Tag="5";
this.btn_5.Text="5";
this.btn_5.UseVisualStyleBackColor=false;
this.btn_5.Click+=newSystem.EventHandler(this.btn_0_Click);
//
//btn_6
//
this.btn_6.BackColor=System.Drawing.SystemColors.ActiveBorder;
this.btn_6.BackgroundImageLayout=ImageLayout.Center;
this=System.Drawing.SystemColors.ButtonFace;
this=System.Drawing.Color.WhiteSmoke;
this.btn_6.Font=newSystem.Drawing.Font("黑体
",10.5F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((
this.btn_6.ForeColor=System.Drawing.Color.Black;
this.btn_6.Location=newSystem.Drawing.Point(126,72);
this.btn_6.Name="btn_6";
this.btn_6.Size=newSystem.Drawing.Size(59,31);
this.btn_6.TabIndex=8;
this.btn_6.Tag="6";
this.btn_6.Text="6";
this.btn_6.UseVisualStyleBackColor=false;
this.btn_6.Click+=newSystem.EventHandler(this.btn_0_Click);
//
//btn_1
//
this.btn_1.BackColor=System.Drawing.SystemColors.ActiveBorder;
this.btn_1.BackgroundImageLayout=ImageLayout.Center;
this=System.Drawing.SystemColors.ButtonFace;
this=System.Drawing.Color.WhiteSmoke;
this.btn_1.Font=newSystem.Drawing.Font("黑体
byte)(134)));
",10.5F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((this.btn_1.ForeColor=System.Drawing.Color.Black;
this.btn_1.Location=newSystem.Drawing.Point(4,108);
this.btn_1.Name="btn_1";
this.btn_1.Size=newSystem.Drawing.Size(59,31);
this.btn_1.TabIndex=9;
this.btn_1.Tag="1";
this.btn_1.Text="1";
this.btn_1.UseVisualStyleBackColor=false;
this.btn_1.Click+=newSystem.EventHandler(this.btn_0_Click);
//
//btn_2
//
th