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

Program : Please make a project file that includes .h file, .cpp file and the ma

ID: 3873324 • Letter: P

Question

Program : Please make a project file that includes .h file, .cpp file and the main file for the program in C++. Task: Evaluate a postfix expression using a stack. See Problem #12, pg. 387, in the Nyhoff textbook. Input: The user will provide a postfix expression consisting of single-digit whole numbers and single-char operators (+, -, *, and /). The input should be accepted as a string. Processing: Use a linked list stack implementation for the program. Only numbers will be stored in the stack. When a digit is read from the input string, it will be pushed onto the stack. When an operator is read from the string, two numbers will be popped from the stack and the operator will be used to evaluate them; then the result will be pushed back onto the stack. Assume that only valid postfix expressions will be entered, so extensive data validation is not needed. Calculations will be done on the numbers as integers. Permit the user to enter multiple expressions for evaluation. Output: As specified by Problem #12, pg. 387 in the Nyhoff text Book. Sample expressions: infix: postfix: result: (2 + 7 ) * (3 - 6) 2 7 + 3 6 - * -27 3 - 4 - 1 + (5 / 2) 3 4 - 1 - 5 2 / + 0

Explanation / Answer

main.cpp

#include<cstdlib>

#include<iostream>

#include"eval.h"

int main()

{

string postfix;

cout<<" Enter postfix string : ";

cin>>postfix;

int x = eval(postfix);

cout<<" Result : "<<x<<endl<<endl;

return 0;

}

eval.h

#ifndef EVAL_H

#define EVAL_H

#include<cstdlib>

#include<iostream>

using namespace std;

struct node

{

int data;

struct node *next;

};

node *makenode(int);

void push(node **, int);

int pop(node **);

int eval(string);

int operate(node **, char);

#endif

eval.cpp

#ifndef _EVAL_

#define _EVAL_

#include<cstdlib>

#include<iostream>

#include"eval.h"

using namespace std;

int operate(node **top, char c)

{

int x = 0, b = pop(top), a = pop(top);

switch(c)

{

case '+': x = a + b;

break;

case '-': x = a - b;

break;

case '/': x = a / b;

break;

case '*': x = a * b;

break;

default: cerr<<" Invalid operator ";

exit(-3);

}

return x;

}

int eval(string str)

{

node *top = NULL;

int x;

for(int i = 0; i < str.length(); i++)

{

if(str[i] == '+' || str[i] == '-' || str[i] == '/' || str[i] == '*')

{

x = operate(&top, str[i]);

push(&top, x);

}

else

{

push(&top, str[i] - '0');

}

}

return pop(&top);

}

int pop(node **top)

{

if(*top == NULL)

{

cerr<<" Stack Underflow ";

exit(-2);

}

node *p = *top;

*top = (*top)->next;

int x = p->data;

free(p), p = NULL;

return x;

}

void push(node **top, int data)

{

if(*top == NULL)

{

*top = makenode(data);

return;

}

node *p = makenode(data);

p->next = *top;

*top = p;

return;

}

node *makenode(int data)

{

node *t = NULL;

t = (node *)malloc(sizeof(node));

if(t == NULL)

{

cerr<<" Dynamic memory allocation failed ";

exit(-1);

}

t->data = data, t->next = NULL;

return t;

}

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote