Esempio n. 1
0
        //static void Main(string[] args)
        //{
        //	//Console.WriteLine(Compute("2-6-7*8/2+5"));
        //	Console.WriteLine(Compute("2*3+5/6*3+15"));

        //}

        static double Compute(string expr)
        {
            var numStack = new Utils.Stack <double>();
            var opStack  = new Utils.Stack <char>();

            for (int i = 0; i < expr.Length; i++)
            {
                var value = findNextNumber(expr, i);
                numStack.Push(value);

                i += value.ToString().Length;

                if (i >= expr.Length)
                {
                    break;
                }

                collapseOp(numStack, opStack, expr[i]);
                opStack.Push(expr[i]);
            }

            if (opStack.Count() == 1 && numStack.Count() == 2)
            {
                collapseOp(numStack, opStack, opStack.Peek());
            }

            if (numStack.Count() == 1)
            {
                return(numStack.Pop());
            }

            throw new InvalidOperationException();
        }
        public static void usingStack(SinglyLinkedListNode <int> node)
        {
            var stck = new Utils.Stack <int>();

            while (node != null)
            {
                stck.Push(node.Data);
                node = node.Next;
            }

            while (!stck.IsEmpty())
            {
                Console.WriteLine(stck.Pop());
            }
        }
Esempio n. 3
0
 static void collapseOp(Utils.Stack <double> numStacks, Utils.Stack <char> opStack, char currentOp)
 {
     while (numStacks.Count() >= 2 && opStack.Count() >= 1)
     {
         if (OpPriority(currentOp) <= OpPriority(opStack.Peek()))
         {
             var num2   = numStacks.Pop();
             var num1   = numStacks.Pop();
             var op     = opStack.Pop();
             var result = applyOp(num1, num2, op);
             numStacks.Push(result);
         }
         else
         {
             break;
         }
     }
 }