Problem p00087 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI = 6 * asin(0.5);
typedef pair<double, double> P;
typedef complex<double> CP;
typedef long long ll;

string s;

int main() {
  while (getline(cin, s)) {
    stack<double> st;
    rep(i, s.size()) {
      // cout<<"size "<<s.size()<<endl;
      if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
        double a1, a2;
        a2 = st.top();
        st.pop();
        a1 = st.top();
        st.pop();
        if (s[i] == '+') {
          a1 += a2;
        } else if (s[i] == '-') {
          a1 -= a2;
        } else if (s[i] == '*') {
          a1 *= a2;
        } else if (s[i] == '/') {
          a1 /= a2;
        }
        st.push(a1);
      }

      else if (s[i] <= '9' && s[i] >= '0') {
        double sum = 0.0;
        for (int j = i; j < s.size(); j++) {
          if (s[j] <= '9' && s[j] >= '0') {
            sum *= 10.0;
            sum += s[j] - '0';
          } else
            break;
          i = j;
        }
        st.push(sum);
      }
      // cout<<"st.top = "<<st.top()<<endl;
    }
    printf("%.6f\n", st.top());

    st.pop();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI = 6 * asin(0.5);
typedef pair<double, double> P;
typedef complex<double> CP;
typedef long long ll;

string s;

int main() {
  while (getline(cin, s)) {
    stack<double> st;
    rep(i, s.size()) {
      // cout<<"size "<<s.size()<<endl;

      if (s[i] == '-' && s[i + 1] <= '9' && s[i + 1] >= '0') {
        double sum = 0.0;
        for (int j = i + 1; j < s.size(); j++) {
          if (s[j] <= '9' && s[j] >= '0') {
            sum *= 10.0;
            sum += s[j] - '0';
          } else
            break;
          i = j;
        }
        sum = -sum;
        st.push(sum);
      }

      else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
        double a1, a2;
        a2 = st.top();
        st.pop();
        a1 = st.top();
        st.pop();
        if (s[i] == '+') {
          a1 += a2;
        } else if (s[i] == '-') {
          a1 -= a2;
        } else if (s[i] == '*') {
          a1 *= a2;
        } else if (s[i] == '/') {
          a1 /= a2;
        }
        st.push(a1);
      }

      else if (s[i] <= '9' && s[i] >= '0') {
        double sum = 0.0;
        for (int j = i; j < s.size(); j++) {
          if (s[j] <= '9' && s[j] >= '0') {
            sum *= 10.0;
            sum += s[j] - '0';
          } else
            break;
          i = j;
        }
        st.push(sum);
      }
      // cout<<"st.top = "<<st.top()<<endl;
    }
    printf("%.6f\n", st.top());

    st.pop();
  }
  return 0;
}