alexjercan/bugnet huggingface dataset:
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

int main() {
  while (1) {
    string str;
    getline(cin, str);

    if (cin.eof())
      break;

    vector<bool> isnum;
    rep(i, str.size()) {
      if (i == str.size() - 1 || str[i + 1] == ' ') {
        char c = str[i];
        if (c >= '0' && c <= '9')
          isnum.push_back(true);
        else
          isnum.push_back(false);
      }
    }

    stringstream sst;
    sst << str;

    stack<double> t;
    rep(i, isnum.size()) {
      if (isnum[i]) {
        int a;
        sst >> a;
        t.push((double)a);

      } else {
        char c;
        sst >> c;

        double b = t.top();
        t.pop();
        double a = t.top();
        t.pop();

        if (c == '*') {
          t.push(a * b);
        }
        if (c == '+') {

          t.push(a + b);
        }
        if (c == '-') {

          t.push(a - b);
        }
        if (c == '/') {

          t.push(a / b);
        }
      }
    }

    printf("%.8lf\\n", t.top());
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

int main() {
  while (1) {
    string str;
    getline(cin, str);

    if (cin.eof())
      break;

    vector<bool> isnum;
    rep(i, str.size()) {
      if (i == 0 || str[i - 1] == ' ') {
        char c = str[i];
        if (c >= '0' && c <= '9')
          isnum.push_back(true);
        else
          isnum.push_back(false);
      }
    }

    stringstream sst;
    sst << str;

    stack<double> t;
    rep(i, isnum.size()) {
      if (isnum[i]) {
        int a;
        sst >> a;
        t.push((double)a);

      } else {
        char c;
        sst >> c;

        double b = t.top();
        t.pop();
        double a = t.top();
        t.pop();

        if (c == '*') {
          t.push(a * b);
        }
        if (c == '+') {

          t.push(a + b);
        }
        if (c == '-') {

          t.push(a - b);
        }
        if (c == '/') {

          t.push(a / b);
        }
      }
    }

    printf("%.8lf\\n", t.top());
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

int main() {
  string str;
  while (getline(cin, str)) {
    stack<double> S;
    stringstream sin(str);
    string temp;
    while (sin >> temp) {
      bool flag = false;
      if (temp.length() == 1) {
        flag = true;
        switch (temp[0]) {
          double a, b;
        case '+':
          a = S.top();
          S.pop();
          b = S.top();
          S.pop();
          S.push(b + a);
          break;
        case '-':
          a = S.top();
          S.pop();
          b = S.top();
          S.pop();
          S.push(b - a);
          break;
        case '*':
          a = S.top();
          S.pop();
          b = S.top();
          S.pop();
          S.push(b * a);
          break;
        case '/':
          a = S.top();
          S.pop();
          b = S.top();
          S.pop();
          S.push(b / a);
          break;
        default:
          flag = false;
        }
      }
      if (!flag) {
        stringstream s1(temp);
        double d;
        s1 >> d;
        S.push(d);
      }
    }
    printf("%.6f\\n", S.top());
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

int main() {
  string str;
  while (getline(cin, str)) {
    stack<double> S;
    stringstream sin(str);
    string temp;
    while (sin >> temp) {
      switch (temp[0]) {
        double a, b;
      case '+':
        a = S.top();
        S.pop();
        b = S.top();
        S.pop();
        S.push(b + a);
        break;
      case '-':
        a = S.top();
        S.pop();
        b = S.top();
        S.pop();
        S.push(b - a);
        break;
      case '*':
        a = S.top();
        S.pop();
        b = S.top();
        S.pop();
        S.push(b * a);
        break;
      case '/':
        a = S.top();
        S.pop();
        b = S.top();
        S.pop();
        S.push(b / a);
        break;
      default:
        stringstream s1(temp);
        double d;
        s1 >> d;
        S.push(d);
      }
    }
    printf("%.6f\\n", S.top());
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <stack>
using namespace std;

int main(void) {
  string str;
  while (getline(cin, str)) {
    stringstream ss(str);
    stack<double> num;
    while (ss >> str) {
      if (isdigit(*str.rbegin())) {
        num.push(atof(str.c_str()));
      } else {
        double b = num.top();
        num.pop();
        double a = num.top();
        num.pop();
        double res = 0;
        switch (str[0]) {
        case '+':
          res = a + b;
          break;
        case '-':
          res = a - b;
          break;
        case '*':
          res = a * b;
          break;
        case '/':
          res = a / b;
          break;
        }
        num.push(res);
      }
    }
    printf("%.6lf\\n", num.top());
    num.pop();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cctype>
#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;

typedef string::const_iterator State;
stack<double> num;

double number(State &begin) {
  double ret = 0;
  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }
  return ret;
}

void solve(State &begin) {
  double op1, op2;
  if (*begin == '+') {
    op2 = num.top();
    num.pop();
    op1 = num.top();
    num.pop();
    num.push(op1 + op2);
  } else if (*begin == '-') {
    op2 = num.top();
    num.pop();
    op1 = num.top();
    num.pop();
    num.push(op1 - op2);
  } else if (*begin == '*') {
    op2 = num.top();
    num.pop();
    op1 = num.top();
    num.pop();
    num.push(op1 * op2);
  } else if (*begin == '/') {
    op2 = num.top();
    num.pop();
    op1 = num.top();
    num.pop();
    num.push(op1 / op2);
  } else if (isdigit(*begin)) {
    num.push(number(begin));
  }
}

int main(void) {
  string str;
  while (getline(cin, str)) {
    State begin = str.begin();
    while (begin != str.end()) {
      solve(begin);
      begin++;
    }
    printf("%.6lf\\n", num.top());
    num.pop();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
using namespace std;

double a, b, n, z;
string s;
stack<double> stk;

void solve(char c) {
  b = stk.top();
  stk.pop();
  a = stk.top();
  stk.pop();

  if (c == '+') {
    n = a + b;
    stk.push(n);
  } else if (c == '-') {
    n = a - b;
    stk.push(n);
  } else if (c == '*') {
    n = a * b;
    stk.push(n);
  } else if (c == '/') {
    n = a / b;
    stk.push(n);
  }
}

int main() {

  while (getline(cin, s)) {
    z = 0;

    for (int i = 0; i < s.size(); i++) {
      if (s[i] >= '0' && s[i] <= '9') {
        if (z < 0) {
          z *= 10;
          z -= s[i] - '0';
        } else {
          z *= 10;
          z += s[i] - '0';
        }
        if (s[i - 1] == '-')
          z *= -1;
      }

      else if (s[i] == '-' && s[i + 1] >= '0' && s[i + 1] <= '9')
        ;

      else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
        solve(s[i]);

      else if (s[i] == ' ' && (s[i - 1] >= '0' && s[i - 1] <= '9')) {
        stk.push(z);
        z = 0;
      }
    }

    if (s.size() == 1) {
      z = s[0] - '0';
      printf("%.6f\\n", z);
    }

    else
      printf("%.6f\\n", stk.top());

    while (!stk.empty())
      stk.pop();
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
using namespace std;

double a, b, n, z;
string s;
stack<double> d;

void solve(char c) {
  a = d.top();
  d.pop();
  b = d.top();
  d.pop();

  if (c == '+') {
    n = b + a;
    d.push(n);
  } else if (c == '-') {
    n = b - a;
    d.push(n);
  } else if (c == '*') {
    n = b * a;
    d.push(n);
  } else if (c == '/') {
    n = b / a;
    d.push(n);
  }
}

int main() {

  while (getline(cin, s)) {
    z = 0;

    for (int i = 0; i < s.size(); i++) {
      if (s[i] >= '0' && s[i] <= '9') {
        z *= 10;
        z += s[i] - '0';
      } else if (s[i] == ' ' && (s[i - 1] >= '0' && s[i - 1] <= '9')) {
        d.push(z);
        z = 0;
      } else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
        solve(s[i]);
    }

    printf("%.7f\\n", d.top());

    while (!d.empty())
      d.pop();
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

int main() {
  long double a, b;

  string s, t;
  while (getline(cin, s) && s != "") {
    stringstream ss(s);
    stack<long double> S;
    while (ss >> t) {
      if (isdigit(t[0]) || t.size() >= 2) {
        S.push(atof(t.c_str()));
      } else {
        switch (t[0]) {
        case '+':
          b = S.top();
          S.pop();
          a = S.top();
          S.pop();
          S.push(a + b);
          break;
        case '-':
          b = S.top();
          S.pop();
          a = S.top();
          S.pop();
          S.push(a - b);
          break;
        case '*':
          b = S.top();
          S.pop();
          a = S.top();
          S.pop();
          S.push(a * b);
          break;
        case '/':
          b = S.top();
          S.pop();
          a = S.top();
          S.pop();
          if (a == 0)
            continue;
          S.push(a / b);
          break;
        default:
          exit(0);
        }
      }
    }
    printf("%.6Lf\\n", S.top());
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

int main() {
  long double a, b;

  string s, t;
  while (getline(cin, s), s != "") {
    stringstream ss(s);
    stack<long double> S;
    while (ss >> t) {
      if (isdigit(t[0])) {
        S.push(atof(t.c_str()));
      } else {
        switch (t[0]) {
        case '+':
          b = S.top();
          S.pop();
          a = S.top();
          S.pop();
          S.push(a + b);
          break;
        case '-':
          b = S.top();
          S.pop();
          a = S.top();
          S.pop();
          S.push(a - b);
          break;
        case '*':
          b = S.top();
          S.pop();
          a = S.top();
          S.pop();
          S.push(a * b);
          break;
        case '/':
          b = S.top();
          S.pop();
          a = S.top();
          S.pop();
          if (a == 0)
            continue;
          S.push(a / b);
          break;
        default:
          exit(0);
        }
      }
    }
    printf("%.6Lf\\n", S.top());
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define lol(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
#include <iomanip>
#include <stack>
double Z(string s) {
  if (s.size() == 1) {
    if (s[0] == '+')
      return 0.1;
    if (s[0] == '-')
      return 0.2;
    if (s[0] == '*')
      return 0.3;
    if (s[0] == '/')
      return 0.4;
    return s[0] - '0' + 0.0;
  }
  double tmp = (s[0] == '-' ? 0 : s[0] - '0') + 0.0;
  for (int i = 1; i < s.size(); i++) {
    tmp *= 10;
    if (tmp <= 0)
      tmp -= s[i] - '0' + 0.0;
    else
      tmp += s[i] - '0' + 0.0;
  }
  return tmp;
}
int main() {
  string s;
  while (getline(cin, s)) {
    stack<double> st;
    s.push_back(' ');
    int left = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == ' ') {
        double tmp = Z(s.substr(left, i - left));
        if (0 < tmp && tmp < 1) {
          double b = st.top();
          st.pop();
          double a = st.top();
          st.pop();
          if (tmp == 0.1)
            st.push(a + b);
          if (tmp == 0.2)
            st.push(a - b);
          if (tmp == 0.3)
            st.push(a * b);
          if (tmp == 0.4)
            st.push(a / b);
        } else
          st.push(tmp);
        left = i + 1;
      }
    }
    cout << fixed << setprecision(10) << st.top() << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string a;
  double num[100];
  while (getline(cin, a)) {
    int keta = 0;
    int numtop = 0;
    for (int i = 0; i < a.size(); i++) {
      if (a[i] >= '0' && a[i] <= '9')
        keta++;
      if (a[i] == ' ' && a[i - 1] >= '0' && a[i - 1] <= '9') {
        num[numtop] = 0;
        int j = 0, k = 1;
        while (true) {
          num[numtop] += k * (a[i - j - 1] - '0');
          k *= 10;
          j++;
          if (j == keta)
            break;
        }
        cout << "#" << num[numtop] << endl;
        numtop++;
        keta = 0;
      }
      if (a[i] == '+') {
        num[numtop - 2] += num[numtop - 1];
        num[numtop - 1] = 0;
        numtop--;
      }
      if (a[i] == '-') {
        num[numtop - 2] -= num[numtop - 1];
        num[numtop - 1] = 0;
        numtop--;
      }
      if (a[i] == '*') {
        num[numtop - 2] *= num[numtop - 1];
        num[numtop - 1] = 0;
        numtop--;
      }
      if (a[i] == '/') {
        num[numtop - 2] /= num[numtop - 1];
        num[numtop - 1] = 0;
        numtop--;
      }
    }
    cout << fixed << setprecision(10) << num[0] << endl;
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

vector<string> split(string in, char sp = ' ') {
  vector<string> ret;
  stringstream ss(in);
  string s;
  while (getline(ss, s, sp)) {
    ret.emplace_back(s);
  }
  return ret;
}

int main() {
  string in;
  while (getline(cin, in)) {
    vector<double> st;
    vector<string> v = split(in);

    for (auto s : v) {
      if (s.size() == 1 and not isdigit(s.front())) {
        double b = st.back();
        st.pop_back();
        double a = st.back();
        st.pop_back();
        switch (s[0]) {
        case '+':
          st.emplace_back(a + b);
          break;
        case '-':
          st.emplace_back(a - b);
          break;
        case '*':
          st.emplace_back(a * b);
          break;
        case '/':
          st.emplace_back(a / b);
          break;
        }
      } else {
        st.emplace_back(stod(s));
      }
    }
    cout << fixed << setprecision(7) << st.front() << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define itrep(it, a) for (it = (a).begin(); it != (a).end(); it++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 30)
using namespace std;

//?????????????????°???????????????
double changeNum(string str) {
  double num = 0, K = 1;
  for (int i = str.size() - 1; i >= 0; i--) {
    num += (str[i] - '0') * K;
    K *= 10;
  }
  return num;
}

void solve(stack<double> &st, char op) {
  double a, b;
  if (st.size() < 2)
    return;
  b = st.top();
  st.pop();
  a = st.top();
  st.pop();

  if (op == '+')
    st.push(a + b);
  else if (op == '-')
    st.push(a - b);
  else if (op == '/')
    st.push(a / b);
  else
    st.push(a * b);
}

int main() {
  string str;
  while (getline(cin, str)) {
    str += ' ';
    stack<double> st;
    string input;
    rep(i, str.size()) {
      if (str[i] == ' ') {
        if (input[0] >= '0' && input[0] <= '9') {
          st.push(changeNum(input));
        } else {
          solve(st, input[0]);
        }
        input = "";
      } else {
        input += str[i];
      }
    }
    cout << st.top() << endl;
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>

double calc(char op, double a, double b) {
  switch (op) {
  case '+':
    return a + b;
  case '-':
    return a - b;
  case '*':
    return a * b;
  case '/':
    return a / b;
  }
}

int main() {
  std::string str;
  while (std::getline(std::cin, str)) {
    std::stack<double> s;
    std::istringstream iss(str);
    std::string item;

    while (iss >> item) {
      if (item == "+" || item == "-" || item == "*" || item == "/") {
        double b = s.top();
        s.pop();
        double a = s.top();
        s.pop();

        s.push(calc(item[0], a, b));
      } else
        s.push(atof(item.c_str()));
    }

    std::cout << std::fixed << std::setprecision(6) << s.top() << std::endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>

double calc(char op, double a, double b) {
  switch (op) {
  case '+':
    return a + b;
  case '-':
    return a - b;
  case '*':
    return a * b;
  case '/':
    return a / b;
  }
}

int main() {
  std::string str;
  while (std::getline(std::cin, str)) {
    std::stack<double> s;
    std::istringstream iss(str);
    std::string item;

    while (iss >> item) {
      if (item[0] == '+' || item[0] == '-' || item[0] == '*' ||
          item[0] == '/') {
        double b = s.top();
        s.pop();
        double a = s.top();
        s.pop();

        s.push(calc(item[0], a, b));
      } else
        s.push(atof(item.c_str()));
    }

    std::cout << std::fixed << std::setprecision(6) << s.top() << std::endl;
  }

  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

void solve() {
  string str;
  string a;
  while (getline(cin, str)) {
    stack<double> Stack;
    stringstream ss;
    ss << str;
    while (ss >> a) {
      if (a == "+" || a == "-" || a == "*" || a == "/") {
        double num[2];
        num[0] = Stack.top();
        Stack.pop();
        num[1] = Stack.top();
        Stack.pop();
        if (a == "+") {
          Stack.push(num[1] + num[0]);
        } else if (a == "-") {
          Stack.push(num[1] - num[0]);
        } else if (a == "*") {
          Stack.push(num[1] * num[0]);
        } else if (a == "/") {
          Stack.push(num[1] / num[0]);
        }
      } else {
        Stack.push(atof(a.c_str()));
      }
    }
    printf("%.6f\\n", Stack.top());
  }
}

int main() {
  solve();
  return (0);
}

Error: -11

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

void solve() {
  string str;
  while (getline(cin, str)) {
    stack<double> Stack;
    char a[100];
    int size = str.size();
    int pos = 0;
    for (int i = 0; i < size; ++i) {
      if (str[i] == ' ') {
        if (a[0] == NULL) {
          continue;
        }
        int n = atoi(a);
        for (int i = pos; i >= 0; --i) {
          a[i] = NULL;
        }
        Stack.push(n);
        pos = 0;
      } else if (str[i] == '+' || str[i] == '-' || str[i] == '*' ||
                 str[i] == '/') {
        double num[2];
        num[0] = Stack.top();
        Stack.pop();
        num[1] = Stack.top();
        Stack.pop();
        if (str[i] == '+') {
          Stack.push(num[1] + num[0]);
        } else if (str[i] == '-') {
          Stack.push(num[1] - num[0]);
        } else if (str[i] == '*') {
          Stack.push(num[1] * num[0]);
        } else if (str[i] == '/') {
          Stack.push(num[1] / num[0]);
        }
      } else {
        a[pos] = str[i];
        pos++;
      }
    }
    printf("%.6f\\n", Stack.top());
  }
}

int main() {
  solve();
  return (0);
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
  long i, j, k;
  string s;
  double a[80];
  for (;;) {
    getline(cin, s);
    if (s == "")
      break;
    i = j = 0;
    for (;;) {
      while (s[j] == ' ')
        j++;
      if (s[j] == 0)
        break;
      else if (s[j] == '+') {
        i--;
        a[i] += a[i + 1];
        j++;
      } else if (s[j] == '-' && (s[j + 1] == ' ' || s[j + 1] == 0)) {
        i--;
        a[i] -= a[i + 1];
        j++;
      } else if (s[j] == '*') {
        i--;
        a[i] *= a[i + 1];
        j++;
      } else if (s[j] == '/') {
        i--;
        a[i] /= a[i + 1];
        j++;
      } else {
        k = s.find(' ', j);
        if (k == string::npos)
          k = s.size();
        i++;
        a[i] = atof(s.substr(j, k - j).c_str());
        j = k;
      }
    }
    printf("%.6f\\n", a[1]);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
  long i, j, k;
  string s;
  double a[80];
  for (;;) {
    getline(cin, s);
    if (s == "")
      break;
    i = -1;
    j = 0;
    for (;;) {
      while (s[j] == ' ')
        j++;
      if (s[j] == 0)
        break;
      else if (s[j] == '+') {
        i--;
        a[i] += a[i + 1];
        j++;
      } else if (s[j] == '-') {
        i--;
        a[i] -= a[i + 1];
        j++;
      } else if (s[j] == '*') {
        i--;
        a[i] *= a[i + 1];
        j++;
      } else if (s[j] == '/') {
        i--;
        a[i] /= a[i + 1];
        j++;
      } else {
        k = s.find(' ', j);
        i++;
        a[i] = atof(s.substr(j, k - j).c_str());
        j = k + 1;
      }
    }
    printf("%.5f\\n", a[0]);
  }
  return 0;
}