Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write, run and verify a C program that implements a Reverse Polish Notation calc

ID: 3544777 • Letter: W

Question

Write, run and verify a C program that implements a Reverse Polish Notation calculator using a finite state automaton similar to the automata designed in class for inputting numbers. The features of the program include


1. A main function to print a welcome to the program and then to call the processEquation function.


2. The processEquation function, void processEquation(void), which implements the finite state machine and allows the user to input as many equations in Reverse Polish Notation format as desired.


3.The stack for storing the operands in the equation with header file, stack.h, and the stack functions, stack.cpp. Add one more function, int size (void) to stack.cpp and stack.h to return the number of elements in the stack.

a.The stack uses a global variable stack which is allowed for this program.


stack.h is included below

-----------------------------------


#ifndef STACK_H
#define STACK_H
#include "stdbool.h"

//prototypes for the stack functions
void make_empty (void);            //sets the stack top to 0
bool is_empty (void);            //determines if the stack is empty
bool is_full(void);                //determines if the stack is full
bool push (int);                //pushes an integer onto the stack
int pop (void);                    //pops an integer off of the stack
void stack_overflow (void);        //prints an error message that no further memory is available
void stack_underflow (void);    //prints an error message that the stack is empty
void print_stack (void);        //prints the stack out
#endif


stack.cpp is included below

--------------------------------------


#include <stdio.h>    
#include "stack.h"

#define STACK_SIZE 10

/* external variables */
int contents[STACK_SIZE];
int top = 0;

//set top to 0 to make the stack empty
void make_empty(void)
{
    top = 0;
}

//see if the stack is empty (top == 0)
bool is_empty(void)
{
    return top == 0;
}

//see if the stack is full (top == STACK_SIZE)
bool is_full(void)
{
    return top == STACK_SIZE;
}

//push i onto the stack
bool push(int i)
{
    bool pushed = false;
    if (is_full())
        stack_overflow();
    else {
        contents[top++] = i;
        pushed = true;
    }
    return pushed;
}

//pop the top of the stack
int pop(void)
{
    int data = 99999999;
    if (is_empty())
        stack_underflow();
    else {
        data = contents[--top];
    }
    return data;
}

//prints an error message in the event of no further array space is available
void stack_overflow (void) {
    printf("Stack Overflow:  no more stack space is available ");
}

//prints an error message that the stack is empty
void stack_underflow (void) {
    printf("Stack Underflow:  the stack is empty ");
}

//prints the stack out
void print_stack (void) {
    int i;
    printf("Stack:");
    for (i = top-1; i >= 0; i--) {
        printf (" %d",contents[i]);
    }
    //check for an empty stack
    if (is_empty()) {
        printf(" empty");
    }
    putchar(' ');
}

Explanation / Answer

#include <stdio.h>

#include "stack.h"

#include <ctype.h>

#define STACK_SIZE 10

/* external variables */

int contents[STACK_SIZE];

int top = 0;

//set top to 0 to make the stack empty

void make_empty(void)

{

top = 0;

}

//see if the stack is empty (top == 0)

bool is_empty(void)

{

return top == 0;

}

//see if the stack is full (top == STACK_SIZE)

bool is_full(void)

{

return top == STACK_SIZE;

}

//push i onto the stack

bool push(int i)

{

bool pushed = false;

if (is_full())

stack_overflow();

else {

contents[top] = i;top++;

pushed = true;

}

return pushed;

}

//pop the top of the stack

int pop(void)

{

int data = 99999999;

if (is_empty())

stack_underflow();

else {top--;

data = contents[top];

}

return data;

}

//prints an error message in the event of no further array space is available

void stack_overflow (void) {

printf("Stack Overflow: no more stack space is available ");

}

//prints an error message that the stack is empty

void stack_underflow (void) {

printf("Stack Underflow: the stack is empty ");

}

//prints the stack out

void print_stack (void) {

int i;

printf("Stack:");

for (i = top-1; i >= 0; i--) {

printf (" %d",contents[i]);

}

//check for an empty stack

if (is_empty()) {

printf(" empty");

}

putchar(' ');

}

int size()

{

return top;

}


void processEquation()

{

char revers_pol[50],ch,ch1;

int i=0,op1,op2;

while(1)

{

printf(" Enter the Postfix Expression : ");

scanf("%s",revers_pol);

while( (ch=revers_pol[i++]) != '')

{

if(isdigit(ch))

push(ch-'0'); /* Push the operand */

else

{ /* Operator,pop two operands */

op2=pop();

op1=pop();

switch(ch)

{

case '+':push(op1+op2);break;

case '-':push(op1-op2);break;

case '*':push(op1*op2);break;

case '/':push(op1/op2);break;

}

}

}

printf(" Result after Evaluation: %d ",contents[top-1]);

printf("Do you want another expression to evaluated(y/n):");

scanf("%c",&ch1);

if(ch1=='n'||ch1=='N')

break;

}

printf(" Good Bye");

}

int main()

{ printf("Welcome ");

processEquation();

return 0;

}