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

    }
}

效果图:

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

(0)

相关推荐

  • C# WinForm程序设计简单计算器

    一个简单的计算器的例子,在这个小程序中我们需要用到的组件有: Button:点击计算 TextBox:输出要运算的数 RadioButton:选择运算类型 GroupBox:绑定RadioButton 首先我们在界面上拖以上的控件,得到如下界面: 这时候监听计算按钮的点击事件: private void button1_Click(object sender, EventArgs e) { double op1, op2, result; if (textBox1.Text == ""

  • c#入门之实现简易存款利息计算器示例

    本想把练习题做了的结果放上来,不过发现附录是有答案的,就算了吧,自己做了没问题就行了哈.之前提到过,要是有朋友有想法,需要做小工具我可以帮忙实现,不过貌似大家都很忙.SO,自己学完第4章后,决定做一个工具:简易存款利息计算器,可以更好地复习前面学过的知识. 原理介绍为啥叫简易呢,因为现在只能计算整存整取(只有1.2.3.5四种年限哈)的利息,并且没有启用自动转存(俗称利滚利,就是把本年和上年的利息之和,作为下年利息计算时的本金)功能,方便和网上已有的工具对比计算结果,判断自己的程序算出来正不正确

  • C#编写的windows计算器的实例代码

    复制代码 代码如下: using System; using System.Drawing; using System.Windows; using System.Windows.Forms; using System.Collections; using System.ComponentModel; using System.Data; namespace comput {     /// <summary>     /// 这是一个计算器的简单实现.     /// </summar

  • 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.Windows.Forms; using System.Collections; usi

  • C#实现简单加减乘除计算器

    第一次学习C#,做了个简单的加减乘除计算器,只能实现两个因数的运算. 主要是练习下C#编程,和以前用过的VB差不多.与VB6不同的是,C#代码区分大小写. Windows窗口程序主要也是由一些控件组成,响应响应的事件(event),实现具体的功能. 1.效果图如下所示 2.代码如下所示 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Syst

  • C#实现Winform版计算器

    本文实例为大家分享Winform版计算器的具体实现方法,供大家参考,具体内容如下 前台页面设计 后台代码实现 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 计

  • C#计算器编写代码

    利用C#编写一个计算器.如下图,能够完成基本的四则运算. 当然这个程序甚至还不上Windows附件那个自带的多功能计算器.  不过这个程序的逻辑还是非常值得思考的,首先你要考虑好用户按+ - * / =等运算符号.数字键之后计算器的状态记录问题.  然后要防止多次按某一个键的问题.比如小数点.就不应该让用户在输入一个数的时候键入两次.  最后,还要弄两个数组,一个存放用户在输入的数字,另一个存放用户输入的符号.  制作过程如下,  1.布局如下,同时可以参考<简单实现C#窗体程序判断是否闰年 >

  • C#实现简单计算器功能

    实现效果: Form1.cs代码: 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 Cal { pu

  • C#实现的简单整数四则运算计算器功能示例

    本文实例讲述了C#实现的简单整数四则运算计算器功能.分享给大家供大家参考,具体如下: 运行效果图如下: 具体代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace 计算器 { public pa

  • C#开发简易winform计算器程序

    临近年关,今日在学习的过程中感觉甚是无聊,便想用C#来开发一个简易的计算器程序,这里记录下今日下午的实现过程,同时也记录下自己的第一遍博客. 一.需求 首先我们先来决定我们的计算器要实现什么功能 功能需求:1.能够实现加.减.乘.除.求余等两个操作数的运算,以及开方.平方单个操作数的运算 2.能够清除错误的输入,能够实现清零操作 显示需求:能够显示操作数与运算内容,显示结果 二.设计界面 1.在明白我们的功能需求后,我们来设计界面,界面主要包括三个部分,用于显示的两个textBox,以及数字键B

随机推荐