Infix to postfix

Below is my code:
https://www.codechef.com/viewsolution/31973722

i dont know why but i am getting WA can somebody help me.

@uti_1234 Add condition op.top()==’-’ in block

if(i=='+')
{
	while(op.top()=='+'||op.top()=='*'||op.top()=='/'||op.top()=='^' || op.top()=='-')
	{
		cout<<op.top();
		op.pop();
	}
	op.push('+');
}

Add condition op.top()==’’* in block

if(i=='/')
{
    while(op.top()=='/'||op.top()=='^' || op.top()=='*' )
	{
		cout<<op.top();
		op.pop();
	}
	op.push('/');
}
1 Like

got it thanks.

try:
test_cases = int(input())
for z in range(test_cases):

    n = int(input())
    memory = list(input())
    print(z)
    stack = ["("]
    postfix = []
    prefence = {"*": 2, "/": 2, "+": 1, "-": 1, "^": 3}
    brakets = ["(", ")"]


    def infixpsotfix(element):
        for element in memory:

            if element not in brakets and element not in prefence:
                postfix.append(element)
            elif element in prefence or element in brakets:

                if element in prefence:
                    if stack[-1] in prefence and prefence[stack[-1]] < prefence[element] and element in prefence:
                        stack.append(element)


                    elif stack[-1] in prefence and prefence[stack[-1]] >= prefence[element]:
                        poped = stack.pop()

                        stack.append(element)
                        postfix.append(poped)
                    else:
                        stack.append(element)
                if element in brakets and element == "(":
                    stack.append(element)
                elif element in brakets and stack[-1] == "(":
                    pass
                elif element in brakets and element == ")":
                    for i in range(len(stack)):
                        pope = stack.pop()
                        if pope != "(":

                            postfix.append(pope)
                        else:
                            break


    infixpsotfix(memory)
    for ii in range(len(stack)):

        if stack[len(stack)-ii-1] == "(":
            break
        else:
            postfix.append(stack[len(stack)-ii-1])

    print("".join(postfix))

except:
pass

This is my Code Anyone Please Tell me where it Going Grong