Question: You are required to write a program in the C Language that calculates and displays the result of arbitrary mathematical expressions entered by the user.

You are required to write a program in the C Language that calculates and displays the result of arbitrary mathematical expressions entered by the user.

Criteria: (must be met)

(1) The program shall display a simple prompt waiting for user input:

>>> 

(2) The following help message shall be displayed whenever the user types the letter 'h' at the input prompt:

Simple Calculator understands the following arithmetic operations: ^ exponentiation + addition - subtraction * multiplication / division 

(3) The program shall exit if the user types the letter 'q' at the input prompt. Before exiting it should print the following message:

Goodbye! 

(4) The program shall be able to perform calculations with the basic arithmetic operators (+, -, *, /), and exponentiation (^).

(5) The program shall accept decimals as well as integers.

(6) The program shall accept negative numbers.

(7) The program shall accept floating point numbers in scientific e notation - e.g. 1.23e4.

(8) The program shall apply correct order of operations to calculations.

(9) The program shall parse parentheses in the input string - ().

(10) Arbitrary whitespace is allowed (but not required) in user input.

(11) The program shall respond to all inputs in a case insensitive manner. i.e. any letters are valid in both upper and lower case.

(12) The program shall validate user input, and recover gracefully from illegal input. Since a user might enter any arbitrary string of characters, many different errors are possible, including:

i. Specifying two operators in a row (you must differentiate between the subtraction operator and the negative sign).

ii. Entering invalid letters.

iii. Entering a floating point number with more than one decimal point.

iv. Finishing the input with an operator rather than an operand.

(13) On detection of illegal input, the following error message shall be displayed:

Error: Illegal input!

(14) On detection of divide-by-zero, the following error message shall be displayed:

Error: Divide by zero!

(15) The program shall display the calculated result to 3 decimal places, with trailing zeros if required.

(16) After displaying the calculated result or an error message, the program shall return to the input prompt (see Item 1 above).

Example Output:

Simple Calculator >>> 1 + 2 * 3 + 4 11.000 >>> hello Error: Illegal input! >>> 2 / 0 Error: Divide by zero! >>> q 

Hints:

Traditional mathematical notation uses infix notation. This means that rules are required for precedence of operators as infix notation can be ambiguous. If you convert the input string to postfix notation there is no ambiguity and evaluating the expression becomes a series of stack operations.

There are functions in the standard libraries that can parse character arrays (i.e. strings) and convert them into floats.

Consider developing your solution in stages. For example, assume that input will always be single space delimited, with no negative numbers and each number consisting of a single digit. Once you can do that, add the additional requirements one by one.

Execution:

Your submission must include a Makefile. Your code will be compiled with the command make being run in the root directory of your submission.

The executable that is created must be called calc . It must be created in the root directory of your submission.

**The main focus of the question is being able to parse expressions.

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 Databases Questions!