int StringCalculator::calculateExpression(char string[]) { StackInterface* stackObject = new PointerStack(); makePolish(string, stackObject); //printf("%s\n", string); int i = 0; int length = strlen(string) + 1; int argument1 = 1; int argument2 = 1; int tmpResult = 0; for (i = 0; i < length; i++) { if (isNumber(string[i])) stackObject->push(convertToInt(string[i])); if (isSign(string[i])) { argument2 = stackObject->pop(); argument1 = stackObject->pop(); tmpResult = makeOperation(string[i], argument1, argument2); stackObject->push(tmpResult); } } int result = stackObject->pop(); delete stackObject; return result; }
bool Stack<T>::operator!=(const StackInterface<T>& rhs) const { if(m_size != rhs.size()) { return(true); } else { return(false); } }
void postFixTester() { string test = "a-(b+c*d)/e"; string postFix = ""; StackInterface<char>* stackPtr = new LinkedStack<char>(test.length()); for (int i = 0; i < test.length(); i++) { char hold = test.at(i); if(checkOperator(hold) == 2) { postFix.append(string(1, hold)); } else if (hold == '(') { stackPtr->push(hold); } else if (checkOperator(hold) == 0) { while(!stackPtr->isEmpty() && stackPtr->peek() != '(' && precedence(hold) <= precedence(stackPtr->peek())) { postFix.append(string(1, stackPtr->peek())); stackPtr->pop(); } stackPtr->push(hold); } else if (hold == ')') { while (stackPtr->peek() != '(') { postFix.append(string(1, stackPtr->peek())); stackPtr->pop(); } } } while (!stackPtr->isEmpty()) { postFix.append(string(1, stackPtr->peek())); stackPtr->pop(); } cout << postFix << endl; system("pause"); } // end postFixTester
bool Stack<T>::operator>(const StackInterface<T>& rhs)const { return this->m_size>rhs.size(); }
bool Stack<T>::operator!=(const StackInterface<T>& rhs)const{ return(size()!=(rhs.size())); }
bool Stack<T>::operator>=(const StackInterface<T>& rhs) const{ return(size()>=(rhs.size())); //note: we can't use m_size becuase it is a member of the child class. Instead, we use the result of the size method, which must exist in all child classes. }
bool Stack<T>::operator>(const StackInterface<T>& rhs) const{ return(size()>(rhs.size())); //note - this is ok as an argument, because we know all stackinterfaces have a size regardless of the data type. }