Question: Write a C program that takes as input a fully parenthesized, arithmetic expression of binary operators + , , * , / , and converts

Write a C program that takes as input a fully parenthesized, arithmetic expression
of binary operators +,,*,/, and converts the expression into a binary expression
tree. Your program should take input from the command line. The entire
expression should be in a character string without any space in it.
An input string only includes floating numbers in the format of Y.YY, that is, one
digit to the left of the decimal point and two digits to the right of the decimal
point, and variables of the form of x1, x2, x3,....
Your program shall allow for the leaves in the expression tree not only to store
floating values but also to store variables of the form x1, x2, x3,..., For example,
expression ((( x1+5.12)(x27.68)/ x3) will be converted into a binary expression
tree like:
/
|
||
* x3
|
||
+-
||
||||
x15.12 x27.68
Your program should then show a menu with the following options:
1. Preorder
2. Inorder
3. Postorder
4. Calculate
5. Exit
Description:
(a) If an option of Preorder, Inorder, or Postorder is selected, your
program should print the expression by the corresponding traversal
order (Note: no parentheses for preorder and postorder traversal but
fully parenthesized for inorder traversal).
(b) Arithmetic calculation is invoked by option Calculate. Your program
should be able to detect errors like divide-by-zero before the
calculation.
This option supports both expressions with and without
variables. The program first checks for any divide-by-zero errors
before calculation.
If the expression contains variables, the program prompts the
user to input values for each variable in floating-point format
(Y.YY). Once all variable values are entered, they are
substituted into the expression. If a variable for any reason is not
defined or found, the program may use 0 as the default value for
the variable. Appropriate messages should be displayed to the
users to inform this.
The program then evaluates the expression and provides the
answer in 2 floating-point format (Y.YY)
If theres an error in the calculation such dividing by 0 or
unknown operator, the program shall display appropriate
messages and the calculate function can return and output 0.
(c) Option Exit terminates your program.
Note: No more than 10 variables will be passed in a given expression

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!