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

Do I need to overload these these arithmatic operators to get this to work? I am

ID: 3724393 • Letter: D

Question

Do I need to overload these these arithmatic operators to get this to work? I am getting a very strange result such as: "." or "m" or a check mark. Assume operand1 and operand2 have already been input as "9" and "3". What I want is when an arthimatic operator occurs for the program to take the two previous inputs (then deletes them and the operator from the adt), calculates their result with that operator and pushes the new value onto the adt. code is below

____________________________________________________________________________________

template
class StackLinked : public Stack {

//skipped irrelevant public functions

private:

class StackNode {

public:

StackNode(const DataType& nodeData, StackNode* nextPtr)

{

dataItem = nodeData;

next = nextPtr;

}

DataType dataItem;

StackNode* next;

};

StackNode* top;

};

______________________________________________________________________________________

template

void StackLinked::push(const DataType& newDataItem) throw (logic_error)

{

DataType tempVal;

if (newDataItem == '+' || newDataItem == '-' || newDataItem == '*' || newDataItem == '/') {

DataType operand1 = top->dataItem;

pop();

DataType operand2 = top->dataItem;

pop();

switch (newDataItem)

{

case '+':

tempVal = operand1 + operand2;

top = new StackNode(tempVal, this->top);

break;

case '-':

tempVal = operand1 - operand2;

top = new StackNode(tempVal, this->top);

break;

case '*':

tempVal = operand1 * operand2;

top = new StackNode(tempVal, this->top);

break;

case '/':

tempVal = operand1 / operand2;

top = new StackNode(tempVal, this->top);

break;

}

}

else

top = new StackNode(newDataItem, this->top);

}

Explanation / Answer

Complete code could have been easier to debug. For example you have only defined a push operation. I get the many errors including.

DataType is undefined.

error: ‘pop’ was not declared in this scope
   pop();

Well my guess is the following.
There is a data type mismatch due to which even though the operations are taking place you are not getting the expected result. Try type casting your output.
For example use code like

cout<<"Result = "<<(int)result;
or if you have defined DataType properly then type
cout<<"Result = "<<(DataType)result;

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