Esempio n. 1
0
        public bool isMatchBacket()
        {
            bool isMatch = false;
            SequencedStack <char> stack = new SequencedStack <char>();
            int i = 0;

            while (i < expstr.Length)
            {
                if (expstr[i] == '(')
                {
                    stack.Push('(');
                }
                else if (expstr[i] == ')')
                {
                    if (stack.Empty)
                    {
                        return(false);
                    }
                    else
                    {
                        if (stack.Peek() == '(')
                        {
                            stack.Pop();
                        }
                    }
                }
                i++;
            }
            isMatch = stack.Empty;
            return(isMatch);
        }
Esempio n. 2
0
        /// <summary>
        /// <para>使用自定义栈SequencedStack进行10进制到16进制转换</para>
        /// </summary>
        private static void trofSequencedStack(int num)
        {
            //由于初始化为10位:故最大只能转换10位的16进制数
            SequencedStack <int> sequencedstack = new SequencedStack <int>(10);

            Console.WriteLine("转换前的十进制整数: {0}", num);
            while (num != 0)
            {
                sequencedstack.Push(num % 16);
                num = num / 16;
            }
            int i = sequencedstack.Count;

            Console.Write("转换后的十六进制整数: ");
            for (; i > 0; i--)
            {
                if (sequencedstack.Peek() < 10)
                {
                    Console.Write(sequencedstack.Pop() + "");
                }
                else
                {
                    switch (sequencedstack.Pop())
                    {
                    case 10: Console.Write("A"); break;

                    case 11: Console.Write("B"); break;

                    case 12: Console.Write("C"); break;

                    case 13: Console.Write("D"); break;

                    case 14: Console.Write("E"); break;

                    case 15: Console.Write("F"); break;

                    default: break;
                    }
                }
            }
            Console.WriteLine();
        }
Esempio n. 3
0
        /// <summary>
        /// 将输入的中序表达式转换为后序表达式
        /// <para>1.从左到右对中缀表达式进行扫描,每次处理一个字符</para>
        /// <para>2.遇到左括号,入栈;遇到数字,原样输出</para>
        /// <para>3.遇到常规运算符时:</para>
        /// <para>①栈非空 且栈顶不是左括号 且栈顶运算符的优先级不低于输入运算符优先级,</para>
        /// <para>反复将栈顶元素出栈输出</para>
        /// <para>②当不符合①时则把输入的运算符压栈</para>
        /// <para>4.若遇到右括号,则运算符出栈,直到出栈的数据元素为左括号停止</para>
        /// <para>5.当表达式的符号序列全部读入后,若栈内仍有元素,把他们依次出栈输出</para>
        /// </summary>
        public static string ConvertExp(String infixStr)
        {
            //创建长度为30的线性字符串栈
            SequencedStack <string> ss = new SequencedStack <string>(30);
            char   ch;
            string tempStr    = "";
            string postfixStr = "";
            int    i          = 0;

            //当表达式还没有读取结束时
            while (i < infixStr.Length)
            {
                ch = infixStr[i];
                switch (ch)
                {
                //当遇到 + - 时(优先级低)
                case '+':
                case '-':
                    //栈非空且栈顶不是 ( 且栈顶运算符的优先级不低于输入的运算符的优先级时
                    //反复将栈顶元素出栈输出直到不符合上述条件
                    while (!ss.Empty && !(ss.Peek()).Equals("("))
                    {
                        tempStr     = ss.Pop();
                        postfixStr += tempStr;
                    }
                    ss.Push(ch.ToString());
                    i++;
                    break;

                //遇到 * / 时 (优先级高)
                case '*':
                case '/':
                    //它的优先级比栈顶数据元素的优先级相同,所以栈顶数据元素出栈
                    //直到新栈顶数据元素的优先级比它低,然后将它入栈
                    while (!ss.Empty && ((ss.Peek()).Equals("*") ||
                                         (ss.Peek()).Equals("/")))
                    {
                        tempStr     = ss.Pop();
                        postfixStr += tempStr;
                    }
                    ss.Push(ch.ToString());
                    i++;
                    break;

                case '(':
                    //遇到左括号时, 入栈(优先级最高)
                    ss.Push(ch.ToString());
                    i++;
                    break;

                case ')':
                    //遇到右括号时,栈顶元素出栈
                    //直到出栈的数据元素为左括号,此时才括号匹配
                    tempStr = ss.Pop();
                    while (!ss.Empty && (tempStr == null || !tempStr.Equals("(")))
                    {
                        postfixStr += tempStr;
                        tempStr     = ss.Pop();
                    }
                    i++;
                    break;

                default:
                    //遇到数字时(根据ASCII比较大小)
                    while (ch >= '0' && ch <= '9')
                    {
                        postfixStr += ch;
                        i++;
                        if (i < infixStr.Length)
                        {
                            ch = infixStr[i];
                        }
                        else
                        {
                            ch = '=';
                        }
                    }
                    // 添加分隔符使表达式美观
                    postfixStr += " ";
                    break;
                }
            }
            while (!ss.Empty)
            {
                tempStr    = ss.Pop();
                postfixStr = postfixStr + tempStr;
            }
            return(postfixStr);
        }
Esempio n. 4
0
        public string Transform()
        {
            pstr.Clear();
            SequencedStack <char> stack = new SequencedStack <char>();
            int  i = 0;
            char ch, temp;

            while (i < expstr.Length)
            {
                ch = expstr[i];
                switch (ch)
                {
                case '+':
                case '-':
                    while ((!stack.Empty) && (stack.Peek() != '('))
                    {
                        temp = stack.Pop();
                        pstr.Append(temp);
                    }
                    pstr.Append(' ');
                    stack.Push(ch);
                    i++;
                    break;

                case '*':
                case '/':
                    while ((!stack.Empty) && ((stack.Peek() == '*') || (stack.Peek() == '/')))
                    {
                        temp = stack.Pop();
                        pstr.Append(temp);
                    }
                    pstr.Append(' ');
                    stack.Push(ch);
                    i++;
                    break;

                case '(':
                    stack.Push(ch);
                    i++;
                    break;

                case ')':
                    while ((!stack.Empty) && (stack.Peek() != '('))
                    {
                        temp = stack.Pop();
                        pstr.Append(temp);
                    }
                    stack.Pop();
                    i++;
                    break;

                case '0':    //数字
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    while ((ch >= '0') && (ch <= '9'))
                    {
                        pstr.Append(ch);
                        i++;
                        if (i < expstr.Length)
                        {
                            ch = expstr[i];
                        }
                        else
                        {
                            ch = '=';
                        }
                    }
                    pstr.Append(' ');
                    break;

                default:
                    i++;
                    break;
                }
            }
            while (!stack.Empty)
            {
                pstr.Append(stack.Pop());
                //pstr.Append(' ');
            }
            return(pstr.ToString());
        }