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;
            //}
        }

    }
}

效果图:

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

相关文章

  • C#实现飞行棋(Winform)

    C#实现飞行棋(Winform)

    这篇文章主要为大家详细介绍了基于Winform框架的飞行棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • C# try catch 使用实例详解

    C# try catch 使用实例详解

    在编程中, try-catch-throw 是一种常见的错误处理模式,这三个关键字通常一起使用,以捕获异常、处理异常和重新抛出异常,这篇文章主要介绍了C# try catch 使用,需要的朋友可以参考下
    2023-09-09
  • 基于c# 类、接口、结构的联系与区别详解

    基于c# 类、接口、结构的联系与区别详解

    本篇文章是对c#中类与接口以及结构的联系与区别进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C#如何使用Bogus创建模拟数据示例代码

    C#如何使用Bogus创建模拟数据示例代码

    这篇文章主要给大家介绍了关于C#如何使用Bogus创建模拟数据的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • C#使用二维数组模拟斗地主

    C#使用二维数组模拟斗地主

    这篇文章主要介绍了C#使用二维数组模拟斗地主的方法,通过C#的二维数组简单实现扑克随机发牌的功能,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • C#调用DeepSeek API的两种实现方案

    C#调用DeepSeek API的两种实现方案

    DeepSeek(深度求索) 最近可谓火爆的一塌糊涂,具体的介绍这里不再赘述,您可以各种搜索其信息,即使您不搜索,只要您拿起手机,各种关于 DeepSeek 的新闻、资讯也会扑面而来的推送到您面前,本文给大家介绍了C#调用DeepSeek API的两种实现方案,需要的朋友可以参考下
    2025-02-02
  • C# wpf Brush转Hex字符串的实例代码

    C# wpf Brush转Hex字符串的实例代码

    这篇文章主要介绍了C# wpf Brush转Hex字符串的实例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • C#动态生成PictureBox并指定图片的方法

    C#动态生成PictureBox并指定图片的方法

    这篇文章主要介绍了C#动态生成PictureBox并指定图片的方法,实例分析了C#图形控件的动态生成及使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • C#的File类实现文件操作实例详解

    C#的File类实现文件操作实例详解

    这篇文章主要介绍了C#的File类实现文件操作的方法,非常实用,需要的朋友可以参考下
    2014-07-07
  • C#画笔Pen绘制曲线的方法

    C#画笔Pen绘制曲线的方法

    这篇文章主要介绍了C#画笔Pen绘制曲线的方法,主要涉及C#画笔中DrawCurve方法的使用技巧,需要的朋友可以参考下
    2015-06-06

最新评论