Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
 
struct stack{
    int size ;
    int top;
    char * arr;
};
 
int isEmpty(struct stack* ptr){
    if(ptr->top == -1){
            return 1;
        }
        else{
            return 0;
        }
}
 
int isFull(struct stack* ptr){
    if(ptr->top == ptr->size - 1){
        return 1;
    }
    else{
        return 0;
    }
}
 
void push(struct stack* ptr, int val){
    if(isFull(ptr)){
        cout<<"Stack Overflow! Cannot push to the stack"<< val;
    }
    else{
        ptr->top++;
        ptr->arr[ptr->top] = val;
    }
}
 
int pop(struct stack* ptr){
    if(isEmpty(ptr)){
        cout<<"Stack Underflow! Cannot pop from the stack";
        return -1;
    }
    else{
        int val = ptr->arr[ptr->top];
        ptr->top--;
        return val;
    }
}
 
int peek(struct stack* sp, int i){
    int arrayInd = sp->top -i ;
    if(arrayInd < 0){
        cout<<"Not a valid position for the stack";
        return -1;
    }
    else{
        return sp->arr[arrayInd];
    }
}
int isoperator(char ch){
    if(ch=='+'|| ch=='*' || ch=='/' || ch=='-'){
        return 1;
    }
    return 0;
}
int solution(int a,int b,char c){
    if(c=='*'){
        return (a*b);
        
    }
    if(c=='+'){
        return (a+b);
    }
    if(c=='-'){
        return (a-b);
    }
    if(c=='/'){
        return (a/b);
    }
    return 0;
}
int stackTop(struct stack* sp){
    return sp->arr[sp->top];
}
int precedence(char ch){
    if(ch=='*' || ch=='/'){
        return 1;
    }
    else if(ch=='+' || ch=='-'){
        return 2;
    }
    else{
        return 0;
    }
}
int postfix( char * postfix){
    struct stack *operator1=new struct stack();
    struct stack *op=new struct stack();
    operator1->size=100;
    operator1->top=-1;
    operator1->arr=new char[operator1->size];
    op->size=100;
    op->top=-1;
    op->arr=new char[op->size];
    int i=0; //postfix
    
    while(postfix[i] !='\0'){
        if(!isoperator(postfix[i])){
        push(op,postfix[i]-'0');
            i++;
            
        }
        else{
            if(precedence(postfix[i])>precedence(stackTop(operator1))){
                push(operator1,postfix[i]);
                i++;
            }
            else{
                int a1=pop(op);
                int a2=pop(op);
                // cout<<endl;
                char optr=pop(operator1);
                int val=solution(a2,a1,optr);
                push(op,val);
                i++;
            }
        }
    }

    return stackTop(op);
}
int main(){
   char abc[]= "5*3+8+6";
    int answer =postfix(abc);
      cout<<endl<<answer;
      
       
    return 0;
}


What I have tried:

ignore postfix,postfix=infix
i am not getting the answer but the last operand value of infix
Posted
Updated 27-May-22 23:59pm

It looks you are trying to implement (without full luck) an algorithm similar to the one shown in this page: Evaluation of Infix expressions | TutorialHorizon[^].
I've fixed your code, try:
C++
#include<string>
#include<cstring>
using namespace std;

struct stack{
    int size ;
    int top;
    char * arr;
};

int isEmpty(struct stack* ptr){
    if(ptr->top == -1){
            return 1;
        }
        else{
            return 0;
        }
}

int isFull(struct stack* ptr){
    if(ptr->top == ptr->size - 1){
        return 1;
    }
    else{
        return 0;
    }
}

void push(struct stack* ptr, int val){
    if(isFull(ptr)){
        cout<<"Stack Overflow! Cannot push to the stack"<< val;
    }
    else{
        ptr->top++;
        ptr->arr[ptr->top] = val;
    }
}

int pop(struct stack* ptr){
    if(isEmpty(ptr)){
        cout<<"Stack Underflow! Cannot pop from the stack";
        return -1;
    }
    else{
        int val = ptr->arr[ptr->top];
        ptr->top--;
        return val;
    }
}

int peek(struct stack* sp, int i){
    int arrayInd = sp->top -i ;
    if(arrayInd < 0){
        cout<<"Not a valid position for the stack";
        return -1;
    }
    else{
        return sp->arr[arrayInd];
    }
}
int isoperator(char ch){
    if(ch=='+'|| ch=='*' || ch=='/' || ch=='-'){
        return 1;
    }
    return 0;
}
int solution(int a,int b,char c){
    if(c=='*'){
        return (a*b);

    }
    if(c=='+'){
        return (a+b);
    }
    if(c=='-'){
        return (a-b);
    }
    if(c=='/'){
        return (a/b);
    }
    return 0;
}
int stackTop(struct stack* sp){
    return sp->arr[sp->top];
}
int precedence(char ch){
    if(ch=='*' || ch=='/'){
        return 2;
    }
    else if(ch=='+' || ch=='-'){
        return 1;
    }
    else{
        return 0;
    }
}
int postfix( char * postfix){
    struct stack *operator1=new struct stack();
    struct stack *op=new struct stack();
    operator1->size=100;
    operator1->top=-1;
    operator1->arr=new char[operator1->size];
    op->size=100;
    op->top=-1;
    op->arr=new char[op->size];
    int i=0; //postfix

    while(postfix[i] !='\0'){
        if(!isoperator(postfix[i])){
        push(op,postfix[i]-'0');
            i++;
        }
        else
        {
            for (;;)
            {
              if ( isEmpty( operator1) || precedence(postfix[i])>precedence(stackTop(operator1)))
              {
                  push(operator1,postfix[i]);
                  break; // exit the loop
              }
              int a1=pop(op);
              int a2=pop(op);
              // cout<<endl;
              char optr=pop(operator1);
              int val=solution(a2,a1,optr);
              push(op,val);
            }
            i++;
        }
    }
    while ( ! isEmpty(operator1))
    {
       int a1=pop(op);
       int a2=pop(op);
       char optr=pop(operator1);
       int val=solution(a2,a1,optr);
        push(op,val);
    }
    return stackTop(op);
}
int main(){
    char abc[]= "5*3+8+6";
    int answer =postfix(abc);
      cout<<endl<<answer;
    return 0;
}


Please note: since you are using C++, it doesn't make sense implementing the stack in a very C-like manner; use a class instead (as matter of fact the C++ standard library gently provides a reference implementation, the std::stack class).
 
Share this answer
 
v2
Comments
Ashish Gautam 2022 28-May-22 6:33am    
K thanks for guidance
CPallini 28-May-22 7:49am    
You are welcome.
Ashish Gautam 2022 28-May-22 6:51am    
without using for loop
#include<string>
#include<cstring>
#include<iostream>
using namespace std;

struct stack{
int size ;
int top;
char * arr;
};

int isEmpty(struct stack* ptr){
if(ptr->top == -1){
return 1;
}
else{
return 0;
}
}

int isFull(struct stack* ptr){
if(ptr->top == ptr->size - 1){
return 1;
}
else{
return 0;
}
}

void push(struct stack* ptr, int val){
if(isFull(ptr)){
cout<<"Stack Overflow! Cannot push to the stack"<< val;
}
else{
ptr->top++;
ptr->arr[ptr->top] = val;
}
}

int pop(struct stack* ptr){
if(isEmpty(ptr)){
cout<<"Stack Underflow! Cannot pop from the stack";
return -1;
}
else{
int val = ptr->arr[ptr->top];
ptr->top--;
return val;
}
}

int peek(struct stack* sp, int i){
int arrayInd = sp->top -i ;
if(arrayInd < 0){
cout<<"Not a valid position for the stack";
return -1;
}
else{
return sp->arr[arrayInd];
}
}
int isoperator(char ch){
if(ch=='+'|| ch=='*' || ch=='/' || ch=='-'){
return 1;
}
return 0;
}
int solution(int a,int b,char c){
if(c=='*'){
return (a*b);

}
if(c=='+'){
return (a+b);
}
if(c=='-'){
return (a-b);
}
if(c=='/'){
return (a/b);
}
return 0;
}
int stackTop(struct stack* sp){
return sp->arr[sp->top];
}
int precedence(char ch){
if(ch=='*' || ch=='/'){
return 2;
}
else if(ch=='+' || ch=='-'){
return 1;
}
else{
return 0;
}
}
int postfix( char * postfix){
struct stack *operator1=new struct stack();
struct stack *op=new struct stack();
operator1->size=100;
operator1->top=-1;
operator1->arr=new char[operator1->size];
op->size=100;
op->top=-1;
op->arr=new char[op->size];
int i=0; //postfix

while(postfix[i] !='\0'){
if(!isoperator(postfix[i])){
push(op,postfix[i]-'0');
i++;
}
else
{

if ( isEmpty( operator1) || precedence(postfix[i])>precedence(stackTop(operator1)))
{
push(operator1,postfix[i]);
i++;
}
else{
int a1=pop(op);
int a2=pop(op);
char optr=pop(operator1);
int val=solution(a2,a1,optr);
push(op,val);
}

}
}
while ( ! isEmpty(operator1))
{
int a1=pop(op);
int a2=pop(op);
char optr=pop(operator1);
int val=solution(a2,a1,optr);
push(op,val);
}
return stackTop(op);
}
int main(){
char abc[]= "5+3+8+6";
int answer =postfix(abc);
cout<
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C++
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Comments
CPallini 28-May-22 6:00am    
Have my balancing 5.
Ashish Gautam 2022 28-May-22 6:35am    
Haha

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