Advertisement
Spocoman

03. Inventory

Nov 4th, 2023
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector<string> getVectorByString(string s, string sep) {
  8.     vector<string> v;
  9.     s += sep;
  10.     for (int i = 0; i < s.length(); i++) {
  11.         if (s.substr(i, sep.length()) == sep) {
  12.             v.push_back(s.substr(0, i));
  13.             s.erase(0, i + sep.length());
  14.             i = 0;
  15.         }
  16.     }
  17.    
  18.     return v;
  19. }
  20.  
  21. int getItemIndex(string item, vector<string> v) {
  22.     for (int i = 0; i < v.size(); i++) {
  23.         if (item == v[i]) {
  24.             return i;
  25.         }
  26.     }
  27.     return -1;
  28. }
  29.  
  30. int main() {
  31.     string items, commands;
  32.     getline(cin, items);
  33.     vector<string> collectedItems = getVectorByString(items, ", ");
  34.  
  35.     getline(cin, commands);
  36.  
  37.     while (commands != "Craft!") {
  38.         vector<string> vec = getVectorByString(commands, " - ");
  39.         string command = vec[0], item = vec[1];
  40.         if (command == "Collect") {
  41.             if (getItemIndex(item, collectedItems) == -1) {
  42.                 collectedItems.push_back(item);
  43.             }
  44.         }
  45.         else if (command == "Drop") {
  46.             if (getItemIndex(item, collectedItems) > -1) {
  47.                 collectedItems.erase(collectedItems.begin() + getItemIndex(item, collectedItems));
  48.             }
  49.         }
  50.         else if (command == "Combine Items") {
  51.             vector<string> oldNewItems = getVectorByString(item, ":");
  52.             string oldItem = oldNewItems[0], newItem = oldNewItems[1];
  53.             if (getItemIndex(oldItem, collectedItems) > -1) {
  54.                 collectedItems.insert(collectedItems.begin() + getItemIndex(oldItem, collectedItems) + 1, newItem);
  55.             }
  56.         }
  57.         else {
  58.             if (getItemIndex(item, collectedItems) > -1) {
  59.                 collectedItems.erase(collectedItems.begin() + getItemIndex(item, collectedItems));
  60.                 collectedItems.push_back(item);
  61.             }
  62.         }
  63.         getline(cin, commands);
  64.     }
  65.  
  66.     for (int i = 0; i < collectedItems.size() - 1; i++) {
  67.         cout << collectedItems[i] << ", ";
  68.     }
  69.     cout << collectedItems.back() << endl;
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement