Click here to Skip to main content
15,887,350 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Calculator Using Stacks Pin
civicnar1-Nov-04 7:57
civicnar1-Nov-04 7:57 
GeneralRe: Calculator Using Stacks Pin
David Crow1-Nov-04 8:31
David Crow1-Nov-04 8:31 
GeneralRe: Calculator Using Stacks Pin
Joaquín M López Muñoz1-Nov-04 8:38
Joaquín M López Muñoz1-Nov-04 8:38 
GeneralRe: Calculator Using Stacks Pin
civicnar1-Nov-04 8:49
civicnar1-Nov-04 8:49 
GeneralRe: Calculator Using Stacks Pin
civicnar1-Nov-04 8:52
civicnar1-Nov-04 8:52 
GeneralRe: Calculator Using Stacks Pin
Joaquín M López Muñoz1-Nov-04 8:57
Joaquín M López Muñoz1-Nov-04 8:57 
GeneralRe: Calculator Using Stacks Pin
civicnar1-Nov-04 9:03
civicnar1-Nov-04 9:03 
GeneralRe: Calculator Using Stacks Pin
Joaquín M López Muñoz1-Nov-04 9:26
Joaquín M López Muñoz1-Nov-04 9:26 
Well, I cannot solve your homework, so I hope some advice can put you on the right track.

First of all, this kind of problem is usually solved with one stack, not two. By inserting the numbers into one stack and the operands into another, you're losing crucial information about the orifinal postfix expression. For instance, consider the two expressions:
1 2 + 3 * [3 * (1 + 2) = 9]
1 2 3 + * [(3 + 2) * 1 = 5]
According to your schema, these two expressions would be stored just the same:
number stack: 3 2 1
operation stack: * +
so you're clearly doing it the wrong way. Ok so far? What you have to do is store everything into one single stack. As the stack can have to types of elements (numbers and operations), create a <ode>struct capable of storing both things, like for instance:
struct stack_element
{
  bool is_operation;
  // the rest you can figure it out: some set of fields
  // or another will be used depending on is_operation
}
Now you have your stack with the given postfix expression conveniently stored in reverse order. You have to devise an algorithm to evaluate the contents of the stack. You can rely on the following two reduction policies:
  • If the top element of the stack is a number, this is what the stack evaluates to.
  • If the top element is an operation then:
    1. Extract it from the stack.
    2. Recursively evaluate the first operand, yielding a number and leaving the expression for the second operand in the stack.
    3. Evaluate the second operand the same way.
    4. You have your operation and your two operands, calculate this and you're done.
I hope this is of some help. If you make some progress in your program and get stucked again, repost here trying to be specific about the particular problem you've run into.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
GeneralRe: Calculator Using Stacks Pin
civicnar1-Nov-04 9:32
civicnar1-Nov-04 9:32 
QuestionHow to Start MFC App with arguments Pin
nonothing1-Nov-04 5:38
nonothing1-Nov-04 5:38 
AnswerRe: How to Start MFC App with arguments Pin
GKarRacer1-Nov-04 5:58
GKarRacer1-Nov-04 5:58 
GeneralRe: How to Start MFC App with arguments Pin
David Crow1-Nov-04 6:41
David Crow1-Nov-04 6:41 
AnswerRe: How to Start MFC App with arguments Pin
Michael Dunn1-Nov-04 8:08
sitebuilderMichael Dunn1-Nov-04 8:08 
QuestionHow to hook system API under win9x? Pin
jedyking1-Nov-04 5:32
jedyking1-Nov-04 5:32 
GeneralThe Focus of a modeless dialog2 Pin
si_691-Nov-04 5:26
si_691-Nov-04 5:26 
GeneralRe: The Focus of a modeless dialog2 Pin
Jim Crafton1-Nov-04 6:15
Jim Crafton1-Nov-04 6:15 
GeneralRe: The Focus of a modeless dialog2 Pin
valikac1-Nov-04 7:37
valikac1-Nov-04 7:37 
GeneralThe Focus of a modeless dialog Pin
si_691-Nov-04 5:25
si_691-Nov-04 5:25 
GeneralEdit control beeps on Esc or Tab keys Pin
Jim Crafton1-Nov-04 5:25
Jim Crafton1-Nov-04 5:25 
GeneralRe: Edit control beeps on Esc or Tab keys Pin
peterchen1-Nov-04 7:24
peterchen1-Nov-04 7:24 
GeneralOne more on print orientation and paper size specifications Pin
Member 12423731-Nov-04 4:06
Member 12423731-Nov-04 4:06 
GeneralRe: One more on print orientation and paper size specifications Pin
Phil J Pearson1-Nov-04 6:10
Phil J Pearson1-Nov-04 6:10 
GeneralRe: One more on print orientation and paper size specifications Pin
Member 12423731-Nov-04 10:36
Member 12423731-Nov-04 10:36 
GeneralRe: One more on print orientation and paper size specifications Pin
Phil J Pearson2-Nov-04 1:25
Phil J Pearson2-Nov-04 1:25 
GeneralRe: One more on print orientation and paper size specifications Pin
Qadddd2-Nov-04 7:45
Qadddd2-Nov-04 7:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.