public static string CheckMissingParanthesis(string expr)
        {
            GenericStack<char> stack = new GenericStack<char>();

            if (expr== null || expr.Length == 0) {
                return "";
            }


            // PUsh if you see a opening bracket to the stack
            // Pop if you see a closing bracket from the stack
            //          If the stack is empty when you encounter a closing bracket, it means that there is a missing opening paranthesis
            // At the end, if the stack is not empty, then it means that there is a missing closing bracket.
            string missingParanthesis = "";
            for (int i=0; i<expr.Length; i++)
            {
                char ch = expr[i];

                if (ch == '(')
                {
                    stack.Push(ch);
                }
                else if (ch == ')')
                {
                    if (stack.StackCount > 0 && stack.Top() == '(')
                    {
                        stack.Pop();
                    }
                    else
                    {
                        missingParanthesis = missingParanthesis + "(\t";
                    }
                }
            }

            if (stack.StackCount > 0)
            {
                while (stack.StackCount != 0)
                {
                    char ch = stack.Pop();

                    if (ch == '(')
                    {
                        missingParanthesis = missingParanthesis + ")\t";
                    }
                }
            }

            return missingParanthesis;
            
            ()((()())))))
            
            }
示例#2
0
        public static int EvaluateExpression(string expr)
        {
            if (expr == null || expr.Length == 0)
            {
                Console.WriteLine("Expression is null or empty");
                throw new Exception("Expression is null or empty");
            }

            GenericStack <int> stack = new GenericStack <int>();

            for (int i = 0; i < expr.Length; i++)
            {
                char ch    = expr[i];
                int  ascii = ch - '0';
                if (ascii >= 0 && ascii <= 9)
                {
                    stack.Push(ascii);
                }
                else if (ch == '*')
                {
                    if (stack.StackCount == 0)
                    {
                        Console.WriteLine("Expression invalid");
                        throw new Exception("Expression invalid");
                    }

                    int prevNum = stack.Pop();
                    int nextNum = expr[i + 1] - '0';
                    if (nextNum < 0 && nextNum > 9)
                    {
                        Console.WriteLine("Expression invalid");
                        throw new Exception("Expression invalid");
                    }

                    stack.Push(prevNum * nextNum);
                    i++;
                }
            }

            var sum = 0;

            while (stack.StackCount > 0)
            {
                sum = sum + stack.Pop();
            }

            return(sum);
        }