温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

WinForm实现的计算器小程序

发布时间:2020-06-16 10:20:14 来源:网络 阅读:1621 作者:fayling865 栏目:编程语言

小练习:使用winform做一个小型计算器实现基本功能。
         
      思路:首先要弄清楚的是计算器的实现原理,当用户按下计算器上的键计算器会做相应的操作,同时在将反馈信息显示在一个文本框中。对应于程序中实际上是处理用户点击某个按钮的事件,当用户点击了某个按键之后,程序会做相应的处理例:如果用户点的是数字键,会直接在计算器上部的文本框中显示出来,其他的就相应做一个判断,上部的文本框是结果的反馈。

        注:在处理事件时为了减少程序代码量,没必要单独为每个button处理其点击事件,例如当用户按下1-9的数字时,在计算器上部的textbox中就会显示用户输入的数字,可以将这几个button事件放在一起处理,减少代码量,具体实现如下:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //处理用户点击数字键的事件
            button2.Click += new EventHandler(Button_Click);//将 button2.Click关联到自定义方法Button_Click
            button3.Click += Button_Click;//将button3.Click关联到Button_Click方法
            button4.Click += Button_Click;
            button5.Click += Button_Click;
            button6.Click += Button_Click;
            button7.Click += Button_Click;
            button8.Click += Button_Click;
            button9.Click += Button_Click;
            button10.Click += Button_Click;
            button11.Click += Button_Click;
           

            //处理用户点击运算符的事件           

            button1.Click += Operator_Click;//将button1.Click事件关联到Operator_Click方法
            button14.Click += Operator_Click;
            button15.Click += Operator_Click;
            button16.Click += Operator_Click;
           
        }
        bool mark = true;//定义一个mark用来判断什么时候可以可以输入运算符,比如用户已经输了一个运算符,就不能再输了
        ///


        ///自定义的一个方法,当用户点击了数字键或运算符时将关联到这个方法,将当前button的文本属性获取显示在textbox中
        ///
        /// 接收关联来的对象
        ///
        private void Button_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")//如果原来的文本框是0,就将按的button的文本值赋给textBox1.Text
            {
               textBox1.Text = (sender as Button).Text;
               return;//跳出整个Button_Click方法
            }

            if (!equalmark)//已经做了结果了运算了,这时就清空原来的文本框,等待输入一个数,否则新输入的数就会直接拼接在结果后               //面
            {
                textBox1.Clear();//清空文本框
            }
            equalmark = true;//表示可以继续拼接数字了

            if (!string.IsNullOrEmpty(operators) && mark)//!string.IsNullOrEmpty(operators)判断如果operators不为空,即用户已经点击过运算符,并且文本框中也有值,表示用户已经输入了第一个操作数,并且mark为true,这是就清空文本框,然后将mark置为false可以无限制输入而不是只能输一位,这里的mark就是这个作用
            {
                textBox1.Clear();//清空文本框
                mark = false;//将mark置为false,表示可以继续点击运算符
            }
           textBox1.Text += (sender as Button).Text;
        }
        ///


        /// 定义一个方法,处理如果点的是运算符所需要做的操作,处理运算符,如果用户输入的是运算符,就清空文本框中
        /// 的内容,但是在清空之前要保存原来文本框中的内容,为后来做运算用;同时将获得的运算符赋给textBox1.Text,
        /// 同样清空,因为计算器中运算符是没有显示出来的
        ///
        ///
        ///
        string str;//定义一个string类型的变量str,获取用户点的数字
        string operators;//定义一个string类型的变量operators,获取用户点的运算符
        private void Operator_Click(object sender, EventArgs e)
        {
            str = textBox1.Text;//获取用户输入的数字赋值给str
            operators = (sender as Button).Text;
        }
        ///
        /// 用户按了=之后触发的事件,根据用户输入计算值

        ///
        /// 当前对象
        ///
        bool equalmark = true;//定义一个bool类型的equalmark表示用户
        private void button13_Click(object sender, EventArgs e)
        {
            switch (operators)
            {
                case "+"://如果用户按的是+号,处理加法
                    textBox1.Text =(double.Parse ( str) + double.Parse (textBox1.Text)).ToString ();//textBox1.Text原来存放的是第二次用户输入的数字加上原来保存的str最后赋值给textBox1.Text,作为最后的显示,这里注意赋值不是拼接
                    break;
                case "-":
                    textBox1.Text = (double.Parse(textBox1.Text)-double.Parse(str) ).ToString();
                    break;
                case "*":
                    textBox1.Text = (double.Parse(str) * double.Parse(textBox1.Text)).ToString();
                    break;
                case "/":
                    textBox1.Text = (double.Parse(textBox1.Text)/double.Parse(str)).ToString();
                    break;
            }
            //运算完成
            operators = "";//将保存运算符的字段置为空
            mark = true;//mark置为true表示当已经点过运算符之后可以继续输入多位数字
            equalmark = false;//equalmark用来实现在输出的结果后面不能继续累加数字的作用
        }
        ///


        /// 处理用户按了小数点之后的事件
        ///
        ///
        ///
        private void button12_Click(object sender, EventArgs e)
        {
            if (!textBox1.Text.Contains('.'))
            {
                if (textBox1.Text == "")//如果原来的文本框包含.并且原来为空,就将里面的文本变为0.
                {
                    textBox1.Text += "0.";
                }
                else
                {
                    textBox1.Text += ".";//如果原来的文本框不为0,并且不包含.,就把.添加进去
                }
            }
          
        }
    }
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI