Advertisement
Spocoman

03. Machine Commands

Nov 22nd, 2023 (edited)
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     string command;
  9.     cin >> command;
  10.  
  11.     vector<int> numbers;
  12.  
  13.     while (command != "end") {      
  14.             if (command == "sum") {
  15.                 numbers[numbers.size() - 2] += numbers[numbers.size() - 1];
  16.                 numbers.pop_back();
  17.             }
  18.             else if (command == "subtract") {
  19.                 numbers[numbers.size() - 1] -= numbers[numbers.size() - 2];
  20.                 numbers.erase(numbers.begin() + numbers.size() - 2);
  21.             }
  22.             else if (command == "concat") {
  23.                 numbers[numbers.size() - 2] = stoi(to_string(numbers[numbers.size() - 2]) + to_string(numbers[numbers.size() - 1]));
  24.                 numbers.pop_back();
  25.             }
  26.             else if (command == "discard") {
  27.                 numbers.pop_back();
  28.             }
  29.             else {
  30.                 numbers.push_back(stoi(command));
  31.             }
  32.        
  33.         cin >> command;
  34.     }
  35.  
  36.     for (auto& n : numbers) {
  37.         cout << n << endl;
  38.     }
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement