C#编写简易计算器(附源代码~)超详细.doc
文本预览下载声明
-
超详细
因为计算器设计的控件太多,不便使用控制台应用程序完成,所以这里使用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
显示全部