Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Help Please review the code below and help fix the errors. Must be able to handle input expressions that contain spaces and valid multi-digit

C++ Help

Please review the code below and help fix the errors.

Must be able to handle input expressions that contain spaces and valid multi-digit integers. So, something like this (( 15 + 2 * 3 - 4 ) / 17 + ( 22 / 11 * 3)) * 101 must work properly. Solution should display the postfix expression resulting from the translation from infix before evaluating the expression to produce the numerical result. Program must repeatedly accept input, translate, and evaluate, until the input contains a single period. (i.e., just a '.')

*****************

StackInterface.h

*****************

// Define the headers

#ifndef _STACK_INTERFACE

#define _STACK_INTERFACE

template

// Class Interface Definition

class StackInterface

{

// Public Scope Functions

public :

/** Sees whether this stack is empty.

@return True if the stack is empty, or false if not. **/

virtual bool isEmpty() const = 0;

/** Adds a new entry to the top of this stack.

@post if the operation was succesful, newEntry is at the top of the stack.

@param newEtry The object to be added as a new entry.

@return True if the addition is successful or false if not. **/

virtual bool push(const ItemType& newEntry) = 0;

/** Removes the top of this stack.

@post If the operation was successful, the top of the stack has been removed.

@return True if the removal is successful or false if not. **/

virtual bool pop() = 0;

/** Returns the top of this stack

@pre The stack is not empty.

@post the top of the stack has been returned, and the stack is unchanged.

@return the top of the stack. **/

virtual ItemType peek() const = 0;

}; // end StackInterface

#endif

*****************

InfixCalc.cpp

*****************

// Include the header files

#include #include #include #include

#include "StackInterface.h"

using namespace std;

// Declare a template to manage different data types

template

// Class OurStack definition

class OurStack

{

// Private scope stack variable to store the items

private:

// Declare the stack with generic data type

stack Ourstack;

// Public scope items of the class

public:

/* Function to check the empty condition of the stack */

bool OurStack::isEmpty()

{

// Return the status of the test

return Ourstack.empty();

}

// Function to push "newEntry" to the stack

bool OurStack::push( const ItemType& newEntry )

{

// Push the "newEntry" to the stack

Ourstack.push(newEntry);

// Return true by the function

return true;

// Push failed, return false

else return false;

}

// Function to pop the items from the stack

bool OurStack:: pop()

{

// If the stack is not empty

if(!Ourstack.empty())

{

// Pop the top from the stack

Ourstack.pop();

// Return the status of the operation

return true;

}

// Stack is empty, cant pop, return false

else return false;

}

// Function to return the top of the stack

ItemType OurStack::peek()

{

// Return the top entry

return Ourstack.top();

}

}; // End class definition

// Class InfixCalc definition

class InfixCalc

{

// Private scope stack variable to store the items

private:

/* Declare the stack with integer type to store the operands of the expression */

OurStack opStack;

/* Declare the stack to hold the operands and intermediate values */

OurStack valStack;

/* String type variable to store the Infix expression */

string Infix;

// Variable to store the result

fload result;

// Public scope items of the class

public:

// Method to set the postfix expression

void setInfixExpr(string inp)

{

// Set the expression

Infix = inp;

} // End the method definition

/* Method to evaluate the infix expression by using the algorith, given in the problem */

void InfixEval()

{

// The for loop to process the string

for(int itr = 0; itr < Infix.length(); itr++)

{

/* The character variable to store the infix characters */

char cha = Infix[itr];

/* Initialize a temporary integer variable */

int temp = 0;

/* The switch statement to handle the different characters of the infix expression */

switch(cha)

{

/* If the character is a single digit operand */

case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':

/* Convert the character into integer type operand */

temp = (int)(cha)-48;

/* Push the operand into the stack */

valStack.push(temp);

// Break the case statement

break;

/* If the character is an open parenthesis */

case '(':

/* Push the parenthesis into the stack */

opStack.push(cha);

// Break the case statement

break;

// If the character is an operator

case '*':

case '/':

case '+':

case '-':

// If the opStack is empty

if(opStack.isEmpty())

{

/* Push the operator into the operator stack */

opStack.push(cha);

}

/* If the precedence of operator at the top of the stack is less than current operator */

else if(GetPrecedence(cha)>GetPrecedence(opStack.peek()))

{

/* Push the operator into the operator stack */

opStack.push(cha);

}

/* If the stack top operator has higher prededence */

else

{

/* The while loop to pop the higher prededence operator from the operator stack */

while (!opStack.isEmpty() &&GetPrecedence(cha) <= GetPrecedence(opStack.peek()))

{

/* Execute the function Execute() */

Execute();

/* Push the operator to the operator stack */

opStack.push(cha);

}

}

// Break the case statement

break;

/* If the character is an closing parenthesis */

case ')':

/* While the stack top is not an open parenthesis */

while(opStack.Peek() !='(')

{

/* Execute the function Execute() */

Execute();

}

/* Pop the open parenthesis from the stack */

opStack.pop();

// Break the case statement

break;

} // End the switch

} // End the for loop

// Pop while the stack becomes empty

while (!opStack.isEmpty())

// Execute the Execute method

Execute();

// Set the evaluation result

result = valStack.peek();

} // End the method

// Method execute to perform the operations

void Execute()

{

// Retrieve the second operand

int operand2 = valStack.peek();

// Pop the stack

valStack.pop();

// Retrieve the first operand

int operand1 = valStack.peek();

// Pop the stack

valStack.pop();

// Retrieve the operator

char op = opStack.peek();

// Pop the stack

opStack.pop();

// Initialize the temporary result variavle

int tempresult = 0;

// Switch to deal with different operations

switch(op)

{

// If the operation is addition

case '+':

// Perforl the operation

tempresult = operand1 + operand2;

// Break the case

break;

// If the operation is subtraction

case '-':

// Perform the operation

tempresult = operand1 - operand2;

// Break the case

break;

// If the operation is multiplication

case '*':

// Perform the operation

tempresult = operand1 * operand2;

// Break the case

break;

// If the operation is division

case '/':

// Perform the operation

tempresult = operand1 / operand2;

// Break the case

break;

}

/* Push the result into the operand stack */

valStack.push(tempresult);

} // End the function Execute

/* Function to return the precedence of an operator */

int GetPrecedence(char in_opr)

{

/* Initialize the integer variable to set the precedence */

int prece = -1;

// Switch to handle different operators

switch(in_opr)

{

// In the case of '('

case '(':

// Set precedence as 0

prece = 0;

// Break the case

break;

// In the case of - and +

case '+':

case '-':

// Set precedence as 1

prece = 1;

// Break the case

break;

// In the case of * and /

case '*':

case '/':

// Set precedence as 2

prece = 2;

// Break the case

break;

}

// Return the obtained precedence value

return prece;

} // End the function GetPrecedence

// Function to display the result

void showresult()

{

// Display the result

cout << "The result of evaluation of" <

}

}; // End of class definition

// Main method to test the Infix calculator class

int main()

{

// Declare the test strings

string test1;

// While loop to repeat the calculator

while(test1!="0")

{

// Display the prompt

cout << "Enter the valid Infix expression or 0 to exit: ";

// Read the infix expression

cin >> test1;

// Create an object of the InfixCalc class

InfixCalc Ifobj;

// Call the function to set the expression

Ifobj.setInfixExpr(test1);

/* Invoke the method to evaluate the infix expression */

Ifobj.InfixEval();

// Call the function to display the result

Ifobj.showresult();

} // End the while loop

// To pause the console window

system("pause");

// Terminate the main function

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions