コード例 #1
0
        public static string revstr(string word)
        {
            string         o  = "";
            stackpp <char> s1 = new stackpp <char>(word.Length);

            foreach (char c in word)
            {
                s1.push(c);
            }
            while (!s1.isempty())
            {
                o += s1.ar[s1.pop()];
            }
            Console.WriteLine();
            return(o);
        }
コード例 #2
0
        public static bool isBalancedParenthesis(string expn)
        {
            stackpp <char> stk = new stackpp <char>(expn.Length);

            foreach (char ch in expn)
            {
                switch (ch)
                {
                case '{':
                case '[':
                case '(':
                    stk.push(ch);
                    break;

                case '}':
                    if (stk.ar[stk.pop()] != '{')
                    {
                        return(false);
                    }
                    break;

                case ']':
                    if (stk.ar[stk.pop()] != '[')
                    {
                        return(false);
                    }
                    break;

                case ')':
                    if (stk.ar[stk.pop()] != '(')
                    {
                        return(false);
                    }
                    break;
                }
            }
            return(stk.isempty());
        }
コード例 #3
0
        static bool InfixToPostfixConvert(ref string infixBuffer, out string postfixBuffer)
        {
            int priority = 0;

            postfixBuffer = "";
            int            f  = 0;
            stackpp <Char> s1 = new stackpp <char>(infixBuffer.Length);

            for (int i = 0; i < infixBuffer.Length; i++)
            {
                char ch = infixBuffer[i];
                if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
                {
                    f = -1;
                    // check the precedence
                    if (s1.isempty())
                    {
                        s1.push(ch);
                    }
                    else
                    {
                        if (s1.ar[s1.peak()] == '*' || s1.ar[s1.peak()] == '/')
                        {
                            priority = 1;
                        }
                        else
                        {
                            priority = 0;
                        }
                        if (priority == 1)
                        {
                            if (ch == '+' || ch == '-')
                            {
                                postfixBuffer += " " + s1.ar[s1.pop()];
                                i--;
                            }
                            else
                            { // Same
                                postfixBuffer += " " + s1.ar[s1.pop()];
                                i--;
                            }
                        }
                        else
                        {
                            if (ch == '+' || ch == '-')
                            {
                                postfixBuffer += " " + s1.ar[s1.pop()];
                                i--;
                            }
                            else
                            {
                                s1.push(ch);
                            }
                        }
                    } //end if stack not empty
                }     //end check operator
                else
                {
                    if (f == -1)
                    {
                        postfixBuffer += " ";
                    }
                    postfixBuffer += ch;
                    f              = 0;
                }
            }
            // postfixBuffer += " ";
            int len = s1.top;

            for (int j = 0; j <= len; j++)
            {
                postfixBuffer += " " + s1.ar[s1.pop()];
            }
            return(true);
        }