Advertisement
Spocoman

02. Shopping List

Nov 9th, 2023
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. vector<string> vectorFiller(string str, char sep) {
  8.     vector<string> v;
  9.     string p = "";
  10.  
  11.     for (int i = 0; i < str.length(); i++) {
  12.         if (str[i] == sep) {
  13.             v.push_back(p);
  14.             p = "";
  15.         }
  16.         else {
  17.             p += str[i];
  18.         }
  19.     }
  20.     v.push_back(p);
  21.     return v;
  22. }
  23.  
  24. int index(vector<string> v, string s) {
  25.     for (int i = 0; i < v.size(); i++) {
  26.         if (v[i] == s) {
  27.             return i;
  28.         }
  29.     }
  30.     return -1;
  31. }
  32.  
  33. int main() {
  34.     string line, product, newProduct, command;
  35.     getline(cin, line);
  36.  
  37.     vector<string> list = vectorFiller(line, '!');
  38.  
  39.     while (true) {
  40.         getline(cin, line);
  41.         if (line == "Go Shopping!") {
  42.             break;
  43.         }
  44.  
  45.         vector<string> currentLine = vectorFiller(line, ' ');
  46.         command = currentLine[0];
  47.         product = currentLine[1];
  48.  
  49.         if (command == "Urgent" && index(list, product) == -1) {
  50.             list.insert(list.begin(), product);
  51.         }
  52.         else if (command == "Unnecessary" && index(list, product) != -1) {
  53.             list.erase(list.begin() + index(list, product));
  54.         }
  55.         else if (command == "Correct" && index(list, product) != -1) {
  56.             newProduct = currentLine[2];
  57.             list[index(list, product)] = newProduct;
  58.         }
  59.         else if (command == "Rearrange" && index(list, product) != -1) {
  60.             list.erase(list.begin() + index(list, product));
  61.             list.push_back(product);
  62.         }
  63.     }
  64.  
  65.     for (int i = 0; i < list.size() - 1; i++) {
  66.         cout << list[i] << ", ";
  67.     }
  68.     cout << list[list.size() - 1] << endl;
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement