Write a program which does the following: Prompts the user to enter a boolean fo
ID: 3682911 • Letter: W
Question
Write a program which does the following:
Prompts the user to enter a boolean formula in prefix form (we’ll tweak the terminal symbols below to make the language ASCII friendly).
Parses the input string and does the following:
If the string is not in the language (syntax error), an error message is printed and the program terminates. (Your error message does not need to give any fancy diagnostics -- just something like “bad input - goodbye!” is sufficient.
Otherwise (the input is well-formed), the program:
Displays the formula in parenthesized infix form.
Reports the value of the entire formula (TRUE or FALSE).
ASCII-friendly grammar: Below is the same grammar as used in Part II, except the operator symbols have been replaced with ASCII-friendly and C-like equivalents.
They remain single characters as far as a program is concerned.
E -> f
E -> t
E -> |EE
E -> &EE
E -> !E
Practice: What infix boolean formula does this correspond to?
&|f~&tf~t
For simplicity, the user input must exactly match strings in the language -- for example, embedded whitespace is not allowed (specifically will result in a syntax error message).
Further, note that every meaningful component of a string is a single character (e.g., we use ‘&’ instead of ‘&&’). This should simplify processing -- take advantage of it!
Usage of your program is trivial: it takes no command line arguments; when run it prompts the user for a formula entered on a single line; it then processes the input according to the specs above and terminates.
E -> f
E -> t
E -> |EE
E -> &EE
E -> !E
Explanation / Answer
$ a.out
Expression Tree Test
Enter equation in Prefix form: +-+7*/935/82*/625
Prefix : +-+7*/935/82*/625
Infix : 7+9/3*5-8/2+6/2*5
Postfix : 793/5*+82/-62/5*+
$ a.out
Expression Tree Test
Enter equation in Prefix form: +-+7*/935/82*/625
Prefix : +-+7*/935/82*/625
Infix : 7+9/3*5-8/2+6/2*5
Postfix : 793/5*+82/-62/5*+
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.