可计算代小数的(+-*/数学公式)字符串

在VS2005.net C#中调试通过....

//只是调用码

//原文出处: http://blog.csdn.net/sunheartlee/archive/2007/02/11/1507384.aspx

//中序转换成后序表达式再计算
        // 如:23+56/(102-100)*((36-24)/(8-6))
        // 转换成:23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
        //以便利用栈的方式都进行计算。
        private string CalculateParenthesesExpression(string Expression)
        {
            ArrayList operatorList = new ArrayList();
            string operator1;
            string ExpressionString = "";
            string operand3;
            Expression = Expression.Replace(" ", "");
            while (Expression.Length > 0)
            {
                operand3 = "";
                //取数字处理
                if (Char.IsNumber(Expression[0]))
                {
                    while (Char.IsNumber(Expression[0]) ||  Expression[0].ToString() == ".")
                    {
                        operand3 += Expression[0].ToString();
                        Expression = Expression.Substring(1);
                        if (Expression == "") break;


                    }
                    //取小数点处理
                 
                    ExpressionString += operand3 + "|";
                }

                //取“C”处理
                if (Expression.Length > 0 && Expression[0].ToString() == "(")
                {
                    operatorList.Add("(");
                    Expression = Expression.Substring(1);
                }

                //取“)”处理
                operand3 = "";
                if (Expression.Length > 0 && Expression[0].ToString() == ")")
                {
                    do
                    {

                        if (operatorList[operatorList.Count - 1].ToString() != "(")
                        {
                            operand3 += operatorList[operatorList.Count - 1].ToString() + "|";
                            operatorList.RemoveAt(operatorList.Count - 1);
                        }
                        else
                        {
                            operatorList.RemoveAt(operatorList.Count - 1);
                            break;
                        }

                    } while (true);
                    ExpressionString += operand3;
                    Expression = Expression.Substring(1);
                }

                //取运算符号处理
                operand3 = "";
                if (Expression.Length > 0 && (Expression[0].ToString() == "*" || Expression[0].ToString() == "/" || Expression[0].ToString() == "+" || Expression[0].ToString() == "-"))
                {
                    operator1 = Expression[0].ToString();
                    if (operatorList.Count > 0)
                    {

                        if (operatorList[operatorList.Count - 1].ToString() == "(" || verifyOperatorPriority(operator1, operatorList[operatorList.Count - 1].ToString()))
                        {
                            operatorList.Add(operator1);
                        }
                        else
                        {
                            operand3 += operatorList[operatorList.Count - 1].ToString() + "|";
                            operatorList.RemoveAt(operatorList.Count - 1);
                            operatorList.Add(operator1);
                            ExpressionString += operand3;

                        }

                    }
                    else
                    {
                        operatorList.Add(operator1);
                    }
                    Expression = Expression.Substring(1);
                }
               
            }

            operand3 = "";
            while (operatorList.Count != 0)
            {
                operand3 += operatorList[operatorList.Count - 1].ToString() + "|";
                operatorList.RemoveAt(operatorList.Count - 1);
            }

            ExpressionString += operand3.Substring(0, operand3.Length - 1); ;


            return CalculateParenthesesExpressionEx(ExpressionString);

        }


        // 第二步:把转换成后序表达的式子计算
        //23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
        private string CalculateParenthesesExpressionEx(string Expression)
        {
            //定义两个栈
            ArrayList operandList = new ArrayList();
            float operand1;
            float operand2;
            string[] operand3;

            Expression = Expression.Replace(" ", "");
            operand3 = Expression.Split(Convert.ToChar("|"));

            for (int i = 0; i < operand3.Length; i++)
            {
                if (Char.IsNumber(operand3[i], 0))
                {
                    operandList.Add(operand3[i].ToString());
                }
                else
                {
                    //两个操作数退栈和一个操作符退栈计算
                    operand2 = (float)Convert.ToDouble(operandList[operandList.Count - 1]);
                    operandList.RemoveAt(operandList.Count - 1);
                    operand1 = (float)Convert.ToDouble(operandList[operandList.Count - 1]);
                    operandList.RemoveAt(operandList.Count - 1);
                    operandList.Add(calculate(operand1, operand2, operand3[i]).ToString());
                }

            }


            return operandList[0].ToString();
        }

 

        //判断两个运算符优先级别
        private bool verifyOperatorPriority(string Operator1, string Operator2)
        {

            if (Operator1 == "*" && Operator2 == "+")
                return true;
            else if (Operator1 == "*" && Operator2 == "-")
                return true;
            else if (Operator1 == "/" && Operator2 == "+")
                return true;
            else if (Operator1 == "/" && Operator2 == "-")
                return true;
            else
                return false;
        }

        //计算
        private float calculate(float operand1, float operand2, string operator2)
        {
            switch (operator2)
            {
                case "*":
                    operand1 *= operand2;
                    break;
                case "/":
                    operand1 /= operand2;
                    break;
                case "+":
                    operand1 += operand2;
                    break;
                case "-":
                    operand1 -= operand2;
                    break;
                default:
                    break;
            }
            return operand1;
        } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值