Link to home
Start Free TrialLog in
Avatar of CRAZY
CRAZY

asked on

DATA STRUCTURE PROGRAMMING

I need to make a C++ program so that you can enter postfix statements and then calculate them for example

postfix       infix       answer
2 3 +     = 2 + 3     =    5
2 3 4 + * = 2 * 3 + 4 =   10

it doesnt have to change the postfix to infix it just has to be able to give the correct answer.(but the programmer should understand how postfix and infix work)

I am thinking that I have to somehow create a stack and allocate and delete space from memory dynamically. But im not sure where I should begin or what level of programming this involves.

if anyone has any code or comments please share them. Thank you.
Avatar of AlexVirochovsky
AlexVirochovsky

homework?
We cannot provide answers to school assignments.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?
ASKER CERTIFIED SOLUTION
Avatar of sumant032199
sumant032199
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think this is OK, even for an assignment:

a stack is a good choice, model the problem by running through some scenarios on paper with a hypothetical stack.  You should see that the problem is actually very simple to solve once you start using the stack.

the worst part is probably reading and parseing the expression from the user, but thats not too bad.

You might want to consider looking at standard library data structures to allow you to implement this solution.
>> I think this is OK,
I agree.
Avatar of ozo
Not much to parse, you've got digits, and operators.  scanf could be adequate to handle it.
Now once you've got each token, you just have to perform the appropriate function with it.
I agree its not much to parse, just numbers and operators, but I reckon getting that code right will be harder than the real 'logic' which is very simple when you work out how to do it.
Avatar of CRAZY

ASKER

Heres what I have, it wont even compile, I  migh have LOTS of problems with this


#include<iostream.h>

char val;

void push(char val);
void pop();

main(){

char stack[20];
int top=0;

while(val != '0'){
cout<<"Enter a number to calculate Then the operator"<<endl;
cin>>val;
push(val);
if (val=='+')
 {val = stack[top-1] + stack[top-2]; pop();}
else if(val=='-')
 {val =isdigit( stack[top-2])  - isdigit( stack[top-1]); pop();}
else if(val=='*')
 {val = stack[top-2] * stack[top-1]; pop();}
else if(val=='/')
 {val = stack[top-2] / stack[top-1]; pop();}
  }//end while loop
}

void push(char val ){
stack[top]=val;
top++;
}

void pop(){

        return stack[top--];

     }
"10265843.cpp", line 20: error(1020): identifier "isdigit" is undefined
        {val =isdigit( stack[top-2])  - isdigit( stack[top-1]); pop();}
              ^

"10265843.cpp", line 29: error(1020): identifier "stack" is undefined
       stack[top]=val;
       ^

"10265843.cpp", line 29: error(1020): identifier "top" is undefined
       stack[top]=val;
             ^

"10265843.cpp", line 35: error(1117): a void function may not return a value
               return stack[top--];
                      ^
Avatar of CRAZY

ASKER

Heres what I have, it wont even compile, I  migh have LOTS of problems with this


#include<iostream.h>

char val;

void push(char val);
void pop();

main(){

char stack[20];
int top=0;

while(val != '0'){
cout<<"Enter a number to calculate Then the operator"<<endl;
cin>>val;
push(val);
if (val=='+')
 {val = stack[top-1] + stack[top-2]; pop();}
else if(val=='-')
 {val =isdigit( stack[top-2])  - isdigit( stack[top-1]); pop();}
else if(val=='*')
 {val = stack[top-2] * stack[top-1]; pop();}
else if(val=='/')
 {val = stack[top-2] / stack[top-1]; pop();}
  }//end while loop
}

void push(char val ){
stack[top]=val;
top++;
}

void pop(){

        return stack[top--];

     }
1. make pop return...something--a character I guess.  like

char pop()
{
    return stak[top--];
};

2.  Include the <ctype.h> include file that defines isdigit().

3. make it so push() and pop() can find the stack.  You either must make the stack array a global array (not local to main) or you must pass the array a parameter to the push and pop() procedures.
Avatar of CRAZY

ASKER

Ahh ctype is what I needed

I got furthur now my program looks like this


#include<stdio.h>
#include<iostream.h>
#include<ctype.h>

int stack[100];
int top = 0;

void isoperator(char);
void evaluate(int,int,int);


void push(int val){
stack[top++] = val;
} //dont need return 0 if we put void

pop(){

   return stack[--top];
     }




main(){

char t;
int val;

while((t=cin.get()) !='\n'){ //while1

val = 0;
if (isdigit(t)){                //if
    while(isdigit(t))         //while2
      {
      val = val * 10 +(t-48);
t=cin.get();
      }                       //while2
cout<<"Value is "<<val<<endl;

push(val);
       


        }                    //if
else if(isoperator(t)){     //if2
   
   int opt1 , opt2
   opt1 = pop();
   opt2 = pop();
val = evaluate(opt1 , opt2 , t);
push(val);

}                           //if2

}                            //while1                    

cout<<"Top is "<<top<<endl;
cout<<"Pop is "<<pop()<<endl;
return 0;
}


//I must define evaluate() and isoperator()

void isoperator(char t)
{

return 0;
}

void evaluate(int opt1, int opt2, char t)
{

return 0;
}




If it wasnt for ctype I wouldnt have been able to do it, thank you!
Your isoperator() and evaluate() fucntions both are declared as returning "void", but both try to return a value.  When something is declared to return "void" it means it does not return a value.  You need to fix this.
What is happening with this question?