#include <iostream> #include <vector> #include <stack> #include <algorithm> using namespace std; string str; void read() { getline (cin, str); } vector <int> isNumber; vector <string> words; vector <int> numbers; void init() { isNumber.clear(); words.clear(); numbers.clear(); } int StringToNum (string x) { int f = 0; if (x[0]=='-') f = 1; int S = 0; for (int i=f; i<x.length(); i++) { S=(S*10)+(x[i]-'0'); } if (f==1) return 0-S; return S; } void separation() //Tach string thanh cac thanh phan rieng biet { stack<string> st; for (int i=str.length()-1; i>=0; i--) { if (str[i]==',' || str[i]=='.') { st.push(""); } else if (str[i]!=' ') { string top = st.top(); st.pop(); top=str[i]+top; st.push(top); } } while (!st.empty()) { string top = st.top(); st.pop(); if (top[0]=='-' || (top[0]>='0' && top[0]<='9')) { numbers.push_back(StringToNum(top)); isNumber.push_back(1); } else { words.push_back(top); isNumber.push_back(0); } } } void Sort() { sort(numbers.begin(), numbers.end()); sort(words.begin(), words.end()); } void Print() { int vN = 0; int vW = 0; for (int i=0; i<isNumber.size()-1; i++) { if (isNumber[i]==1) { cout<<numbers[vN]<<", "; vN++; } else { cout<<words[vW]<<", "; vW++; } } if (isNumber[isNumber.size()-1]==1) { cout<<numbers[vN]<<"."; vN++; } else { cout<<words[vW]<<"."; vW++; } cout<<endl; } int main () { while (1) { read(); if (str==".") break; init(); separation(); Sort(); Print(); } return 0; }
0. banana, strawberry, orange. banana, strawberry, orange. 10, 8, 6, 4, 2, 0. x, 30, -20, z, 1000, 1, y. 50, 7, kitten, puppy, 2, orangutan, 52, -100, bird, worm, 7, beetle. .
0. banana, orange, strawberry. banana, orange, strawberry. 0, 2, 4, 6, 8, 10. x, -20, 1, y, 30, 1000, z. -100, 2, beetle, bird, 7, kitten, 7, 50, orangutan, puppy, 52, worm.