Question: /* * File: rpncalc.c * --------------- * This program simulates an electronic calculator that uses * reverse Polish notation, in which the operators come after



/* * File: rpncalc.c * --------------- * This program simulates an electronic calculator that uses * reverse Polish notation, in which the operators come after * the operands to which they apply. */
#include
/* Private function prototypes */
static void ApplyOperator(char op, stackADT operandStack); static void HelpCommand(void); static void ClearStack(stackADT operandStack); static void DisplayStack(stackADT operandStack);
/* Main program */
main() { stackADT operandStack; string line; char ch;
printf("RPN Calculator Simulation (type H for help) "); operandStack = NewStack(); while (TRUE) { printf("> "); line = GetLine(); ch = toupper(line[0]); switch (ch) { case 'Q': exit(0); case 'H': HelpCommand(); break; case 'C': ClearStack(operandStack); break; case 'S': DisplayStack(operandStack); break; default: if (isdigit(ch)) { Push(operandStack, StringToReal(line)); } else { ApplyOperator(ch, operandStack); } break; } } }
/* Private functions */
/* * Function: ApplyOperator * Usage: ApplyOperator(op, operandStack); * --------------------------------------- * This function applies the operator to the top two elements on * the operand stack. Because the elements on the stack are * popped in reverse order, the right operand is popped before * the left operand. */
static void ApplyOperator(char op, stackADT operandStack) { double lhs, rhs, result;
rhs = Pop(operandStack); lhs = Pop(operandStack); switch (op) { case '+': result = lhs + rhs; break; case '-': result = lhs - rhs; break; case '*': result = lhs * rhs; break; case '/': result = lhs / rhs; break; default: Error("Illegal operator %c", op); } printf("%g ", result); Push(operandStack, result); }
/* * Function: HelpCommand * Usage: HelpCommand(); * --------------------- * This function generates a help message for the user. */
static void HelpCommand(void) { printf("Enter expressions in Reverse Polish Notation, "); printf("in which operators follow the operands to which "); printf("they apply. Each line consists of a number, an "); printf("operator, or one of the following commands: "); printf(" Q -- Quit the program "); printf(" H -- Display this help message "); printf(" C -- Clear the calculator stack "); printf(" S -- Display all values in the stack "); }
/* * Function: ClearStack * Usage: ClearStack(stack); * ------------------------- * This function clears the stack by popping elements until it is * empty. */
static void ClearStack(stackADT stack) { while (!StackIsEmpty(stack)) { (void) Pop(stack); } }
/* * Function: DisplayStack * Usage: DisplayStack(stack); * --------------------------- * This function displays the contents of a stack. */
static void DisplayStack(stackADT stack) { int i, depth;
printf("Stack: "); depth = StackDepth(stack); if (depth == 0) { printf("empty "); } else { for (i = depth - 1; i >= 0; i--) { if (i
-----------------------------------------------------------------------
/* * File: stack.h * ------------- * This interface defines an abstraction for stacks. In any * single application that uses this interface, the values in * the stack are constrained to a single type, although it * is easy to change that type by changing the definition of * stackElementT in this interface. */
#ifndef _stack_h #define _stack_h
#include "genlib.h"
/* * Type: stackElementT * ------------------- * The type stackElementT is used in this interface to indicate * the type of values that can be stored in the stack. Here the * stack is used to store values of type double, but that can * be changed by editing this definition line. */
typedef double stackElementT;
/* * Type: stackADT * -------------- * The type stackADT represents the abstract type used to store * the elements that have been pushed. Because stackADT is * defined only as a pointer to a concrete structure that is not * itself defined in the interface, clients have no access to * the underlying fields. */
typedef struct stackCDT *stackADT;
/* * Function: NewStack * Usage: stack = NewStack(); * -------------------------- * This function allocates and returns a new stack, which is * initially empty. */
stackADT NewStack(void);
/* * Function: FreeStack * Usage: FreeStack(stack); * ------------------------ * This function frees the storage associated with the stack. */
void FreeStack(stackADT stack);
/* * Function: Push * Usage: Push(stack, element); * ---------------------------- * This function pushes the specified element onto the stack. */
void Push(stackADT stack, stackElementT element);
/* * Function: Pop * Usage: element = Pop(stack); * ---------------------------- * This function pops the top element from the stack and returns * that value. The first value popped is always the last one * that was pushed. If the stack is empty when Pop is called, * the function calls Error with an appropriate message. */
stackElementT Pop(stackADT stack);
/* * Functions: StackIsEmpty, StackIsFull * Usage: if (StackIsEmpty(stack)) . . . * if (StackIsFull(stack)) . . . * ------------------------------------- * This functions test whether the stack is empty or full. */
bool StackIsEmpty(stackADT stack); bool StackIsFull(stackADT stack);
/* * Function: StackDepth * Usage: depth = StackDepth(stack); * --------------------------------- * This function returns the number of elements currently pushed * on the stack. */
int StackDepth(stackADT stack);
/* * Function: GetStackElement * Usage: element = GetStackElement(stack, index); * ----------------------------------------------- * This function returns the element at the specified index in * the stack, where the top of the stack is defined as index 0. * For example, calling GetStackElement(stack, 0) returns the top * element on the stack without removing it. If the caller tries * to select an out-of-range element, GetStackElement calls Error. * Note: This function is not a fundamental stack operation and * is instead provided principally to facilitate debugging. */
stackElementT GetStackElement(stackADT stack, int index);
#endif
(Library - Abstract Data Type) Exercise: Reimplementation of RPN calculator and Proper matching #ifndef-stack-h #define-stack-h - Suppose you cannot change #include-gerlib he typedefvoid stackElement stack.h and instead of char or double, it currently export typedef void *stackEleme Given this version of stack.h implement RPN calc and proper matching What will be main difference - Dynamically allocate memory for each value that you typedef struct stackCDTstadADT: stackADT NewStackivoid; void FreeStackistackADT stack; void PushistackADT stack stackElementT element) stackElementT PopistackADT stack); bool StacdsEmpty stackADT stack; bool StacksFull stackADT stack int StacDepth stacADT stack; stackElementT GetStackElemn stackADT stack int index); # endif 24 push, free when you pop and/or reuse... In this exercise, you are simply asked to re-implement RPN calculator by using stack.h that exports typedef void *stackElementT; (just doind rpn calc is enough to submit, you can do the proper matching on your own time) (Library - Abstract Data Type) Exercise: Reimplementation of RPN calculator and Proper matching #ifndef-stack-h #define-stack-h - Suppose you cannot change #include-gerlib he typedefvoid stackElement stack.h and instead of char or double, it currently export typedef void *stackEleme Given this version of stack.h implement RPN calc and proper matching What will be main difference - Dynamically allocate memory for each value that you typedef struct stackCDTstadADT: stackADT NewStackivoid; void FreeStackistackADT stack; void PushistackADT stack stackElementT element) stackElementT PopistackADT stack); bool StacdsEmpty stackADT stack; bool StacksFull stackADT stack int StacDepth stacADT stack; stackElementT GetStackElemn stackADT stack int index); # endif 24 push, free when you pop and/or reuse... In this exercise, you are simply asked to re-implement RPN calculator by using stack.h that exports typedef void *stackElementT; (just doind rpn calc is enough to submit, you can do the proper matching on your own time)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
