IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C#实现计算器 -> 正文阅读

[C++知识库]C#实现计算器

最终效果图:
在这里插入图片描述
编辑器:Visual Studio 2022
具体步骤:
1、创建窗体应用
在这里插入图片描述
2、拖拽公共组件 label
在这里插入图片描述
并且 修改 字体
(name)labelout
font 宋体 常规 一号
Text 0
textalign BottomRight
autoSize false (可以任意拖拽大小 位置)
在这里插入图片描述
3、复制窗体1
在这里插入图片描述
(name)labelbefore
font 宋体 常规 二号
4、复制窗体2 最上面的
在这里插入图片描述
(name)labelmode
然后将 第一行 第二行 的 text 文本0 置空
在这里插入图片描述
5、拖拽公共组件button
(name)num1
text 1
然后复制 将对应的name text 改成对应的数字

在这里插入图片描述
6、双击数字 添加对应数字的点击事件(运行 保存完 双击才能加事件 )
在这里插入图片描述
在这里插入图片描述
7、选中数字,点击工具箱中的公共组件 指针 复制 按钮
Text 改为 清除
name clean
双击清除 添加事件
number = 0;
label2.Text = Convert.ToString(number);
在这里插入图片描述
8、复制 清除按钮 添加 加减乘除,并且双击添加事件
plus ? ? ??????????????? +
minus ? ??????????????? -
multiplication ?????? *
division ???????? ?????? /
在这里插入图片描述
9、添加事件

	double number = 0, number2 = 0;
    int inputNumber;

    enum Operator { none,plus,minus,multiplication,divsion }
    Operator mode = Operator.none;
    bool ismode = false;

    public void calll(int an)
    {
        if (ismode == false) {
            number = number * 10 + an;
            label2.Text = Convert.ToString(number);
        }
        else 
        {
            number2 = number2 * 10 + an;
            label2.Text = Convert.ToString(number2);
        }
    }


		inputNumber = 1;
        calll(inputNumber);

在这里插入图片描述
10、添加 等于号 equal
text =
name equal
定义变量

在这里插入图片描述
添加事件
在这里插入图片描述
源代码:

namespace call
{
    public partial class labelout : Form
    {

        double number = 0, number2 = 0, result;

        int inputNumber;

        enum Operator { none,plus,minus,multiplication,divsion }
        Operator mode = Operator.none;
        bool isequal = false;

      

        public labelout()
        {
            InitializeComponent();
        }

        private void num1_Click(object sender, EventArgs e)
        {
            inputNumber = 1;
            calll(inputNumber);
        }

        private void num2_Click(object sender, EventArgs e)
        {
            inputNumber = 2;
            calll(inputNumber);
        }

        private void num3_Click(object sender, EventArgs e)
        {
            inputNumber = 3;
            calll(inputNumber);
        }

        private void num4_Click(object sender, EventArgs e)
        {
            inputNumber = 4;
            calll(inputNumber);
        }

        private void num5_Click(object sender, EventArgs e)
        {
            inputNumber = 5;
            calll(inputNumber);
        }

        private void num6_Click(object sender, EventArgs e)
        {
            inputNumber = 6;
            calll(inputNumber);
        }

        private void num7_Click(object sender, EventArgs e)
        {
            inputNumber = 7;
            calll(inputNumber);
        }

        private void num8_Click(object sender, EventArgs e)
        {
            inputNumber = 8;
            calll(inputNumber);
        }

        private void num9_Click(object sender, EventArgs e)
        {
            inputNumber = 9;
            calll(inputNumber);
        }

        private void num0_Click(object sender, EventArgs e)
        {
            inputNumber = 0;
            calll(inputNumber);
        }

        private void clean_Click(object sender, EventArgs e)
        {
            //清除
            //第一次代码
            //number = 0;
            //label2.Text = Convert.ToString(number);
            //第二次代码
            cleanall();
        }

        private void minus_Click(object sender, EventArgs e)
        {
            // -
            mode = Operator.minus;
            switchmode();
        }

        private void plus_Click(object sender, EventArgs e)
        {
            // +
            mode = Operator.plus;
            switchmode();
        }

        private void multiplication_Click(object sender, EventArgs e)
        {
            // *
            mode = Operator.multiplication;
            switchmode();
        }

        private void equal_Click(object sender, EventArgs e)
        {
            //等于
            switch (mode)
            {
                case Operator.plus:
                    result = number + number2;
                    break;
                case Operator.minus:
                    result = number - number2;
                    break;
                case Operator.multiplication:
                    result = number * number2;
                    break;
                case Operator.divsion:
                    result = number / number2;
                    break;
            }
            number = 0;
            number2= 0;
            isequal = true;
            labelbefore.Text = "";
            labelmode.Text = "";
            label2.Text = Convert.ToString(result);
        }

        private void division_Click(object sender, EventArgs e)
        {
            // /
            mode = Operator.divsion;
            switchmode();
        }

        //清空
        public void cleanall()
        {
            number = 0;
            number2 = 0;
            label2.Text = Convert.ToString(number);
            labelbefore.Text = "";
            labelmode.Text = "";
            isequal = false;
            mode = Operator.none;
        }


        //
        public void switchmode()
        {
            switch (mode)
            {
                case Operator.plus:
                     labelmode.Text = "+";
                     break;
                case Operator.minus:
                    labelmode.Text = "-";
                    break;
                case Operator.multiplication:
                    labelmode.Text = "*";
                    break;
                case Operator.divsion:
                    labelmode.Text = "/";
                    break;
            }
            if (isequal == true) 
            {
                number = result;
            }
            labelbefore.Text = Convert.ToString(number);
            label2.Text = Convert.ToString(number2);
        }
    }
}
namespace call
{
    partial class labelout
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.labelbefore = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.labelmode = new System.Windows.Forms.Label();
            this.num1 = new System.Windows.Forms.Button();
            this.num2 = new System.Windows.Forms.Button();
            this.num6 = new System.Windows.Forms.Button();
            this.num5 = new System.Windows.Forms.Button();
            this.num4 = new System.Windows.Forms.Button();
            this.num3 = new System.Windows.Forms.Button();
            this.num7 = new System.Windows.Forms.Button();
            this.num9 = new System.Windows.Forms.Button();
            this.num8 = new System.Windows.Forms.Button();
            this.num0 = new System.Windows.Forms.Button();
            this.clean = new System.Windows.Forms.Button();
            this.minus = new System.Windows.Forms.Button();
            this.multiplication = new System.Windows.Forms.Button();
            this.division = new System.Windows.Forms.Button();
            this.plus = new System.Windows.Forms.Button();
            this.equal = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // labelbefore
            // 
            this.labelbefore.Font = new System.Drawing.Font("宋体", 22.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.labelbefore.Location = new System.Drawing.Point(36, 78);
            this.labelbefore.Margin = new System.Windows.Forms.Padding(7, 0, 7, 0);
            this.labelbefore.Name = "labelbefore";
            this.labelbefore.Size = new System.Drawing.Size(1004, 57);
            this.labelbefore.TabIndex = 0;
            this.labelbefore.TextAlign = System.Drawing.ContentAlignment.BottomRight;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(36, 169);
            this.label2.Margin = new System.Windows.Forms.Padding(7, 0, 7, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(1004, 99);
            this.label2.TabIndex = 1;
            this.label2.Text = "0";
            this.label2.TextAlign = System.Drawing.ContentAlignment.BottomRight;
            // 
            // labelmode
            // 
            this.labelmode.Font = new System.Drawing.Font("宋体", 22.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.labelmode.Location = new System.Drawing.Point(36, 135);
            this.labelmode.Margin = new System.Windows.Forms.Padding(7, 0, 7, 0);
            this.labelmode.Name = "labelmode";
            this.labelmode.Size = new System.Drawing.Size(1004, 51);
            this.labelmode.TabIndex = 2;
            this.labelmode.TextAlign = System.Drawing.ContentAlignment.BottomRight;
            // 
            // num1
            // 
            this.num1.Location = new System.Drawing.Point(62, 289);
            this.num1.Name = "num1";
            this.num1.Size = new System.Drawing.Size(175, 59);
            this.num1.TabIndex = 3;
            this.num1.Text = "1";
            this.num1.UseVisualStyleBackColor = true;
            this.num1.Click += new System.EventHandler(this.num1_Click);
            // 
            // num2
            // 
            this.num2.Location = new System.Drawing.Point(256, 289);
            this.num2.Name = "num2";
            this.num2.Size = new System.Drawing.Size(175, 59);
            this.num2.TabIndex = 4;
            this.num2.Text = "2";
            this.num2.UseVisualStyleBackColor = true;
            this.num2.Click += new System.EventHandler(this.num2_Click);
            // 
            // num6
            // 
            this.num6.Location = new System.Drawing.Point(449, 395);
            this.num6.Name = "num6";
            this.num6.Size = new System.Drawing.Size(175, 59);
            this.num6.TabIndex = 5;
            this.num6.Text = "6";
            this.num6.UseVisualStyleBackColor = true;
            this.num6.Click += new System.EventHandler(this.num6_Click);
            // 
            // num5
            // 
            this.num5.Location = new System.Drawing.Point(256, 395);
            this.num5.Name = "num5";
            this.num5.Size = new System.Drawing.Size(175, 59);
            this.num5.TabIndex = 6;
            this.num5.Text = "5";
            this.num5.UseVisualStyleBackColor = true;
            this.num5.Click += new System.EventHandler(this.num5_Click);
            // 
            // num4
            // 
            this.num4.Location = new System.Drawing.Point(62, 395);
            this.num4.Name = "num4";
            this.num4.Size = new System.Drawing.Size(175, 59);
            this.num4.TabIndex = 7;
            this.num4.Text = "4";
            this.num4.UseVisualStyleBackColor = true;
            this.num4.Click += new System.EventHandler(this.num4_Click);
            // 
            // num3
            // 
            this.num3.Location = new System.Drawing.Point(449, 289);
            this.num3.Name = "num3";
            this.num3.Size = new System.Drawing.Size(175, 59);
            this.num3.TabIndex = 8;
            this.num3.Text = "3";
            this.num3.UseVisualStyleBackColor = true;
            this.num3.Click += new System.EventHandler(this.num3_Click);
            // 
            // num7
            // 
            this.num7.Location = new System.Drawing.Point(62, 502);
            this.num7.Name = "num7";
            this.num7.Size = new System.Drawing.Size(175, 59);
            this.num7.TabIndex = 9;
            this.num7.Text = "7";
            this.num7.UseVisualStyleBackColor = true;
            this.num7.Click += new System.EventHandler(this.num7_Click);
            // 
            // num9
            // 
            this.num9.Location = new System.Drawing.Point(449, 502);
            this.num9.Name = "num9";
            this.num9.Size = new System.Drawing.Size(175, 59);
            this.num9.TabIndex = 10;
            this.num9.Text = "9";
            this.num9.UseVisualStyleBackColor = true;
            this.num9.Click += new System.EventHandler(this.num9_Click);
            // 
            // num8
            // 
            this.num8.Location = new System.Drawing.Point(256, 502);
            this.num8.Name = "num8";
            this.num8.Size = new System.Drawing.Size(175, 59);
            this.num8.TabIndex = 11;
            this.num8.Text = "8";
            this.num8.UseVisualStyleBackColor = true;
            this.num8.Click += new System.EventHandler(this.num8_Click);
            // 
            // num0
            // 
            this.num0.Location = new System.Drawing.Point(256, 602);
            this.num0.Name = "num0";
            this.num0.Size = new System.Drawing.Size(175, 59);
            this.num0.TabIndex = 12;
            this.num0.Text = "0";
            this.num0.UseVisualStyleBackColor = true;
            this.num0.Click += new System.EventHandler(this.num0_Click);
            // 
            // clean
            // 
            this.clean.Location = new System.Drawing.Point(849, 289);
            this.clean.Name = "clean";
            this.clean.Size = new System.Drawing.Size(175, 59);
            this.clean.TabIndex = 13;
            this.clean.Text = "清除";
            this.clean.UseVisualStyleBackColor = true;
            this.clean.Click += new System.EventHandler(this.clean_Click);
            // 
            // minus
            // 
            this.minus.Location = new System.Drawing.Point(648, 395);
            this.minus.Name = "minus";
            this.minus.Size = new System.Drawing.Size(175, 59);
            this.minus.TabIndex = 14;
            this.minus.Text = "-";
            this.minus.UseVisualStyleBackColor = true;
            this.minus.Click += new System.EventHandler(this.minus_Click);
            // 
            // multiplication
            // 
            this.multiplication.Location = new System.Drawing.Point(648, 502);
            this.multiplication.Name = "multiplication";
            this.multiplication.Size = new System.Drawing.Size(175, 59);
            this.multiplication.TabIndex = 15;
            this.multiplication.Text = "*";
            this.multiplication.UseCompatibleTextRendering = true;
            this.multiplication.UseVisualStyleBackColor = true;
            this.multiplication.Click += new System.EventHandler(this.multiplication_Click);
            // 
            // division
            // 
            this.division.Location = new System.Drawing.Point(648, 602);
            this.division.Name = "division";
            this.division.Size = new System.Drawing.Size(175, 59);
            this.division.TabIndex = 16;
            this.division.Text = "/";
            this.division.UseVisualStyleBackColor = true;
            this.division.Click += new System.EventHandler(this.division_Click);
            // 
            // plus
            // 
            this.plus.Location = new System.Drawing.Point(648, 289);
            this.plus.Name = "plus";
            this.plus.Size = new System.Drawing.Size(175, 59);
            this.plus.TabIndex = 17;
            this.plus.Text = "+";
            this.plus.UseVisualStyleBackColor = true;
            this.plus.Click += new System.EventHandler(this.plus_Click);
            // 
            // equal
            // 
            this.equal.Location = new System.Drawing.Point(449, 602);
            this.equal.Name = "equal";
            this.equal.Size = new System.Drawing.Size(175, 59);
            this.equal.TabIndex = 18;
            this.equal.Text = "=";
            this.equal.UseVisualStyleBackColor = true;
            this.equal.Click += new System.EventHandler(this.equal_Click);
            // 
            // labelout
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(22F, 43F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1075, 696);
            this.Controls.Add(this.equal);
            this.Controls.Add(this.plus);
            this.Controls.Add(this.division);
            this.Controls.Add(this.multiplication);
            this.Controls.Add(this.minus);
            this.Controls.Add(this.clean);
            this.Controls.Add(this.num0);
            this.Controls.Add(this.num8);
            this.Controls.Add(this.num9);
            this.Controls.Add(this.num7);
            this.Controls.Add(this.num3);
            this.Controls.Add(this.num4);
            this.Controls.Add(this.num5);
            this.Controls.Add(this.num6);
            this.Controls.Add(this.num2);
            this.Controls.Add(this.num1);
            this.Controls.Add(this.labelmode);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.labelbefore);
            this.Font = new System.Drawing.Font("宋体", 25.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.Margin = new System.Windows.Forms.Padding(7, 6, 7, 6);
            this.Name = "labelout";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private Label labelbefore;
        private Label label2;
        private Label labelmode;
        private Button num1;
        private Button num2;
        private Button num6;
        private Button num5;
        private Button num4;
        private Button num3;
        private Button num7;
        private Button num9;
        private Button num8;
        private Button num0;
        private Button clean;
        private Button minus;
        private Button multiplication;
        private Button division;
        private Button plus;
        private Button equal;
    }
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-04-18 17:22:53  更:2022-04-18 17:26:37 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 23:42:50-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码