如何用C#写一个普通的计算器.欢迎大家发表意见,回答满意者给100分,我是新手,望大家写的尽量详细,,

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication18
    {
        class Program
        {
            public class calc
            {
                private System.Data.DataTable dt = new System.Data.DataTable();
                public string Compute(string str)
                {
                    return dt.Compute(str, "").ToString();
                }
            }
            static void Main(string[] args)
            {
                calc c= new calc();
                Console.WriteLine(c.Compute("1+1"));
                Console.WriteLine(c.Compute("1*3"));
                Console.WriteLine(c.Compute("(1+3)*2/5"));
                Console.ReadKey();
            }
        }
    }
      

  2.   

    这个如果写出来不出bug,估计需要很多次测试的
    可以仿照windows自带的计算器界面
    科学计算器实现起来估计会更困难,一般的计算方法参考math下的方法就ok
      

  3.   

    是控制台的还是winform的这个东东在网上一定能下的到
      

  4.   

    问题补充:用C#实现计算器的Windows应用程序.
      

  5.   

    System.Diagnostics.Process.Start("calc.exe");蹭点分
      

  6.   

    这个是个面试题吧。经常考的是面对对象的方法。
    蛮复杂的。
    用工厂方法。来做。
    C#的一般开发模式介绍的相当详细。
    class operation
    {
    math(x,y)
    }
    class operationadd:operation
    {
    math(x,y)
    {
    x+y
    ]}
    class operationdel:operation
    {
    x-y
    }
    factoryoperation,工厂类。{
    operation getoperation(string o)
    {
    swith o
    case "+"
    return new operationadd
    break}
    }
      

  7.   

    不知道你要控制台程序,还是windows应用程序代码!
      

  8.   

    我都说了是Windows程序!!!!!!!!!!!!!!!!!!!
      

  9.   

    来学习。
    Windows程序比较费劲。。
      

  10.   

    搜索“C# 计算器”
    http://search.download.csdn.net/search/C%23%20%E8%AE%A1%E7%AE%97%E5%99%A8http://download.csdn.net/source/179632楼主自己选几个下来看看吧。
      

  11.   

       呵呵,下面的网址里有一个C#写的Windows标准计算器。有源码,不知道是不是楼主想要的?
    http://bbs.chinaitlab.com/frame.php?frameon=yes&referer=http%3A//bbs.chinaitlab.com/thread-249039-1-2.html
      

  12.   

     这个案例书本很多。网络代码也很多。找找。
    写这个算是比较容易的。但不出BUGGER是比较难的
      

  13.   

    估计没几个人能写的想Windows自带的计算器那样的完美,很佩服ms的那些牛人
      

  14.   

    这个我没事的时候还真写过一个 用委托应该比较容易实现简单功能 下面为实现简单的加减乘除
            double first, second;
            string input;
            bool sfcz = false;
            delegate double ProcessDelegate(double param1,double param2);        private double add(double param1, double param2)
            {
                return param1 + param2;
            }        private double del(double param1, double param2)
            {
                return param1 - param2;
            }        private double Multiply(double param1, double param2)
            {
                return param1 * param2;
            }        private double Divide(double param1, double param2)
            {
                return param1 / param2;
            }        //数字1按钮事件 其他数字类似 不多写了
            private void button1_Click(object sender, EventArgs e)
            {
                if (sfcz)
                {
                    sfcz = false;
                    textBox1.Text = "";
                }
                textBox1.Text += "1";
            }        //加号按钮事件 其他减乘除类似 不多写了
            private void button11_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Trim() != "")
                {
                    sfcz = true;
                }
                first = Convert.ToDouble(textBox1.Text);
                input = "加";
            }//等号按钮事件 
     private void button15_Click(object sender, EventArgs e)
            {
                second = Convert.ToDouble(textBox1.Text);
                ProcessDelegate process;
                if (input == "加")
                {
                    process = new ProcessDelegate(add);
                }
                else if (input == "减")
                {
                    process = new ProcessDelegate(del);
                }
                else if (input == "乘")
                {
                    process = new ProcessDelegate(Multiply);
                }
                else if (input == "除")
                {
                    process = new ProcessDelegate(Divide);
                }
                else
                {
                    //textBox1.Text = "请选择操作符";
                    MessageBox.Show("请选择操作符");
                    return;
                }
                textBox1.Text = process(first, second).ToString();
            }简单的实现了下 ,我想思路应该就这样 很多细节都没处理 要想做一个好的计算器软件出来还要有很多工作要做
      

  15.   

    http://www.yesky.com/20011130/207838.shtml
      

  16.   

    http://www.zydg.net/computer/book/read/csharp/d902137000.html
    http://download.csdn.net/source/291518
    http://tech.ddvip.com/2006-08/11544026776577.html
      

  17.   

    普通型可以完全模仿的,也不需要用栈的算法即可实现。而科学型的运算规则则复杂得多,我当时用了栈的数据结构才模拟成功。讨论帖子在这:http://topic.csdn.net/u/20070828/02/48a980b0-2a22-4eb5-b34a-f0ba55bb2250.html?seed=1919322052
      

  18.   

    写了个成品,比较忠实地模仿了win自带的普通型和科学型计算器: 
    硅谷动力下载链接:  
    http://download.enet.com.cn/html/033432007101201.html  
    华军软件园:  
    http://www.newhua.com/soft/61874.htm  
      

  19.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace 简易计算器
    {
        public partial class Form1 : Form
        {
            double num1 = 0, num2 = 0, result = 0;
            int type = 0;
            int dot_num = 1;
            bool is_num1 = false;
            bool is_dot = false;
            bool is_fushu = false;
            bool doted = false;        void func(int numb)
            {
                if (is_num1 == false)
                {
                    if (is_dot == true)
                    {
                        double tempNum = 1;
                        for (int i = 0; i < dot_num; i++)
                        {
                            tempNum = 10 * tempNum;
                        }
                        if (!is_fushu)
                            num1 = num1 + (double)numb / (double)(tempNum);
                        else
                            num1 = num1 - (double)numb / (double)(tempNum);
                        dot_num++;
                    }
                    else
                    {
                        if (!is_fushu)
                            num1 = num1 * 10 + numb;
                        else
                            num1 = num1 * 10 - numb;
                    }
                    textBox1.Text = num1.ToString();
                }
                else
                {
                    if (is_dot == true)
                    {
                        double tempNum = 1;
                        for (int i = 0; i < dot_num; i++)
                        {
                            tempNum = 10 * tempNum;
                        }
                        if (!is_fushu)
                            num2 = num2 + (double)numb / (double)(tempNum);
                        else
                            num2 = num2 - (double)numb / (double)(tempNum);
                        dot_num++;
                    }
                    else
                    {
                        if (!is_fushu)
                            num2 = num2 * 10 + numb;
                        else
                            num2 = num2 * 10 - numb;
                    }
                    textBox1.Text = num2.ToString();
                }
            }        void calType()
            {
                is_num1 = true;
                dot_num = 1;
                is_dot = false;
                is_fushu = false;
                doted = false;
            }
                
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                func(1);
            }        private void button2_Click(object sender, EventArgs e)
            {
                func(2);
            }        private void button3_Click(object sender, EventArgs e)
            {
                func(3);
            }        private void button4_Click(object sender, EventArgs e)
            {
                func(4);
            }        private void button5_Click(object sender, EventArgs e)
            {
                func(5);
            }        private void button6_Click(object sender, EventArgs e)
            {
                func(6);
            }        private void button7_Click(object sender, EventArgs e)
            {
                func(7);
            }        private void button8_Click(object sender, EventArgs e)
            {
                func(8);
            }        private void button9_Click(object sender, EventArgs e)
            {
                func(9);
            }        private void button10_Click(object sender, EventArgs e)
            {
                func(0);
            }        private void button11_Click(object sender, EventArgs e)
            {
                is_dot = true;
                if (doted == false)
                {
                    doted = true;
                    dot_num = 1;
                }
            
            }        private void button13_Click(object sender, EventArgs e)
            {
                type = 1;
                calType();
                textBox1.Text = "+";
            }        private void button14_Click(object sender, EventArgs e)
            {
                type = 2;
                textBox1.Text = "-";
                calType();
            }        private void button15_Click(object sender, EventArgs e)
            {
                type = 3;
                textBox1.Text = "*";
                calType();
            }        private void button16_Click(object sender, EventArgs e)
            {
                type = 4;
                textBox1.Text = "/";
                calType();
            }        private void button12_Click(object sender, EventArgs e)
            {
                switch (type)
                {
                    case 1: result = num1 + num2;
                        break;
                    case 2: result = num1 - num2;
                        break;
                    case 3: result = num1 * num2;
                        break;
                    case 4: result = num1 / num2;
                        break;
                }
                textBox1.Text = textBox1.Text + "=" + result.ToString();
                num1 = 0;
                num2 = 0;
                is_num1 = false;
                dot_num = 1;
                is_dot = false;
                is_fushu = false;
                doted = false;
            }        private void button17_Click(object sender, EventArgs e)
            {
                textBox1.Text = "";
                num1 = 0;
                num2 = 0;
                result = 0;
                type = 0;
                dot_num = 1;
                is_num1 = false;
                is_dot = false;
                is_fushu = false;
                doted = false;
            }
        }
    }
      

  20.   

    参考:
    http://www.wfsoft.com/software_info.asp?id=30090
    http://www.wfsoft.com/software_info.asp?id=30010
      

  21.   

    以前写过一个
    不过是winform
    而且调试了半天我都懒得看了
      

  22.   

    你试试:只是表达式计算、后面有些没用的不必管using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;namespace WindowsApplication1
    {
        public partial class expression : Form
        {
            public expression()
            {
                InitializeComponent();
            }        /**
             * 单击“计算结果”按钮时触发的事件方法
             */
            private void getResult(object sender, EventArgs e)
            {
                string expStr = expTxt.Text;//获取输入框--表达式的内容
                if (!Analisys(expStr))
                    message.Text = "输入的表达式不合法!";
                else
                {
                    message.Text = "";
                    resultTxt.Text = parseAndResult(expStr);
                }
            }        /**
             * 单击“取消”按钮,关闭窗口
             */
            private void cancal(object sender, EventArgs e)
            {
                this.Close();
            }        /**
             * 判断录入的字符串是不是一个合法的数学表达式
             */
            private bool Analisys(string expStr)
            {
                int dep = 0;                                  //表达式嵌套括号的层数
                int temp = 0;
                for (int i = 0; i < expStr.Length; i++)
                {
                    if (expStr[i] == '(')
                        temp++;
                    else if (expStr[i] == ')')
                        temp--;
                    if (temp > dep)
                        dep = temp;
                }
                string signUnit = @"(\+|\-|\*|\/)";
                string unitRegex = @"(\-)?(([0-9]+)|([0-9]+\.[0-9]+))(" + signUnit 
                    + @"(((([0-9]+)|([0-9]+\.[0-9]+)))|(\(\-(([0-9]+)|([0-9]+\.[0-9]+))\))))*";
                string braUnit = @"(\(" + unitRegex + @"\))";
                string halfEndRegex = "((" + unitRegex + ")|(" + braUnit + "(" + signUnit + "(" + braUnit + "|" + unitRegex + "))+))";
                string endRegex =  halfEndRegex +"("+ signUnit +"("+halfEndRegex +"|"+braUnit + "))*";
                for (int i = 1; i < dep; i++)
                {
                    string tempRule = @"(\(" + endRegex + @"\))";
                    string rule = "((" + endRegex + ")|(" + tempRule + "(" + signUnit + "(" + endRegex + "|" + tempRule + "))+))";
                    endRegex = rule + "(" + signUnit + "(" + rule + "|" + tempRule + "))*";
                }
                string endRule = "^" + endRegex+"$";
                Regex regex = new Regex(endRule);
                Match match = regex.Match(expStr);
                if(match.Success)
                    return true;
                return false;
            }        /**
             * 解析并计算数学表达式的结果
             */
            private string parseAndResult(string exp)
            {//递归方法,用计算结果替换表达式被计算过的部分,再调用该方法!
                if (exp.IndexOf('(') == -1)
                    return calculate(exp);
                int n = exp.IndexOf('(') + 1;
                int i = exp.IndexOf('(') + 1;
                for (; i < exp.Length; i++)
                {
                    if (exp[i] == '(')
                        n = i + 1;
                    else if (exp[i] == ')')
                        break;
                }
                string tempResult = calculate(exp.Substring(n, i - n));
                return parseAndResult(exp.Substring(0, n - 1) + tempResult + exp.Substring(i + 1));
            }        /**
             * 计算不含有括号的数学表达式的结果
             */
            private string calculate(string exp)
            {
                int n = 0;          //运算符的位置
                String tempA = "";  //运算符前面的数
                String tempB = "";  //运算符后面的数
                if ((n = exp.IndexOf('/')) != -1 || (n = exp.IndexOf('*')) != -1)
                {//当表达式中含有'/'先进行除运算,如果表达式中含有'*'运算则进行乘运算
                    int i = n - 1;
                    for (; i >= 0; i--)
                    {
                        if (!Char.IsDigit(exp[i]) && i != 0 && exp[i] != '.')
                            break;
                    }
                    tempA = exp.Substring(i + 1, n - i - 1);
                    int j = n + 1;
                    for (; j < exp.Length; j++)
                    {
                        if (j == n + 1 && exp[j] == '-')
                            continue;
                        if (!Char.IsDigit(exp[j]) && exp[j] != '.')
                            break;
                    }
                    tempB = exp.Substring(n + 1, j - n - 1);
                    if (exp[n] == '*')
                        return calculate(exp.Substring(0, i + 1) + (double.Parse(tempA) * double.Parse(tempB)) + exp.Substring(j));
                    else
                        return calculate(exp.Substring(0, i + 1) + (double.Parse(tempA) / double.Parse(tempB)) + exp.Substring(j));
                }
                else if ((n = exp.IndexOf('+')) != -1 || (n = exp.IndexOf('-')) != -1)
                {//如果表达式中不含有'*'和'/'但含有'-'(并且不是第一个字符,是第一个字符时则把它看做是单项运算符)和'+'完成减、加运算
                    for (int i = 1; i < exp.Length; i++)
                    {
                        if (!Char.IsDigit(exp[i]) && exp[i] != '.')
                        {
                            n = i;
                            break;
                        }
                    }
                    tempA = exp.Substring(0, n);
                    int j = n + 1;
                    for (; j < exp.Length; j++)
                    {
                        if (j == n + 1 && exp[j] == '-')
                            continue;
                        if (!Char.IsDigit(exp[j]) && exp[j] != '.')
                            break;
                    }
                    tempB = exp.Substring(n + 1, j - n - 1);
                    if (exp[n] == '+')
                        return calculate("" + (double.Parse(tempA) + double.Parse(tempB)) + exp.Substring(j));
                    else if (n != 0)
                        return calculate("" + (double.Parse(tempA) - double.Parse(tempB)) + exp.Substring(j));
                }
                return exp;
            }
    }
      

  23.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                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 ;
                        }
                    }
                }
                button12 .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;
                }
                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();
                }
                button12.Select();   //设置"="号的焦点
            }        //设置运算类型
            private void setOperator(int operation)
            {
                if (textBox1 .Text .Length  > 0)
                {
                    opMain = operation;
                    mainNum1 = double.Parse(textBox1 .Text );
                    isSecond = true;
                    isDone = false;
                    isNokeydown = true;
                    button12.Select();   //设置"="号的焦点
                }
            }        //设置小数点
            private void setDecimal()
            {
                if (!isDecimal)
                {
                    setText(".");
                    isDecimal = true;
                }
                button12.Select();   //设置"="号的焦点
            }        private void button10_Click(object sender, EventArgs e)
            {
                setText("0");
            }        private void button11_Click(object sender, EventArgs e)     //小数点
            {
                setDecimal();
            }        private void button1_Click(object sender, EventArgs e)
            {
                setText("1");
            }        private void button2_Click(object sender, EventArgs e)
            {
                setText("2");
            }        private void button3_Click(object sender, EventArgs e)
            {
                setText("3");
            }        private void button4_Click(object sender, EventArgs e)
            {
                setText("4");
            }        private void button5_Click(object sender, EventArgs e)
            {
                setText("5");
            }        private void button6_Click(object sender, EventArgs e)
            {
                setText("6");
            }        private void button7_Click(object sender, EventArgs e)
            {
                setText("7");
            }        private void button8_Click(object sender, EventArgs e)
            {
                setText("8");
            }        private void button9_Click(object sender, EventArgs e)
            {
                setText("9");
            }        private void button12_Click(object sender, EventArgs e)     //"="号键
            {
                doEquals();   
            }        private void button13_Click(object sender, EventArgs e)     //"+"号键
            {
                setOperator(1);
            }        private void button14_Click(object sender, EventArgs e)     //"-"号键
            {
                setOperator(2);
            }        private void button15_Click(object sender, EventArgs e)     //"*"号键
            {
                setOperator(3);
            }        private void button16_Click(object sender, EventArgs e)     //"/"号键
            {
                setOperator(4);
            }        private void button17_Click(object sender, EventArgs e)     //"c"清除键
            {
                isSecond = false;
                setText("clear");
            }        private void button18_Click(object sender, EventArgs e)
            {
                changeSign();
            }    }
    }
      

  24.   

    详细代码:
    http://www.qqread.com/csharp/d902137000.html
    希望对你帮助或启发作用!
      

  25.   

    http://download.csdn.net/source/463254
    里面有源码,自己看。
      

  26.   

    http://download.csdn.net/source/463254 
    里面有源码,自己看。功能和Windows的基本功能差不多,但没科学计算器那么夸张哈。只是N年前练手的东西
      

  27.   

    真的是體力活。以前用delphi寫過。上網查源碼算了
      

  28.   

    网上有好多关于C#写的计算器源码
    http://www.knowsky.com/300795.html,看看满意不
      

  29.   

    最近正好模仿WINDOWS计算器写了个,不过只是实现了标准型部分,对标准型和科学型的切换,没找到好方案,各位指点下,还有计算器重新启动后会根据上次选择标准型还是科学型,自动启动相应的选择,这个我是想通过INI配置文件实现,不知道WINDOWS自带的计算器怎么实现的,好像没找到配置文件。
    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.Runtime.InteropServices;namespace Calculator
    {
        public partial class FormCalculator : Form
        {
            #region 成员变量定义        private bool b_decimalSign;     //小数标识
            private bool b_negativeSign;    //负数标识
            private char c_operatorSign;    //当前被按操作符
            private char c_oldOperatorSign; //上一个被按的操作符
            private bool b_operatorClick;   //操作符是否被按下
            private double d_result;        //保存计算结果
            private double d_middle;        //保存一些中间数据
            private bool b_exception;       //异常标识
            private bool b_singleOperatorClick; //单操作符(如sqrt,1/x)是否被按下
            private bool b_equalSigh;        //"="符号是否被按下 
            private double d_temp;          //保存一些临时数据
            private double d_mStore;        //M*记忆功能储存        #endregion        #region 构造函数        public FormCalculator()
            {
                InitializeComponent();
                ClearAll();         //调用方法初始化变量
            }        #endregion        #region 变量值清空方法,用于按钮调用        private void ClearAll()
            {
                Clear();
                c_oldOperatorSign = '+';
                c_operatorSign = '+';
                d_result = 0;
                b_exception = false;
                b_operatorClick = false;
                b_singleOperatorClick = false;
                b_equalSigh = false;
                d_middle = 0;
                d_temp = 0;
            }        private void Clear()
            {
                b_decimalSign = false;
                b_negativeSign = false;
                textBoxOut.Text = "0.";
            }        #endregion        #region 异常提醒        [DllImport("user32.dll")]
            extern static bool MessageBeep(int a, int b); //插入MessageBeep方法用来异常提醒        //异常提醒方法
            private bool CheckException()
            {
                if (b_exception == true)
                {
                    MessageBeep(100, 100);
                    return true;
                }
                else
                {
                    return false;
                }
            }        #endregion        #region 通过按钮输入数字的方法        private void InputNumber(string number)
            {
                if (CheckException())
                {
                    return;
                }
                if (b_operatorClick == true)
                {
                    Clear();
                    b_operatorClick = false;
                }
                if (b_singleOperatorClick == true)
                {
                    Clear();
                    b_singleOperatorClick = false;
                }
                if (b_decimalSign == true)
                {
                    textBoxOut.Text += number;
                }
                else
                {
                    if (textBoxOut.Text[0] == '0' || (textBoxOut.Text[0] == '-' && textBoxOut.Text[1] == '0'))
                    {
                        if (textBoxOut.Text[0] == '0')
                        {
                            textBoxOut.Text = textBoxOut.Text.Remove(0, 1);
                        }
                        else
                        {
                            textBoxOut.Text = textBoxOut.Text.Remove(1, 1);
                        }
                    }
                    textBoxOut.Text=textBoxOut.Text.Insert(textBoxOut.Text.IndexOf('.'), number);
                }
            }        #endregion
      

  30.   

            #region 用于计算的方法,包括异常处理        private void Calculate()
            {
                if (b_equalSigh != true)
                {
                    d_middle = Double.Parse(textBoxOut.Text);
                    d_temp = d_middle;
                }
                else
                {
                    d_middle = d_temp;
                    b_equalSigh = false;
                }
                try
                {
                    switch (c_oldOperatorSign)
                    {
                        case '+':
                            d_result += d_middle;
                            if (d_result.ToString() == "正无穷大" || d_result.ToString() == "负无穷大")
                            {
                                b_exception = true;
                                textBoxOut.Text = "运算结果溢出。";
                            }
                            if (d_result.ToString().Contains('.'))
                            {
                                textBoxOut.Text = d_result.ToString();
                            }
                            else
                            {
                                textBoxOut.Text = d_result.ToString() + ".";
                            }
                            break;
                        case '-':
                            d_result -= d_middle;
                            if (d_result.ToString() == "正无穷大" || d_result.ToString() == "负无穷大")
                            {
                                b_exception = true;
                                textBoxOut.Text = "运算结果溢出。";
                            }
                            if (d_result.ToString().Contains('.'))
                            {
                                textBoxOut.Text = d_result.ToString();
                            }
                            else
                            {
                                textBoxOut.Text = d_result.ToString() + ".";
                            }
                            break;
                        case '*':
                            d_result *= d_middle;
                            if (d_result.ToString() == "正无穷大" || d_result.ToString() == "负无穷大")
                            {
                                b_exception = true;
                                textBoxOut.Text = "运算结果溢出。";
                            }
                            else if (d_result.ToString().Contains('.'))
                            {
                                textBoxOut.Text = d_result.ToString();
                            }
                            else
                            {
                                textBoxOut.Text = d_result.ToString() + ".";
                            }
                            break;
                        case '/':
                            d_result /= d_middle;
                            if (d_result.ToString() == "正无穷大" || d_result.ToString() == "负无穷大")
                            {
                                if (d_middle == 0)
                                {
                                    b_exception = true;
                                    textBoxOut.Text = "除数不能为零。";
                                }
                                else
                                {
                                    b_exception = true;
                                    textBoxOut.Text = "运算结果溢出。";
                                }
                            }
                            else if (d_result.ToString() == "非数字")
                            {
                                b_exception = true;
                                textBoxOut.Text = "函数结果未定义。";
                            }
                            else if (d_result.ToString().Contains('.'))
                            {
                                textBoxOut.Text = d_result.ToString();
                            }
                            else
                            {
                                textBoxOut.Text = d_result.ToString() + ".";
                            }
                            break;
                        case '%':
                            d_result %= d_middle;
                            if (d_result.ToString() == "非数字")
                            {
                                b_exception = true;
                                textBoxOut.Text = "函数输入无效。";
                            }
                            else if (d_result.ToString().Contains('.'))
                            {
                                textBoxOut.Text = d_result.ToString();
                            }
                            else
                            {
                                textBoxOut.Text = d_result.ToString() + ".";
                            }
                            break;
                        case 'S':
                            d_middle = Math.Sqrt(d_middle);
                            if (d_middle.ToString() == "非数字")
                            {
                                b_exception = true;
                                textBoxOut.Text = "函数输入无效。";
                            }
                            else if (d_middle.ToString().Contains('.'))
                            {
                                textBoxOut.Text = d_middle.ToString();
                            }
                            else
                            {
                                textBoxOut.Text = d_middle.ToString() + ".";
                            }
                            c_oldOperatorSign = c_operatorSign;
                            break;
                        case 'R':
                            d_middle = 1.0 / d_middle;
                            if (d_middle.ToString() == "正无穷大" || d_middle.ToString() == "负无穷大")
                            {
                                b_exception = true;
                                textBoxOut.Text = "除数不能为零。";
                            }
                            else if (d_middle.ToString().Contains('.'))
                            {
                                textBoxOut.Text = d_middle.ToString();
                            }
                            else
                            {
                                textBoxOut.Text = d_middle.ToString() + ".";
                            }
                            c_oldOperatorSign = c_operatorSign;
                            break;
                    }
                }
                catch
                {
                    b_exception = true;
                }        }        #endregion
      

  31.   

            #region 0~9 数字按钮方法        private void button0_Click(object sender, EventArgs e)
            {
                InputNumber("0");
            }        private void button1_Click(object sender, EventArgs e)
            {
                InputNumber("1");
            }
            private void button2_Click(object sender, EventArgs e)
            {
                InputNumber("2");
            }        private void button3_Click(object sender, EventArgs e)
            {
                InputNumber("3");
            }        private void button4_Click(object sender, EventArgs e)
            {
                InputNumber("4");
            }        private void button5_Click(object sender, EventArgs e)
            {
                InputNumber("5");
            }        private void button6_Click(object sender, EventArgs e)
            {
                InputNumber("6");
            }        private void button7_Click(object sender, EventArgs e)
            {
                InputNumber("7");
            }        private void button8_Click(object sender, EventArgs e)
            {
                InputNumber("8");
            }        private void button9_Click(object sender, EventArgs e)
            {
                InputNumber("9");
            }        #endregion        #region 小数点、正负号按钮方法        private void buttonPoint_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                if (b_operatorClick == true)
                {
                    Clear();
                    b_operatorClick = false;
                }
                b_decimalSign = true;
            }        private void buttonSign_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                if (b_operatorClick == true)
                {
                    Clear();
                    b_operatorClick = false;
                }
                if (b_negativeSign == true)
                {
                    b_negativeSign = false;
                    textBoxOut.Text = textBoxOut.Text.Trim('-');
                }
                else
                {
                    b_negativeSign = true;
                    textBoxOut.Text = '-' + textBoxOut.Text;
                }        }        #endregion        #region +、-、*、/按钮方法        private void buttonAdd_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                if (b_operatorClick == true)
                {
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '+';
                }
                else
                {
                    b_operatorClick = true;
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '+';
                    Calculate();
                }
            }        private void buttonSub_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                if (b_operatorClick == true)
                {
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '-';
                }
                else
                {
                    b_operatorClick = true;
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '-';
                    Calculate();
                }
            }        private void buttonMul_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                if (b_operatorClick == true)
                {
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '*';
                }
                else
                {
                    b_operatorClick = true;
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '*';
                    Calculate();
                }
            }        private void buttonDiv_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                if (b_operatorClick == true)
                {
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '/';
                }
                else
                {
                    b_operatorClick = true;
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '/';
                    Calculate();
                }
            }        #endregion        #region 清除、清除全部按钮方法        private void buttonClear_Click(object sender, EventArgs e)
            {
                if (b_exception==true)
                {
                    ClearAll();
                }
                else
                {
                    Clear();
                }        }        private void buttonClearAll_Click(object sender, EventArgs e)
            {
                ClearAll();
            }        #endregion        #region =、%(求余)、求倒数、开平方方法        private void buttonEqual_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                if (c_operatorSign != '=')
                {
                    c_oldOperatorSign = c_operatorSign;
                }
                else
                {
                    b_equalSigh = true;
                }
                b_operatorClick = true;
                c_operatorSign = '=';
                Calculate();
            }        private void buttonResidue_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                if (b_operatorClick == true)
                {
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '%';
                }
                else
                {
                    b_operatorClick = true;
                    c_oldOperatorSign = c_operatorSign;
                    c_operatorSign = '%';
                    Calculate();
                }
            }        private void buttonReciprocal_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                b_operatorClick = false;
                b_singleOperatorClick = true;
                c_oldOperatorSign = 'R';
                Calculate();
            }        private void buttonSqrt_Click(object sender, EventArgs e)
            {
                if (CheckException())
                {
                    return;
                }
                b_operatorClick = false;
                b_singleOperatorClick = true;
                c_oldOperatorSign = 'S';
                Calculate();
            }        #endregion