Question: Create C++ program. Background This assignment is simply to evaluate a well-formed arithmetic expression with arbitrary number of unsigned operands and operators +, -, *,

Create C++ program. Background This assignment is simply to evaluate a well-formed arithmetic expression with arbitrary number of unsigned operands and operators +, -, *, /. An arithmetic expression is described as well formed if there is one pair of parentheses per operation. For example, ((8+((7*4)*(4+6)))*10) and (8+((7*4)*(4+6))) are well formed arithmetic expressions, but (8+((7*4)*(4+6)))*10 is not. In addition, your program should report the condition of dividing by ZERO and terminate the program gracefully such as de-allocating any storage spaces previously dynamically allocated by your program.

Description of an algorithm Your algorithm should read in a sequence of characters from one line of standard input and print out a message echoing the arithmetic expression is equal to an evaluated result. Your algorithm may use constants, counters, flags, sentinels, and temporary variables (for storing characters of the input arithmetic expression), etc, but it may not store the arithmetic expression in any structure other than the stack.

Specification of an abstract data type : stack A stack is a linear data structure in which elements are ordered according to when(Last In First Out) they are put on the stack. Elements on the stack are specified by user and we will use user data type, node, for this assignment. However, you should keep in mind that the elements shall be easily modified or re-defined in the future assignments.

struct std_element {

char operator;

unsigned operand; };

struct node {

std_element el;

node *next; };

struct stack { node *top; };

stack *create_stack();

results: the linear data structure, stack, is initialized (or created)

void push(stack *,std_element); requires: create_stack() is already executed results: std_element is the most recently arrived element of the stack

std_element pop(stack *); requires: create_stack() is already executed and top != NULL

results: the most recent element pushed onto the stack is retrieved and it is no longer on the stack void terminate_stack(stack *); requires: create_stack() is already executed results: terminate the stack by resetting the top

Implementation In the implementation, you can assume that the input text consists only unsigned integer and operators + - * /. The input text will be terminated by [enter]. In this assignment, you are no longer allowed to use a single file to contain all your source code. Please separate your stack implementation into stack.h and stack.cpp, then use the header file in your main() function to access the stack related functions. pseudo-code of the general algorithm which you may need to modify to suit the program requirements such as dividing by ZERO: variable ch : char while not end_of_line do begin read(ch) if ch is a digit then read in the whole number and push onto the stack else if ch is an operator (+ , - , * , /) push it onto the stack else if ch is a ) // assumed the expression is well-formed pop the 1st number from the stack pop the operator pop the 2nd number from the stack apply operation to both numbers and push result onto the stack end pop the result from the stack and display it

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!