The code below compiles but produces the wrong output. The code is supposed to convert an infix string to postfix. the current output is only the arithmetic operator, unless there are parenthesis then it will only show the right parenthesis. I have been woking on this for hours and am getting nowhere. Please help.
#ifndef H_stackType
#define H_stackType
#include <iostream>
#include <cassert>
using namespace std;
template<class Type>
class stackType
{
public:
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void push(const Type& newItem);
Type top();
void pop();
stackType(int stackSize);
~stackType();
private:
int maxStackSize;
int stackTop;
Type *list;
};
template<class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}
template<class Type>
bool stackType<Type>::isEmptyStack()
{
return(stackTop == 0);
}
template<class Type>
bool stackType<Type>::isFullStack()
{
return(stackTop == maxStackSize);
}
template<class Type>
void stackType<Type>::push(const Type& newItem)
{
if(!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cerr<<"Cannot add to a full stack." << endl;
}
template<class Type>
Type stackType<Type>::top()
{
assert(stackTop != 0);
return list[stackTop - 1];
}
template<class Type>
void stackType<Type>::pop()
{
if(!isEmptyStack())
stackTop--;
else
cerr<<"Cannot remove from an empty stack."<<endl;
}
template<class Type>
stackType<Type>::stackType(int stackSize)
{
if(stackSize <= 0)
{
cerr<<"Size of the array to hold the stack must "
<<"be positive."<<endl;
cerr<<"Creating an array of size 50."<<endl;
maxStackSize = 50;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new Type[maxStackSize];
assert(list != NULL);
}
template<class Type>
stackType<Type>::~stackType()
{
delete [] list;
}
#endif
#include <iostream>
#include <string>
#include "stackType.h"
using namespace std;
string rpn(string infix);
int main()
{
string infix;
cout << "NOTE: Enter ! for infix expression to exit." << endl;
for (;;)
{
cout << "Infix Expression? ";
getline(cin, infix);
if (infix == "!") break;
cout << "RPN Expression is: " << rpn(infix) << endl;
}
return 0;
}
string rpn(string infix)
{
char stackOpr;
string RPNexp;
const string BLANK = " ";
stackType<char> stack(50);
stack.initializeStack();
for (unsigned i = 0; i < infix.length(); i++)
{
switch(infix[i])
{
case ' ' : break;
case '(' : stack.push(infix[i]);
break;
case ')' : stackOpr = stack.top();
stack.pop();
if(!stack.isEmptyStack())
{
stackOpr = stack.top();
stack.pop();
}
else
break;
case '+' : case '-' :
case '*' : case '/' :
for (;;)
{
if (stack.isEmptyStack() ||
stack.top() == '(' ||
(infix[i] == '*' || infix[i] == '/') &&
(stack.top() == '+' || stack.top() == '-')
)
{
stack.push(infix[i]);
break;
}
else
{
stackOpr = stack.top();
stack.pop();
}
}
break;
default : RPNexp.append(BLANK + stackOpr);
}
}
for (;;)
{
if (stack.isEmptyStack()) break;
stackOpr = stack.top();
stack.pop();
if (stackOpr != '(')
{
RPNexp.append(BLANK + stackOpr);
}
else
{
cout << " *** Error in infix expression ***\n";
break;
}
}
return RPNexp;
}