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

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

int main() {
  while (1) {
    stack<double> st;
    string s;
    if (!getline(cin, s))
      break;

    for (int i = 0; i < s.size(); i++) {
      while (s[i] == ' ' && i < s.size())
        i++;
      if ('0' <= s[i] && s[i] <= '9') {
        double n = 0;
        while ('0' <= s[i] && s[i] <= '9') {
          n *= 10;
          n += s[i] - '0';
          i++;
        }
        st.push(n);
      }
      if (s[i] == '-') {
        if ('0' <= s[i + 1] && s[i + 1] <= '9') {
          i++;
          double n = 0;
          while ('0' <= s[i] && s[i] <= '9') {
            n *= 10;
            n += s[i] - '0';
            i++;
          }
          st.push(-n);
        } else {
          double b = st.top();
          st.pop();
          double a = st.top();
          st.pop();
          st.push(a - b);
        }
      }
      if (s[i] == '+' || s[i] == '*' || s[i] == '/') {
        double b = st.top();
        st.pop();
        double a = st.top();
        st.pop();
        switch (s[i]) {
        case '+':
          st.push(a + b);
          break;
        case '*':
          st.push(a * b);
          break;
        case '/':
          st.push(a / b);
          break;
        }
      }
    }
    if (!st.empty())
      printf("%.7lf\\n", st.top());
  }
}

Error: -11

Fixed submission:

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

int main() {
  while (1) {
    stack<double> st;
    string s;
    if (!getline(cin, s))
      break;

    for (int i = 0; i < s.size(); i++) {
      while (s[i] == ' ' && i < s.size())
        i++;
      if ('0' <= s[i] && s[i] <= '9') {
        double n = 0;
        while ('0' <= s[i] && s[i] <= '9') {
          n *= 10;
          n += s[i] - '0';
          i++;
        }
        st.push(n);
      }
      if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
        double b = st.top();
        st.pop();
        double a = st.top();
        st.pop();
        switch (s[i]) {
        case '+':
          st.push(a + b);
          break;
        case '-':
          st.push(a - b);
          break;
        case '*':
          st.push(a * b);
          break;
        case '/':
          st.push(a / b);
          break;
        }
      }
    }
    if (!st.empty())
      printf("%.7lf\\n", st.top());
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <stack>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(int argc, char const *argv[]) {
  char str[81];
  stack<double> data;
  double a, b;

  while (cin.getline(str, 80)) {
    char *t;

    while (data.empty() == false)
      data.pop();

    t = strtok(str, " ");
    if (strcmp(t, "+") == 0) {
      a = data.top();
      data.pop();
      b = data.top();
      data.pop();
      data.push(b + a);
    } else if (strcmp(t, "-") == 0) {
      a = data.top();
      data.pop();
      b = data.top();
      data.pop();
      data.push(b - a);
    } else if (strcmp(t, "/") == 0) {
      a = data.top();
      data.pop();
      b = data.top();
      data.pop();
      data.push(b / a);
    } else if ((strcmp(t, "*") == 0)) {
      a = data.top();
      data.pop();
      b = data.top();
      data.pop();
      data.push(b * a);
    } else {
      data.push(atof(t));
    }

    while ((t = strtok(NULL, " ")) != NULL) {
      if (strcmp(t, "+") == 0) {
        a = data.top();
        data.pop();
        b = data.top();
        data.pop();
        data.push(b + a);
      } else if (strcmp(t, "-") == 0) {
        a = data.top();
        data.pop();
        b = data.top();
        data.pop();
        data.push(b - a);
      } else if (strcmp(t, "/") == 0) {
        a = data.top();
        data.pop();
        b = data.top();
        data.pop();
        data.push(b / a);
      } else if ((strcmp(t, "*") == 0)) {
        a = data.top();
        data.pop();
        b = data.top();
        data.pop();
        data.push(b * a);
      } else {
        data.push(atof(t));
      }
    }
    printf("%.6f\\n", data.top());
    data.pop();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <stack>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(int argc, char const *argv[]) {
  char str[81];
  stack<double> data;
  double a, b;

  while (cin.getline(str, 80)) {
    char *t;

    while (data.empty() == false)
      data.pop();

    t = strtok(str, " ");
    if (t[0] < '0' || t[0] > '9') {
      a = data.top();
      data.pop();
      b = data.top();
      data.pop();
      if (t[0] == '+') {
        data.push(b + a);
      }
      if (t[0] == '-') {
        data.push(b - a);
      }
      if (t[0] == '/') {
        data.push(b / a);
      }
      if (t[0] == '*') {
        data.push(b * a);
      }
    } else {
      data.push(atof(t));
    }

    while ((t = strtok(NULL, " ")) != NULL) {
      if (t[0] < '0' || t[0] > '9') {
        a = data.top();
        data.pop();
        b = data.top();
        data.pop();
        if (t[0] == '+') {
          data.push(b + a);
        }
        if (t[0] == '-') {
          data.push(b - a);
        }
        if (t[0] == '/') {
          data.push(b / a);
        }
        if (t[0] == '*') {
          data.push(b * a);
        }
      } else {
        data.push(atof(t));
      }
    }
    printf("%.6f\\n", data.top());
    data.pop();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
int main(void) {

  string str;
  while (getline(cin, str)) {
    double ans = 0;
    stack<double> s;
    stringstream ss(str);
    string tmp;
    while (ss >> tmp) {
      stringstream s1(tmp);
      stringstream s2(tmp);
      int n;
      char c;
      if (s1 >> n) {
        s.push(n);
      } else if (s2 >> c) {

        double y = s.top();
        s.pop();
        double x = s.top();
        s.pop();
        if (c == '+') {
          s.push(x + y);
        } else if (c == '-') {
          s.push(x - y);
        } else if (c == '*') {
          s.push(x * y);
        } else if (c == '/') {
          s.push(x / y);
        }
      }
    }
    ans = s.top();
    s.pop();
    printf("%.6lf\\n", ans);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main(void) {

  char str[100] = "";
  string tmp;
  while (getline(cin, tmp)) {
    for (int i = 0; i < tmp.size(); i++) {
      str[i] = tmp[i];
    }
    stack<double> s;
    for (int i = 0; i < tmp.size(); i++) {
      if ('0' <= str[i] && str[i] <= '9') {
        double x = 0;
        for (int j = i;; j++) {
          if (j >= tmp.size()) {
            i = j;
            s.push(x);
            break;
          } else if (str[j] < '0' || '9' < str[j]) {
            i = j;
            s.push(x);
            break;
          } else {
            x *= 10;
            x += str[j] - '0';
          }
        }

      } else if (str[i] == '+' || str[i] == '-' || str[i] == '*' ||
                 str[i] == '/') {
        double y = s.top();
        s.pop();
        double x = s.top();
        s.pop();
        if (str[i] == '+') {
          s.push(x + y);
        } else if (str[i] == '-') {
          s.push(x - y);
        } else if (str[i] == '*') {
          s.push(x * y);
        } else if (str[i] == '/') {
          s.push(x / y);
        }
      }
    }
    double ans = s.top();
    s.pop();
    printf("%.6lf\\n", ans);
  }

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

#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;

int main() {
  string str;
  while (getline(cin, str)) {
    stack<double> stf;
    stringstream ss(str);
    string str0;
    while (ss >> str0) {
      double a, b;
      if (str0 == "+") {
        b = stf.top();
        stf.pop();
        a = stf.top();
        stf.pop();
        stf.push(a + b);
      } else if (str0 == "-") {
        b = stf.top();
        stf.pop();
        a = stf.top();
        stf.pop();
        stf.push(a - b);
      } else if (str0 == "*") {
        b = stf.top();
        stf.pop();
        a = stf.top();
        stf.pop();
        stf.push(a * b);
      } else if (str0 == "/") {
        b = stf.top();
        stf.pop();
        a = stf.top();
        stf.pop();
        stf.push(a / b);
      } else {
        stf.push(stod(str0));
      }
    }
    cout << fixed << setprecision(6) << stf.top() << endl;
    stf.pop();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;

int main() {
  while (true) {
    string str;
    stack<float> stf;
    getline(cin, str);
    stringstream ss(str);
    string str0;
    while (ss >> str0) {
      float a, b;
      if (str0 == "+") {
        b = stf.top();
        stf.pop();
        a = stf.top();
        stf.pop();
        stf.push(a + b);
      } else if (str0 == "-") {
        b = stf.top();
        stf.pop();
        a = stf.top();
        stf.pop();
        stf.push(a - b);
      } else if (str0 == "*") {
        b = stf.top();
        stf.pop();
        a = stf.top();
        stf.pop();
        stf.push(a * b);
      } else if (str0 == "/") {
        b = stf.top();
        stf.pop();
        a = stf.top();
        stf.pop();
        stf.push(a / b);
      } else {
        stf.push(stof(str0));
      }
    }
    cout << fixed << setprecision(6) << stf.top() << endl;
    stf.pop();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

double calc(double a, double b, string op) {
  if (op == "+")
    return a + b;
  if (op == "-")
    return a - b;
  if (op == "*")
    return a * b;
  if (op == "/")
    return a / b;
}

int main() {
  string str;
  double a, b;
  while (getline(cin, str)) {
    stack<double> st;
    stringstream ss;
    ss << str;
    while (ss >> str) {
      if (str == "+" || str == "-" || str == "*" || str == "/") {
        a = st.top();
        st.pop();
        b = st.top();
        st.pop();
        st.push(calc(b, a, str));

      } else {
        st.push(atoi(str.c_str()));
      }
    }
    double ans = st.top();
    st.pop();
    printf("%0.6f\\n", ans);
  }
  return 0;
}

Error: WA

Fixed submission:

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

double calc(double a, double b, char op) {
  if (op == '+')
    return a + b;
  if (op == '-')
    return a - b;
  if (op == '*')
    return a * b;
  if (op == '/')
    return a / b;
}

int main() {
  string str;
  double a, b;
  int tmp;
  while (getline(cin, str)) {
    deque<double> num;
    for (int i = 0; i < str.size(); i++) {
      if (str[i] == ' ')
        continue;

      if (isdigit(str[i])) {
        tmp = 1;
        tmp *= str[i] - '0';
        while (str[i + 1] != ' ') {
          i++;
          tmp *= 10;
          tmp += str[i] - '0';
        }
        num.push_front(tmp);
      }

      else {
        a = num.front();
        num.pop_front();
        b = num.front();
        num.pop_front();
        double res = calc(b, a, str[i]);
        num.push_front(res);
      }
    }
    double ans = num.front();
    printf("%.6f\\n", ans);
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <ctype.h>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

double sum(double a, double b) { return a + b; }
double dif(double a, double b) { return a - b; }
double mul(double a, double b) { return a * b; }
double div(double a, double b) { return a / b; }
double (*ops[])(double, double) = {sum, dif, mul, div};

int main() {
  int i;
  char in[256], *ind, op[] = {'+', '-', '*', '/'};
  stack<double> st;
  double p1, p2;

  while (fgets(in, 256, stdin) != NULL) {
    for (ind = strtok(in, " "); ind != NULL; ind = strtok(NULL, " ")) {
      for (i = 0; i < 4; i++)
        if (op[i] == ind[0] && !isdigit(ind[1])) {
          p1 = st.top();
          st.pop();
          p2 = st.top();
          st.pop();
          st.push(ops[i](p2, p1));
          goto jump;
        }
      st.push(atof(ind));
    jump:;
    }
    printf("%.6lf\\n", st.top());
    st.pop();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

double sum(double a, double b) { return a + b; }
double dif(double a, double b) { return a - b; }
double mul(double a, double b) { return a * b; }
double div(double a, double b) { return a / b; }
double (*ops[])(double, double) = {sum, dif, mul, div};

int main() {
  int i;
  char in[256], *ind, op[] = {'+', '-', '*', '/'};
  stack<double> st;
  double p1, p2;

  while (fgets(in, 256, stdin) != NULL) {
    for (ind = strtok(in, " "); ind != NULL; ind = strtok(NULL, " ")) {
      for (i = 0; i < 4; i++)
        if (op[i] == ind[0]) {
          p1 = st.top();
          st.pop();
          p2 = st.top();
          st.pop();
          st.push(ops[i](p2, p1));
          goto jump;
        }
      st.push(atof(ind));
    jump:;
    }
    printf("%lf\\n", st.top());
    st.pop();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  double a, b;
  string str;
  vector<double> st;
  while (cin >> str) {
    if (str == "+") {
      b = st.back();
      st.pop_back();
      a = st.back();
      st.pop_back();
      st.push_back(a + b);
    } else if (str == "-") {
      b = st.back();
      st.pop_back();
      a = st.back();
      st.pop_back();
      st.push_back(a - b);
    } else if (str == "*") {
      b = st.back();
      st.pop_back();
      a = st.back();
      st.pop_back();
      st.push_back(a * b);
    } else if (str == "/") {
      b = st.back();
      st.pop_back();
      a = st.back();
      st.pop_back();
      st.push_back(a / b);
    } else
      st.push_back(stof(str));
  }
  for (auto i : st)
    cout << fixed << setprecision(6) << i << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  double a, b;
  char ch;
  stack<double> st;
  while ((ch = getchar()) != EOF) {
    if (ch == '+') {
      b = st.top();
      st.pop();
      a = st.top();
      st.pop();
      st.push(a + b);
    } else if (ch == '-') {
      ch = getchar();
      if (ch == EOF) {
        b = st.top();
        st.pop();
        a = st.top();
        st.pop();
        st.push(a - b);
        printf("%.6f\\n", st.top());
        st.pop();
        break;
      }
      if (isdigit(ch)) {
        ungetc(ch, stdin);
        ungetc('-', stdin);
        scanf("%d", &n);
        st.push((double)n);
      } else {
        b = st.top();
        st.pop();
        a = st.top();
        st.pop();
        st.push(a - b);
      }
    } else if (ch == '*') {
      b = st.top();
      st.pop();
      a = st.top();
      st.pop();
      st.push(a * b);
    } else if (ch == '/') {
      b = st.top();
      st.pop();
      a = st.top();
      st.pop();
      st.push(a / b);
    } else if (isdigit(ch)) {
      ungetc(ch, stdin);
      scanf("%d", &n);
      st.push((double)n);
    } else if (ch != ' ') {
      printf("%.6f\\n", st.top());
      st.pop();
    }
  }
  if (st.size() > 1)
    for (int i = 0; i < 10000; i++)
      st.pop();
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

// Name: Strange Mathematical Expression
// Level: 1
// Category: 逆ポーランド記法,やるだけ
// Note:

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

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  string line;
  while (getline(cin, line) && !cin.eof()) {
    istringstream is(line);
    stack<double> stk;
    string token;
    while (is >> token) {
      if (isdigit(token[0]) || token.size() >= 2) {
        stk.push(atof(token.c_str()));
      } else {
        double b = stk.top();
        stk.pop();
        double a = stk.top();
        stk.pop();
        switch (token[0]) {
        case '+':
          stk.push(a + b);
          break;
        case '-':
          stk.push(a - b);
          break;
        case '*':
          stk.push(a * b);
          break;
        case '/':
          stk.push(a / b);
          break;
        }
      }
    }
    printf("%.10f\\n", stk.top());
  }
  return 0;
}

Error: -11

Fixed submission:

// Name: Strange Mathematical Expression
// Level: 1
// Category: 逆ポーランド記法,やるだけ
// Note:

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

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  string line;
  while (getline(cin, line) && !cin.eof()) {
    istringstream is(line);
    stack<double> stk;
    string token;
    while (is >> token) {
      if (isdigit(token[0])) {
        stk.push(atof(token.c_str()));
      } else {
        double b = stk.top();
        stk.pop();
        double a = stk.top();
        stk.pop();
        switch (token[0]) {
        case '+':
          stk.push(a + b);
          break;
        case '-':
          stk.push(a - b);
          break;
        case '*':
          stk.push(a * b);
          break;
        case '/':
          stk.push(a / b);
          break;
        }
      }
    }
    printf("%.10f\\n", stk.top());
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

stack<double> st;

double stod(string s) {
  double ret = 0.0;
  bool flag = false;
  if (s[0] == '-')
    flag = true;
  for (int p = (flag ? 1 : 0); p < s.length(); p++) {
    ret = ret * 10.0 + (double)(s[p] - '0');
  }
  if (flag)
    return -ret;
  else
    ret;
}

int main() {
  string s;
  while (getline(cin, s)) {
    stringstream ss(s);
    string t;
    while (getline(ss, t, ' ')) {
      if (t == "+") {
        double c1 = st.top();
        st.pop();
        double c2 = st.top();
        st.pop();
        st.push(c2 + c1);
      } else if (t == "-") {
        double c1 = st.top();
        st.pop();
        double c2 = st.top();
        st.pop();
        st.push(c2 - c1);
      } else if (t == "*") {
        double c1 = st.top();
        st.pop();
        double c2 = st.top();
        st.pop();
        st.push(c2 * c1);
      } else if (t == "/") {
        double c1 = st.top();
        st.pop();
        double c2 = st.top();
        st.pop();
        st.push(c2 / c1);
      } else {
        st.push(stod(t));
      }
    }
    printf("%.15f\\n", st.top());
    while (!st.empty())
      st.pop();
  }
  return 0;
}

Error: -11

Fixed submission:

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

stack<double> st;

int main() {
  string s;
  bool flag = false;
  while (getline(cin, s)) {
    double tmp = 0.0;
    double c1, c2;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == ' ') {
        if (flag) {
          st.push(tmp);
          tmp = 0.0;
        }
        flag = false;
      } else if ('0' <= s[i] && s[i] <= '9') {
        tmp = tmp * 10 + (s[i] - '0');
        flag = true;
      } else {
        c1 = st.top();
        st.pop();
        c2 = st.top();
        st.pop();
        if (s[i] == '+')
          st.push(c1 + c2);
        if (s[i] == '-')
          st.push(c2 - c1);
        if (s[i] == '*')
          st.push(c1 * c2);
        if (s[i] == '/')
          st.push(c2 / c1);
      }
    }
    printf("%.8f\\n", st.top());
    while (!st.empty())
      st.pop();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  string line;
  while (getline(cin, line)) {
    char str[81];
    strcpy(str, line.c_str());
    char *p;
    p = strtok(str, " ");
    stack<double> sk;
    while (p) {
      if (strlen(p) == 1 && !isdigit(*p)) {
        double tmp2 = sk.top();
        sk.pop();
        double tmp1 = sk.top();
        sk.pop();
        if (*p == '+')
          sk.push(tmp1 + tmp2);
        else if (*p == '-')
          sk.push(tmp1 - tmp2);
        else if (*p == '*')
          sk.push(tmp1 * tmp2);
        else if (*p == '/')
          sk.push(tmp1 / tmp2);
      } else
        sk.push(atof(p));
      p = strtok(NULL, " ");
    }
    printf("%.6lf\\n", sk.top());
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  string line;
  while (getline(cin, line)) {
    char str[81];
    strcpy(str, line.c_str());
    char *p;
    p = strtok(str, " ");
    stack<double> sk;
    while (p) {
      if (!isdigit(*p)) {
        double tmp2 = sk.top();
        sk.pop();
        double tmp1 = sk.top();
        sk.pop();
        if (*p == '+')
          sk.push(tmp1 + tmp2);
        else if (*p == '-')
          sk.push(tmp1 - tmp2);
        else if (*p == '*')
          sk.push(tmp1 * tmp2);
        else if (*p == '/')
          sk.push(tmp1 / tmp2);
      } else
        sk.push(atof(p));
      p = strtok(NULL, " ");
    }
    printf("%.6lf\\n", sk.top());
  }
}