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

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

char in[100];
int main() {
  while (cin.getline(in, 99) && in[0]) {
    stack<long double> s;
    char *c = in;
    do {
      if ('0' <= *c && *c <= '9' || *c == '-' && '0' < c[1] && c[1] <= '9') {
        int a = 0;
        bool minus = false;
        if (c[0] == '-') {
          minus = true;
          ++c;
        }
        while ('0' <= *c && *c <= '9') {
          a *= 10;
          a += *c - '0';
          ++c;
        }
        if (minus)
          a = -a;
        s.push((long double)a);
      } else {
        long double b = s.top();
        s.pop();
        long double a = s.top();
        s.pop();
        switch (*c) {
        case '+':
          s.push(a + b);
          break;
        case '-':
          s.push(a - b);
          break;
        case '*':
          s.push(a * b);
          break;
        case '/':
          s.push(a / b);
          break;
        }
        ++c;
      }
      ++c;
    } while (*c);
    printf("%.6f\\n", (double)s.top());
    memset(in, 0, sizeof in);
  }
}

Error: -11

Fixed submission:

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

char in[100];
int main() {
  while (cin.getline(in, 99) && in[0]) {
    stack<double> s;
    char *c = in;
    do {
      if ('0' <= *c && *c <= '9') {
        int a = 0;
        while ('0' <= *c && *c <= '9') {
          a *= 10;
          a += *c - '0';
          ++c;
        }
        s.push((double)a);
      } else {
        double b = s.top();
        s.pop();
        double a = s.top();
        s.pop();
        switch (*c) {
        case '+':
          s.push(a + b);
          break;
        case '-':
          s.push(a - b);
          break;
        case '*':
          s.push(a * b);
          break;
        case '/':
          s.push(a / b);
          break;
        }
        ++c;
      }
      ++c;
    } while (*c);
    while (s.empty())
      ;
    printf("%.6f\\n", s.top());
    memset(in, 0, sizeof in);
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <stack>

using namespace std;

bool isope(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; }

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

int main() {
  while (1) {
    stack<double> st;

    while (1) {
      char s[16];
      int f = scanf("%s", s);
      if (!~f)
        return 0;

      if (s[1] == '\\0' && isope(s[0])) {
        double b = st.top();
        st.pop();
        double a = st.top();
        st.pop();
        st.push(calc(a, s[0], b));
      } else {
        double a;
        sscanf(s, "%lf", &a);
        st.push(a);
      }

      if (getchar() == '\\n')
        break;
    }

    printf("%f\\n", st.top());
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <stack>

using namespace std;

bool isope(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; }

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

int main() {
  while (1) {
    stack<double> st;

    while (1) {
      char s[16];
      int f = scanf("%s", s);
      if (!~f)
        return 0;

      if (isope(s[0])) {
        double b = st.top();
        st.pop();
        double a = st.top();
        st.pop();
        st.push(calc(a, s[0], b));
      } else {
        double a;
        sscanf(s, "%lf", &a);
        st.push(a);
      }

      if (getchar() == '\\n')
        break;
    }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  char buf[256];
  while (cin.getline(buf, sizeof(buf))) {
    vector<string> s;
    string S = buf;
    int sc = 0;
    S += " ";

    for (int i = 0; i < (int)S.size(); i++) {
      if (S[i] == ' ') {
        s.push_back(S.substr(sc, i - sc));
        sc = i + 1;
      }
    }
    stack<double> st;
    for (int i = 0; i < (int)s.size(); i++) {
      if (s[i] == "-" || s[i] == "+" || s[i] == "/" || s[i] == "*") {
        double k1 = st.top();
        st.pop();
        double k2 = st.top();
        st.pop();
        if (s[i] == "+")
          st.push(k2 + k1);
        else if (s[i] == "-")
          st.push(k2 - k1);
        else if (s[i] == "/")
          st.push(k2 / k1);
        else if (s[i] == "*")
          st.push(k2 * k1);
      } else {
        double c;
        sscanf(s[i].c_str(), "%lf", &c);
        st.push(c);
      }
    }
    printf("%.6lf\\n", st.top());
    while (!st.empty())
      st.pop();
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  char buf[256];
  while (cin.getline(buf, sizeof(buf))) {
    vector<string> s;
    string S = buf;
    int sc = 0;
    S += " ";

    for (int i = 0; i < (int)S.size(); i++) {
      if (S[i] == ' ') {
        s.push_back(S.substr(sc, i - sc));
        sc = i + 1;
      }
    }
    stack<double> st;
    for (int i = 0; i < (int)s.size(); i++) {
      if ('9' >= s[i][0] && s[i][0] >= '0') {
        double c;
        sscanf(s[i].c_str(), "%lf", &c);
        st.push(c);
      } else {
        double k1 = st.top();
        st.pop();
        double k2 = st.top();
        st.pop();
        if (s[i] == "+")
          st.push(k2 + k1);
        else if (s[i] == "-")
          st.push(k2 - k1);
        else if (s[i] == "/")
          st.push(k2 / k1);
        else if (s[i] == "*")
          st.push(k2 * k1);
      }
    }
    printf("%.6lf\\n", st.top());
    while (!st.empty())
      st.pop();
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

int s2i(string s) {
  stringstream ss(s);
  int d;
  ss >> d;
  return d;
}

int main() {
  string s;
  double a, b, x;
  while (getline(cin, s)) {
    stack<double> X;
    stringstream ss(s);
    while (ss >> s) {
      if (s == "+" || s == "-" || s == "*" || s == "/") {
        b = X.top();
        X.pop();
        a = X.top();
        X.pop();
        if (s == "+")
          x = a + b;
        else if (s == "-")
          x = a - b;
        else if (s == "*")
          x = a * b;
        else if (s == "/")
          x = a / b;
        X.push(x);
      } else {
        X.push(s2i(s));
      }
    }
    printf("%.6f\\n", X.top());
  }
}

Error: -11

Fixed submission:

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

double s2d(string s) {
  stringstream ss(s);
  double d;
  ss >> d;
  return d;
}

int main() {
  string s;
  double a, b, x;
  while (getline(cin, s)) {
    stack<double> X;
    stringstream ss(s);
    while (ss >> s) {
      char c = s.c_str()[0];
      if (c < '0') {
        b = X.top();
        X.pop();
        a = X.top();
        X.pop();
        if (c == '+')
          x = a + b;
        else if (c == '-')
          x = a - b;
        else if (c == '*')
          x = a * b;
        else if (c == '/')
          x = a / b;
        X.push(x);
      } else {
        X.push(s2d(s));
      }
    }
    printf("%.7f\\n", X.top());
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

bool isop(string &s) { return s == "+" || s == "*" || s == "/" || s == "-"; }

int main() {
  string s;
  while (getline(cin, s)) {
    istringstream iss(s);
    stack<double> s;
    string b;
    while (iss >> b) {

      if (!isop(b)) {
        double x;
        sscanf(b.c_str(), "%lf", &x);
        s.push(x);
        continue;
      }

      switch (b[0]) {
      case '+': {
        double x = s.top();
        s.pop();
        double y = s.top();
        s.pop();
        s.push(x + y);
        break;
      }
      case '-': {
        double x = s.top();
        s.pop();
        double y = s.top();
        s.pop();
        s.push(y - x);
        break;
      }
      case '*': {
        double x = s.top();
        s.pop();
        double y = s.top();
        s.pop();
        s.push(x * y);
        break;
      }
      case '/': {
        double x = s.top();
        s.pop();
        double y = s.top();
        s.pop();
        s.push(y / x);
        break;
      }
      }
    }
    printf("%lf\\n", s.top());
  }
  return 0;
}

Error: -11

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string s;
  while (getline(cin, s)) {
    istringstream iss(s);
    stack<double> s;
    string b;
    while (iss >> b) {
      switch (b[0]) {
      case '+': {
        double x = s.top();
        s.pop();
        double y = s.top();
        s.pop();
        s.push(x + y);
        break;
      }
      case '-': {
        double x = s.top();
        s.pop();
        double y = s.top();
        s.pop();
        s.push(y - x);
        break;
      }
      case '*': {
        double x = s.top();
        s.pop();
        double y = s.top();
        s.pop();
        s.push(x * y);
        break;
      }
      case '/': {
        double x = s.top();
        s.pop();
        double y = s.top();
        s.pop();
        s.push(y / x);
        break;
      }
      default: {
        double x;
        sscanf(b.c_str(), "%lf", &x);
        s.push(x);
        break;
      }
      }
    }
    printf("%lf\\n", s.top());
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  while (1) {
    stack<double> st;
    while (1) {
      char tmp[128];
      if (scanf("%s", tmp) == EOF)
        return 0;
      if ((tmp[0] == '+' || tmp[0] == '-' || tmp[0] == '*' || tmp[0] == '/') &&
          tmp[1] == '\\0') {
        double a = st.top();
        st.pop();
        double b = st.top();
        st.pop();
        if (tmp[0] == '+')
          st.push(a + b);
        if (tmp[0] == '-')
          st.push(b - a);
        if (tmp[0] == '*')
          st.push(b * a);
        if (tmp[0] == '/')
          st.push(b / a);
      } else {
        double a;
        sscanf(tmp, "%lf", &a);
        st.push(a);
      }
      if (getchar() == '\\n')
        break;
    }
    printf("%lf\\n", st.top());
  }
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  while (1) {
    stack<double> st;
    while (1) {
      char tmp[128];
      if (scanf("%s", tmp) == EOF)
        return 0;
      if ('0' <= tmp[0] && tmp[0] <= '9') {
        double a;
        sscanf(tmp, "%lf", &a);
        st.push(a);
      } else if (tmp[1] == '\\0') {
        double a = st.top();
        st.pop();
        double b = st.top();
        st.pop();
        if (tmp[0] == '+')
          st.push(a + b);
        if (tmp[0] == '-')
          st.push(b - a);
        if (tmp[0] == '*')
          st.push(b * a);
        if (tmp[0] == '/')
          st.push(b / a);
      }
      if (getchar() == '\\n')
        break;
    }
    printf("%.6lf\\n", st.top());
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  string s;
  while (getline(cin, s)) {
    istringstream sin(s);
    vector<double> v;
    string w;
    while (sin >> w) {
      if (w.size() > 1 || isdigit(w[0]))
        v.push_back(atof(w.c_str()));
      else {
        const double a = v[v.size() - 2];
        const double b = v[v.size() - 1];
        v.pop_back();
        v.pop_back();
        if (w[0] == '+')
          v.push_back(a + b);
        if (w[0] == '-')
          v.push_back(a - b);
        if (w[0] == '*')
          v.push_back(a * b);
        if (w[0] == '/')
          v.push_back(a / b);
      }
    }
    printf("%.6f\\n", v[0]);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  string s;
  while (getline(cin, s)) {
    istringstream sin(s);
    vector<double> v;
    string w;
    while (sin >> w) {
      if (isdigit(w[0]))
        v.push_back(atof(w.c_str()));
      else {
        const double a = v[v.size() - 2];
        const double b = v[v.size() - 1];
        v.pop_back();
        v.pop_back();
        if (w[0] == '+')
          v.push_back(a + b);
        if (w[0] == '-')
          v.push_back(a - b);
        if (w[0] == '*')
          v.push_back(a * b);
        if (w[0] == '/')
          v.push_back(a / b);
      }
    }
    if (v.size())
      printf("%.6f\\n", v[0]);
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {

  string in;
  string e;
  stack<double> s;

  while (getline(cin, in)) {
    e = "";
    while (!s.empty())
      s.pop();
    for (int i = 0; i < in.size(); i++) {
      if (in[i] != ' ') {
        e += in[i];
      }
      if (in[i] == ' ' || i == in.size() - 1) {
        if (e == "+") {
          double a = s.top();
          s.pop();
          double b = s.top();
          s.pop();
          s.push(b + a);
        } else if (e == "*") {
          double a = s.top();
          s.pop();
          double b = s.top();
          s.pop();
          s.push(b * a);
        } else if (e == "/") {
          double a = s.top();
          s.pop();
          double b = s.top();
          s.pop();
          s.push(b / a);
        } else if (e == "-") {
          double a = s.top();
          s.pop();
          double b = s.top();
          s.pop();
          s.push(b - a);
        } else {
          s.push(atoi(e.c_str()));
        }
        e = "";
      }
    }
    printf("%.12f\\n", s.top());
    s.pop();
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

double s_d(string a) {
  double b = 0;
  double zy = 0.1;
  bool hu = false;
  bool dot = false;
  for (int i = 0; i < a.size(); i++) {
    if (dot) {
      b += zy * (a[i] - '0');
      zy *= 0.1;
    } else if (a[i] == '-')
      hu = true;
    else if (a[i] == '.') {
      dot = true;
    } else {
      b *= 10;
      b += a[i] - '0';
    }
  }

  if (hu)
    b *= -1;
  return b;
}

int main() {

  stack<string> siki;
  stack<double> bnum;
  string nums = "";
  double a, b, c;
  char in;

  while (cin >> nums) {
    siki.push(nums);
    nums = "";
    while (1) {
      in = getchar();
      if (in == '*' || in == '-' || in == '/' || in == '+') {
        if (siki.top() == "?") {
          b = bnum.top();
          bnum.pop();
          siki.pop();
        } else {
          b = s_d(siki.top());
          siki.pop();
        }
        if (siki.top() == "?") {
          a = bnum.top();
          bnum.pop();
          siki.pop();
        } else {
          a = s_d(siki.top());
          siki.pop();
        }
        if (in == '*')
          c = a * b;
        else if (in == '+')
          c = a + b;
        else if (in == '-')
          c = a - b;
        else if (in == '/')
          c = a / b;
        siki.push("?");
        bnum.push(c);
      } else if (in == ' ') {
        if (!nums.empty()) {
          siki.push(nums);
          nums = "";
        }
      } else if (in == '\\n') {
        break;
      } else {
        nums += in;
      }
    }
    if (bnum.empty()) {
      if (!siki.empty())
        cout << siki.top() << endl;
    } else {
      printf("%.6f\\n", bnum.top());
    }
    while (!siki.empty())
      siki.pop();
    while (!bnum.empty())
      bnum.pop();
  }

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

#include <cctype>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
int main() {
  string str;
  while (getline(cin, str)) {
    stack<double> S;
    stringstream ss;
    ss << str;
    while (ss >> str) {
      if (str[0] >= '0' && str[0] <= '9' || str.size() > 1) {
        S.push(atoi(str.c_str()));
      } else {
        char ope = str[0];
        double d1 = S.top();
        S.pop();
        double d2 = S.top();
        S.pop();
        if (ope == '+')
          S.push(d1 + d2);
        else if (ope == '-')
          S.push(d2 - d1);
        else if (ope == '*')
          S.push(d1 * d2);
        else
          S.push(d2 / d1);
      }
    }
    cout << fixed << S.top() << endl;
  }
}

Error: -11

Fixed submission:

#include <cctype>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
int main() {
  string str;
  while (getline(cin, str)) {
    stack<double> S;
    stringstream ss;
    ss << str;
    while (ss >> str) {
      if (isdigit(str[0])) {
        double d = (double)atoi(str.c_str());
        S.push(d);
      } else {
        char ope = str[0];
        double d1 = S.top();
        S.pop();
        double d2 = S.top();
        S.pop();
        if (ope == '+')
          S.push(d1 + d2);
        else if (ope == '-')
          S.push(d2 - d1);
        else if (ope == '*')
          S.push(d1 * d2);
        else
          S.push(d2 / d1);
      }
    }
    cout << fixed << S.top() << endl;
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

const string op = "+-*/";

int main() {
  string s;

  while (getline(cin, s)) {
    stringstream ss;
    ss << s;
    stack<double> st;
    while (ss >> s) {
      int t = op.find(s);
      if (t >= 0) {
        double b = st.top();
        st.pop();
        double a = st.top();
        st.pop();
        switch (t) {
        case 0:
          st.push(a + b);
          break;
        case 1:
          st.push(a - b);
          break;
        case 2:
          st.push(a * b);
          break;
        case 3:
          st.push(a / b);
          break;
        }
      } else {
        st.push(atoi(s.c_str()));
      }
    }
    printf("%.8f\\n", st.top());
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
int main() {
  string str;
  stack<double> st;
  string num;

  while (getline(cin, str)) {
    cin.ignore();
    if (cin.eof())
      break;
    while (!st.empty())
      st.pop();
    num = "";
    for (int i = 0; i < (int)str.size(); i++) {

      if (str[i] == ' ' && num != "") {
        double t = atoi(num.c_str());
        st.push(t);
        num = "";

      } else if ('0' <= str[i] && str[i] <= '9') {

        num += str[i];

      } else {

        double a, b;
        switch (str[i]) {
        case '-':
          a = st.top();
          st.pop();
          b = st.top();
          st.pop();
          st.push(b - a);
          num = "";
          break;

        case '+':
          a = st.top();
          st.pop();
          b = st.top();
          st.pop();
          st.push(b + a);
          num = "";
          break;

        case '*':
          a = st.top();
          st.pop();
          b = st.top();
          st.pop();
          st.push(b * a);
          num = "";
          break;

        case '/':
          a = st.top();
          st.pop();
          b = st.top();
          st.pop();
          st.push(b / a);
          num = "";
          break;
        }
      }
    }

    if (!st.empty())
      printf("%.8f\\n", st.top());
    cin.clear();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
  string str;
  double s[128];

  while (getline(cin, str)) {
    int t = 0;
    stringstream ss;
    ss << str;
    while (ss >> str) {
      if (str == "+") {
        s[t - 2] = s[t - 2] + s[t - 1];
        t--;
      } else if (str == "-") {
        s[t - 2] = s[t - 2] - s[t - 1];
        t--;
      } else if (str == "*") {
        s[t - 2] = s[t - 2] * s[t - 1];
        t--;
      } else if (str == "/") {
        s[t - 2] = s[t - 2] / s[t - 1];
        t--;
      } else {
        s[t] = atoi(str.c_str());
        t++;
      }
    }

    printf("%.6lf\\n", s[0]);
  }
}

Error: -11

Fixed submission:

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

int main() {
  string str;
  char c, s;
  double t, x, y;

  while (getline(cin, str)) {
    stringstream ss;
    ss << str;

    stack<double> buf;
    while (ss >> str) {
      if ('0' <= str[0] && str[0] <= '9') {
        buf.push(atof(str.c_str()));
      } else {
        y = buf.top();
        buf.pop();
        x = buf.top();
        buf.pop();
        switch (str[0]) {
        case '+':
          t = x + y;
          break;
        case '-':
          t = x - y;
          break;
        case '*':
          t = x * y;
          break;
        case '/':
          t = x / y;
          break;
        }
        buf.push(t);
      }
    }

    printf("%.6lf\\n", buf.top());
    buf.pop();
  }
}