Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- vector<string> getVectorByString(string s, string sep) {
- vector<string> v;
- s += sep;
- for (int i = 0; i < s.length(); i++) {
- if (s.substr(i, sep.length()) == sep) {
- v.push_back(s.substr(0, i));
- s.erase(0, i + sep.length());
- i = 0;
- }
- }
- return v;
- }
- int getItemIndex(string item, vector<string> v) {
- for (int i = 0; i < v.size(); i++) {
- if (item == v[i]) {
- return i;
- }
- }
- return -1;
- }
- int main() {
- string items, commands;
- getline(cin, items);
- vector<string> collectedItems = getVectorByString(items, ", ");
- getline(cin, commands);
- while (commands != "Craft!") {
- vector<string> vec = getVectorByString(commands, " - ");
- string command = vec[0], item = vec[1];
- if (command == "Collect") {
- if (getItemIndex(item, collectedItems) == -1) {
- collectedItems.push_back(item);
- }
- }
- else if (command == "Drop") {
- if (getItemIndex(item, collectedItems) > -1) {
- collectedItems.erase(collectedItems.begin() + getItemIndex(item, collectedItems));
- }
- }
- else if (command == "Combine Items") {
- vector<string> oldNewItems = getVectorByString(item, ":");
- string oldItem = oldNewItems[0], newItem = oldNewItems[1];
- if (getItemIndex(oldItem, collectedItems) > -1) {
- collectedItems.insert(collectedItems.begin() + getItemIndex(oldItem, collectedItems) + 1, newItem);
- }
- }
- else {
- if (getItemIndex(item, collectedItems) > -1) {
- collectedItems.erase(collectedItems.begin() + getItemIndex(item, collectedItems));
- collectedItems.push_back(item);
- }
- }
- getline(cin, commands);
- }
- for (int i = 0; i < collectedItems.size() - 1; i++) {
- cout << collectedItems[i] << ", ";
- }
- cout << collectedItems.back() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement