Problem p01087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef string::const_iterator State;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

int number(string s) {
  int ret = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    if (isdigit(s[i])) {
      ret *= 10;
      ret += (s[i] - '0');
    } else {
      break;
    }
  }
  return ret;
}

int calc(int ret, int num, char x) {
  if (ret == -1)
    return num;
  if (x == '+')
    return ret + num;
  if (x == '-')
    return ret - num;
  if (x == '*')
    return ret * num;
  if (x == '/')
    return ret / num;
}

int main() {
  int n;
  while (cin >> n, n) {
    string s;
    vector<pair<int, string>> v(n);
    int maxi = 0;
    rep(i, n) {
      cin >> s;
      int cnt = 0;
      for (int j = 0; j < s.size(); j++) {
        if (s[j] == '.')
          cnt++;
      }
      maxi = max(maxi, cnt);
      s.erase(s.begin(), s.begin() + cnt);
      v[i].first = cnt;
      v[i].second = s;
    }
    maxi--;

    while (v.size() != 1) {
      bool update = false;
      for (int i = 0; i < v.size(); i++) {
        int start;
        if (v[i].first == maxi && !isdigit(v[i].second[0])) {
          start = i;
          char c = v[i].second[0];
          i++;
          int ret = -1;
          while (i < v.size() && v[i].first == maxi + 1) {
            // cout<<v[i].second<<endl;
            ret = calc(ret, number(v[i].second), c);
            i++;
          }
          v[start].second = to_string(ret);
          v.erase(v.begin() + start + 1, v.begin() + i);
          update = true;
          break;
        }
      }
      // cout<<"debug >> "<<endl;
      // for(auto x : v)cout<<x.first<<" "<<x.second<<endl;
      if (!update)
        maxi--;
    }
    cout << v[0].second << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef string::const_iterator State;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

int number(string s) {
  int ret = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    if (isdigit(s[i])) {
      ret *= 10;
      ret += (s[i] - '0');
    } else {
      break;
    }
  }
  return ret;
}

int calc(int ret, int num, char x) {
  if (ret == -1)
    return num;
  if (x == '+')
    return ret + num;
  if (x == '-')
    return ret - num;
  if (x == '*')
    return ret * num;
  if (x == '/')
    return ret / num;
}

int main() {
  int n;
  while (cin >> n, n) {
    string s;
    vector<pair<int, string>> v(n);
    int maxi = 0;
    rep(i, n) {
      cin >> s;
      int cnt = 0;
      for (int j = 0; j < s.size(); j++) {
        if (s[j] == '.')
          cnt++;
      }
      maxi = max(maxi, cnt);
      s.erase(s.begin(), s.begin() + cnt);
      v[i].first = cnt;
      v[i].second = s;
    }
    maxi--;

    while (v.size() != 1) {
      bool update = false;
      for (int i = 0; i < v.size(); i++) {
        int start;
        if (v[i].first == maxi && !isdigit(v[i].second[0])) {
          start = i;
          char c = v[i].second[0];
          i++;
          int ret = -1;
          while (i < v.size() + 1 && v[i].first == maxi + 1) {
            ret = calc(ret, number(v[i].second), c);
            i++;
          }
          v[start].second = to_string(ret);
          v.erase(v.begin() + start + 1, v.begin() + i);
          update = true;
          break;
        }
      }
      if (!update)
        maxi--;
    }
    cout << v[0].second << endl;
  }
  return 0;
}