Postfix to Infix
Oct 29, 2009 at 12:36pm UTC
Hi. What I want to do is convert a postfix expression into a infix expression. I am reading the postfix expression from an input file and I want to print the infix expression to an output file. I basically just need help setting up my function, so it will work. Please help if you can. Thank you
Input file:
ABC+-D-E-
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "stack.h"
#include <sstream>
using namespace std;
bool isOperand(string expr);
bool isOperator(string expr);
string PostfixToInfix(string expr, stackType<string> stack);
int main()
{
stringstream ss;
stackType<string> stack;
string Infix;
string line;
string word;
string expr;
ifstream inData("input.txt" );
ofstream outData("output.txt" );
while (getline( inData, line ))
{
ss << line;
}
while ( ss >> word)
{
stack.push(word);
}
outData<<line;
inData.close();
outData.close();
system("PAUSE" );
return 0;
}
bool isOperand(string expr)
{
return (!isOperator(expr)? true : false );
}
bool isOperator(string expr)
{
return ((expr=="+" || expr=="-" || expr=="*" || expr=="/" || expr=="(" || expr==")" )? true : false );
}
string PostfixToInfix(string expr,stackType<string> stack)
{
for (int i=0; i<expr.length(); i++)
{
if (isOperand(expr[i]))
{
stack.push(expr.length()[i]);
}
else
{
string temp = stack.top();
stack.pop();
int stack.push()=stack.top()+expr.length()[i]+temp;
stack.pop();
stack.push();
}
}
return (stack.top());
}
stack.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
#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);
}
#endif
Topic archived. No new replies allowed.