Why is my Pre to Postfix code not working?
- by Anthony Glyadchenko
For a class assignment, I have to use two stacks in C++ to make an equation to be converted to its left to right equivalent: 2+4*(3+4*8) -- 35*4+2 -- 142
Here is the main code:
#include <iostream>
#include <cstring>
#include "ctStack.h"
using namespace std;
int main (int argc, char * const argv[]) {
string expression = "2+4*2";
ctstack *output = new ctstack(expression.length());
ctstack *stack = new ctstack(expression.length());
bool previousIsANum = false;
for(int i = 0; i < expression.length(); i++){
switch (expression[i]){
case '(':
previousIsANum = false;
stack->cmstackPush(expression[i]);
break;
case ')':
previousIsANum = false;
char x;
while (x != '('){
stack->cmstackPop(x);
output->cmstackPush(x);
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
cout << "A number" << endl;
previousIsANum = true;
output->cmstackPush(expression[i]);
break;
case '+':
previousIsANum = false;
cout << "+" << endl;
break;
case '-':
previousIsANum = false;
cout << "-" << endl;
break;
case '*':
previousIsANum = false;
cout << "*" << endl;
break;
case '/':
previousIsANum = false;
cout << "/" << endl;
break;
default:
break;
}
}
char i = ' ';
while (stack->ltopOfStack > 0){
stack->cmstackPop(i);
output->cmstackPush(i);
cout << i << endl;
}
return 0;
}
Here is the stack code (watch out!):
#include <cstdio>
#include <assert.h>
#include <new.h>
#include <stdlib.h>
#include <iostream>
class ctstack {
private:
long* lpstack ; // the stack itself
long ltrue ; // constructor sets to 1
long lfalse ; // constructor sets to 0
// offset to top of the stack
long lmaxEleInStack ; // maximum possible elements of stack
public:
long ltopOfStack ;
ctstack ( long lnbrOfEleToAllocInStack ) { // Constructor
lfalse = 0 ; // set to zero
ltrue = 1 ; // set to one
assert ( lnbrOfEleToAllocInStack > 0 ) ; // assure positive argument
ltopOfStack = -1 ; // ltopOfStack is really an index
lmaxEleInStack = lnbrOfEleToAllocInStack ; // set lmaxEleInStack to max ele
lpstack = new long [ lmaxEleInStack ] ; // allocate stack
assert ( lpstack ) ; // assure new succeeded
}
~ctstack ( ) { // Destructor
delete [ ] lpstack ; // Delete the stack itself
}
ctstack& operator= ( const ctstack& ctoriginStack) { // Assignment
if ( this == &ctoriginStack ) // verify x not assigned to x
return *this ;
if ( this -> lmaxEleInStack <
ctoriginStack . lmaxEleInStack ) { // if destination stack is smaller than
delete [ ] this -> lpstack ; // original stack, delete dest and alloc
this -> lpstack = // sufficient memory
new long [ ctoriginStack . lmaxEleInStack ] ;
assert ( this -> lpstack ) ; // assure new succeeded
// reset stack size attribute
this -> lmaxEleInStack = ctoriginStack . lmaxEleInStack ;
}
// copy original to destination stack
for ( long i = 0 ; i < ctoriginStack . lmaxEleInStack ; i ++ )
*( this -> lpstack + i ) = *( ctoriginStack . lpstack + i ) ;
this -> ltopOfStack = ctoriginStack . ltopOfStack ; // reset stack position attribute
return *this ;
}
long cmstackPush (char lplaceInStack ) { // Push Method
if ( ltopOfStack == lmaxEleInStack - 1 ) // stack is full can't add element
return lfalse ;
ltopOfStack ++ ; // acquire free slot
*(lpstack + ltopOfStack ) = lplaceInStack ; // add element
return ltrue ; // any number other than zero is true
}
long cmstackPop (char& lretrievedStackEle ) { // Pop Method
if ( ltopOfStack < 0 ) { // stack has no elements
lretrievedStackEle = -1 ; // dummy element
return lfalse ;
}
lretrievedStackEle = *( lpstack + ltopOfStack ) ; // stack has element -- return it
ltopOfStack -- ; // stack is pop'd
return ltrue ; // any number other than zero is true
}
long cmstackLookAtTop (char& lretrievedStackEle ) { // Pop Method
if ( ltopOfStack < 0 ) { // stack has no elements
lretrievedStackEle = -1 ; // dummy element
return lfalse ;
}
lretrievedStackEle = *( lpstack + ltopOfStack ) ; // stack has element -- return it
return ltrue ; // any number other than zero is true
}
long cmstackHasAnEle (char& lretrievedTopOfStack ) { // Has element method
lretrievedTopOfStack = ltopOfStack ;
return ltopOfStack < 0 ? lfalse : ltrue ; // 0 - false stack does not have any ele
} // 1 - true stack has at least one element
long cmstackMaxNbrOfEle (char& lretrievedMaxStackEle ) { // Maximum element method
lretrievedMaxStackEle = lmaxEleInStack ; // return stack size in reference var
return ltrue ; // Return Maximum Size of Stack
}
} ;
Thanks, Anthony.