C#实现按键计算器功能2(增强版)

2021-06-30 19:06

阅读:501

标签:string   reading   行操作   oid   i++   read   缺陷   mode   使用   

1.   实验目的

  1)熟悉C#语言的使用和语法知识

2.   实验要求

(1)设计简单的含交互界面的计算器软件,具有较强的用户体验感。

(2)使用C#语言进行编程,创建窗体应用程序。

(3)实现较简单加减乘除等运算。

3.    运行效果如下:

技术分享图片

4.    效果特点:

(1)每个按钮均可输入,但会阻止一些非法输入,例如,在没有输入数字的时候就输入运算符,点号的多次输入,这些都会阻止。

(2)实现了运算的优先级,即乘除是先与加减计算的。

(3)可实现小数的计算,即弥补了整型无法得打小数的缺陷

(4)增加了归零按钮,一键返回到初始状态

(5)按钮和键盘绑定,可实现键盘敲击时,界面按钮同时相应

(6)结果可继续运算,在点击“=”完成后,可在第二个文本框中输出结果,这时,如果在点击运算符,可对这个结果继续进行操作,实现完美的可继续性

5.代码如下:

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 按键计算器
{
    public partial class Form1 : Form
    {
        double[] number = new double[50];
        char[] operat = new char[50];
        bool isNumber = false;
        bool isDot = false;
        bool isOperat = false;
        bool isEqual = false;

        int numberIndex = 0;
        int operatIndex = 0;

        int dotCount = 0;
       





        public Form1()
        {
            InitializeComponent();
            btnleft.Hide();
            btnright.Hide();

        }

        //将键盘与按钮关联
        protected override bool ProcessCmdKey(ref   Message msg, Keys keyData)        
        {       
            if (keyData == Keys.NumPad1||keyData==Keys.D1)            
            {
                this.btn1.Focus();
                this.btn1.PerformClick(); 
             }
            if (keyData == Keys.NumPad2 || keyData == Keys.D2)
            {
                this.btn2.Focus();
                this.btn2.PerformClick();
            }
            if (keyData == Keys.NumPad3 || keyData == Keys.D3)
            {
                this.btn3.Focus();
                this.btn3.PerformClick();
            }
            if (keyData == Keys.NumPad4 || keyData == Keys.D4)
            {
                this.btn4.Focus();
                this.btn4.PerformClick();
            }
            if (keyData == Keys.NumPad5 || keyData == Keys.D5)
            {
                this.btn5.Focus();
                this.btn5.PerformClick();
            }
            if (keyData == Keys.NumPad6 || keyData == Keys.D6)
            {
                this.btn6.Focus();
                this.btn6.PerformClick();
            }
            if (keyData == Keys.NumPad7 || keyData == Keys.D7)
            {
                this.btn7.Focus();
                this.btn7.PerformClick();
            }
            if (keyData == Keys.NumPad8 || keyData == Keys.D8)
            {
                this.btn8.Focus();
                this.btn8.PerformClick();
            }
            if (keyData == Keys.NumPad9 || keyData == Keys.D9)
            {
                this.btn9.Focus();
                this.btn9.PerformClick();
            }
            if (keyData == Keys.NumPad0 || keyData == Keys.D0)
            {
                this.btn0.Focus();
                this.btn0.PerformClick();
            }
            if (keyData == Keys.Add)
            {
                this.btnadd.Focus();
                this.btnadd.PerformClick();
            }
            if (keyData == Keys.Subtract || keyData == Keys.OemMinus)
            {
                this.btnsub.Focus();
                this.btnsub.PerformClick();
            }
            if (keyData == Keys.Multiply)
            {
                this.btnmul.Focus();
                this.btnmul.PerformClick();
            }
            if (keyData == Keys.Divide)
            {
                this.btndiv.Focus();
                this.btndiv.PerformClick();
            }
            if (keyData == Keys.Enter || keyData == Keys.Oemplus)
            {
                this.btnequ.Focus();
                this.btnequ.PerformClick();
            }
            if (keyData == Keys.Back)
            {
                this.btncle.Focus();
                this.btncle.PerformClick();
            }      
        return base.ProcessCmdKey(ref   msg, keyData);  
        }  


        private void btn1_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {
             
                textBox1.Text ="";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }
            textBox1.Text = textBox1.Text + "1";
            textBox2.Text = textBox2.Text + "1";

            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;


        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {

                textBox1.Text = "";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }

            textBox1.Text = textBox1.Text + "2";
            textBox2.Text = textBox2.Text + "2";

            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;

        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {

                textBox1.Text = "";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }

            textBox1.Text = textBox1.Text + "3";
            textBox2.Text = textBox2.Text + "3";

            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;

        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {

                textBox1.Text = "";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }

            textBox1.Text = textBox1.Text + "4";
            textBox2.Text = textBox2.Text + "4";

            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;

        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {

                textBox1.Text = "";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }

            textBox1.Text = textBox1.Text + "5";
            textBox2.Text = textBox2.Text + "5";

            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;

        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {

                textBox1.Text = "";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }

            textBox1.Text = textBox1.Text + "6";
            textBox2.Text = textBox2.Text + "6";
            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {

                textBox1.Text = "";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }

            textBox1.Text = textBox1.Text + "7";
            textBox2.Text = textBox2.Text + "7";

            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {

                textBox1.Text = "";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }

            textBox1.Text = textBox1.Text + "8";
            textBox2.Text = textBox2.Text + "8";

            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {

                textBox1.Text = "";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }
            textBox1.Text = textBox1.Text + "9";
            textBox2.Text = textBox2.Text + "9";

            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {

                textBox1.Text = "";
                textBox2.Text = "";

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }
            textBox1.Text = textBox1.Text + "0";
            textBox2.Text = textBox2.Text + "0";

            isNumber = true;
            isDot = false;
            isOperat = false;
            isEqual = false;
        }

        private void btnf_Click(object sender, EventArgs e)
        {
            if ((isNumber == true)&&(dotCount==0))
            {
                textBox1.Text = textBox1.Text + ".";
                textBox2.Text = textBox2.Text + ".";

                dotCount++;
                isNumber = false;
                isDot = true;
                isOperat = false;
                isEqual = false;
            }
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {
                number[numberIndex] = double.Parse(textBox2.Text);
                operat[operatIndex] = +;
                textBox1.Text = textBox2.Text + "+";
                textBox2.Text = "";

                numberIndex++;
                operatIndex++;

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }
            
            if ((isOperat==false)&&(isDot==false)&&(isNumber == true))
            {
              
                    number[numberIndex] = double.Parse(textBox2.Text);
                    operat[operatIndex] = +;
                    textBox1.Text = textBox1.Text + "+";
                    textBox2.Text = "";

                    numberIndex++;
                    operatIndex++;

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;


            }
            

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        private void btnmul_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {
                number[numberIndex] = double.Parse(textBox2.Text);
                operat[operatIndex] = *;
                textBox1.Text = textBox2.Text + "*";
                textBox2.Text = "";

                numberIndex++;
                operatIndex++;

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }

            if ((isOperat == false) && (isDot == false) && (isNumber == true))
            {

                number[numberIndex] = double.Parse(textBox2.Text);
                operat[operatIndex] = *;
                textBox1.Text = textBox1.Text + "*";
                textBox2.Text = "";

                numberIndex++;
                operatIndex++;

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;


            }

        }

        private void btndiv_Click(object sender, EventArgs e)
        {

            if (isEqual == true)
            {
                number[numberIndex] = double.Parse(textBox2.Text);
                operat[operatIndex] = /;
                textBox1.Text = textBox2.Text + "/";
                textBox2.Text = "";

                numberIndex++;
                operatIndex++;

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }
            if ((isOperat == false) && (isDot == false) && (isNumber == true))
            {

                number[numberIndex] = double.Parse(textBox2.Text);
                operat[operatIndex] = /;
                textBox1.Text = textBox1.Text + "/";
                textBox2.Text = "";

                numberIndex++;
                operatIndex++;

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;


            }


        }

        private void btnsub_Click(object sender, EventArgs e)
        {
            if (isEqual == true)
            {
                number[numberIndex] = double.Parse(textBox2.Text);
                operat[operatIndex] = -;
                textBox1.Text = textBox2.Text + "-";
                textBox2.Text = "";

                numberIndex++;
                operatIndex++;

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;

            }
            if ((isOperat == false) && (isDot == false) && (isNumber == true))
            {

                number[numberIndex] = double.Parse(textBox2.Text);
                operat[operatIndex] = -;
                textBox1.Text = textBox1.Text + "-";
                textBox2.Text = "";

                numberIndex++;
                operatIndex++;

                dotCount = 0;
                isNumber = false;
                isDot = false;
                isOperat = true;
                isEqual = false;


            }
        }

        private void btnequ_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                number[numberIndex] = double.Parse(textBox2.Text);



                //判断乘除
                for (int i = 0; i  operatIndex;)
                {

                    //对乘号分析
                    if (operat[i] == *)
                    {
                        number[i] = number[i] * number[i + 1];
                        for (int j = i + 1; j )
                        {
                            number[j] = number[j + 1];
                            operat[j - 1] = operat[j];

                        }
                        numberIndex--;
                        operatIndex--;

                    }
                    //对除号分析
                    else if (operat[i] == /)
                    {
                        number[i] = number[i] / number[i + 1];
                        for (int j = i + 1; j )
                        {
                            number[j] = number[j + 1];
                            operat[j - 1] = operat[j];

                        }
                        numberIndex--;
                        operatIndex--;

                    }
                    else
                    { i++; }

                }


                //判断加减
                for (int i = 0; i  operatIndex;)
                {

                    //对加号分析
                    if (operat[i] == +)
                    {
                        number[i] = number[i] + number[i + 1];
                        for (int j = i + 1; j )
                        {
                            number[j] = number[j + 1];
                            operat[j - 1] = operat[j];

                        }
                        numberIndex--;
                        operatIndex--;

                    }
                    //对减号分析
                    else if (operat[i] == -)
                    {
                        number[i] = number[i] - number[i + 1];
                        for (int j = i + 1; j )
                        {
                            number[j] = number[j + 1];
                            operat[j - 1] = operat[j];

                        }
                        numberIndex--;
                        operatIndex--;

                    }
                    else { i++; }

                }

                textBox2.Text = number[0].ToString();
                isNumber = false;
                isDot = false;
                isOperat = false;
                isEqual = true;
                numberIndex = 0;
                operatIndex = 0;
            }


        }
        private void btncle_Click(object sender, EventArgs e)
        {
           isNumber = false;
           isDot = false;
           isOperat = false;
           isEqual = false;

            numberIndex = 0;
            operatIndex = 0;

           dotCount = 0;
          
           textBox1.Text = "";
           textBox2.Text = "";

        }

        private void btnleft_Click(object sender, EventArgs e)
        {
           
        }

        private void btnright_Click(object sender, EventArgs e)
        {

        }
    }
}

 

C#实现按键计算器功能2(增强版)

标签:string   reading   行操作   oid   i++   read   缺陷   mode   使用   

原文地址:https://www.cnblogs.com/junjunjun123/p/9974590.html


评论


亲,登录后才可以留言!