fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. string str;
  8. void read()
  9. {
  10. getline (cin, str);
  11. }
  12.  
  13. vector <int> isNumber;
  14. vector <string> words;
  15. vector <int> numbers;
  16.  
  17. void init()
  18. {
  19. isNumber.clear();
  20. words.clear();
  21. numbers.clear();
  22. }
  23.  
  24. int StringToNum (string x)
  25. {
  26. int f = 0;
  27. if (x[0]=='-')
  28. f = 1;
  29. int S = 0;
  30. for (int i=f; i<x.length(); i++)
  31. {
  32. S=(S*10)+(x[i]-'0');
  33. }
  34. if (f==1) return 0-S;
  35. return S;
  36. }
  37.  
  38. void separation() //Tach string thanh cac thanh phan rieng biet
  39. {
  40. stack<string> st;
  41. for (int i=str.length()-1; i>=0; i--)
  42. {
  43. if (str[i]==',' || str[i]=='.')
  44. {
  45. st.push("");
  46. }
  47. else if (str[i]!=' ')
  48. {
  49. string top = st.top();
  50. st.pop();
  51. top=str[i]+top;
  52. st.push(top);
  53. }
  54. }
  55. while (!st.empty())
  56. {
  57. string top = st.top();
  58. st.pop();
  59. if (top[0]=='-' || (top[0]>='0' && top[0]<='9'))
  60. {
  61. numbers.push_back(StringToNum(top));
  62. isNumber.push_back(1);
  63. }
  64. else
  65. {
  66. words.push_back(top);
  67. isNumber.push_back(0);
  68. }
  69. }
  70. }
  71.  
  72. void Sort()
  73. {
  74. sort(numbers.begin(), numbers.end());
  75. sort(words.begin(), words.end());
  76. }
  77.  
  78. void Print()
  79. {
  80. int vN = 0;
  81. int vW = 0;
  82. for (int i=0; i<isNumber.size()-1; i++)
  83. {
  84. if (isNumber[i]==1)
  85. {
  86. cout<<numbers[vN]<<", ";
  87. vN++;
  88. }
  89. else
  90. {
  91. cout<<words[vW]<<", ";
  92. vW++;
  93. }
  94. }
  95. if (isNumber[isNumber.size()-1]==1)
  96. {
  97. cout<<numbers[vN]<<".";
  98. vN++;
  99. }
  100. else
  101. {
  102. cout<<words[vW]<<".";
  103. vW++;
  104. }
  105. cout<<endl;
  106. }
  107.  
  108. int main ()
  109. {
  110. while (1)
  111. {
  112. read();
  113. if (str==".") break;
  114. init();
  115. separation();
  116. Sort();
  117. Print();
  118. }
  119. return 0;
  120. }
Success #stdin #stdout 0s 4472KB
stdin
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.
.
stdout
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.