Exemplo n.º 1
0
 public void ProcessOperators(Stack <Element> st, Element element, Element top)
 {
     while (st.Count > 0 && Precedence((OperatorElement)element) >= Precedence((OperatorElement)top))
     {
         Element p = st.Pop();
         if (((OperatorElement)p).type == OperatorType.OPAREN)
         {
             break;
         }
         converted.Add(p);
         if (st.Count > 0)
         {
             top = st.First();
         }
     }
 }
Exemplo n.º 2
0
            public List <Element> ConvertFromInfixToPostFix(List <Element> e)
            {
                List <Element>  stack1 = new List <Element>(e);
                Stack <Element> st     = new Stack <Element>();

                for (int i = 0; i < stack1.Count; i++)
                {
                    Element element = stack1[i];
                    if (element.GetType().Equals(typeof(OperatorElement)))
                    {
                        if (st.Count == 0 || ((OperatorElement)element).type == OperatorType.OPAREN)
                        {
                            st.Push(element);
                        }
                        else
                        {
                            Element top = st.First();
                            if (((OperatorElement)element).type == OperatorType.CPAREN)
                            {
                                ProcessOperators(st, element, top);
                            }
                            else if (Precedence((OperatorElement)element) < Precedence((OperatorElement)top))
                            {
                                st.Push(element);
                            }
                            else
                            {
                                ProcessOperators(st, element, top);
                                st.Push(element);
                            }
                        }
                    }
                    else
                    {
                        converted.Add(element);
                    }
                }

                //pop all operators in stack
                while (st.Count > 0)
                {
                    Element b1 = st.Pop();
                    converted.Add(b1);
                }

                return(converted);
            }