View Code of Problem 114

#include<iostream>
#include<string>
#include<stack>
using namespace std;

//  ;'asd;'
//  ;'dsa;'
int main()
{
	string str;
	while (getline(cin, str)) {
		stack<char> pp; // 先进后出
		for (int i = 0; i < str.size(); i++) {
			if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
				pp.push(str[i]); // 压入字符 
			}
			else { // 遇到非字母输出元素
				// 先把以前的字母输出 在输出非字母 使其顺序一致
				while (!pp.empty()) { // 栈内不为空则逆序输出字母
					cout << pp.top(); 
					pp.pop();
				}
				cout << str[i]; // 输出非字母的字符
			}
		}
		while (pp.size() != 0) { // 剩下的元素全为字母的情况下 逆序输出字母
			cout << pp.top();
			pp.pop();
		}
		cout << endl;
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 114