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

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

double calc(string e) {
  double num2 = st.top();
  st.pop();
  double num1 = st.top();
  st.pop();
  if (e == "+")
    return num1 + num2;
  else if (e == "-")
    return num1 - num2;
  else if (e == "*")
    return num1 * num2;
  else
    return num1 / num2;
}

int main() {
  string str;
  while (cin >> str) {
    if (str == "+" || str == "-" || str == "*" || str == "/") {
      st.push(calc(str));
    } else {
      st.push(atoi(str.c_str()));
    }
    if (getchar() == '\\n') {
      printf("%.10f\\n", st.top());
      st.pop();
    }
  }
}

Error: -11

Fixed submission:

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

double calc(char e) {
  double num2 = st.top();
  st.pop();
  double num1 = st.top();
  st.pop();
  if (e == '+')
    return num1 + num2;
  else if (e == '-')
    return num1 - num2;
  else if (e == '*')
    return num1 * num2;
  else
    return num1 / num2;
}

int main() {
  string str;
  while (cin >> str) {
    if (str[0] == '+' || str[0] == '-' || str[0] == '*' || str[0] == '/') {
      st.push(calc(str[0]));
    } else {
      st.push(atoi(str.c_str()));
    }
    if (getchar() == '\\n') {
      printf("%.10f\\n", st.top());
      st.pop();
    }
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

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

Error: -11

Fixed submission:

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

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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  char buf[128];
  while (!cin.eof()) {
    cin.getline(buf, sizeof(buf));
    if (strlen(buf) == 0)
      break;

    stack<double> s;
    for (char *p = strtok(buf, " "); p; p = strtok(NULL, " ")) {
      if (isdigit(*p) || strlen(p) >= 2)
        s.push(atof(p));
      else {
        double a, b;
        b = s.top();
        s.pop();
        a = s.top();
        s.pop();
        double res;
        if (*p == '+')
          res = a + b;
        else if (*p == '-')
          res = a - b;
        else if (*p == '*')
          res = a * b;
        else if (*p == '/')
          res = a / b;
        s.push(res);
      }
    }
    printf("%.6f\\n", s.top());
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  char buf[81];
  while (!cin.eof()) {
    cin.getline(buf, sizeof(buf));
    if (strlen(buf) == 0)
      break;

    stack<double> s;
    for (char *p = strtok(buf, " "); p; p = strtok(NULL, " ")) {
      if (isdigit(*p))
        s.push(atof(p));
      else {
        double a, b;
        b = s.top();
        s.pop();
        a = s.top();
        s.pop();
        double res;
        if (*p == '+')
          res = a + b;
        else if (*p == '-')
          res = a - b;
        else if (*p == '*')
          res = a * b;
        else if (*p == '/')
          res = a / b;
        s.push(res);
      }
    }
    printf("%.6f\\n", s.top());
  }

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

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

int main() {
  char str[128];

  while (fgets(str, sizeof(str), stdin) != NULL) {
    stack<double> s;

    for (int i = 0; str[i] && str[i] != '\\n';) {
      if (str[i] == ' ')
        i++;
      else if ('0' <= str[i] && str[i] <= '9') {
        int p = 0;
        for (; str[i] && str[i] != ' ' && str[i] != '\\n'; i++) {
          p *= 10;
          p += str[i] - '0';
        }
        s.push((double)p);
      }

      else if (str[i] == '-' && str[i + 1] != ' ' && str[i + 1] != '\\n') {
        int p = 0;
        i++;
        for (; str[i] && str[i] != ' ' && str[i] != '\\n'; i++) {
          p *= 10;
          p += str[i] - '0';
        }
        s.push((double)p * (-1));
      }

      else {
        double r, l;
        double p;
        r = s.top();
        s.pop();
        l = s.top();
        s.pop();

        switch (str[i]) {
        case '+':
          p = l + r;
          break;
        case '-':
          p = l - r;
          break;
        case '*':
          p = l * r;
          break;
        case '/':
          p = l / r;
          break;
        }
        s.push(p);
        i++;
      }
    }
    printf("%f\\n", s.top());
  }
}

Error: -11

Fixed submission:

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

int main() {
  char str[128];

  while (fgets(str, sizeof(str), stdin) != NULL) {
    stack<double> s;

    for (int i = 0; str[i] && str[i] != '\\n';) {
      if (str[i] == ' ')
        i++;
      else if ('0' <= str[i] && str[i] <= '9') {
        int p = 0;
        for (; str[i] && str[i] != ' ' && str[i] != '\\n'; i++) {
          p *= 10;
          p += str[i] - '0';
        }
        s.push((double)p);
      }

      else {
        double r, l;
        double p;
        r = s.top();
        s.pop();
        l = s.top();
        s.pop();

        switch (str[i]) {
        case '+':
          p = l + r;
          break;
        case '-':
          p = l - r;
          break;
        case '*':
          p = l * r;
          break;
        case '/':
          p = l / r;
          break;
        }
        s.push(p);
        i++;
      }
    }
    printf("%f\\n", s.top());
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  cout.precision(10);
  cout.setf(ios::fixed);

  for (string line; getline(cin, line);) {
    istringstream is(line);
    stack<double> sta;

    string token;
    while (is >> token) {
      if (isdigit(token[0]) || (token[0] == '-' && token.size() >= 2)) {
        sta.push(stod(token));
      } else { // binary operator
        assert(token.size() == 1);

        const double b = sta.top();
        sta.pop();
        const double a = sta.top();
        sta.pop();

        switch (token[0]) {
        case '+':
          sta.push(a + b);
          break;
        case '-':
          sta.push(a - b);
          break;
        case '*':
          sta.push(a * b);
          break;
        case '/':
          sta.push(a / b);
          break;
        default:
          assert(false);
        }
      }
    }

    assert(sta.size() == 1);
    cout << sta.top() << endl;
  }

  return EXIT_SUCCESS;
}

Error: -6
51e4843e-ea30-41eb-9fa0-e85028c1061f.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00087/C++/s202283844.cpp:18: int main(): Assertion `token.size() == 1' failed.


Fixed submission:

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

int main() {
  cout.precision(10);
  cout.setf(ios::fixed);

  for (string line; getline(cin, line);) {
    istringstream is(line);
    stack<double> sta;

    string token;
    while (is >> token) {
      if (isdigit(token[0])) {
        sta.push(stod(token));
      } else { // operator
        assert(token.size() == 1);

        const double b = sta.top();
        sta.pop();
        const double a = sta.top();
        sta.pop();

        switch (token[0]) {
        case '+':
          sta.push(a + b);
          break;
        case '-':
          sta.push(a - b);
          break;
        case '*':
          sta.push(a * b);
          break;
        case '/':
          sta.push(a / b);
          break;
        default:
          assert(false);
        }
      }
    }

    assert(sta.size() == 1);
    cout << sta.top() << endl;
  }

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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
double cul(double a, double b, string e) {
  double sum;
  if (e == "+")
    sum = a + b;
  else if (e == "-")
    sum = a - b;
  else if (e == "*")
    sum = a * b;
  else
    sum = a / b;

  return sum;
}
int main() {

  string s;

  while (getline(cin, s)) {
    stack<double> st;
    string e = "";
    for (int i = 0; i <= s.size(); i++) {
      if (s[i] != ' ' && i != s.size())
        e = e + s[i];
      else {
        if (e == "+" || e == "-" || e == "*" || e == "/") {
          double b = st.top();
          st.pop();
          double a = st.top();
          st.pop();
          st.push(cul(a, b, e));
        } else {
          st.push(stod(e));
        }
        e = "";
      }
    }

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

Error: -11

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {

  string s;
  stack<double> que;
  while (getline(cin, s)) {
    while (!que.empty()) {
      que.pop();
    }
    string e = "";
    for (int i = 0; i < s.size(); i++) {
      if (s[i] >= '0' && s[i] <= '9') {
        e = e + s[i];
      } else if (s[i] == ' ') {
        if (e != "") {
          que.push(stoi(e));
          e = "";
        }
      } else {
        double sum = 0;
        double b = que.top();
        que.pop();
        double a = que.top();
        que.pop();
        if (s[i] == '-')
          sum = a - b;
        else if (s[i] == '+')
          sum = a + b;
        else if (s[i] == '*')
          sum = a * b;
        else if (s[i] == '/')
          sum = a / b;
        que.push(sum);
      }
    }
    printf("%.6lf\\n", que.top());
  }
}
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, n) range(i, 0, n)
using namespace std;

int main(void) {
  while (1) {
    string s;
    getline(cin, s);
    if (cin.eof())
      break;
    stringstream ss(s);
    vector<double> stk;
    while (1) {
      if (ss.eof())
        break;
      string n;
      ss >> n;
      if (n.size() == 1 && !isdigit(n[0])) {
        double b = stk.back();
        stk.pop_back();
        double a = stk.back();
        stk.pop_back();
        switch (n[0]) {
        case '+':
          stk.push_back(a + b);
          break;
        case '-':
          stk.push_back(a - b);
          break;
        case '*':
          stk.push_back(a * b);
          break;
        case '/':
          stk.push_back(a / b);
          break;
        }
      } else {
        int num = stoi(n);
        stk.push_back(1.0 * num);
      }
      // for(auto &i:stk) cout << i << endl;
    }
    cout.precision(10);
    cout << fixed << stk[0] << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi


Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rep(i, n) range(i, 0, n)
using namespace std;

int main(void) {
  while (1) {
    if (cin.eof())
      break;
    string s;
    getline(cin, s);
    stringstream ss(s);
    vector<int> stk;
    while (1) {
      if (ss.eof())
        break;
      string n;
      ss >> n;
      if (n.size() == 1 && !isdigit(n[0])) {
        int b = stk.back();
        stk.pop_back();
        int a = stk.back();
        stk.pop_back();
        switch (n[0]) {
        case '+':
          stk.push_back(a + b);
          break;
        case '-':
          stk.push_back(a - b);
          break;
        case '*':
          stk.push_back(a * b);
          break;
        case '/':
          stk.push_back(a / b);
          break;
        }
      } else {
        int num = stoi(n);
        stk.push_back(num);
      }
      // for(auto &i:stk) cout << i << endl;
    }
    cout.precision(10);
    cout << stk[0] << endl;
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

double dbl(string s) {
  stringstream ss(s);
  double ret;
  ss >> ret;
  return ret;
}

int main() {
  string line;
  while (getline(cin, line)) {
    stringstream ss(line);
    stack<double> stk;
    string s;
    while (ss >> s) {
      if (s == "+") {
        double b = stk.top();
        stk.pop();
        double a = stk.top();
        stk.pop();
        stk.push(a + b);
      } else if (s == "-") {
        double b = stk.top();
        stk.pop();
        double a = stk.top();
        stk.pop();
        stk.push(a - b);
      } else if (s == "*") {
        double b = stk.top();
        stk.pop();
        double a = stk.top();
        stk.pop();
        stk.push(a * b);
      } else if (s == "/") {
        double b = stk.top();
        stk.pop();
        double a = stk.top();
        stk.pop();
        stk.push(a / b);
      } else {
        stk.push(dbl(s));
      }
    }
    cout.precision(6);
    cout << fixed << stk.top() << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

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

double dbl(string s) {
  stringstream ss(s);
  double ret;
  ss >> ret;
  return ret;
}

string calc(string sa, string sb, string op) {
  double a, b;
  a = dbl(sa);
  b = dbl(sb);
  stringstream ret;
  if (op == "+") {
    ret << a + b;
  } else if (op == "-") {
    ret << a - b;
  } else if (op == "*") {
    ret << a * b;
  } else if (op == "/") {
    ret << a / b;
  }
  return ret.str();
}

int main() {
  string line;
  while (getline(cin, line)) {
    stringstream ss(line);
    stack<string> stk;
    string s;
    while (ss >> s) {
      if (isdigit(s[0])) {
        stk.push(s);
      } else {
        string b = stk.top();
        stk.pop();
        string a = stk.top();
        stk.pop();
        stk.push(calc(a, b, s));
      }
    }
    cout.precision(6);
    cout << fixed << dbl(stk.top()) << endl;
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string s;
  while (getline(cin, s)) {
    stack<double> stk;
    string sa;

    for (int i = 0; i < s.size(); i++) {
      if (s[i] != ' ') {
        sa += (s[i]);
      } else {
        if (sa == "+") {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(a + b);
        } else if (sa == "-") {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(b - a);
        } else if (sa == "*") {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(a * b);
        } else if (sa == "/") {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(b / a);
        } else {
          int a = stoi(sa);
          stk.push(a);
        }
        sa.clear();
      }
    }
    if (!sa.empty() && sa != "+" && sa != "-" && sa != "*" && sa != "/")
      stk.push(stoi(sa));
    else {
      if (sa == "+") {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(a + b);
      } else if (sa == "-") {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(b - a);
      } else if (sa == "*") {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(a * b);
      } else if (sa == "/") {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(b / a);
      }
    }
    printf("%.9lf\\n", stk.top());
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi


Fixed submission:

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

int main() {
  string s;
  while (getline(cin, s)) {
    stack<double> stk;
    string sa;

    for (int i = 0; i < s.size(); i++) {
      if (s[i] != ' ') {
        sa += (s[i]);
      } else {
        if (sa == "+") {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(a + b);
        } else if (sa == "-") {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(b - a);
        } else if (sa == "*") {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(a * b);
        } else if (sa == "/") {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(b / a);
        } else {
          int a = stoi(sa);
          stk.push(a);
        }
        i++;
        sa.clear();
      }
    }
    printf("%.9lf\\n", stk.top());
  }
  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 r;
  while (getline(cin, r)) {
    r += ' ';
    int mark = -1, range = 1;
    stack<double> num;
    char ord = 'k';
    for (int i = 0; i < r.size(); i++) {
      if (r[i] == ' ') {
        if (mark != -1) {
          double x = atof(r.substr(mark, range).c_str());
          if (ord == '-') {
            x = -x;
            ord = 'k';
          }
          num.push(x);
          mark = -1;
          range = 1;
        } else {
          double a = num.top();
          num.pop();
          double b = num.top();
          num.pop();
          if (ord == '+') {
            num.push(b + a);
          }
          if (ord == '-') {
            num.push(b - a);
          }
          if (ord == '/') {
            num.push(b / a);
          }
          if (ord == '*') {
            num.push(b * a);
          }
          ord = 'k';
        }
      } else if ((r[i] >= '0' && r[i] <= '9') || r[i] == '.') {
        if (mark == -1) {
          mark = i;
        } else {
          range++;
        }
      } else {
        ord = r[i];
      }
    }
    printf("%.6lf\\n", num.top());
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
  string r;
  while (getline(cin, r)) {
    r += ' ';
    double x = 0;
    stack<double> num;
    char ord = 'k';
    for (int i = 0; i < r.size(); i++) {
      if (r[i] == ' ') {
        if (ord == 'k') {
          num.push(x);
          x = 0;
        } else {
          double a = num.top();
          num.pop();
          double b = num.top();
          num.pop();
          if (ord == '+') {
            num.push(b + a);
          }
          if (ord == '-') {
            num.push(b - a);
          }
          if (ord == '/') {
            num.push(b / a);
          }
          if (ord == '*') {
            num.push(b * a);
          }
          ord = 'k';
        }
      } else if (r[i] >= '0' && r[i] <= '9') {
        x = x * 10 + r[i] - '0';
      } else {
        ord = r[i];
      }
    }
    printf("%.6lf\\n", num.top());
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
int main() {
  string s, t;
  while (getline(cin, s)) {
    stringstream x(s);
    stack<double> num;
    while (x >> t) {
      if (t == "+" || t == "-" || t == "*" || t == "/") {
        double a, b;
        a = num.top();
        num.pop();
        b = num.top();
        num.pop();
        if (t == "+")
          num.push(b + a);
        if (t == "-")
          num.push(b - a);
        if (t == "*")
          num.push(b * a);
        if (t == "/")
          num.push(b / a);
      } else
        num.push(atof(t.c_str()));
    }
    printf("%.6lf\\n", num.top());
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
  string s;
  while (getline(cin, s)) {
    stack<double> num;
    double t = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] >= '0' && s[i] <= '9')
        t = t * 10 + s[i] - '0';
      else if (s[i] == ' ') {
        num.push(t);
        t = 0;
      } else {
        double a, b;
        a = num.top();
        num.pop();
        b = num.top();
        num.pop();
        if (s[i] == '-')
          num.push(b - a);
        if (s[i] == '+')
          num.push(b + a);
        if (s[i] == '*')
          num.push(b * a);
        if (s[i] == '/')
          num.push(b / a);
        i++;
      }
    }
    printf("%.6lf\\n", num.top());
  }
  return 0;
}