I need help trying to convert a postfix expression to Infix. I have tried to search books and internet material to help me take on this task but no luck. Below is what I created, but I know it is far from right which is why I appreciate if someone can help me.
Targeted Function:
void toInfix(string expression)
{
stackType<string> stack,here;
string Infix;
string priority;
string number, hold, again;
again=expression;
ifstream inData("input.txt");
ofstream outData("output.txt");
while (getline( inData, expression ))
{
cout << expression;
}
for (int a=0; a < expression.length(); a++)
{
number = here = expression[a];
cout << "The expression " << here << endl;
cout << "The Infix " << Infix << endl;
if ( expression[a] == '+' || expression[a] == '-')
{
if (priority <= 2 )
{
here.pop(); here.push();
}
else
{
here.push();
}
}
else if ( expression[a] == '*' || expression[a] == '/' || expression[a] == '%' )
{
if (priority == 1 )
{
here=stack[0]; stack[0]=expression[a]; Infix = Infix + here;
}
else
{
here.push;
}
}
else if ( expression[a] == '(' )
{
here.push();
}
else if ( expression[a] == ')' )
{
here.pop();
}
else if ( isdigit(again[a]) )
{
if ( isdigit(again[a+1]) )
{
int b=a+1;
while ( isdigit(again[b]) )
{
hold=again[b];
number = number + hold; // number already equals the string here
++b;
}
infix = infix + ' ' + number;
a=b-1;
}
else
{
infix = infix + ' ' + number;
}
} // end of else if
cout << "\nINFIX "<< infix << endl;
} // end of for
Infix = Infix + ' ' + stack;
}
#endif
header file:
#ifndef H_StackType
#define H_StackType
#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;
template<class Type>
class stackType
{
public:
const stackType<Type>& operator=(const stackType<Type>&);
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void destroyStack();
void push(const Type& newItem);
Type top();
void pop();
stackType(int stackSize = 100);
stackType(const stackType<Type>& otherStack);
~stackType();
bool operator== (const stackType<Type>&);
bool operator!= (const stackType<Type>&);
private:
int maxStackSize;
int stackTop;
Type *list;
void copyStack(const stackType<Type>& otherStack);
bool isEqual(const stackType<Type>&);
};
template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
assert(list != NULL);
for(int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
}
template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
list = NULL;
copyStack(otherStack);
}
template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
if (this != &otherStack)
copyStack(otherStack);
return *this;
}
template<class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}
template<class Type>
void stackType<Type>::destroyStack()
{
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<<"The size of the array to hold the stack must "
<<"be positive."<<endl;
cerr<<"Creating an array of size 100."<<endl;
maxStackSize = 100;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new Type[maxStackSize];
}
template<class Type>
stackType<Type>::~stackType()
{
delete [] list;
}
template<class Type>
bool stackType<Type>::isEqual(const stackType<Type>& otherStack) {
bool bRet = false;
if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop){
bRet = true;
for (int j = 0; j < stackTop; ++j) {
if (otherStack.list[j] != list[j]) {
// cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
bRet = false;
break;
}
}
}
return bRet;
}
template<class Type>
bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
return isEqual(otherStack);
}
template<class Type>
bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
return !isEqual(otherStack); //!(*this == otherStack);
}
void toInfix(string expression)
{
stackType<string> stack,here;
string Infix;
string priority;
string number, hold, again;
again=expression;
ifstream inData("input.txt"); // declaration of inData
ofstream outData("output.txt"); // declaration of outData
while (getline( inData, expression )) // reads words in file using getline to capture all spaces
{ /*something to break up line into individual character*/
cout << expression;
}
for (int a=0; a < expression.length(); a++)
{
number = here = expression[a];
cout << "The expression " << here << endl;
cout << "The Infix " << Infix << endl;
if ( expression[a] == '+' || expression[a] == '-')
{
if (priority <= 2 )
{
here.pop(); here.push();
}
else
here.push();
}
else if ( expression[a] == '*' || expression[a] == '/' || expression[a] == '%' )
{
if (priority == 1 )
{
here=stack[0]; stack[0]=expression[a]; Infix = Infix + here;
}
else
here.push;
}
else if ( expression[a] == '(' ) here.push();
else if ( expression[a] == ')' ) here.pop();
else if ( isdigit(again[a]) )
{ // for dealing with multiple digits and single digit numbers
if ( isdigit(again[a+1]) )
{
int b=a+1;
while ( isdigit(again[b]) )
{
hold=again[b];
number = number + hold; // number already equals the string here
++b;
}
infix = infix + ' ' + number;
a=b-1;
}
else
{
infix = infix + ' ' + number;
}
} // end of else if
cout << "\nINFIX "<< infix << endl;
} // end of for
Infix = Infix + ' ' + stack;
}
#endif
main.cpp
#include <iostream>
#include <stdlib.h>
#include "stack.h"
using namespace std;
int main()
{
toInfix(string expression);
system("PAUSE");
return 0;
}