mitakvd

2. Garbage Sorting

Nov 24th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. void showlist(list <int> out)
  9. {
  10.     list<int>::iterator it;
  11.     for (it = out.begin(); it != out.end(); ++it)
  12.     {
  13.         cout << *it << ' ';
  14.     }
  15. }
  16.  
  17. int main()
  18. {
  19.     int numberOfCommands;
  20.     cin >> numberOfCommands;
  21.     cin.ignore();
  22.  
  23.     list<int> glass, metal, plastic;
  24.     for (int i = 1; i <= numberOfCommands; i++)
  25.     {
  26.         string input;
  27.         getline(cin, input);
  28.         istringstream istr(input);
  29.         string line, command;
  30.         istr >> line >> command;
  31.  
  32.         if (line == "glass")
  33.         {
  34.             if (command == "front")
  35.             {
  36.                 glass.push_front(i);
  37.             }
  38.             else
  39.             {
  40.                 glass.push_back(i);
  41.             }
  42.         }
  43.         else if (line == "metal")
  44.         {
  45.             if (command == "front")
  46.             {
  47.                 metal.push_front(i);
  48.             }
  49.             else
  50.             {
  51.                 metal.push_back(i);
  52.             }
  53.         }
  54.         else if (line == "plastic")
  55.         {
  56.             if (command == "front")
  57.             {
  58.                 plastic.push_front(i);
  59.             }
  60.             else
  61.             {
  62.                 plastic.push_back(i);
  63.             }
  64.         }
  65.         else
  66.         {
  67.             i -= 1;
  68.             --numberOfCommands;
  69.         }
  70.     }
  71.     if (!glass.empty())
  72.     {
  73.         cout << "glass - ";
  74.         showlist(glass);
  75.         cout << endl;
  76.     }
  77.     if (!metal.empty())
  78.     {
  79.         cout << "metal - ";
  80.         showlist(metal);
  81.         cout << endl;
  82.     }
  83.     if (!plastic.empty())
  84.     {
  85.         cout << "plastic - ";
  86.         showlist(plastic);
  87.         cout << endl;
  88.     }
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment