C#编写简易计算器(附源代码)
超详细
一、因为计算器设计的控件太多,不便使用控制台应用程序完成,所以这里使用Windows窗体应用程序,并命名为Calc,如下图所示:
二、向窗体中拖入需要的控件,如下图所示:
(完成效果图)
结果显示区(作者博客左边的文本框)是TextBox控件,并修改其name为txtShow ,按键0~9为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值分别修改为btn_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 System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Calc
{
/// <summary>
/// QQ:479340056 温柔一刀C#简易计算器的实现
/// </summary>
public class CalcForm : 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.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 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.Forms.Button btn_dot;
private System.Windows.Forms.Button btn_rev;
private System.Windows.Forms.TextBox txtShow;
private System.Windows.Forms.Button btn_sqr;
private PictureBox pictureBox1;
private LinkLabel linkLabel1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public CalcForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法- 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CalcForm));
this网页计算器html代码.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();
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.Windows.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();
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();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// btn_9
//
this.btn_9.BackColor = System.Drawing.SystemColors.ActiveBorder;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论