mstoyanov7

2.stackLab

May 26th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3. #include <string>
  4. #include <sstream>
  5. #include <stack>
  6. #include <cctype>
  7.  
  8. const int maxSize = 100;
  9.  
  10. std::array<int, maxSize> readInput(int& arrSize) {
  11. std::array<int, maxSize> arr{};
  12. std::string line;
  13. getline(std::cin, line);
  14. std::istringstream istr(line);
  15. int num;
  16.  
  17. while (istr >> num) {
  18. arr[arrSize] = num;
  19. ++arrSize;
  20. }
  21. return arr;
  22. }
  23.  
  24. std::string caseInsensitive(std::string& command) {
  25. for (int i = 0; i < command.size(); ++i) {
  26. command[i] = tolower(command[i]);
  27. }
  28. return command;
  29. }
  30.  
  31. void moveNumsInStack(std::array<int, maxSize>& input, int& arrSize, std::stack<int>& sum) {
  32. for (int i = 0; i < arrSize; ++i) {
  33. sum.push(input[i]);
  34. }
  35. }
  36.  
  37. void calculateSum(std::stack<int>& sum) {
  38. int sumOfAll = 0;
  39. while (!sum.empty()) {
  40. sumOfAll += sum.top();
  41. sum.pop();
  42. }
  43.  
  44. std::cout << "Sum: " << sumOfAll;
  45. }
  46.  
  47.  
  48. int main() {
  49. int arrSize = 0;
  50. std::array<int, maxSize> input = readInput(arrSize);
  51.  
  52. std::stack<int> sum;
  53.  
  54. moveNumsInStack(input, arrSize, sum);
  55.  
  56.  
  57. std::string command;
  58. std::cin >> command;
  59.  
  60. const std::string delimiter = "end";
  61.  
  62. while (true) {
  63. caseInsensitive(command);
  64.  
  65. if (command == delimiter) {
  66. break;
  67. }
  68.  
  69. if (command == "add") {
  70. int a, b;
  71. std::cin >> a >> b;
  72. sum.push(a);
  73. sum.push(b);
  74. }
  75.  
  76. if (command == "remove") {
  77. int a;
  78. std::cin >> a;
  79.  
  80. if (a < sum.size()) {
  81. for (int i = 0; i < a; ++i) {
  82. sum.pop();
  83. }
  84. }
  85. }
  86. std::cin >> command;
  87. }
  88.  
  89. calculateSum(sum);
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment