C#实现简易计算器功能(附源码)

 更新时间:2021年07月21日 12:16:21   作者:Just Do Its  
这篇文章主要为大家详细介绍了C#实现简易计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#实现简易计算器功能的具体代码,供大家参考,具体内容如下

剖析:

1、先设计界面(按钮、文本框(一个显示算式,一个显示结果))布局
2、单击按钮将其对应内容显示在文本框中
3、单击符号(+、-、×、÷、%)时将第一次输入的数储存起来
4、单击等号时将第二次输入的数存储起来并将第一次输入的数与第二次输入的数按照所单击的符号进行运算将结果显示在第一个文本框中
5、单击C时将两个文本框中的内容清空

重点:

1、声明一个bool类型的变量用于实现单击符号再次输入数字时第一次输入的数字清空显示第二次输入的数字
2、声明两个double类型的变量用于装第一次输入的数和装第二次输入的数
3、声明一个string类型的变量用于判断运算符号

界面布局:

具体代码如下:

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 Test_Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //声明三个变量
        string type; //符号类型
        double x;//装第一个数(按符号(+-×÷%)时textbox1中的数字)
        double y;//装第二个数(按等号时textbox1中的数字)
        bool c=false;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();//窗体居中显示
            this.Text = "计算器";
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            textBox1.ReadOnly = true;//文本框只读
            textBox2.TabIndex = 0;//光标焦点在textbox2中
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (c==true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "1";
            textBox2.Text += "1";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "2";
            textBox2.Text += "2";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "3";
            textBox2.Text += "3";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "4";
            textBox2.Text += "4";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "5";
            textBox2.Text += "5";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "6";
            textBox2.Text += "6";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "7";
            textBox2.Text += "7";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "8";
            textBox2.Text += "8";
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "9";
            textBox2.Text += "9";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (c == true)
            {
                c = false;
                textBox1.Text = "";
            }
            textBox1.Text += "0";
            textBox2.Text += "0";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox1.Text += ".";
            textBox2.Text += ".";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            c = true;
            type = "+";
            textBox2.Text += "+";
            x = double.Parse(textBox1.Text);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            c = true;
            type = "-";
            textBox2.Text += "-";
            x = double.Parse(textBox1.Text);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            c = true;
            type = "×";
            textBox2.Text += "×";
            x = double.Parse(textBox1.Text);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            c = true;
            type = "÷";
            textBox2.Text += "÷";
            x = double.Parse(textBox1.Text);
        }

        private void button18_Click(object sender, EventArgs e)
        {
            c = true;
            type = "%";
            textBox2.Text += "%";
            x = double.Parse(textBox1.Text);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            y = double.Parse(textBox1.Text);
            //法一
            while (type=="+")
            {
                textBox1.Text = (x + y).ToString();
                textBox2.Text += "=" + textBox1.Text;
                return;
            }
            while (type == "-")
            {
                textBox1.Text = (x - y).ToString();
                textBox2.Text += "=" + textBox1.Text;
                return;
            } 
            while (type == "×")
            {
                textBox1.Text = (x * y).ToString();
                textBox2.Text += "=" + textBox1.Text;
                return;
            } 
            while (type == "÷")
            {
                if (y!=0)
                {
                    textBox1.Text = (x / y).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                }
                else
                {
                    MessageBox.Show("请重新输入","错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    textBox1.Text = "";
                    textBox2.Text = "";
                }
                return;
            }
            while (type == "%")
            {
                textBox1.Text = (x % y).ToString();
                textBox2.Text += "=" + textBox1.Text;
                return;
            }
            
            //法二:
            //if (type=="+")
            //{
            //    textBox1.Text=(x + y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
            //if (type=="-")
            //{
            //    textBox1.Text = (x - y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
            //if (type=="×")
            //{
            //    textBox1.Text = (x * y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
            //if (type=="÷")
            //{
            //    textBox1.Text = (x / y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
            //if (type=="%")
            //{
            //    textBox1.Text = (x % y).ToString();
            //    textBox2.Text += "=" + textBox1.Text;
            //}
        }

    }
}

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • UGUI轮播图组件实现方法详解

    UGUI轮播图组件实现方法详解

    这篇文章主要为大家详细介绍了UGUI轮播图组件的实现方法,支持自动轮播、手势切换等功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C#迭代器模式(Iterator Pattern)实例教程

    C#迭代器模式(Iterator Pattern)实例教程

    这篇文章主要介绍了C#迭代器模式(Iterator Pattern),包括了迭代器的适用范围及用法实例,需要的朋友可以参考下
    2014-09-09
  • C#实现分治算法求解股票问题

    C#实现分治算法求解股票问题

    本文主要介绍了C#实现分治算法求解股票问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • c#高效的线程安全队列ConcurrentQueue<T>的实现

    c#高效的线程安全队列ConcurrentQueue<T>的实现

    这篇文章主要介绍了c#高效的线程安全队列ConcurrentQueue<T>的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • C#数据结构揭秘一

    C#数据结构揭秘一

    本文一介绍了数据结构的基本概念 而介绍了算法的基本概念,并且重点讨论了算法时间复杂度,并且用程序予以证明
    2012-10-10
  • C# 操作XML文档 使用XmlDocument类方法

    C# 操作XML文档 使用XmlDocument类方法

    对于很大的XML文档,可以使用XmlReader类来读取。因为XmlReader使用Steam(流)来读取文件,所以不会对内存造成太大的消耗。下面就来看一下如何使用XmlDocument类,需要的朋友可以参考下
    2012-11-11
  • C#及WPF获取本机所有字体和颜色的方法

    C#及WPF获取本机所有字体和颜色的方法

    这篇文章主要介绍了C#及WPF获取本机所有字体和颜色的方法,实例分析了C#及WPF获取本机字体及颜色的相关技巧,非常简单实用,需要的朋友可以参考下
    2015-09-09
  • C#设置Word文本框中改变文字方向的方法

    C#设置Word文本框中改变文字方向的方法

    在Word中可插入文本框,默认情况下插入的文本框中的文字方向为横向排列,对于一些特殊文档的设计要求,需要改变文字方向,本文就详细的介绍一下使用,感兴趣的可以了解一下
    2021-06-06
  • C#中异步Socket通信编程代码实例

    C#中异步Socket通信编程代码实例

    这篇文章主要介绍了C#中异步Socket通信编程代码实例,本文讲解了什么是Socket编程的异步是实现、如何实现Socket异步通信并给出了代码实例,需要的朋友可以参考下
    2015-03-03
  • 基于C#后台调用跨域MVC服务及带Cookie验证的实现

    基于C#后台调用跨域MVC服务及带Cookie验证的实现

    本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
    2013-04-04

最新评论