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

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

bool isNumber(char c) {
  if (c >= '0' && c <= '9')
    return true;
  return false;
}

int main() {
  string s;
  while (getline(cin, s)) {
    int minusFlag = 1;
    double n = 0.0;
    stack<double> st;
    s += " ";

    for (int i = 0; i < s.size(); i++) {
      char c = s[i];
      if (c == '-' && isNumber(s[i + 1])) {
        minusFlag = -1;
      } else if (isNumber(c)) {
        n *= 10.0;
        n += c - '0';
      } else if (c == ' ') {
        if (isNumber(s[i - 1])) {
          n = n * minusFlag;
          st.push(n);
          n = 0.0;
          minusFlag = 1;
        }
      } else if (c == '+' || c == '-' || c == '*' || c == '/') {
        double a = st.top();
        st.pop();
        double b = st.top();
        st.pop();
        if (c == '+')
          st.push(b + a);
        if (c == '-')
          st.push(b - a);
        if (c == '*')
          st.push(b * a);
        if (c == '/')
          st.push(b / a);
      }
    }
    // cout << st.top() << endl;
    printf("%.8f\\n", st.top());
  }
}

Error: -11

Fixed submission:

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

int main() {
  string s;
  while (getline(cin, s)) {
    stack<double> st;
    double n = 0;
    bool flag = false;
    s += " ";

    for (int i = 0; i < s.size(); i++) {
      char c = s[i];

      if (c >= '0' && c <= '9') {
        flag = true;
        n *= 10;
        n += c - '0';
      } else if (c == ' ') {
        if (flag == true) {
          flag = false;
          st.push(n);
          n = 0;
        } else {
          n = 0;
        }
      }
      if (c == '+' || c == '-' || c == '*' || c == '/') {
        flag = false;
        double a = st.top();
        st.pop();
        double b = st.top();
        st.pop();
        if (c == '+')
          st.push(b + a);
        if (c == '-')
          st.push(b - a);
        if (c == '*')
          st.push(b * a);
        if (c == '/')
          st.push(b / a);
      }
    }
    cout << st.top() << endl;
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

deque<string> split_words(string s) {
  bool flag = false;
  deque<string> words;
  string str;
  s.push_back(' ');

  for (unsigned int i = 0; i < s.size(); ++i) {
    char c = s[i];
    if (flag == false) {
      if (c != ' ') {
        flag = true;
        str.clear();
        str.push_back(c);
      }
    } else {
      if (c != ' ') {
        str.push_back(c);
      }
      if (c == ' ') {
        flag = false;
        words.push_back(str);
        str.clear();
      }
    }
  }
  return words;
}

bool isOperator(string s) {
  return (s == "+" || s == "-" || s == "*" || s == "/") ? true : false;
}

int s_to_i(string s) {
  bool flag = false;
  int n = 0;

  for (unsigned int i = 0; i < s.size(); ++i) {
    if (i == 0 && s[i] == '-') {
      flag = true;
    } else {
      n *= 10;
      n += s[i] - '0';
    }
  }
  return (flag) ? -n : n;
}

double accum(double a, double b, string s) {
  if (s == "+")
    return a + b;
  else if (s == "-")
    return a - b;
  else if (s == "*")
    return a * b;
  else if (s == "/")
    return a / b;
  return 0;
}

int main() {
  string s;
  while (getline(cin, s)) {
    deque<string> words = split_words(s);
    stack<double> n;

    while (!words.empty()) {
      deque<string>::iterator it = words.begin();
      string str = *it;
      words.erase(it);
      if (isOperator(str)) {
        double a = n.top();
        n.pop();
        double b = n.top();
        n.pop();
        n.push(accum(b, a, str));
      } else {
        n.push(s_to_i(str));
      }
    }
    // cout.setf( ios::showpoint );
    // cout.precision( 8 );
    printf("%.6lf\\n", n.top());
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  stack<double> st;
  string s;
  int n = 0;
  bool flag = false;
  double n1, n2;

  while (getline(cin, s)) {
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
        n2 = st.top();
        st.pop();
        n1 = st.top();
        st.pop();
        if (s[i] == '+') {
          st.push(n1 + n2);
        } else if (s[i] == '-') {
          st.push(n1 - n2);
        } else if (s[i] == '*') {
          st.push(n1 * n2);
        } else if (s[i] == '/') {
          st.push(n1 / n2);
        }
      } else if (s[i] >= '0' && s[i] <= '9') {
        n *= 10;
        n += s[i] - '0';
        flag = true;
      } else if (s[i] == ' ' && flag) {
        st.push(n);
        n = 0;
      }
    }
    cout << fixed << st.top() << endl;
    st.pop();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

double eval(double a, double b, char oper) {
  if (oper == '+')
    return a + b;
  if (oper == '-')
    return a - b;
  if (oper == '*')
    return a * b;
  if (oper == '/')
    return a / b;
  return 0.0;
}

int main() {
  string expr;
  while (getline(cin, expr)) {
    stack<double> stk;
    string::iterator it = expr.begin();
    while (true) {
      if ((*it == '-' && it + 1 != expr.end() && isdigit(*(it + 1))) ||
          isdigit(*it)) {
        string::iterator tmp = it;
        if (*it == '-')
          it++;
        double a = 0;
        while (it != expr.end() && *it != '\\n' && *it != ' ') {
          a *= 10;
          a += *it - '0';
          it++;
        }
        if (*tmp == '-')
          a *= -1;
        stk.push(a);
      } else {
        double b, c;
        b = stk.top();
        stk.pop();
        c = stk.top();
        stk.pop();
        stk.push(eval(c, b, *it));
        it++;
      }
      if (it == expr.end())
        break;
      it++;
    }
    printf("%.6lf\\n", stk.top());
    stk.pop();
  }
  return 0;
}

Error: -11

Fixed submission:

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

double eval(double a, double b, char oper) {
  if (oper == '+')
    return a + b;
  if (oper == '-')
    return a - b;
  if (oper == '*')
    return a * b;
  if (oper == '/')
    return a / b;
  return 0.0;
}

int main() {
  string expr;
  while (getline(cin, expr)) {
    stack<double> stk;
    string::iterator it = expr.begin();
    while (true) {
      if (isdigit(*it)) {
        int a = 0;
        while (it != expr.end() && *it != '\\n' && *it != ' ') {
          a *= 10;
          a += *it - '0';
          it++;
        }
        stk.push(a);
      } else {
        int b, c;
        b = stk.top();
        stk.pop();
        c = stk.top();
        stk.pop();
        stk.push(eval(c, b, *it));
        it++;
      }
      if (it == expr.end())
        break;
      it++;
    }
    cout << stk.top() << endl;
    stk.pop();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

vector<string> split(string src, char delim) {
  istringstream ss(src);
  string s;
  vector<string> ret;
  while (getline(ss, s, delim))
    ret.push_back(s);
  return ret;
}

int main() {
  string str;
  while (getline(cin, str)) {
    stack<double> s;
    for (const string &t : split(str, ' ')) {
      if (t == "+") {
        double op1 = s.top();
        s.pop();
        double op2 = s.top();
        s.pop();
        double ans = op2 + op1;
        s.push(ans);
      } else if (t == "-") {
        double op1 = s.top();
        s.pop();
        double op2 = s.top();
        s.pop();
        double ans = op2 - op1;
        s.push(ans);
      } else if (t == "*") {
        double op1 = s.top();
        s.pop();
        double op2 = s.top();
        s.pop();
        double ans = op2 * op1;
        s.push(ans);
      } else if (t == "/") {
        double op1 = s.top();
        s.pop();
        double op2 = s.top();
        s.pop();
        double ans = op2 / op1;
        s.push(ans);
      } else {
        s.push(atoi(t.c_str()));
      }
    }
    cout << fixed << setprecision(6) << s.top() << endl;
    s.pop();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

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

int main() {
  string str, s, item;
  while (getline(cin, str)) {
    stack<double> num;
    stringstream ss(str);
    while (getline(ss, item, ' ')) {
      if (item.empty())
        break;
      if (isdigit(item[0]))
        num.push(strtodbl(item));
      else {
        double a = num.top();
        num.pop();
        double b = num.top();
        num.pop();
        double c;
        if (item == "+")
          c = b + a;
        else if (item == "-")
          c = b - a;
        else if (item == "*")
          c = b * a;
        else if (item == "/")
          c = b / a;

        num.push(c);
      }
    }
    cout << fixed << setprecision(6) << num.top() << endl;
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  string s;
  while (getline(cin, s)) {
    s += ' ';
    stack<double> st;
    double p = 0;
    int t = 1;
    r(i, s.size()) {
      if (s[i] == ' ' && isdigit(s[i - 1])) {
        if (p != 0) {
          if (!t)
            st.push(p), p = 0;
          else
            st.push(t * p), p = 0, t = 1;
        }
      } else if (s[i] == '*') {
        double x1 = st.top();
        st.pop();
        double x2 = st.top();
        st.pop();
        st.push(x2 * x1);
      } else if (s[i] == '+') {
        double x1 = st.top();
        st.pop();
        double x2 = st.top();
        st.pop();
        st.push(x2 + x1);
      } else if (s[i] == '/') {
        double x1 = st.top();
        st.pop();
        double x2 = st.top();
        st.pop();
        st.push(x2 / x1);
      } else if (s[i] == '-') {
        if (isdigit(s[i + 1]))
          t = -1;
        else {
          double x1 = st.top();
          st.pop();
          double x2 = st.top();
          st.pop();
          st.push(x2 - x1);
        }
      } else if (isdigit(s[i])) {
        p *= 10, p += s[i] - '0';
      }
    }
    printf("%.6f\\n", st.top());
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  string s;
  while (getline(cin, s)) {
    s += 'q';
    stack<double> st;
    double p = 0;
    int t = 0;
    r(i, s.size()) {
      if (s[i] == ' ' && isdigit(s[i - 1])) {
        if (p != 0) {
          if (!t)
            st.push(p), p = 0;
          else
            st.push(-p), p = 0, t = 0;
        }
      } else if (s[i] == '*') {
        double x1 = st.top();
        st.pop();
        double x2 = st.top();
        st.pop();
        st.push(x2 * x1);
      } else if (s[i] == '+') {
        double x1 = st.top();
        st.pop();
        double x2 = st.top();
        st.pop();
        st.push(x2 + x1);
      } else if (s[i] == '/') {
        double x1 = st.top();
        st.pop();
        double x2 = st.top();
        st.pop();
        st.push(x2 / x1);
      } else if (s[i] == '-') {
        if (isdigit(s[i + 1]))
          t++;
        else {
          double x1 = st.top();
          st.pop();
          double x2 = st.top();
          st.pop();
          st.push(x2 - x1);
        }
      } else if (isdigit(s[i])) {
        p *= 10, p += s[i] - '0';
      }
    }
    printf("%.6f\\n", st.top());
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

// 21
#include <cctype>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

int main() {
  for (string s; getline(cin, s);) {
    stringstream ss(s);
    stack<double> stk;
    for (string t; ss >> t;) {
      if (t.size() > 1 || isdigit(t.c_str()[0])) {
        stk.push(atof(t.c_str()));
      } else {
        double a, b;
        a = stk.top();
        stk.pop();
        b = stk.top();
        stk.pop();
        switch (t.c_str()[0]) {
        case '+':
          stk.push(b + a);
          break;
        case '-':
          stk.push(b - a);
          break;
        case '*':
          stk.push(b * a);
          break;
        case '/':
          stk.push(b / a);
          break;
        }
      }
    }
    if (!stk.empty()) {
      cout << fixed << setprecision(5) << stk.top() << endl;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

int main() {
  for (string s; getline(cin, s);) {
    stringstream ss(s);
    stack<double> stk;
    for (string t; ss >> t;) {
      if (isdigit(t.c_str()[0])) {
        stk.push(atof(t.c_str()));
      } else {
        double a, b;
        a = stk.top();
        stk.pop();
        b = stk.top();
        stk.pop();
        switch (t.c_str()[0]) {
        case '+':
          stk.push(b + a);
          break;
        case '-':
          stk.push(b - a);
          break;
        case '*':
          stk.push(b * a);
          break;
        case '/':
          stk.push(b / a);
          break;
        }
      }
    }
    if (!stk.empty()) {
      cout << stk.top() << endl;
    }
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/*
int stringToint(const string S)
{
   int result=0;
   if(S[0]=='-')
   {
     for(int i=1;i<S.length();i++)
     {
       result=10*result+(S[i]-'0');
     }
     result=-result;
   }
   else
   {
     for(int i=0;i<S.length();i++)
     {
       result=10*result+(S[i]-'0');
     }
   }
   return result;
}
*/

int main() {
  string s;
  string word;
  stack<double> mm;
  while (cin >> word) {
    if (word == "+" || word == "-" || word == "*" || word == "/") {
      double a = mm.top();
      mm.pop();
      double b = mm.top();
      mm.pop();
      double result;
      if (word == "+")
        result = b + a;
      else if (word == "-")
        result = b - a;
      else if (word == "*")
        result = b * a;
      else
        result = b / a;
      mm.push(result);
    } else if (word.length() >= 2 && word[0] == '-' || (isdigit(word[0]))) {
      // double kk=1.0*(stringToint(word));
      mm.push(atoi(word.c_str()));
      // mm.push(kk);
    }
    if (getchar() == '\\n') {
      double final_result = mm.top();
      printf("%.10lf\\n", final_result);
      mm.pop();
    }
  }
  // while(1);
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/*
int stringToint(const string S)
{
   int result=0;
   if(S[0]=='-')
   {
     for(int i=1;i<S.length();i++)
     {
       result=10*result+(S[i]-'0');
     }
     result=-result;
   }
   else
   {
     for(int i=0;i<S.length();i++)
     {
       result=10*result+(S[i]-'0');
     }
   }
   return result;
}
*/

int main() {
  string s;
  string word;
  stack<double> mm;
  while (cin >> word) {
    if (word == "+" || word == "-" || word == "*" || word == "/") {
      double a = mm.top();
      mm.pop();
      double b = mm.top();
      mm.pop();
      double result;
      if (word == "+")
        result = b + a;
      else if (word == "-")
        result = b - a;
      else if (word == "*")
        result = b * a;
      else
        result = b / a;
      mm.push(result);
    } else if (word.length() == 2 && word[0] == '-' || (isdigit(word[0]))) {
      // double kk=1.0*(stringToint(word));
      mm.push(atoi(word.c_str()));
      // mm.push(kk);
    }
    if (getchar() == '\\n') {
      double final_result = mm.top();
      printf("%.10lf\\n", final_result);
      mm.pop();
    }
  }
  // while(1);
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <sstream>
#include <stack>
using namespace std;
int main(void) {
  while (cin.eof() == false) {
    stack<double> s;

    string input;
    getline(cin, input);
    if (cin.eof())
      break;

    string token;
    istringstream is(input);
    while (is >> token) {
      istringstream is2(token);
      istringstream is3(token);
      int n;
      char c;
      if (is2 >> n) {
        s.push(n);
      } else if (is3 >> c) {
        double n1 = s.top();
        s.pop();
        double n2 = s.top();
        s.pop();
        double n3 = 0.0;

        if (c == '+') {
          n3 = n2 + n1;
        } else if (c == '-') {
          n3 = n2 - n1;
        } else if (c == '*') {
          n3 = n2 * n1;
        } else if (c == '/') {
          n3 = n2 / n1;
        }

        s.push(n3);
      }
    }

    double answer = 0.0;
    if (s.empty() == false) {
      answer = s.top();
    }
    cout.setf(ios::fixed);
    cout.precision(6);
    cout << answer << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <sstream>
#include <stack>
using namespace std;
int main(void) {
  while (cin.eof() == false) {
    stack<double> s;

    string input;
    getline(cin, input);
    if (cin.eof())
      break;

    string token;
    istringstream is(input);
    while (is >> token) {
      istringstream is2(token);
      int n = -1;
      string c = "#";
      is2 >> n;
      if (n != -1) {
        s.push(n);
      } else {
        if (s.empty())
          continue;
        c = token;
        double n2 = s.top();
        s.pop();
        double n1 = s.top();
        s.pop();

        double n3;
        if (c == "+") {
          n3 = n1 + n2;
        } else if (c == "-") {
          n3 = n1 - n2;
        } else if (c == "*") {
          n3 = n1 * n2;
        } else if (c == "/") {
          n3 = n1 / n2;
        }

        s.push(n3);
      }
    }

    double answer = s.top();
    cout.setf(ios::fixed);
    cout.precision(6);
    cout << answer << endl;
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
  string in;
  while (getline(cin, in)) {
    stack<double> st;
    double a, b;
    for (int i = 0; i < in.size(); ++i) {
      if (in[i] == '+') {
        a = st.top();
        st.pop();
        b = st.top();
        st.pop();

        st.push(b + a);
        i++;
      } else if (in[i] == '-') {
        if (i + 1 < in.size() && '0' <= in[i + 1] && in[i + 1] <= '9') {
          i++;
          double tmp = 0;
          while (i < in.size() && in[i] != ' ') {
            tmp *= 10;
            tmp += in[i] - '0';
            i++;
          }
          st.push(tmp * (-1.0));
        } else {
          a = st.top();
          st.pop();
          b = st.top();
          st.pop();

          st.push(b - a);
          i++;
        }
      } else if (in[i] == '*') {
        a = st.top();
        st.pop();
        b = st.top();
        st.pop();

        st.push(b * a);
        i++;
      } else if (in[i] == '/') {
        a = st.top();
        st.pop();
        b = st.top();
        st.pop();

        st.push(b / a);
        i++;
      } else {
        double tmp = 0;
        while (i < in.size() && in[i] != ' ') {
          tmp *= 10;
          tmp += in[i] - '0';
          i++;
        }
        st.push(tmp);
      }
    }
    printf("%.9lf\\n", st.top());
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
  string in;
  while (getline(cin, in)) {
    stack<double> st;
    double a, b;
    for (int i = 0; i < in.size(); ++i) {

      if (in[i] == '+') {
        a = st.top();
        st.pop();
        b = st.top();
        st.pop();

        st.push(b + a);
        i++;
      } else if (in[i] == '-') {
        a = st.top();
        st.pop();
        b = st.top();
        st.pop();

        st.push(b - a);
        i++;
      } else if (in[i] == '*') {
        a = st.top();
        st.pop();
        b = st.top();
        st.pop();

        st.push(b * a);
        i++;
      } else if (in[i] == '/') {
        a = st.top();
        st.pop();
        b = st.top();
        st.pop();

        st.push(b / a);
        i++;
      } else {
        double tmp = 0;
        while (i < in.size() && in[i] != ' ') {
          tmp *= 10;
          tmp += in[i] - '0';
          i++;
        }
        st.push(tmp);
      }
    }
    printf("%.9lf\\n", st.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 s;
  stack<double> st;
  while (cin >> s) {
    if (s == "+" || s == "-" || s == "*" || s == "/") {
      double a, b;
      a = st.top();
      st.pop();
      b = st.top();
      st.pop();
      if (s == "+")
        st.push(b + a);
      else if (s == "-")
        st.push(b - a);
      else if (s == "*")
        st.push(b * a);
      else if (s == "/")
        st.push(b / a);
    } else
      st.push(atoi(s.c_str()));
    if (getchar() == '\\n') {
      printf("%f\\n", st.top());
      st.pop();
    }
  }
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  stack<double> st;
  stack<int> num;
  string s;
  while (getline(cin, s)) {
    for (int i = 0; i < s.size(); i++) {
      if (isdigit(s[i]))
        num.push(s[i] - '0');
      else {
        if (s[i] == ' ') {
          if (isdigit(s[i - 1])) {
            int n = 0;
            for (int j = 0; !num.empty(); j++) {
              n += num.top() * (int)pow(10.0, (double)j);
              num.pop();
            }
            st.push((double)n);
          }
        } else {
          double a, b;
          a = st.top();
          st.pop();
          b = st.top();
          st.pop();
          if (s[i] == '+')
            st.push(b + a);
          else if (s[i] == '-')
            st.push(b - a);
          else if (s[i] == '*')
            st.push(b * a);
          else if (s[i] == '/')
            st.push(b / a);
        }
      }
    }
    printf("%f\\n", st.top());
    st.pop();
  }
  return 0;
}