Guest User

Day6Part2

a guest
Dec 9th, 2025
52
0
330 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. void resolver(vector<string> problems) {
  7.     long totalsum = 0;
  8.     bool operador;
  9.     int resparcial = 0;
  10.     string numstr = "";
  11.     vector<int> problem;
  12.     for(int i = problems.front().size() -1; i >= 0 ;--i){
  13.         for(int j = 0; j < problems.size()-1; j++) {
  14.             if (problems[j][i] != ' ') {
  15.                 numstr += problems[j][i];
  16.             }
  17.         }
  18.         if (problems[problems.size()-1][i] == '+') {
  19.                 if (numstr != "") { // Primer espacio después de leer números
  20.                     printf("%i,", stoi(numstr));
  21.                     problem.push_back(stoi(numstr));
  22.                     numstr = "";
  23.                 }
  24.                 resparcial = 0;
  25.                 for (int& x: problem) {
  26.                     resparcial += x;
  27.                 }
  28.                 printf("+=%i\n",resparcial);
  29.                 totalsum+=resparcial;
  30.                 numstr = "";
  31.                 problem.clear();
  32.             } else if (problems[problems.size()-1][i] == '*'){
  33.                 if (numstr != "") { // Primer espacio después de leer números
  34.                     printf("%i,", stoi(numstr));
  35.                     problem.push_back(stoi(numstr));
  36.                     numstr = "";
  37.                 }
  38.                 resparcial = 1;
  39.                 for (int& x: problem) {
  40.                     resparcial *= x;
  41.                 }
  42.                 printf("*=%i\n",resparcial);
  43.                 totalsum+=resparcial;
  44.                 numstr = "";
  45.                 problem.clear();
  46.             } else if (problems[problems.size()-1][i] == ' ') {
  47.                 if (numstr != "") { // Primer espacio después de leer números
  48.                     printf("%i,", stoi(numstr));
  49.                     problem.push_back(stoi(numstr));
  50.                     numstr = "";
  51.                 }
  52.             }
  53.     }
  54.     printf("%li\n",totalsum);
  55. }
  56.  
  57.  
  58. int main() {
  59.     string line;
  60.     getline(cin, line);
  61.     vector<string> problems;
  62.     while(line != "") {
  63.         problems.push_back(line);
  64.         getline(cin, line);
  65.     }
  66.    
  67.     resolver(problems);
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment