iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >C#怎么实现计算器四则运算
  • 400
分享到

C#怎么实现计算器四则运算

2023-06-29 01:06:16 400人浏览 薄情痞子
摘要

这篇文章主要讲解了“C#怎么实现计算器四则运算”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#怎么实现计算器四则运算”吧!初始化,实现四则运算using System;using

这篇文章主要讲解了“C#怎么实现计算器四则运算”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#怎么实现计算器四则运算”吧!

初始化,实现四则运算

C#怎么实现计算器四则运算

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.windows.FORMs;namespace WindowsFormsAppCalculator{    public partial class Form1 : Form    {        double number1 = 0;        double number2 = 0;        double result;        int inputNumber;        enum Operator { none, plus, minus, multiplication, division}//判断运算法则        Operator mode = Operator.none;        bool isequal = false;        public Form1()        {            InitializeComponent();        }        private void num1_Click(object sender, EventArgs e)        {            inputNumber = 1;            WindowsFormsAppCalculator(inputNumber);            //number1 = number1 * 10 + 1;            //labelout.Text = Convert.ToString(number1);                  }        private void num2_Click(object sender, EventArgs e)        {            inputNumber = 2;            WindowsFormsAppCalculator(inputNumber);        }        private void num3_Click(object sender, EventArgs e)        {            inputNumber = 3;            WindowsFormsAppCalculator(inputNumber);        }        private void num4_Click(object sender, EventArgs e)        {            inputNumber = 4;            WindowsFormsAppCalculator(inputNumber);        }        private void num5_Click(object sender, EventArgs e)        {            inputNumber = 5;            WindowsFormsAppCalculator(inputNumber);        }        private void num6_Click(object sender, EventArgs e)        {            inputNumber = 6;            WindowsFormsAppCalculator(inputNumber);        }        private void num7_Click(object sender, EventArgs e)        {            inputNumber = 7;            WindowsFormsAppCalculator(inputNumber);        }        private void num8_Click(object sender, EventArgs e)        {            inputNumber = 8;            WindowsFormsAppCalculator(inputNumber);        }        private void num9_Click(object sender, EventArgs e)        {            inputNumber = 9;            WindowsFormsAppCalculator(inputNumber);        }        private void num0_Click(object sender, EventArgs e)        {            inputNumber = 0;            WindowsFormsAppCalculator(inputNumber);        }        private void Form1_Load(object sender, EventArgs e)        {            labelout.Text = Convert.ToString(number1);        }        private void clean_Click(object sender, EventArgs e)        {            cleanAll();        }        public void WindowsFormsAppCalculator(int an) //不懂这段怎么来的        {            if (mode == Operator.none)            {                number1 = number1 * 10 + an;                labelout.Text = Convert.ToString(number1);            }            else            {                number2 = number2 * 10 + an;                labelout.Text = Convert.ToString(number2);            }        }        private void plus_Click(object sender, EventArgs e)        {            mode = Operator.plus;            switchmode();            //isequal = true;         }        private void minus_Click(object sender, EventArgs e)        {            mode = Operator.minus;            switchmode();        }        private void multiplication_Click(object sender, EventArgs e)        {            mode = Operator.multiplication;            switchmode();        }        private void division_Click(object sender, EventArgs e)        {            mode = Operator.division;            switchmode();        }        private void equal_Click(object sender, EventArgs e)        {            switch (mode)            {                case Operator.plus:                     result = number1 + number2;                    break;                case Operator.minus:                     result = number1 - number2;                    break;                case Operator.multiplication:                     result = number1 * number2;                    break;                case Operator.division:                     result = number1 / number2;                    break;            }                        isequal = true;            labelbefore.Text = "";            labelmode.Text = "";            labelout.Text = Convert.ToString(result);                    }        public void cleanAll()        {            number1 = 0;            number2 = 0;            labelout.Text = Convert.ToString(number1);            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.division:                    labelmode.Text = "/";                    break;            }            //isequal = true;            //cleanAll();                       labelbefore.Text = Convert.ToString(number1);            labelout.Text = Convert.ToString(number2);        }    }}
namespace WindowsFormsAppCalculator{    partial class Form1    {        /// <summary>        /// 必需的设计器变量。        /// </summary>        private System.ComponentModel.IContainer components = null;        /// <summary>        /// 清理所有正在使用的资源。        /// </summary>        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region Windows 窗体设计器生成的代码        /// <summary>        /// 设计器支持所需的方法 - 不要修改        /// 使用代码编辑器修改此方法的内容。        /// </summary>        private void InitializeComponent()        {            this.labelout = new System.Windows.Forms.Label();            this.labelbefore = new System.Windows.Forms.Label();            this.labelmode = new System.Windows.Forms.Label();            this.num1 = new System.Windows.Forms.Button();            this.num6 = new System.Windows.Forms.Button();            this.num8 = new System.Windows.Forms.Button();            this.num7 = new System.Windows.Forms.Button();            this.num5 = new System.Windows.Forms.Button();            this.num4 = new System.Windows.Forms.Button();            this.num9 = new System.Windows.Forms.Button();            this.num2 = new System.Windows.Forms.Button();            this.num3 = 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();            //             // labelout            //             this.labelout.Font = new System.Drawing.Font("宋体", 25.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.labelout.Location = new System.Drawing.Point(26, 111);            this.labelout.Name = "labelout";            this.labelout.Size = new System.Drawing.Size(463, 49);            this.labelout.TabIndex = 0;            this.labelout.TextAlign = System.Drawing.ContentAlignment.BottomRight;            //             // labelbefore            //             this.labelbefore.Font = new System.Drawing.Font("宋体", 22.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.labelbefore.Location = new System.Drawing.Point(27, 9);            this.labelbefore.Name = "labelbefore";            this.labelbefore.Size = new System.Drawing.Size(463, 43);            this.labelbefore.TabIndex = 1;            this.labelbefore.TextAlign = System.Drawing.ContentAlignment.BottomRight;            //             // labelmode            //             this.labelmode.Font = new System.Drawing.Font("宋体", 22.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            this.labelmode.Location = new System.Drawing.Point(28, 61);            this.labelmode.Name = "labelmode";            this.labelmode.Size = new System.Drawing.Size(463, 39);            this.labelmode.TabIndex = 2;            this.labelmode.TextAlign = System.Drawing.ContentAlignment.BottomRight;            //             // num1            //             this.num1.Location = new System.Drawing.Point(35, 199);            this.num1.Name = "num1";            this.num1.Size = new System.Drawing.Size(71, 41);            this.num1.TabIndex = 3;            this.num1.Text = "1";            this.num1.UseVisualStyleBackColor = true;            this.num1.Click += new System.EventHandler(this.num1_Click);            //             // num6            //             this.num6.Location = new System.Drawing.Point(218, 253);            this.num6.Name = "num6";            this.num6.Size = new System.Drawing.Size(71, 41);            this.num6.TabIndex = 6;            this.num6.Text = "6";            this.num6.UseVisualStyleBackColor = true;            this.num6.Click += new System.EventHandler(this.num6_Click);            //             // num8            //             this.num8.Location = new System.Drawing.Point(130, 312);            this.num8.Name = "num8";            this.num8.Size = new System.Drawing.Size(71, 41);            this.num8.TabIndex = 7;            this.num8.Text = "8";            this.num8.UseVisualStyleBackColor = true;            this.num8.Click += new System.EventHandler(this.num8_Click);            //             // num7            //             this.num7.Location = new System.Drawing.Point(35, 312);            this.num7.Name = "num7";            this.num7.Size = new System.Drawing.Size(71, 41);            this.num7.TabIndex = 8;            this.num7.Text = "7";            this.num7.UseVisualStyleBackColor = true;            this.num7.Click += new System.EventHandler(this.num7_Click);            //             // num5            //             this.num5.Location = new System.Drawing.Point(130, 253);            this.num5.Name = "num5";            this.num5.Size = new System.Drawing.Size(71, 41);            this.num5.TabIndex = 9;            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(35, 253);            this.num4.Name = "num4";            this.num4.Size = new System.Drawing.Size(71, 41);            this.num4.TabIndex = 10;            this.num4.Text = "4";            this.num4.UseVisualStyleBackColor = true;            this.num4.Click += new System.EventHandler(this.num4_Click);            //             // num9            //             this.num9.Location = new System.Drawing.Point(218, 312);            this.num9.Name = "num9";            this.num9.Size = new System.Drawing.Size(71, 41);            this.num9.TabIndex = 11;            this.num9.Text = "9";            this.num9.UseVisualStyleBackColor = true;            this.num9.Click += new System.EventHandler(this.num9_Click);            //             // num2            //             this.num2.Location = new System.Drawing.Point(130, 199);            this.num2.Name = "num2";            this.num2.Size = new System.Drawing.Size(71, 41);            this.num2.TabIndex = 12;            this.num2.Text = "2";            this.num2.UseVisualStyleBackColor = true;            this.num2.Click += new System.EventHandler(this.num2_Click);            //             // num3            //             this.num3.Location = new System.Drawing.Point(218, 199);            this.num3.Name = "num3";            this.num3.Size = new System.Drawing.Size(71, 41);            this.num3.TabIndex = 13;            this.num3.Text = "3";            this.num3.UseVisualStyleBackColor = true;            this.num3.Click += new System.EventHandler(this.num3_Click);            //             // num0            //             this.num0.Location = new System.Drawing.Point(130, 372);            this.num0.Name = "num0";            this.num0.Size = new System.Drawing.Size(71, 41);            this.num0.TabIndex = 14;            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(387, 199);            this.clean.Name = "clean";            this.clean.Size = new System.Drawing.Size(71, 41);            this.clean.TabIndex = 15;            this.clean.Text = "清除";            this.clean.UseVisualStyleBackColor = true;            this.clean.Click += new System.EventHandler(this.clean_Click);            //             // minus            //             this.minus.Location = new System.Drawing.Point(305, 253);            this.minus.Name = "minus";            this.minus.Size = new System.Drawing.Size(71, 41);            this.minus.TabIndex = 16;            this.minus.Text = "-";            this.minus.UseVisualStyleBackColor = true;            this.minus.Click += new System.EventHandler(this.minus_Click);            //             // multiplication            //             this.multiplication.Location = new System.Drawing.Point(305, 312);            this.multiplication.Name = "multiplication";            this.multiplication.Size = new System.Drawing.Size(71, 41);            this.multiplication.TabIndex = 17;            this.multiplication.Text = "*";            this.multiplication.UseVisualStyleBackColor = true;            this.multiplication.Click += new System.EventHandler(this.multiplication_Click);            //             // division            //             this.division.Location = new System.Drawing.Point(305, 372);            this.division.Name = "division";            this.division.Size = new System.Drawing.Size(71, 41);            this.division.TabIndex = 18;            this.division.Text = "/";            this.division.UseVisualStyleBackColor = true;            this.division.Click += new System.EventHandler(this.division_Click);            //             // plus            //             this.plus.Location = new System.Drawing.Point(305, 199);            this.plus.Name = "plus";            this.plus.Size = new System.Drawing.Size(71, 41);            this.plus.TabIndex = 19;            this.plus.Text = "+";            this.plus.UseVisualStyleBackColor = true;            this.plus.Click += new System.EventHandler(this.plus_Click);            //             // equal            //             this.equal.Location = new System.Drawing.Point(218, 372);            this.equal.Name = "equal";            this.equal.Size = new System.Drawing.Size(71, 41);            this.equal.TabIndex = 20;            this.equal.Text = "=";            this.equal.UseVisualStyleBackColor = true;            this.equal.Click += new System.EventHandler(this.equal_Click);            //             // Form1            //             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.ClientSize = new System.Drawing.Size(502, 466);            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.num3);            this.Controls.Add(this.num2);            this.Controls.Add(this.num9);            this.Controls.Add(this.num4);            this.Controls.Add(this.num5);            this.Controls.Add(this.num7);            this.Controls.Add(this.num8);            this.Controls.Add(this.num6);            this.Controls.Add(this.num1);            this.Controls.Add(this.labelmode);            this.Controls.Add(this.labelbefore);            this.Controls.Add(this.labelout);            this.Name = "Form1";            this.Text = "Form1";            this.Load += new System.EventHandler(this.Form1_Load);            this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Label labelout;        private System.Windows.Forms.Label labelbefore;        private System.Windows.Forms.Label labelmode;        private System.Windows.Forms.Button num1;        private System.Windows.Forms.Button num6;        private System.Windows.Forms.Button num8;        private System.Windows.Forms.Button num7;        private System.Windows.Forms.Button num5;        private System.Windows.Forms.Button num4;        private System.Windows.Forms.Button num9;        private System.Windows.Forms.Button num2;        private System.Windows.Forms.Button num3;        private System.Windows.Forms.Button num0;        private System.Windows.Forms.Button clean;        private System.Windows.Forms.Button minus;        private System.Windows.Forms.Button multiplication;        private System.Windows.Forms.Button division;        private System.Windows.Forms.Button plus;        private System.Windows.Forms.Button equal;    }}

感谢各位的阅读,以上就是“C#怎么实现计算器四则运算”的内容了,经过本文的学习后,相信大家对C#怎么实现计算器四则运算这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: C#怎么实现计算器四则运算

本文链接: https://www.lsjlt.com/news/321831.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#怎么实现计算器四则运算
    这篇文章主要讲解了“C#怎么实现计算器四则运算”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#怎么实现计算器四则运算”吧!初始化,实现四则运算using System;using...
    99+
    2023-06-29
  • JavaScript实现计算器的四则运算功能
    目录一、需求 + 最终实现1. 需求2. 说明:利用了字符串(split、replace)和数组(splice)的方法。3. 代码实现二、实现步骤1. 版本一:实现基础加减乘除2. ...
    99+
    2024-04-02
  • php四则运算怎么实现
    在PHP中,可以使用基本的数学运算符(+,-,*,/)来实现四则运算。以下是一个示例代码,演示了如何实现四则运算:```php```...
    99+
    2023-08-24
    php
  • JavaScript如何实现计算器的四则运算功能
    小编给大家分享一下JavaScript如何实现计算器的四则运算功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、需求 + 最终实现注:只是前端实现1. 需求需...
    99+
    2023-06-29
  • golang 四则运算计算器yacc归约手写实现
    目录缘起目标难点总体流程main.gotokens/tokens.gostates/states.golexer/lexer.goparser/tStackNode.goparser...
    99+
    2024-04-02
  • shell中怎么实现四则运算
    本篇文章为大家展示了shell中怎么实现四则运算,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1.简单方法代码如下:$ b=$((5*5+5-3/2)) $ echo $b29 在linux she...
    99+
    2023-06-09
  • php实现四则运算的方法
    这篇文章给大家分享的是有关php实现四则运算的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。php实现四则运算的方法:首先创建一个PHP示例文件;然后声明数字栈和符号栈;接着把运算串分解成每个字符到$arr数...
    99+
    2023-06-09
  • shell四则运算怎么使用
    在Shell中,可以使用各种工具和语法来进行四则运算。1. 使用`expr`命令:`expr`命令可以用来进行简单的算术运算,例如加...
    99+
    2023-09-08
    shell
  • Go语言如何实现四则运算
    在Go语言中,四则运算是通过基本的算术运算符来实现的。常用的四则运算操作:1、加法(+): 用于将两个数相加;2、减法(-): 用于将第二个数从第一个数中减去;3、乘法(*): 用于将两个数相乘;4、除法(/): 用于将第一个数除以第二个数...
    99+
    2023-12-21
    go语言 四则运算
  • 实现一个【伪】四则运算封闭的符号运算和
    最后的效果: if __name__ == '__main__': import doctest doctest.testmod() x = Symbols("x") print(x * 2 + 1 == ...
    99+
    2023-01-31
    符号
  • gojson编译原理XJSON实现四则运算
    目录前言转义字符性能优化实现四则运算总结前言 在上一篇中介绍了xjson的功能特性以及使用查询语法快速方便的获取JSON中的值。 同时这次也更新了一个版本,主要是两个升级: 对转义...
    99+
    2024-04-02
  • Java实现四则混合运算代码示例
    使用栈来实现,可以处理运算优先级。使用自然四则运算表达式即可,如:4+(3*(3-1)+2)/2。无需把表达式先转换为逆波兰等形式。package com.joshua.cal; import java.util.Collections; ...
    99+
    2023-05-31
    java 四则运算 ava
  • c语言四则运算程序如何写
    下面是一个简单的C语言四则运算程序的示例:#include int main() {int num1, num2;char oper...
    99+
    2023-08-24
    c语言
  • python的简单四则运算语法树可视化怎么实现
    本文小编为大家详细介绍“python的简单四则运算语法树可视化怎么实现”,内容详细,步骤清晰,细节处理妥当,希望这篇“python的简单四则运算语法树可视化怎么实现”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。简...
    99+
    2023-07-05
  • C#怎么实现计算器功能
    今天小编给大家分享一下C#怎么实现计算器功能的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。代码:using Sys...
    99+
    2023-06-29
  • C++怎么实现加一运算
    本篇内容介绍了“C++怎么实现加一运算”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Plus One 加一运算Given a no...
    99+
    2023-06-20
  • Go语言基础教程:四则运算的实现方法
    Go语言基础教程:四则运算的实现方法,需要具体代码示例引言:Go语言作为一门开发云原生应用的编程语言,受到越来越多开发者的青睐。作为学习Go语言的初学者,掌握基本的运算操作是必不可少的。本文将介绍Go语言下实现四则运算的基本方法,并提供具体...
    99+
    2023-12-23
    Go语言 基础教程 四则运算
  • 功能强大:用Go语言实现四则运算,轻松应对复杂运算需求
    标题:功能强大:用Go语言实现四则运算,轻松应对复杂运算需求 随着计算机领域的发展,四则运算作为最基本的数学运算之一,常常在各类软件开发中被广泛使用。为了更好地满足复杂运算需求,许多开...
    99+
    2024-02-26
    go语言 四则运算 复杂需求
  • C#怎么实现运算符重载
    本篇内容介绍了“C#怎么实现运算符重载”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!运算符重载的实现下面的程序演示了完整的实现:using&...
    99+
    2023-06-17
  • C#实现计算器功能
    本文实例为大家分享了C#实现计算器功能的具体代码,供大家参考,具体内容如下 在刚刚接触c#的时候,就想做一个简单加减乘除计算器。这就是目标,可惜一直没有动手去做,今天特意把它简单做了...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作