Beispiel #1
0
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