【贴吧】计算器代码注释

界面布局如下:

代码:

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 jisuanqi : Form
    {
        public jisuanqi()
        {
         
            InitializeComponent();
        }
        private int opMain = 0;        // 运算类型,其中1(加法)  2(减法)  3(乘法)  4(除法) 
        private double mainNum1 = 0;    // 存储第一个数 
        private double mainNum2 = 0;    // 存储第二个数 
        private bool isSecond = false;  // 用来判断输入的是第一个还是第二个数 
        private bool isDone = false;    // 用来判断是否按了等于按钮 
        private bool isDecimal = false; // 用来判断是否有小数 
        private bool isNokeydown = false;// 用来判断是否没输入第二个数而按了"="号键 
        private bool isKeyupclear = true;//用来判断是否按了clear键,程序开始前默认按了; 

        public void setText(string textest)  //设置文本框的值 
        {
            if (textest.Equals("clear"))
            {
                textBox1.Text = "0.";
                isSecond = false;
                isDone = false;
                isDecimal = false;
                isKeyupclear = true;
            }
            else
            {
                if (isSecond)
                {
                    textBox1.Text = textest;
                    isSecond = false;
                    isDecimal = false;
                }
                else
                {
                    if (isDone)
                    {
                        textBox1.Text = textest;
                        isDone = false;
                    }
                    else
                    {
                        if (isKeyupclear)              //对是否按下clear键的判断 
                        {
                            textBox1.Text = textest;
                            isKeyupclear = false;
                        }
                        else
                            textBox1.Text += textest;
                    }
                }
            }
            btnEqual.Select();  //设置"="号的焦点 
        }


        public void Calc(double num1, double num2, int op)
        {
            double answer = 0;
            switch (op)         //判断所进行的运算 
            {
                case 1:
                    answer = num1 + num2;
                    break;
                case 2:
                    answer = num1 - num2;
                    break;
                case 3:
                    answer = num1 * num2;
                    break;
                case 4:
                    answer = num1 / num2;
                    break;
                case 5:
                    answer = num1 % num2;
                    break;
            }
            setText(answer.ToString());  //显示结果 
        }

        //执行运算 
        private void doEquals()
        {
            if (isNokeydown)            //判断已经输入第二个数后按了"="号键 
            {
                mainNum2 = double.Parse(textBox1.Text);
                setText("clear");
                Calc(mainNum1, mainNum2, opMain);
                isDone = true;
                isNokeydown = false;
            }

        }

        //切换正负 
        private void changeSign()
        {
            double storNum;
            if (textBox1.Text.Length > 0)
            {
                storNum = double.Parse(textBox1.Text);
                storNum *= -1;
                textBox1.Text = storNum.ToString();
            }
            btnEqual.Select();  //设置"="号的焦点 
        }

        //设置运算类型 
        private void setOperator(int operation)
        {
            if (textBox1.Text.Length > 0)
            {
                opMain = operation;
                mainNum1 = double.Parse(textBox1.Text);
                isSecond = true;
                isDone = false;
                isNokeydown = true;
                btnEqual.Select();  //设置"="号的焦点 
            }
        }

        //设置小数点 
        private void setDecimal()
        {
            if (!isDecimal)
            {
                setText(".");
                isDecimal = true;
            }
            btnEqual.Select();  //设置"="号的焦点 
        }

        //开平方
        private void doSquart()
        {

            double storNum;
            storNum = double.Parse(textBox1.Text);
            if (storNum > 0)
            {
                storNum = Math.Sqrt(storNum);
                textBox1.Text = storNum.ToString();
            }
            else
                textBox1.Text = "亲,负数不能开平方。";
            btnEqual.Select();  //设置"="号的焦点 
        }

        //求倒数
        private void doreciprocal()
        {
            double storNum;
            storNum = double.Parse(textBox1.Text);
            if (storNum != 0)
            {
                storNum = 1 / storNum;
                textBox1.Text = storNum.ToString();
            }
            else
                textBox1.Text = "亲,除数不能为零。";

            btnEqual.Select();

        }


        private void btn7_Click(object sender, EventArgs e)
        {
            setText("7");
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            setText("8");
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            setText("9");
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            setText("4");
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            setText("5");
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            setText("6");
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            setText("1");
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            setText("2");
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            setText("3");
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            setText("0");
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            setDecimal();
        }

        private void btnSubtract_Click(object sender, EventArgs e)//正负号交换
        {
            changeSign();
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            doEquals();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            setOperator(1);
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            setOperator(2);
        }

        private void btnMulti_Click(object sender, EventArgs e)
        {
            setOperator(3);
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            setOperator(4);
        }

        private void btnC_Click(object sender, EventArgs e)//清除键
        {
            isSecond = false;
            setText("clear");
        }

        private void btnSqrt_Click(object sender, EventArgs e)//开平方
        {
            doSquart();
        }

        private void btnPercentage_Click(object sender, EventArgs e)//求模
        {
            setOperator(5);
        }

        private void btnReciprocal_Click(object sender, EventArgs e)//求倒
        {
            doreciprocal();
        }

        private void btnCE_Click(object sender, EventArgs e)//CE
        {
            isSecond = false;
            setText("clear");
        }

        private void btnBackspace_Click(object sender, EventArgs e)//退格键
        {
            string old = textBox1.Text;
            if (old.Length > 1 && old != "0.")
                textBox1.Text = old.Substring(0, old.Length - 1);
            else
                textBox1.Text = "0.";

        }

   
     

       
      
  
  
       

      
     
 

    

     

    

     

    }
    
}

 

posted @ 2014-03-15 22:49  李小争  阅读(491)  评论(0编辑  收藏  举报