Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <array>
- #include <string>
- #include <sstream>
- #include <stack>
- #include <cctype>
- const int maxSize = 100;
- std::array<int, maxSize> readInput(int& arrSize) {
- std::array<int, maxSize> arr{};
- std::string line;
- getline(std::cin, line);
- std::istringstream istr(line);
- int num;
- while (istr >> num) {
- arr[arrSize] = num;
- ++arrSize;
- }
- return arr;
- }
- std::string caseInsensitive(std::string& command) {
- for (int i = 0; i < command.size(); ++i) {
- command[i] = tolower(command[i]);
- }
- return command;
- }
- void moveNumsInStack(std::array<int, maxSize>& input, int& arrSize, std::stack<int>& sum) {
- for (int i = 0; i < arrSize; ++i) {
- sum.push(input[i]);
- }
- }
- void calculateSum(std::stack<int>& sum) {
- int sumOfAll = 0;
- while (!sum.empty()) {
- sumOfAll += sum.top();
- sum.pop();
- }
- std::cout << "Sum: " << sumOfAll;
- }
- int main() {
- int arrSize = 0;
- std::array<int, maxSize> input = readInput(arrSize);
- std::stack<int> sum;
- moveNumsInStack(input, arrSize, sum);
- std::string command;
- std::cin >> command;
- const std::string delimiter = "end";
- while (true) {
- caseInsensitive(command);
- if (command == delimiter) {
- break;
- }
- if (command == "add") {
- int a, b;
- std::cin >> a >> b;
- sum.push(a);
- sum.push(b);
- }
- if (command == "remove") {
- int a;
- std::cin >> a;
- if (a < sum.size()) {
- for (int i = 0; i < a; ++i) {
- sum.pop();
- }
- }
- }
- std::cin >> command;
- }
- calculateSum(sum);
- }
Advertisement
Add Comment
Please, Sign In to add comment