Hello guys,
If anyone can help me to solve this program i will be so thankfull.
Write a program called read_and_evaluate_expression that reads in a evaluates a postfix expression (defined below) and writes out its value.
There is one stack for holding operands, call numbers, which is initially empty.
• Read in the next digit or operator.
• Whenever you read a digit, push it onto the numbers stack.
• When you read in an operator - let's call it op - since it is a binary operator, it requires two arguments:
o remove the first two number from numbers; call the first one removed R and the second one L.
o evaluate L op R and push the result onto numbers.
• Ignore any blank space.
Note the numbers is from 0 to 9 and separated by one space
• Example: input line is 2 3 4 * + 5 -
• read '2', push it onto numbers.
• read following space.
• read '3', push it onto numbers.
• read following space.
• read '4', push it onto numbers.
• read following space.
• read '*'. Pop numbers: R=4. Pop numbers: L=3. L op R = 3 * 4 evaluates to 12. Push 12 onto numbers.
• read following space.
• read '+'. Pop numbers: R=12. Pop numbers: L=2. L op R = 2 + 12 evaluates to 14. Push 14 onto numbers.
• read following space.
• read '5'. Push it onto numbers.
• read following space.
• read '-'. Pop numbers: R=5. Pop numbers: L=14. L op R = 14 - 5 evaluates to 9. Push 9 onto numbers.
• When you reach end of the string . Pop numbers to get final answer 9 and write it out.
Configuration: Windows XP Internet Explorer 6.0
Here is a website providing nice tutorials for C++ hopes it will help....
|