#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define size 400
using namespace std;
char infix[size],postfix[size],Stack[size];
int top;

int precedence(char ch)
{
    switch(ch)
    {
        case '^':return 5;
        case '/':return 4;
        case '*':return 4;
        case '+':return 3;
        case '-':return 3;
        default:return 0;
    }
}

char Pop()
{
    char ret;
    if(top!=-1)
    {
        ret=Stack[top];
        top--;
        return ret;
    }
    else return '#';
}

char Topelem()
{
    char ch;
    if(top!=-1) ch=Stack[top];
    else ch='#';
    return ch;
}

char Push(char ch)
{
    if(top!=size-1)
    {
        top++;
        Stack[top]=ch;
    }
}

int braces(char* s)
{
    int l,r;
    l=0;r=0;
    for(int i=0;s[i];i++)
    {
        if(s[i]='(') l++;
        if(s[i]=')') r++;
    }
    if(l==r) return 0;
    else if(l<r) return 1;
    else return -1;
}

int main()
{
    char ele,elem,st[2];
    int T,prep,pre,popped,j=0,chk=0;
    cin>>T;
    while(T--)
    {
        j=0;chk=0;top=-1;
        strcpy(postfix," ");
        cin>>infix;
        chk=braces(infix);
        if(chk==0)
        {
            for(int i=0;infix[i];i++)
            {
                if(infix[i]=='(')
                {
                    elem=infix[i];
                    Push(elem);
                }
                else if(infix[i]==')')
                {
                    while((popped=Pop())!='(')
                    {
                        postfix[j++]=popped;
                    }
                }
                else if(infix[i]=='^'||infix[i]=='/'||infix[i]=='*'||infix[i]=='+'||infix[i]=='-')
                {
                    elem=infix[i];
                    pre=precedence(elem);
                    ele=Topelem();
                    prep=precedence(ele);
                    if(pre>prep) Push(elem);
                    else
                    {
                        while(prep>=pre)
                        {
                            if(ele=='#') break;
                            popped=Pop();
                            ele=Topelem();
                            postfix[j++]=popped;
                            prep=precedence(ele);
                        }
                        Push(elem);
                    }
                }
            }
        while((popped=Pop())!='#') postfix[j++]=popped;
        postfix[j]='\0';
        cout<<postfix;
        }   
    }
}

The Code isn't working. Please Help!

Recommended Answers

All 5 Replies

Just saying it "isn't working" isn't very helpful. Does the code not build? Does it build but not run? Does it run but produce unexpected output? Show us what you've been trying; please be specific.

The code compiles, but whenever I input an expression, it just comes to an halt, and some time after, it's --"The program is not responding". Using Dev-C++ with MinGW compiler. Pls help.

whenever I input an expression, it just comes to an halt, and some time after, it's --"The program is not responding"

That's not what happens when I run it... depending on the input, I've been able to make it stop without doing anything, and also segfault.

What input are you giving it when it spins forever?

The code compiles

Do you get any warnings? GCC gives me a few that you should address. Let's fix these, and see if the situation improves:

main.cpp|50|warning: no return statement in function returning non-void [-Wreturn-type]|

char Push(char ch)

It doesn't return anything. Did you mean void Push(char ch)?

main.cpp|58|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
main.cpp|59|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|

if(s[i]='(') l++;
if(s[i]=')') r++;

= is assignment, not an equality comparison. You meant ==.

main.cpp|68|warning: unused variable 'st' [-Wunused-variable]|

What is st for?

My Bad. I forgot to complete the program. Sorry!
Thank you very Much
Problem Solved. Hooray!

Congratulations on finishing it, though once again, more detail on how you fixed it would have been appreciated.

Also, just as a friendly reminder: when you do finish with the issue at hand, please mark the thread as closed, so that everyone knows you've solved the problem. TIA.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.