1、C#编写简易计算器附源代码超详细超详细一、因为计算器设计的控件太多,不便使用控制台应用程序完成,所以这里使用Windows窗体应用程序,并命名为Calc,如下图所示:二、向窗体中拖入需要的控件,如下图所示:完成效果图结果显示区作者博客左边的文本框是TextBox控件,并修改其name为txtShow ,按键09为Button控件,并将其name分别修改为btn_0、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值分别修改为b
2、tn_add、btn_sub、btn_mul、btn_div,按键=的name值修改为btn_equ,按键倒数的name值修改为btn_rev,按键平方的name值修改为btn_sqr,按键开方的name值修改为btn_sqrt。右边的计算器图片空间是PictureBox,作者博客控件是LinkLabel,可以不添加,以上所有控件均可按照需求添加,只保留自己需要的按钮控件和textbox控件即可。三、代码部分含解释,采用switch多分支语句编写using System;using System.Drawing;using System.Collections;using Systempone
3、ntModel;using System.Windows.Forms;using System.Data;namespace Calc/ QQ:479340056 温柔一刀C#简易计算器的实现/publicclassCalcForm : System.Windows.Forms.Form private System.Windows.Forms.Button btn_0;private System.Windows.Forms.Button btn_1;private System.Windows.Forms.Button btn_2;private System.Windows.Forms.
4、Button btn_3;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 btn_7;private System.Windows.Forms.Button btn_8;private System.Windows.Forms.Button btn_9;private System.Windows.Forms.Button
5、 btn_add;private System.Windows.Forms.Button btn_sub;private System.Windows.Forms.Button btn_mul;private System.Windows.Forms.Button btn_div;private System.Windows.Forms.Button btn_sqrt;private System.Windows.Forms.Button btn_sign;private System.Windows.Forms.Button btn_equ;private System.Windows.Fo
6、rms.Button btn_dot;private System.Windows.Forms.Button btn_rev;private System.Windows.Forms.TextBox txtShow;private System.Windows.Forms.Button btn_sqr;privatePictureBox pictureBox1;privateLinkLabel linkLabel1;/必需的设计器变量。/private SystemponentModel.Container components = null;public CalcForm / Windows
7、 窗体设计器支持所必需的/ InitializeComponent;/ TODO: 在InitializeComponent 调用后添加任何构造函数代码/ /清理所有正在使用的资源。/protectedoverridevoid Dispose if if components.Dispose; base.Dispose; #region Windows Form Designer generated code/设计器支持所需的方法- 不要使用代码编辑器修改/此方法的内容。/privatevoid InitializeComponent SystemponentModelponentResour
8、ceManager resources = new SystemponentModelponentResourceManagertypeof;this.btn_9 = new System.Windows.Forms.Button;this.txtShow = new System.Windows.Forms.TextBox;this.btn_8 = new System.Windows.Forms.Button;this.btn_7 = new System.Windows.Forms.Button;this.btn_div = new System.Windows.Forms.Button
9、;this.btn_sqrt = 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.btn_1 = new System.Windows.Forms.Button;this.btn_2 = new System.Windows.Forms.Button;this.btn_3 = new System.Wi
10、ndows.Forms.Button;this.btn_0 = new System.Windows.Forms.Button;this.btn_mul = new System.Windows.Forms.Button;this.btn_sub = new System.Windows.Forms.Button;this.btn_sign = new System.Windows.Forms.Button;this.btn_equ = new System.Windows.Forms.Button;this.btn_add = new System.Windows.Forms.Button;
11、this.btn_dot = new System.Windows.Forms.Button;this.btn_sqr = new System.Windows.Forms.Button;this.btn_rev = new System.Windows.Forms.Button;this.pictureBox1 = new System.Windows.Forms.PictureBox;this.linkLabel1 = new System.Windows.Forms.LinkLabel; .BeginInit;this.SuspendLayout;/ / btn_9/ this.btn_
12、9.BackColor = System.Drawing.SystemColors.ActiveBorder;this.btn_9.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;this.btn_9.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace;this.btn_9.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke;this.
13、btn_9.Font = new System.Drawing.Font黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ;this.btn_9.ForeColor = System.Drawing.Color.Black;this.btn_9.Location = new System.Drawing.Point;this.btn_9.Name = btn_9;this.btn_9.Size = new System.Drawing.Size;this.btn_9.TabIndex = 0
14、;this.btn_9.Tag = 9;this.btn_9.Text = 9;this.btn_9.UseVisualStyleBackColor = false;this.btn_9.Click += new System.EventHandler;/ / txtShow/ this.txtShow.Location = new System.Drawing.Point;this.txtShow.Name = txtShow;this.txtShow.ReadOnly = true;this.txtShow.Size = new System.Drawing.Size;this.txtSh
15、ow.TabIndex = 1;this.txtShow.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;/ / btn_8/ this.btn_8.BackColor = System.Drawing.SystemColors.ActiveBorder;this.btn_8.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;this.btn_8.FlatAppearance.MouseDownBackColor = System.Drawing.
16、SystemColors.ButtonFace;this.btn_8.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke;this.btn_8.Font = new System.Drawing.Font黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ;this.btn_8.ForeColor = System.Drawing.Color.Black;this.btn_8.Location = new Sy
17、stem.Drawing.Point;this.btn_8.Name = btn_8;this.btn_8.Size = new System.Drawing.Size;this.btn_8.TabIndex = 2;this.btn_8.Tag = 8;this.btn_8.Text = 8;this.btn_8.UseVisualStyleBackColor = false;this.btn_8.Click += new System.EventHandler;/ / btn_7/ this.btn_7.BackColor = System.Drawing.SystemColors.Act
18、iveBorder;this.btn_7.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;this.btn_7.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace;this.btn_7.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke;this.btn_7.Font = new System.Drawing.Font黑体, 10.5F
19、, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ;this.btn_7.ForeColor = System.Drawing.Color.Black;this.btn_7.Location = new System.Drawing.Point;this.btn_7.Name = btn_7;this.btn_7.Size = new System.Drawing.Size;this.btn_7.TabIndex = 3;this.btn_7.Tag = 7;this.btn_7.Text = 7;this.
20、btn_7.UseVisualStyleBackColor = false;this.btn_7.Click += new System.EventHandler;/ / btn_div/ this.btn_div.BackColor = System.Drawing.SystemColors.ActiveBorder;this.btn_div.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;this.btn_div.FlatAppearance.MouseDownBackColor = System.Drawin
21、g.SystemColors.ButtonFace;this.btn_div.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke;this.btn_div.Font = new System.Drawing.Font黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ;this.btn_div.ForeColor = System.Drawing.Color.Red;this.btn_div.Location
22、= new System.Drawing.Point;this.btn_div.Name = btn_div;this.btn_div.Size = new System.Drawing.Size;this.btn_div.TabIndex = 4;this.btn_div.Text = /;this.btn_div.UseVisualStyleBackColor = false;this.btn_div.Click += new System.EventHandler;/ / btn_sqrt/ this.btn_sqrt.BackColor = System.Drawing.SystemC
23、olors.ActiveBorder;this.btn_sqrt.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;this.btn_sqrt.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace;this.btn_sqrt.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke;this.btn_sqrt.Font = new System.
24、Drawing.Font黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ;this.btn_sqrt.ForeColor = System.Drawing.Color.Black;this.btn_sqrt.Location = new System.Drawing.Point;this.btn_sqrt.Name = btn_sqrt;this.btn_sqrt.Size = new System.Drawing.Size;this.btn_sqrt.TabIndex = 5;this.
25、btn_sqrt.Text = 开方;this.btn_sqrt.UseVisualStyleBackColor = false;this.btn_sqrt.Click += new System.EventHandler;/ / btn_4/ this.btn_4.BackColor = System.Drawing.SystemColors.ActiveBorder;this.btn_4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;this.btn_4.FlatAppearance.MouseDownBac
26、kColor = System.Drawing.SystemColors.ButtonFace;this.btn_4.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke;this.btn_4.Font = new System.Drawing.Font黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ;this.btn_4.ForeColor = System.Drawing.Color.Black;this
27、.btn_4.Location = new System.Drawing.Point;this.btn_4.Name = btn_4;this.btn_4.Size = new System.Drawing.Size;this.btn_4.TabIndex = 6;this.btn_4.Tag = 4;this.btn_4.Text = 4;this.btn_4.UseVisualStyleBackColor = false;this.btn_4.Click += new System.EventHandler;/ / btn_5/ this.btn_5.BackColor = System.
28、Drawing.SystemColors.ActiveBorder;this.btn_5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;this.btn_5.FlatAppearance.MouseDownBackColor = System.Drawing.SystemColors.ButtonFace;this.btn_5.FlatAppearance.MouseOverBackColor = System.Drawing.Color.WhiteSmoke;this.btn_5.Font = new Syst
29、em.Drawing.Font黑体, 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ;this.btn_5.ForeColor = System.Drawing.Color.Black;this.btn_5.Location = new System.Drawing.Point;this.btn_5.Name = btn_5;this.btn_5.Size = new System.Drawing.Size;this.btn_5.TabIndex = 7;this.btn_5.Tag = 5;this.btn_5.Text = 5;this.btn_5.UseVisualStyleBackColor =
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2