C# solutions


  • 0
    J
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        //using System;
        class QPostFixExp
        {
            private static Dictionary<char, int> priorityMap = new Dictionary<char, int>();
            static QPostFixExp()
            {
                Dictionary<char, int> priorityMap = new Dictionary<char, int>();
                priorityMap.Add('+', 1);
                priorityMap.Add('-', 1);
                priorityMap.Add('*', 2);
                priorityMap.Add('/', 2);
            }
            private static int ComparePriority(char op1, char op2)
            {
                return priorityMap[op1] - priorityMap[op2];
            }
            public static string TranToPostFix(string infixExp)
            {
                if (string.IsNullOrWhiteSpace(infixExp))
                    return null;
    
                Stack<char> ops = new Stack<char>();
                StringBuilder result = new StringBuilder(infixExp.Length);
    
                for (int i = 0; i < infixExp.Length; i++)
                {
                    char ch = infixExp[i];
                    switch(ch)
                    {
                        case ' ':
                            //ignore blank space
                            break;
                        case '+':
                        case '-':
                        case '*':
                        case '/':
                            if (ops.Count == 0)
                                ops.Push(ch);
                            else
                            {
                                while (ops.Count > 0)
                                {
                                    char peek = ops.Peek();
                                    if (ComparePriority(ch, peek) > 0)
                                    {
                                        break;
                                    }
                                    else
                                        result.Append(ops.Pop());
                                }
                                ops.Push(ch);
                            }
                            result.Append(" ");
                            break;
                        case '(':
                            ops.Push(ch);
                            break;
                        case ')':
                            while (ops.Count > 0)
                            {
                                char pop = ops.Pop();
                                if (pop == '(')
                                    break;
                                else
                                    result.Append(pop);
                            }
                            break;
                        default:
                            result.Append(ch);
                            break; 
                    }
                }
    
                for (int i = 0; i < ops.Count; i++)
                {
                    result.Append(" " + ops.Pop());
                }
                return result.ToString();
            }
    
            private static bool IsOperator(char op)
            {
                return "+-*/".Contains(op);
            }
            private static int Cal(int pop1, int pop2, char op)
            {
                if (op == '+')
                    return pop2 + pop1;
                else if (op == '-')
                    return pop2 - pop1;
                else if (op == '*')
                    return pop2 * pop1;
                else
                    return pop2 / pop1;
            }
            public static int Calculate(string postFixExp)
            {
                if (string.IsNullOrEmpty(postFixExp))
                    return 0;
                int i = 0;
                Stack<int> stack = new Stack<int>();
    
                while (i < postFixExp.Length)
                {
                    if (char.IsDigit(postFixExp[i]))
                        stack.Push(postFixExp[i] - '0');
                    else if (IsOperator(postFixExp[i])) // if can be removed, suppose only digits & +-*/ existing in postfix
                    {
                        //stack.Push(num);
    
                        int pop1 = stack.Pop();
                        int pop2 = stack.Pop();
                        stack.Push(Cal(pop1, pop2, postFixExp[i]));
                    }
    
                    i++;
                }
    
                return stack.Peek();
            }
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.