fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <map>
  4. #include <vector>
  5. #include <string>
  6. #include <functional>
  7. #include <algorithm>
  8. #include <limits>
  9. using namespace std;
  10.  
  11. vector<int> insertion_sort(const string &input)
  12. {
  13. vector<int> numbers;
  14. int num;
  15. istringstream iss(input);
  16. while (iss >> num)
  17. {
  18. numbers.push_back(num);
  19. }
  20. sort(numbers.begin(), numbers.end());
  21. return numbers;
  22. }
  23.  
  24. string trim(string str)
  25. {
  26. str.erase(0, str.find_first_not_of("\t\n\v\f\r "));
  27. str.erase(str.find_last_not_of("\t\n\v\f\r ") + 1);
  28. return str;
  29. }
  30.  
  31. void Sort(const string &params)
  32. {
  33. vector<int> data;
  34.  
  35. string numbers;
  36. getline(cin, numbers);
  37.  
  38. data = insertion_sort(numbers);
  39.  
  40. if (params == "DESCENDING")
  41. {
  42. // DESCENDING
  43. for (auto it = data.rbegin(); it != data.rend(); ++it)
  44. {
  45. cout << *it << " ";
  46. }
  47. }
  48. else
  49. {
  50. // ASCENDING
  51. for (auto it = data.begin(); it != data.end(); ++it)
  52. {
  53. cout << *it << " ";
  54. }
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60. map<string, function<void(const string&)>> functions;
  61. functions["SORT"] = Sort;
  62. /*
  63.   functions["IS_SORTED"] = Is_Sorted;
  64.   functions["GETMAX"] = GetMax;
  65.   functions["APPEND"] = AppendSorted;
  66.   functions["GET_HISTOGRAM"] = GetHistogram;
  67. */
  68.  
  69. int num;
  70. cin >> num;
  71.  
  72. while (num > 0)
  73. {
  74. string func;
  75. cin >> func;
  76.  
  77. auto it = functions.find(func);
  78. if (it != functions.end())
  79. {
  80. string params;
  81. getline(cin, params);
  82. it->second(trim(params));
  83. }
  84. else
  85. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  86.  
  87. cout << endl;
  88.  
  89. --num;
  90. }
  91.  
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0.01s 5532KB
stdin
2
SORT
1 -2 13 24 -100
SORT DESCENDING
1 2 3 5 6
stdout
-100 -2 1 13 24 
6 5 3 2 1