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.
CRAZYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexVirochovskyCommented:
homework?
0
nietodCommented:
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?
0
sumant032199Commented:
You can use gets() to get the string including numbers and postfix. Then you can use atoi(), atol(), sscanf() family of functions which convert string into integer, float, double as you wish.
After extracting number from the string
remove it from the string and determine the postfix used. The functions are defined in stdlib.h file.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

jasonclarkeCommented:
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.
0
nietodCommented:
>> I think this is OK,
I agree.
0
ozoCommented:
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.
0
jasonclarkeCommented:
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.
0
CRAZYAuthor Commented:
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--];

     }
0
ozoCommented:
"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--];
                      ^
0
CRAZYAuthor Commented:
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--];

     }
0
nietodCommented:
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.
0
CRAZYAuthor Commented:
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!
0
nietodCommented:
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.
0
nietodCommented:
What is happening with this question?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.