Пример #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);
        }
        public static void Main(string[] args)
        {
            SequencedQueue <int> queue = new SequencedQueue <int>();
            SequencedStack <int> stack = new SequencedStack <int>();
            int length = 0;

            queue.Enqueue(10);
            queue.Enqueue(20);
            queue.Enqueue(30);
            queue.Enqueue(50);
            queue.Enqueue(50);
            queue.Enqueue(60);
            Console.WriteLine("The num in original order:");
            length = queue.Count;
            for (int i = 0; i < length; i++)
            {
                int temp = queue.Dequeue();
                Console.Write(temp + "  ");
                stack.Push(temp);
            }
            Console.WriteLine();
            Console.WriteLine("The num in reverse order:");
            length = stack.Count;
            for (int i = 0; i < length; i++)
            {
                Console.Write(stack.Pop() + "  ");
            }
            Console.WriteLine();
        }
Пример #3
0
        public int Eval()
        {
            SequencedStack <int> stack = new SequencedStack <int>();
            int    i = 0;
            char   ch;
            string num = "";

            while (i < pstr.Length)
            {
                ch = pstr[i];
                if ((ch >= '0') && (ch <= '9'))//遇到数字就入栈
                {
                    num = "";
                    while (ch != ' ')
                    {
                        num += ch;
                        i++;
                        if (i < pstr.Length)
                        {
                            ch = pstr[i];
                        }
                    }
                    stack.Push(int.Parse(num));
                    i++;
                }
                else if ((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/')) //遇到运算符,出栈两个数字进行运算
                {
                    int a, b;                                                      //注意两个元素的先后顺序
                    a = stack.Pop();
                    b = stack.Pop();
                    switch (ch)
                    {
                    case '+':
                        stack.Push(b + a);
                        break;

                    case '-':
                        stack.Push(b - a);
                        break;

                    case '*':
                        stack.Push(b * a);
                        break;

                    case '/':
                        stack.Push(b / a);
                        break;

                    default:
                        break;
                    }
                    i++;
                }
                else
                {
                    i++;
                }
            }
            return(stack.Pop());
        }
Пример #4
0
        static void Main(string[] args)
        {
            SequencedQueue <int> sequencedqueue = new SequencedQueue <int>(NUM);
            SequencedStack <int> sequencedstack = new SequencedStack <int>(NUM);

            //以下for可以合并为1个
            //将数字放入队列并显示
            for (int i = 0; i < NUM; i++)
            {
                sequencedqueue.Enqueue(i);
            }
            Console.Write("原来的队列: ");
            sequencedqueue.Show(false);

            //将队列元素依次入栈
            for (int i = 0; i < NUM; i++)
            {
                sequencedstack.Push(sequencedqueue.Dequeue());
            }

            foreach (var item in sequencedstack.Elements)
            {
                Console.Write("{0} ", item);
            }

            //将栈元素依次弹出到队列
            for (int i = 0; i < NUM; i++)
            {
                sequencedqueue.Enqueue(sequencedstack.Pop());
            }
            Console.Write("反序后队列: ");
            sequencedqueue.Show(false);
        }
Пример #5
0
        public static void Main(string[] args)
        {
            SequencedStack <int> num = new SequencedStack <int>();
            int           decimal_num = default(int), length;
            StringBuilder Hex_num = new StringBuilder();

            Console.Write("Please input a num in decimal:");
            try
            {
                decimal_num = int.Parse(Console.ReadLine());
            }
            catch (FormatException e)
            {
                Console.WriteLine("Please input a num in decimal!!! " + e.GetType());
            }
            while (decimal_num > 0)
            {
                num.Push(decimal_num % 16);
                decimal_num /= 16;
            }
            //由于栈的长度会随着Pop()的调用而变化,所以需要一个变量保存
            length = num.Count;
            for (int i = 0; i < length; i++)
            {
                Hex_num.Append(Num2Str(num.Pop()));
            }
            Console.WriteLine(Hex_num);
        }
Пример #6
0
        /// <summary>
        /// 判断表达式括号是否匹配(中序表达式)
        /// </summary>
        public static string MatchingBracket(string expstr)
        {
            SequencedStack <char> ss = new SequencedStack <char>(30);
            char nextToken, outToken;
            int  i = 0;
            bool LlrR = true;

            while (LlrR && i < expstr.Length)
            {
                nextToken = expstr[i];
                i++;
                switch (nextToken)
                {
                //遇到 ( ,将其入栈
                case '(':
                    ss.Push(nextToken);
                    break;

                //遇到 ) ,弹出栈顶元素
                case ')':
                    if (ss.Empty)
                    {
                        LlrR = false;
                    }
                    else
                    {
                        outToken = ss.Pop();
                        //遇到 ( ,但出栈的不是 )
                        if (!outToken.Equals('('))
                        {
                            LlrR = false;
                        }
                    }
                    break;
                }
            }
            if (LlrR)
            {
                return(ss.Empty ? "OK!": "期望)!");
            }
            else
            {
                return("期望(!");
            }
        }
Пример #7
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();
        }
Пример #8
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);
        }
Пример #9
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());
        }