I have an assignment to write a program that converts an infix mathematical expression into a postfix mathematical expression using a template class and a stack. I am inexperienced at using both and have been learning as I go along. What I have written so far works in my head but I cannot seem to get it to compile.
And here is the implimentation file for the stack:Code:#include<iostream> #include<iomanip> #include<fstream> #include<cmath> #include "header.h" using namespace std; int main() { char infix[50]; char postfix[50]; int k=0; int j=0; int token; for(int h=0; h<50; h++) { infix[h] = '#'; postfix[h] = '#'; } cout << "enter infix expression: "; cin.getline(infix, '\n'); StackType<char> Operator; char temp; token = infix[0]; while(token != '=') { if(checkOperand(token)) { postfix[k] = token; k++; } if(token == ')') { temp = Operator.top(); Operator.pop(); while(temp != '(') { postfix[k] = temp; k++; Operator.pop(); } } if(checkOperator(token)) { if(tokenPrecedence(token)>=tokenPrecedence(Operator.top())||token=='(') { Operator.push(token); } else { temp = Operator.top(); Operator.pop(); postfix[k] = temp; k++; Operator.push(token); } } j++; token = infix[j]; } while(Operator.isEmpty()==false) { temp = Operator.top(); postfix[k] = temp; k++; Operator.pop(); } postfix[k] = token; return 0; } bool checkOperand(int token) { switch(token) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 0: return true; default: return false; } } bool checkOperator(int token) { switch(token) { case '+': case '-': case '/': case '*': return true; default: return false; } } int tokenPrecedence(int token) { int prec=0; switch(token) { case '*': case '/': prec = 1; break; case '+': case '-': prec = -1; break; default: break; } }
The errors I get areCode://Michael Bradford //CPSCI 131 #include<iostream> #include<iomanip> using namespace std; template <class plate> StackType<plate>::StackType() { top = -1; //initializing position of top value to -1 MaxSize=50; //initializing the maximum size of the stack to 50 size=0; //size of the stack the_array = new int[size]; //initializing the stack to the size of 0 } template <class plate> StackType<plate>::~StackType() { delete [] the_array; } template <class plate> bool StackType<plate>::isEmpty() { bool truthvalue=false; //true or false if empty if(top==-1) //if the stack is empty than truthvalue is true truthvalue = true; return truthvalue; } template <class plate> bool StackType<plate>::isFull() { bool truthvalue=false; //true or false if full if(top==(MaxSize-1)) //if the stack us full than truthvalue is true truthvalue = true; return truthvalue; } template <class plate> void StackType<plate>::push(int newItem) { try { if(isFull()) //if stack is full than throw an error throw newItem; top++; //if stack is not full than make room the_array[top] = newItem; //add newItem to the top of the stack } catch(int e) { cout << "error! stack is full!"; //error if the stack is full } } template <class plate> void StackType<plate>::pop() { try { if(isEmpty()) //if stack is empty than throw an error throw isEmpty(); top--;//if stack is not empty than remove item from top of stack } catch(bool e) { cout << "error! stack is empty!"; //error if the stack is empty } } template <class plate> int StackType<plate>::top() { try { if(isEmpty()) //if the stack is empty than throw an error throw isEmpty(); return the_array[Top]; //if the stack is not empty than return the top value } catch(bool e) { cout << "error! stack is empty!"; //error if the stack is empty } }
implementation.cpp: In constructor ‘StackType<plate>::StackType() [with plate = char]’:
Assignment4.cpp:21: instantiated from here
implementation.cpp:9: error: invalid use of member (did you forget the ‘&’ ?)
implementation.cpp:12: error: cannot convert ‘int*’ to ‘char*’ in assignment
implementation.cpp: In member function ‘void StackType<plate>::pop() [with plate = char]’:
Assignment4.cpp:34: instantiated from here
implementation.cpp:57: error: no post-decrement operator for type
implementation.cpp: In member function ‘void StackType<plate>::push(int) [with plate = char]’:
Assignment4.cpp:46: instantiated from here
implementation.cpp:42: error: no post-increment operator for type
implementation.cpp:43: error: invalid use of member (did you forget the ‘&’ ?)
implementation.cpp:43: error: array subscript is not an integer
implementation.cpp: In member function ‘bool StackType<plate>::isEmpty() [with plate = char]’:
Assignment4.cpp:60: instantiated from here
implementation.cpp:23: error: invalid use of member (did you forget the ‘&’ ?)
implementation.cpp: In member function ‘bool StackType<plate>::isFull() [with plate = char]’:
implementation.cpp:40: instantiated from ‘void StackType<plate>::push(int) [with plate = char]’
Assignment4.cpp:46: instantiated from here
implementation.cpp:31: error: invalid use of member (did you forget the ‘&’ ?)
Any help would be greatly appreciated, thanks.



LinkBack URL
About LinkBacks


