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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

class fraction {
  ll numel; // ??????
  ll denom; // ??????

  map<ll, ll> prime_factor(ll n) {
    map<ll, ll> res;
    for (ll i = 2; i * i <= n; i++) {
      while (n % i == 0) {
        ++res[i];
        n /= i;
      }
    }
    if (n != 1)
      res[n] = 1;
    return res;
  }

  pair<ll, ll> reduce(ll n, ll d) {
    map<ll, ll> pfs = prime_factor(d);
    for (auto it = pfs.begin(); it != pfs.end(); it++) {
      while ((*it).second > 0 && n % (*it).first == 0) {
        n /= (*it).first;
        d /= (*it).first;
        (*it).second--;
      }
    }
    return pair<ll, ll>(n, d);
  }

public:
  fraction() {
    this->numel = 0;
    this->denom = 1;
  }
  fraction(string s) {
    int pos = 0;
    while (s[pos] != '/')
      pos++;
    ll n = stoi(string(s.begin(), s.begin() + pos));
    ll d = stoi(string(s.begin() + pos + 1, s.end()));

    pair<int, int> ret = reduce(n, d);
    this->numel = ret.first;
    this->denom = ret.second;
  }
  fraction(ll n, ll d) {
    pair<ll, ll> ret = reduce(n, d);
    this->numel = ret.first;
    this->denom = ret.second;
  }

  friend ostream &operator<<(ostream &os, const fraction &fr);

  fraction operator+(const fraction &fr) const {
    ll n = this->numel * fr.denom + fr.numel * this->denom;
    ll d = this->denom * fr.denom;
    return fraction(n, d);
  }
  fraction operator-(const fraction &fr) const {
    ll n = this->numel * fr.denom - fr.numel * this->denom;
    ll d = this->denom * fr.denom;
    return fraction(n, d);
  }
};

ostream &operator<<(ostream &os, const fraction &fr) {
  os << to_string(fr.numel);
  if (fr.denom != 1)
    os << "/" + to_string(fr.denom);
  return os;
}

fraction calc_dir(const vector<int> &dirs, int pos) {
  if (pos == dirs.size() - 1)
    return fraction(dirs[pos], 1);

  int denom = pow(2, dirs.size() - pos - 1);
  if (dirs[pos] == 0) {
    return calc_dir(dirs, pos + 1) - fraction(90, denom);
  }

  return calc_dir(dirs, pos + 1) + fraction(90, denom);
}

int main(void) {

  string s;
  while (cin >> s) {
    if (s == "#")
      break;

    int pos = 0;
    vector<int> dirs;
    while (pos < s.size()) {
      if (s.substr(pos, 5) == "north") {
        dirs.push_back(0);
        pos += 5;
      } else {
        // west
        dirs.push_back(90);
        pos += 4;
      }
    }

    fraction ret = calc_dir(dirs, 0);
    cout << ret << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

class fraction {
  int numel; // ??????
  int denom; // ??????

  map<int, int> prime_factor(int n) {
    map<int, int> res;
    for (int i = 2; i * i <= n; i++) {
      while (n % i == 0) {
        ++res[i];
        n /= i;
      }
    }
    if (n != 1)
      res[n] = 1;
    return res;
  }

  pair<int, int> reduce(int n, int d) {
    map<int, int> pfs = prime_factor(d);
    for (auto it = pfs.begin(); it != pfs.end(); it++) {
      while ((*it).second > 0 && n % (*it).first == 0) {
        n /= (*it).first;
        d /= (*it).first;
        (*it).second--;
      }
    }
    return pair<int, int>(n, d);
  }

public:
  fraction() {
    this->numel = 0;
    this->denom = 1;
  }
  fraction(string s) {
    int pos = 0;
    while (s[pos] != '/')
      pos++;
    int n = stoi(string(s.begin(), s.begin() + pos));
    int d = stoi(string(s.begin() + pos + 1, s.end()));

    pair<int, int> ret = reduce(n, d);
    this->numel = ret.first;
    this->denom = ret.second;
  }
  fraction(int n, int d) {
    pair<int, int> ret = reduce(n, d);
    this->numel = ret.first;
    this->denom = ret.second;
  }

  friend ostream &operator<<(ostream &os, const fraction &fr);

  fraction operator+(const fraction &fr) const {
    int n = this->numel * fr.denom + fr.numel * this->denom;
    int d = this->denom * fr.denom;
    return fraction(n, d);
  }
  fraction operator-(const fraction &fr) const {
    int n = this->numel * fr.denom - fr.numel * this->denom;
    int d = this->denom * fr.denom;
    return fraction(n, d);
  }
};

ostream &operator<<(ostream &os, const fraction &fr) {
  os << to_string(fr.numel);
  if (fr.denom != 1)
    os << "/" + to_string(fr.denom);
  return os;
}

fraction calc_dir(const vector<int> &dirs, int pos) {
  if (pos == dirs.size() - 1)
    return fraction(dirs[pos], 1);

  int denom = pow(2, dirs.size() - pos - 1);
  if (dirs[pos] == 0) {
    return calc_dir(dirs, pos + 1) - fraction(90, denom);
  }

  return calc_dir(dirs, pos + 1) + fraction(90, denom);
}

int main(void) {

  string s;
  while (cin >> s) {
    if (s == "#")
      break;

    int pos = 0;
    vector<int> dirs;
    while (pos < s.size()) {
      if (s.substr(pos, 5) == "north") {
        dirs.push_back(0);
        pos += 5;
      } else {
        // west
        dirs.push_back(90);
        pos += 4;
      }
    }

    fraction ret = calc_dir(dirs, 0);
    cout << ret << endl;
  }

  return 0;
}