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
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; }