Question: Done in Cygwin using g++ compiler This is a three part project: Part one - build a program that: Prompts the user for a string

Done in Cygwin using g++ compiler

This is a three part project:

  • Part one - build a program that:
    • Prompts the user for a string of user input .
    • Treat that string as a mathematical expression in an infix form.
    • Convert the string into a series of tokens where each token is an operator, or a value delimited by whitespace , operator, or
  • Part two -
    • Convert to postfix and print out the results. ( or print out an error if badly formed)
  • Part Three - further.
    • Do the math to evaluate the expression when all operands are values ( not variables)

For this project the operators you will support are:

Plus +

Minus -

Times *

Divide /

Modula %

When I test the program I would expect to see something like this: ( Bold is what I enter on the command line)

Input an expression: A+ B * 31

Tokens: A + B * 31

Postfix: A B + 31 *

Value: Cannot be calculated

Input and Expression: 3+4*12

Tokens: 3 + 4* 12

Postfix: 3 4 12* +

Value: 51

If the input expression is invalid, print out that fact. Use exceptions to catch any error conditions.

(Note - put a space between each Token for clarity)

How do you look at a string and know what the operands vs operators. IE if the input string is:

A+FRED+23*13-GG OR A + FRED +23* -GG

How do you identify that the tokens are A, FRED, 23, * etc. - and which tokens are operands vs operators?

The easiest way is with a stack.

Put the first character on the stack.

Keep putting characters on until you see an unpend char ( a space) OR an operator or a

Pop the chars off the first stack onto a second

Pop the chars off the second stack ( now they are in proper order)

Pass the token to your infx to postgix code.

If you have an operator.Send that

Repeat until no more characters.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!