#include <iostream>
#include <stdexcept> // for exception
#include <string>
#include <cstddef> // for NULL
#include <new> // for bad_alloc
#include <stdlib.h>
usingnamespace std;
typedeffloat StackItemType;
class StackException : public logic_error
{
public:
StackException(const string& message = "")
: logic_error(message.c_str())
{}
};
class Stack
{
public:
Stack();/** Default constructor. */
Stack(const Stack& aStack);/** Copy constructor. * @param aStack The stack to copy. */
~Stack(); //Destructor
// Stack operations:
bool isEmpty() const;
void push(const StackItemType& newItem) throw(StackException);
void pop() throw(StackException);
void pop(StackItemType& stackTop) throw(StackException);
void getTop(StackItemType& stackTop) constthrow(StackException);
private:
struct StackNode/** A node on the stack. */
{
StackItemType item; /** A data item on the stack. */
StackNode *next;/** Pointer to next node. */
};
StackNode *topPtr; /** Pointer to first node in the stack. */
};
Stack::Stack() : topPtr(NULL)
{
}
// Copy constructor, Deep copy
Stack::Stack(const Stack& aStack)
{
if (aStack.topPtr == NULL)
topPtr = NULL; // original list is empty
else
{
// copy first node
topPtr = new StackNode;
topPtr->item = aStack.topPtr->item;
// copy rest of list
StackNode *newPtr = topPtr; // new node pointer
for (StackNode *origPtr = aStack.topPtr->next;
origPtr != NULL; origPtr = origPtr->next)
{
newPtr->next = new StackNode;
newPtr = newPtr->next;
newPtr->item = origPtr->item;
}
newPtr->next = NULL; //tail
}
}
Stack::~Stack()
{
// pop until stack is empty
while (!isEmpty())
pop();
// Assertion: topPtr == NULL
}
bool Stack::isEmpty() const
{
return topPtr == NULL;
}
void Stack::push(const StackItemType& newItem) throw(StackException)
{
// create a new node
try
{
StackNode *newPtr = new StackNode;
// set data portion of new node
newPtr->item = newItem;
// insert the new node
newPtr->next = topPtr; //newPtr->next pointer points to topPtr
topPtr = newPtr;
}
catch (bad_alloc e)
{
throw StackException(
"StackException: push cannot allocate memory.");
}
}
void Stack::pop() throw(StackException)
{
if (isEmpty())
throw StackException("StackException: stack empty on pop");
else
{
// stack is not empty; delete top
StackNode *temp = topPtr;
topPtr = topPtr->next;
// return deleted node to system
temp->next = NULL; // safeguard, don't point anything, points to nothing
delete temp;
}
}
void Stack::pop(StackItemType& stackTop) throw(StackException)
{
if (isEmpty())
throw StackException("StackException: stack empty on pop");
else
{
// stack is not empty; retrieve and delete top
stackTop = topPtr->item;
StackNode *temp = topPtr;
topPtr = topPtr->next;
// return deleted node to system
temp->next = NULL; // safeguard, points to nothing
delete temp;
}
}
void Stack::getTop(StackItemType& stackTop) constthrow(StackException)
{
if (isEmpty())
throw StackException("StackException: stack empty on getTop");
else
// stack is not empty; retrieve top
stackTop = topPtr->item;
}
// TODO: Write your code here
// Begin Question
float evalPostfixExpression (string str)
{
Stack s;
Stack g;
StackItemType op1;
StackItemType op2;
StackItemType result;
for(int i=0;i<str.size();i++)
{
if(str[i]!='+'&& str[i]!='-' && str[i]!='*'&& str[i]!='/')
{
cout<<str[i]<<endl;
s.push(str[i]);
}
else
{
cout<<str[i];
s.pop(op1);
cout<<"op1="<<op1;
s.pop(op2);
cout<<"op2="<<op2;
switch(str[i])
{
case'+' :
result = op1 + op2;
break;
case'-' :
result = op1 - op2;
break;
case'*' :
result = op1 * op2;
break;
case'/' :
result = op1 / op2;
break;
}
}
s.push(result);
}
return result;
}
// End Question
int main()
{
system("title ADT Stack Application - Evaluating Postfix Expressions");
string postfixExp;
float result;
cout << "Enter a postfix expression to be evaluated: ";
getline(cin, postfixExp);
result = evalPostfixExpression (postfixExp);
cout << postfixExp << " = " << result << endl;
system("pause");
return 0;
}
i use the cout op1 and op2 to check my value, i found that it pops some unknown value that i din input, can someone explain to me what's wrong with my program?
The stack is fine. On line 168, you push the ascii value instead of the number. subtract '0' from it.
On line 198, you push result for every character in the string. This fills your stack with some 0s. You only need this when an operator is called.