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

Turn this C++ code into project form with a specification file, a implementation

ID: 3872853 • Letter: T

Question

Turn this C++ code into project form with a specification file, a implementation file, and with a driver program instead of being inline in one program.

#include <iostream>
#include <stack>
using namespace std;

int postFixExprEval(string postfixExpr)

{

int i;

/* Creating stack to hold operands in the expression */

stack<int> exprEvalStack;

/* Looping over the string to evaluate the expression */

for (i = 0; i < postfixExpr.size(); ++i)

{

/* reading each character of string */

char c = postfixExpr.at(i);

/* skipping white space */

if (c == ' ')

{

continue;

}

/* Checking the character using switch statement */

switch(c)

{

case '+':

{

/* Performing addition */

/* Checking the stack size */

if (exprEvalStack.size() >= 2)

{

int val1;

int val2;

/* Getting top element of the stack */

val1 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Getting top element of the stack */

val2 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Adding two popped elements and pushing the result into stack */

exprEvalStack.push(val1 + val2);

}

}

break;

case '-':

{

if (exprEvalStack.size() >= 2)

{

int val1;

int val2;

/* Getting top element of the stack */

val1 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Getting top element of the stack */

val2 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Substracting two popped elements and pushing the result into stack */

exprEvalStack.push(val2 - val1);

}

}

break;

case '*':

{

if (exprEvalStack.size() >= 2)

{

int val1;

int val2;

/* Getting top element of the stack */

val1 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Getting top element of the stack */

val2 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Multiplying two popped elements and pushing the result into stack */

exprEvalStack.push(val1 * val2);

}

}

break;

case '/':

{

if (exprEvalStack.size() >= 2)

{

int val1;

int val2;

/* Getting top element of the stack */

val1 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Getting top element of the stack */

val2 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Dividing two popped elements and pushing the result into stack */

exprEvalStack.push(val2 / val1);

}

}

break;

case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':

{

/* Pushing number into stack */

exprEvalStack.push(c - 48);

}

break;

default:

{

/* If invalid symbol found in the expression, It prints the error message and returns 0. */

cout << "Invalid symbol found in the expression. Try again!" << endl;

return 0;

}

}

}

/* Checking the stack size and returning the evaluation result */

if (exprEvalStack.size())

return exprEvalStack.top();

/* If above statement is not executed, then expression in invalid */

cout << "Invalid expression. Try again!" << endl;

return 0;

}

int main()

{

string expr;

/* Taking multiple expressions from the user */

while (1)

{

cout << "Enter Postfix Expression: (For example 2 7 + 3 6 - *) Enter Ctrl-C to exit! ";

getline(cin, expr);

/* Calling postFixExprEval() function to evaluate the postfix expression */

cout << "Result: " << postFixExprEval(expr) << endl;

}

return 0;

}

Explanation / Answer

specification file

postfix.h


#include <iostream>
#include <stack>
using namespace std;
int postFixExprEval(string postfixExpr);
===============================================================
Implementation file

#include "postfix.h"

int postFixExprEval(string postfixExpr)

{

int i;

/* Creating stack to hold operands in the expression */

stack<int> exprEvalStack;

/* Looping over the string to evaluate the expression */

for (i = 0; i < postfixExpr.size(); ++i)

{

/* reading each character of string */

char c = postfixExpr.at(i);

/* skipping white space */

if (c == ' ')

{

continue;

}

/* Checking the character using switch statement */

switch(c)

{

case '+':

{

/* Performing addition */

/* Checking the stack size */

if (exprEvalStack.size() >= 2)

{

int val1;

int val2;

/* Getting top element of the stack */

val1 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Getting top element of the stack */

val2 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Adding two popped elements and pushing the result into stack */

exprEvalStack.push(val1 + val2);

}

}

break;

case '-':

{

if (exprEvalStack.size() >= 2)

{

int val1;

int val2;

/* Getting top element of the stack */

val1 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Getting top element of the stack */

val2 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Substracting two popped elements and pushing the result into stack */

exprEvalStack.push(val2 - val1);

}

}

break;

case '*':

{

if (exprEvalStack.size() >= 2)

{

int val1;

int val2;

/* Getting top element of the stack */

val1 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Getting top element of the stack */

val2 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Multiplying two popped elements and pushing the result into stack */

exprEvalStack.push(val1 * val2);

}

}

break;

case '/':

{

if (exprEvalStack.size() >= 2)

{

int val1;

int val2;

/* Getting top element of the stack */

val1 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Getting top element of the stack */

val2 = exprEvalStack.top();

/* Pop up the top element on the stack */

exprEvalStack.pop();

/* Dividing two popped elements and pushing the result into stack */

exprEvalStack.push(val2 / val1);

}

}

break;

case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':

{

/* Pushing number into stack */

exprEvalStack.push(c - 48);

}

break;

default:

{

/* If invalid symbol found in the expression, It prints the error message and returns 0. */

cout << "Invalid symbol found in the expression. Try again!" << endl;

return 0;

}

}

}

/* Checking the stack size and returning the evaluation result */

if (exprEvalStack.size())

return exprEvalStack.top();

/* If above statement is not executed, then expression in invalid */

cout << "Invalid expression. Try again!" << endl;

return 0;

}

===============================================================
Driver Code

main.cpp

#include <iostream>
#include <stack>
using namespace std;

int main()

{

string expr;

/* Taking multiple expressions from the user */

while (1)

{

cout << "Enter Postfix Expression: (For example 2 7 + 3 6 - *) Enter Ctrl-C to exit! ";

getline(cin, expr);

/* Calling postFixExprEval() function to evaluate the postfix expression */

cout << "Result: " << postFixExprEval(expr) << endl;

}

return 0;

}

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