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

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

using namespace std;

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

Error: -11

Fixed submission:

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

string str;
int convert(char a) {
  switch (a) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  }
}
int main() {
  double a;
  double t;
  while (getline(cin, str)) {
    stack<double> values;
    a = 0;
    for (int i = 0; i < str.size(); i++) {
      if ('0' <= str[i] && str[i] <= '9')
        a = a * 10 + convert(str[i]);
      else if (str[i] != ' ') {
        t = values.top();
        values.pop();
        switch (str[i]) {
        case '+':
          t = values.top() + t;
          break;
        case '-':
          t = values.top() - t;
          break;
        case '*':
          t = values.top() * t;
          break;
        case '/':
          t = values.top() / t;
          break;
        }
        values.pop();
        a = t;
      } else {
        values.push(a);
        a = 0;
      }
    }
    cout << a << endl;
    str.clear();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;
inline double toInt(string s) {
  double v;
  istringstream sin(s);
  sin >> v;
  return v;
}

int main() {
  string str;
  for (; getline(cin, str);) {
    istringstream ss(str);
    stack<double, vector<double>> st;
    string tmp;
    for (; ss >> tmp;) {
      if (tmp == "+") {
        double a = st.top();
        st.pop();
        double b = st.top();
        st.pop();
        st.push(b + a);
      } else if (tmp == "-") {
        double a = st.top();
        st.pop();
        double b = st.top();
        st.pop();
        st.push(b - a);
      } else if (tmp == "*") {
        double a = st.top();
        st.pop();
        double b = st.top();
        st.pop();
        st.push(b * a);
      } else if (tmp == "/") {
        double a = st.top();
        st.pop();
        double b = st.top();
        st.pop();
        st.push(b / a);
      } else {
        st.push(toInt(tmp));
        //              cout<<"tmp"<<st.top()<<endl;
      }
    }
    cout << fixed << setprecision(6) << st.top() << endl;
  }
}

Error: -11

Fixed submission:

#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}

vector<string> split(const string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while ((found = str.find_first_of(delim, current)) != string::npos) {
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int main() {
  string str;
  for (; getline(cin, str);) {
    vector<string> data = split(str, ' ');
    stack<double, vector<double>> st;
    for (int i = 0; i < data.size(); i++) {
      double tmp;
      string ss = data[i];
      if (ss[0] >= '0' && ss[0] <= '9')
        tmp = (double)toInt(ss);
      else {
        double data2 = st.top();
        st.pop();
        double data1 = st.top();
        st.pop();
        if (ss[0] == '+')
          tmp = data1 + data2;
        else if (ss[0] == '-')
          tmp = data1 - data2;
        else if (ss[0] == '*')
          tmp = data1 * data2;
        else if (ss[0] == '/')
          tmp = data1 / data2;
      }
      st.push(tmp);
    }
    cout << fixed << setprecision(6);
    cout << st.top() << endl;
    st.pop();
  }
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

double operate(double a, double b, string opc) {
  return (opc == "+" ? a + b : opc == "-" ? a - b : opc == "*" ? a * b : a / b);
}

int main() {
  string line;
  while (getline(cin, line)) {
    istringstream iss(line);
    stack<double> st;
    string item;
    while (iss >> item) {
      if (('0' <= item[0] && item[0] <= '9') ||
          (item[0] == '-' && item.size() >= 2)) {
        st.push(atof(item.c_str()));
      } else {
        double b = st.top();
        st.pop();
        double a = st.top();
        st.pop();
        st.push(operate(a, b, item));
      }
    }
    printf("%.6f\\n", st.top());
    st.pop();
  }
  return EXIT_SUCCESS;
}

Error: -11

Fixed submission:

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

double operate(double a, double b, string opc) {
  return (opc == "+" ? a + b : opc == "-" ? a - b : opc == "*" ? a * b : a / b);
}

int main() {
  string line;
  while (getline(cin, line)) {
    istringstream iss(line);
    stack<double> st;
    string item;
    while (iss >> item) {
      if ('0' <= item[0] && item[0] <= '9') {
        st.push(atoi(item.c_str()));
      } else {
        int b = st.top();
        st.pop();
        int a = st.top();
        st.pop();
        st.push(operate(a, b, item));
      }
    }
    printf("%.6f\\n", st.top());
    st.pop();
  }
  return EXIT_SUCCESS;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <stack>
using namespace std;

char buf[100000];

int main() {
  while (1) {
    if (fgets(buf, sizeof(buf), stdin) == NULL)
      return 0;

    char *p;
    p = strchr(buf, '\\n');
    if (p)
      *p = '\\0';

    stack<double> st;
    int n = strlen(buf);
    p = buf;
    while (p < buf + n) {
      if (*p == ' ') {
        ++p;
        continue;
      }

      char *next;
      long t = strtol(p, &next, 10);
      if (p != next) {
        st.push(t);
        p = next;
      } else {
        double b = st.top();
        st.pop();
        double a = st.top();
        st.pop();

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

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

Error: -11

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <stack>
using namespace std;

char buf[100000];

int main() {
  while (1) {
    if (fgets(buf, sizeof(buf), stdin) == NULL)
      return 0;

    char *p;
    p = strchr(buf, '\\n');
    if (p)
      *p = '\\0';

    stack<double> st;
    int n = strlen(buf);
    p = buf;
    while (p < buf + n) {
      if (*p == ' ') {
        ++p;
        continue;
      }

      if (isdigit(*p)) {
        char *next;
        st.push(strtoll(p, &next, 10));
        p = next;
      } else {
        if (st.size() < 2)
          break;

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

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

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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
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.length() > 1) {
        s.push(atoi(str.c_str()));
      } else {
        double a = s.top();
        s.pop();
        double b = s.top();
        s.pop();
        double c;
        if (str[0] == '+')
          c = a + b;
        else if (str[0] == '-')
          c = b - a;
        else if (str[0] == '*')
          c = a * b;
        else
          c = b / a;
        s.push(c);
      }
    }
    printf("%.6lf\\n", s.top());
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
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' || s.size() > 1) {
        s.push(atoi(str.c_str()));
      } else {
        double a = s.top();
        s.pop();
        double b = s.top();
        s.pop();
        double c;
        if (str[0] == '+')
          c = a + b;
        else if (str[0] == '-')
          c = b - a;
        else if (str[0] == '*')
          c = a * b;
        else
          c = b / a;
        s.push(c);
      }
    }
    printf("%.6lf\\n", s.top());
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
int main() {
  string str;
  while (getline(cin, str)) {
    stack<double> s;
    stringstream ss;
    ss << str;
    while (ss >> str) {
      if (isdigit(str[0]) || str.length() > 1) {
        s.push(atoi(str.c_str()));
      } else {
        double a = s.top();
        s.pop();
        double b = s.top();
        s.pop();
        double c;
        if (str[0] == '+')
          c = a + b;
        else if (str[0] == '-')
          c = b - a;
        else if (str[0] == '*')
          c = a * b;
        else
          c = b / a;
        s.push(c);
      }
    }
    printf("%.6lf\\n", s.top());
  }
  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
int main() {
  string str;
  while (getline(cin, str)) {
    stack<double> s;
    stringstream ss;
    ss << str;
    while (ss >> str) {
      if (isdigit(str[0])) {
        s.push(atoi(str.c_str()));
      } else {
        double a = s.top();
        s.pop();
        double b = s.top();
        s.pop();
        double c;
        if (str[0] == '+')
          c = a + b;
        else if (str[0] == '-')
          c = b - a;
        else if (str[0] == '*')
          c = a * b;
        else
          c = b / a;
        s.push(c);
      }
    }
    printf("%.6lf\\n", s.top());
  }
  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 {
      // 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: -11

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;
  while (cin >> word) {
    stack<double> mm;
    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 {
      // 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 <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 {
      // 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.length() == 2 && word[0] == '-') || (isdigit(word[0]))) {
      // double kk=1.0*(stringToint(word));
      mm.push(atoi(word.c_str()));
      // mm.push(kk);
    } else 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);
    }
    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 <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int main() {
  int num = 0;
  double p, q;
  stack<double> st;
  string s;
  int sign = 1;
  while (getline(cin, s)) {
    s = s + " ";
    for (int i = 0; i < s.length(); ++i) {
      if (isdigit(s[i])) {
        num *= 10;
        num += s[i] - '0';
      } else if (s[i] == '-' && isdigit(s[i + 1])) {
        sign = -1;
      } else if (s[i] == ' ' && isdigit(s[i - 1])) {
        num = num * sign;
        st.push(num);
        num = 0;
        sign = 1;
      } else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
        p = st.top();
        st.pop();
        q = st.top();
        st.pop();
        if (s[i] == '+')
          st.push(p + q);
        if (s[i] == '-')
          st.push(q - p);
        if (s[i] == '*')
          st.push(p * q);
        if (s[i] == '/')
          st.push(q / p);
      }
    }
    cout << fixed << setprecision(9) << st.top() << endl;
    st.pop();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int main() {
  int num = 0;
  double p, q;
  stack<double> st;
  string s;
  int sign = 1;
  while (getline(cin, s)) {
    for (int i = 0; i < s.length(); ++i) {
      if (isdigit(s[i])) {
        num *= 10;
        num += s[i] - '0';
      } else if (s[i] == '-' && Isdigit(s[i + 1])) {
        sign = -1;
      } else if (s[i] == ' ' && Isdigit(s[i - 1])) {
        num = num * sign;
        st.push(num);
        num = 0;
        sign = 1;
      } else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
        p = st.top();
        st.pop();
        q = st.top();
        st.pop();
        if (s[i] == '+')
          st.push(p + q);
        if (s[i] == '-')
          st.push(q - p);
        if (s[i] == '*')
          st.push(p * q);
        if (s[i] == '/')
          st.push(q / p);
      }
    }
    cout << fixed << setprecision(9) << st.top() << endl;
    st.pop();
  }
  return 0;
}
Problem p00087 (C++) — buggy submission, verdict: Runtime Error

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

vector<string> parse(string s) {
  s += " ";
  vector<string> res;
  string str;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == ' ') {
      res.push_back(str);
      str = "";
    } else
      str += s[i];
  }

  return res;
}

int main() {
  string s;
  while (getline(cin, s)) {
    vector<string> v = parse(s);

    // cout << s << endl;
    // for (string u : v) cout << u << endl;

    stack<double> st;
    for (string u : v) {
      if (u.size() == 1 && !('0' <= u[0] && u[0] <= '9')) {
        double b = st.top();
        st.pop();
        double a = st.top();
        st.pop();
        double x;
        if (u == "+")
          x = a + b;
        if (u == "-")
          x = a - b;
        if (u == "*")
          x = a * b;
        if (u == "/")
          x = a / b;
        st.push(x);
        // cout << a << " " << b << " " << x << endl;
      } else {
        double x = 0, num = 1;
        for (int i = 0; i < u.size(); ++i) {
          if ('0' <= u[i] && u[i] <= '9') {
            x = 10 * x + (u[i] - '0');
          } else
            num = -1;
        }

        st.push(x * num);
      }
    }

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

Error: WA

Fixed submission:

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

int main() {
  string po;

  while (true) {
    getline(cin, po);
    if (po.size() == 0)
      break;

    stack<double> s;
    double x = 0, n = 1;
    bool f = false;
    for (int i = 0; i < po.size(); ++i) {
      if (po[i] == ' ') {
        if (f)
          s.push(x * n);
        x = 0;
        n = 1;
        f = false;
      } else if (po[i] == '-' && '0' <= po[i + 1] && po[i + 1] <= '9') {
        n = -1;
      } else if ('0' <= po[i] && po[i] <= '9') {
        x = 10 * x + (po[i] - '0');
        f = true;
      } else {
        double y = s.top();
        s.pop();
        double x = s.top();
        s.pop();

        if (po[i] == '+')
          s.push(x + y);
        if (po[i] == '-')
          s.push(x - y);
        if (po[i] == '*')
          s.push(x * y);
        if (po[i] == '/')
          s.push(x / y);
        // cout << x << " " << y << " " << s.top() << endl;
      }
    }

    printf("%.10f\\n", s.top());
    // cout << po << endl;
  }
}