数据结构之应用"栈(Stack)"实现: 解析算术表达式及计算

 
//using System;
class Class1
{
    public static void Main()
    {
        System.Console.WriteLine("Hello World!");
        //中缀 => 后缀表达式
        string s = "(  1.9 +(20 +41)/ (25*11) -3 ) * 2"; //中缀; //中缀
        string S = ""; //后缀
        char[] Operators = new char[s.Length];
        int Top = -1;
        for (int i = 0; i < s.Length; i++)
        {
            char C = s[i];
            switch (C)
            {
            case ' ': //忽略空格
                break;
            case '+': //操作符
            case '-':
                while (Top >= 0) //栈不为空时
                {
                    char c = Operators[Top--]; //pop Operator
                    if (c == '(')
                    {
                        Operators[++Top] = c; //push Operator
                        break;
                    }
                    else
                    {
                        S = S + c;
                    }
                }
                Operators[++Top] = C; //push Operator
                S += " ";
                break;
            case '*': //忽略空格
            case '/':
                while (Top >= 0) //栈不为空时
                {
                    char c = Operators[Top--]; //pop Operator
                    if (c == '(')
                    {
                        Operators[++Top] = c; //push Operator
                        break;
                    }
                    else
                    {
                        if (c == '+' || c == '-')
                        {
                            Operators[++Top] = c; //push Operator
                            break;
                        }
                        else
                        {
                            S = S + c;
                        }
                    }
                }
                Operators[++Top] = C; //push Operator
                S += " ";
                break;
            case '(':
                Operators[++Top] = C;
                S += " ";
                break;
            case ')':
                while (Top >= 0) //栈不为空时
                {
                    char c = Operators[Top--]; //pop Operator
                    if (c == '(')
                    {
                        break;
                    }
                    else
                    {
                        S = S + c;
                    }
                }
                S += " ";
                break;
            default:
                S = S + C;
                break;

            }
        }
        while (Top >= 0)
        {
            S = S + Operators[Top--]; //pop Operator
        }

        System.Console.WriteLine(S); //后缀

        //后缀表达式计算
        double[] Operands = new double[S.Length];
        double x, y, v;
        Top = -1;
        string Operand = "";
        for (int i = 0; i < S.Length; i++)
        {
            char c = S[i];
            if ((c >= '0' && c <= '9') || c == '.')
            {
                Operand += c;
            }

            if ((c == ' ' || i == S.Length - 1) && Operand != "") //Update
            {
                Operands[++Top] = System.Convert.ToDouble(Operand); //push Operands
                Operand = "";
            }

            if (c == '+' || c == '-' || c == '*' || c == '/')
            {
                if ((Operand != ""))
                {
                    Operands[++Top] = System.Convert.ToDouble(Operand); //push Operands
                    Operand = "";
                }
                y = Operands[Top--]; //pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响
                x = Operands[Top--]; //pop 双目运算符的第一操作数
                switch (c)
                {
                case '+':
                    v = x + y;
                    break;
                case '-':
                    v = x - y;
                    break;
                case '*':
                    v = x * y;
                    break;
                case '/':
                    v = x / y; // 第一操作数 / 第二操作数 注意操作数顺序对除法的影响
                    break;
                default:
                    v = 0;
                    break;
                }
                Operands[++Top] = v; //push 中间结果再次入栈
            }
        }
        v = Operands[Top--]; //pop 最终结果
        System.Console.WriteLine(v);
        System.Console.ReadLine();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值