Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
void push (char operand)
{       ++top;
        stack [top] = operand;
        printf("Stack: %s \n", stack);

}
int pop (char operand)
{
int ret;
    if (top == -1)
    {   ret = 0;
    }
    else
    {
        ret = stack [top];
--top;
    }
return ret;
}

int checkErrors(char array[10][100]){
    int l;
    int m;
    for(l=0; l<i;>    for(m=0; m<100; m++){
        if((array[l][m] != '\0')&&((array[l][m]=='$')|| (array[l][m]=='@'))){
            m++;
    printf("Equation %d is invalid\n", l);
    return 1;
        }
    }
    }

}

void postfix(char eqArray[100]){
    int j;
    int k=0;
    char op1;
    memset(&pf,'\0',100);
    memset(&stack,'\0',20);
    top=-1;
    int length=strlen(eqArray);
    //if(!checkErrors) &&&&&& add the paranthesis stuff
    for(j=0; j<length;>    {
        op1=eqArray[j];
        if(op1==')'){
           while(stack[top]!='(')
                    pop(stack[top]);
                    //pf[k]=stack[top];
                    //k++;
        }
        else if(eqArray[j]!='+'&&eqArray[j]!='-'&&eqArray[j]!='*'&&eqArray[j]!='/'&&eqArray[j]!=' '&&eqArray[j]!='('){
                pf[k]= eqArray[j];
                k++;
        }
        else if(top==-1)
        {
            push(op1);
        }
        else if (isPrior(op1,stack[top]))
        {
                pf[k]=stack[top];
                pop(stack[top]);
                push(op1);
                k++;
        }
        else
            push(op1);
    }
    while(top!=-1){
        pf[k]=stack[top];
        pop(stack[top]);
        k++;
    }

printf("Postfix : %s \n",  pf);
}

int isPrior(char op1, char op2)
{if((op1=='*')||(op1=='/'))
 return 1;
else if((op1=='+')||(op1=='-')||(op1=='('))
    return 0;
}

double calculate(char pf[100]){
    int q;
    char num;
    int answer;
    memset(&stack,'\0',20);
    top=-1;
    int length2=strlen(pf);
    printf("%s\n", pf);
    char op1,op2;
    //if(!checkErrors) && add the paranthesis stuff
    for(q=0; q<length2;>    {   num=pf[q];
        if(isdigit(pf[q]))
        {push(num);
        }
        else
            {
            op2=stack[top];
            pop(stack[top]);
            op1=stack[top];
            pop(stack[top]);
    switch(num){
        case '+':
            pop(stack[top]);
            push((op1+op2) - '0' );
            break;
        case '-':
            push((op1-op2) - '0');
            break;
        case '*':
            push((op1*op2) - '0');
            break;
        case '/':
            push((op1/op2) - '0');
            break;
            }
            }

    }
return 0;
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 5-May-14 4:29am
v5
Comments
Sergey Alexandrovich Kryukov 5-May-14 10:25am    
How about running it under the debugger and see what is pushed on your stack?
—SA
Member 10795900 5-May-14 10:28am    
I'm still a beginner and don't really know how to use the debugger but I tried using printf statements and still didn't understand how it got those symbols into the stack

1 solution

Member 10795900 wrote:
I'm still a beginner and don't really know how to use the debugger but I tried using printf statements and still didn't understand how it got those symbols into the stack
So, here is my advice: stop doing all you are doing and learn using the debugger.

You will find the description of the use of the debugger in the documentation of the IDE you use, whatever it is. Don't worry, it is easy enough, and can give you a lot more understanding on how programming works in general.

Doing any further development without the debugger simply makes little to no sense. You should better use the debugger in all cases you have even a little concern of your runtime behavior. You should also use the debugger before asking your questions on CodeProject.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900