当前位置:编程学习 > C#/ASP.NET >>

关于计算器连加中的一个问题



        string num_First = null;//第一个数
        string num_Second = null;//第二个数
        string num_Result = null;//结果
        string num_Circle = null;//循环运算
        string symbol_Variable="0";//记录符号
        bool mode_Operation = true;//判断运算是否结束
        bool mode_Error = false;//判断是否出现运算错误
        bool mode_New = true;//判断是否需要输入新的数字
        private void method_But_Num(string num)//数字键的方法
        {
            if (textBox1.Text == "0" || mode_Operation == true|| mode_Error == true || mode_New == true)
         {
             textBox1.Text = "";
             num_Circle = null;
         }
            textBox1.Text += num;
            mode_Operation = false;
            mode_Error = false;
            mode_New = false;
        }
        private void method_But_Symbol(string M)//运算符按键方法,为按键和方法提供一个接口
        {
            if (mode_Error == true)
            {
                textBox1.Text = "0";
                mode_Error = false;
            }

            else if (num_First == null)
            {
                num_First = textBox1.Text;
                mode_New = true;
            }


            else
            {
                if (num_Second == null && mode_New == true) { }

                else
                {
                    method_Operation();
                    mode_Operation = false;
                    mode_New = true;
                }
            }
            symbol_Variable = M;
        }
        private void method_Operation()//运算方法
        {
            switch (symbol_Variable)
            {
                case "+":
                    num_Second = textBox1.Text;
                    try
                    {
                        num_Result = Convert.ToString(Convert.ToDecimal(num_First) + Convert.ToDecimal(num_Second));
                    }
                    catch (Exception)
                    {
                        textBox1.Text = "太大啦!";
                        mode_Error = true;
                        method_AllClean();
                        return;
                    }
                    num_First = num_Result;
                    num_Second = null;
                    textBox1.Text = num_Result;
                    break;
                case "-":
                    num_Second = textBox1.Text;
                    num_Result = Convert.ToString(Convert.ToDecimal(num_First) - Convert.ToDecimal(num_Second));
                    num_First = num_Result;
                    num_Second = null;
                    textBox1.Text = num_Result;
                    break;
                case "*":
                    num_Second = textBox1.Text;
                    try
                    {
                        num_Result = Convert.ToString(Convert.ToDecimal(num_First) * Convert.ToDecimal(num_Second));
                    }
                    catch (Exception)
                    {
                        textBox1.Text = "太大啦!";
                        mode_Error = true;
                        method_AllClean();
                        return;
                    }
                    num_First = num_Result;
                    num_Second = null;
                    textBox1.Text = num_Result;
                    break;
                case "/":
                    num_Second = textBox1.Text;
                    if (num_Second == "0")
                    {
                        textBox1.Text = "除数不能为零";
                        method_AllClean();
                        return;
                    }

                    else
                    {
                        num_Result = Convert.ToString(Convert.ToDecimal(num_First) / Convert.ToDecimal(num_Second));
                        num_First = num_Result;
                        num_Second = null;
                        textBox1.Text = num_Result;
                    }
                    break;
                default: break;
            }
        }
        private void method_AllClean()//全部清除
        {
            num_First = null;
            num_Second = null;
            num_Result = null;
            num_Circle = null;
            symbol_Variable ="";
            mode_Operation = true;
            mode_Error = false;
            mode_New = true;
        }

        private void button1_Click(object sender, EventArgs e)//按数字键事件
        {
            method_But_Num(((Button)sender).Text);
        }
        private void button10_Click(object sender, EventArgs e)//小数点事件
        {
            if (mode_Operation == true || mode_Error == true || mode_New == true)
            {
                textBox1.Text = "0.";
            }

            else
            {
                if (textBox1.Text.IndexOf(".") > 0) { }

                else
                {
                    textBox1.Text += ".";
                }
            }

            mode_Operation = false;
            mode_New = false;
        }
        private void button12_Click(object sender, EventArgs e)//正负号事件
        {
            if (mode_Error == true)
            {
               /* num_First = null;
                num_Second = null;
                num_Result = null;
                symbol_Variable = "";
                mode_Operation = true;
                mode_Error = false;
                mode_New = true;
                textBox1.Text = "0";
                return;*/
                method_AllClean();
                textBox1.Text = "0";
            }
            else
            {
                decimal a = Convert.ToDecimal(textBox1.Text);
                a = a * -1;
                textBox1.Text = Convert.ToString(a);
            }
        }
        private void button13_Click(object sender, EventArgs e)//加减乘除按键事件
        {
            method_But_Symbol(((Button)sender).Text);
        }

        private void button27_Click(object sender, EventArgs e)//归零
        {
            method_AllClean();
            textBox1.Text = "0";
        }

        private void button25_Click(object sender, EventArgs e)//c事件
        {
            textBox1.Text = "0";
        }

        private void button26_Click(object sender, EventArgs e)//等于号事件
        {
            if (mode_Operation == true)
            {
                switch (symbol_Variable)
                {
                    case "+":
                        try
                        {
                            textBox1.Text = Convert.ToString(Convert.ToDecimal(textBox1.Text) + Convert.ToDecimal(num_Circle));
                        }
                        catch
                        {
                            textBox1.Text = "太大啦!";
                            mode_Error = true;
                            method_AllClean();
                            return;
                        }
                        break;
                    case "-":
                        textBox1.Text = Convert.ToString(Convert.ToDecimal(textBox1.Text) - Convert.ToDecimal(num_Circle));
                        break;
                    case "*":
                        try
                        {
                            textBox1.Text = Convert.ToString(Convert.ToDecimal(textBox1.Text) * Convert.ToDecimal(num_Circle));
                        }
                        catch
                        {
                            textBox1.Text = "太大啦!";
                            mode_Error = true;
                            method_AllClean();
                            return;
                        }
                        break;
                    case "/":
                        textBox1.Text = Convert.ToString(Convert.ToDecimal(textBox1.Text) / Convert.ToDecimal(num_Circle));
                        break;
                    default: break;
                }
            }

            else
            {
                num_Circle = textBox1.Text;//作用?
                method_Operation();
            }

            num_First = null;
            num_Second = null;
            num_Result = null;
            mode_Operation = true;
            mode_Error = false;
        }

        private void button17_Click(object sender, EventArgs e)//退格事件
        {

            if (mode_Operation == true)
            {
                textBox1.Text = "0";
            }

            else if (textBox1.TextLength > 1)
            {
                textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
            }

            else
            {
                textBox1.Text = "0";
            }
        }

        private void button20_Click(object sender, EventArgs e)//根号事件
        {
            if (mode_Error == true)
            {
                textBox1.Text = "0";
            }
            else
            {
                if (Convert.ToDecimal(textBox1.Text) < 0)
                {
                    textBox1.Text = "被开方数不能为负";
                    method_AllClean();
                    return;
                }
                else
                {
                    textBox1.Text = Convert.ToString(System.Math.Sqrt(Convert.ToDouble(textBox1.Text)));
                }
                mode_New = true;
            }
        }
      
        private void button18_Click(object sender, EventArgs e)//百分号
        {
            if (num_First == null)
            {
                textBox1.Text = "0";
            }
            else
            {
                textBox1.Text = Convert.ToString(Convert.ToDecimal(num_First) * Convert.ToDecimal(textBox1.Text) * (decimal)0.01);
            }
            mode_New = true;
        }

  

    }
}
switch exception
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,