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

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

int top, S[1000];

void push(int x);
int pop();

int main() {
  top = 0;
  int a, b;
  char c;

  while (scanf("%s", &c) != EOF) {
    switch (c) {
    case '+':
      b = pop();
      a = pop();
      push(a + b);
      break;
    case '-':
      b = pop();
      a = pop();
      push(a - b);
      break;
    case '*':
      b = pop();
      a = pop();
      push(a * b);
      break;
    default:
      push(atoi(&c));
      break;
    }
  }

  cout << pop() << endl;
}

void push(int x) {
  ++top;
  S[top] = x;
}

int pop() {
  --top;
  return S[top + 1];
}

Error: -11

Fixed submission:

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

int top, S[1000];

void push(int x);
int pop();

int main() {
  top = 0;
  int a, b;
  char c;

  while (scanf("%s", &c)) {
    switch (c) {
    case '+':
      b = pop();
      a = pop();
      push(a + b);
      break;
    case '-':
      b = pop();
      a = pop();
      push(a - b);
      break;
    case '*':
      b = pop();
      a = pop();
      push(a * b);
      break;
    default:
      push(atoi(&c));
      break;
    }
  }

  cout << pop() << endl;
}

void push(int x) {
  ++top;
  S[top] = x;
}

int pop() {
  --top;
  return S[top + 1];
}
Problem p02263 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAX 200
#define max 1000000

void initialize();
int isEmpty();
int isFull();
void push(int);
int pop();

int top;
int S[max];

int main() {
  char c[max];
  int sum = 0;
  int a, b;

  initialize();

  while (cin >> c) {
    if (c[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
    } else if (c[0] == '+') {
      push(pop() + pop());
    } else if (c[0] == '*') {
      push(pop() * pop());
    } else {
      push(atoi(c));
    }
  }

  printf("%d\\n", S[1]);

  return 0;
}

void initialize() { top = 0; }

int isEmpty() {
  if (top == 0)
    return 1;
  return 0;
}

int isFull() {
  if (top >= MAX - 1)
    return 1;
  return 0;
}

void push(int x) {
  if (isFull())
    fprintf(stderr, "Overflow");
  top++;
  S[top] = x;
}

int pop() {
  if (isEmpty())
    fprintf(stderr, "Anderflow");
  top--;
  return S[top + 1];
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAX 100
#define max 1000000

void initialize();
int isEmpty();
int isFull();
void push(int);
int pop();

int top;
int S[max];

int main() {
  char c[max];
  int sum = 0;
  int a, b;

  initialize();

  while (cin >> c) {
    if (c[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
    } else if (c[0] == '+') {
      push(pop() + pop());
    } else if (c[0] == '*') {
      push(pop() * pop());
    } else {
      push(atoi(c));
    }
  }

  printf("%d\\n", S[1]);

  return 0;
}

void initialize() { top = 0; }

int isEmpty() {
  if (top == 0)
    return 1;
  return 0;
}

int isFull() {
  if (top >= MAX - 1)
    return 1;
  return 0;
}

void push(int x) {
  if (isFull())
    fprintf(stderr, "Overflow");
  top++;
  S[top] = x;
}

int pop() {
  if (isEmpty())
    fprintf(stderr, "Anderflow");
  top--;
  return S[top + 1];
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

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

const int maxn = 100 + 5;

int top = 0;
int S[1000];

void push(int key) {
  S[++top] = key;
  return;
}

int pop() {
  top--;
  return S[top + 1];
}

int main() {
  int a, b;
  top = 0;
  char s[maxn];
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(s));
    }
  }
  printf("%d\\n", pop());
}

Error: -11

Fixed submission:

#include <cctype>
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<int> k;
int main() {
  string s;
  int a, b;
  while (cin >> s) {
    while (!k.empty())
      k.pop();
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '+') {
        a = k.top();
        k.pop();
        b = k.top();
        k.pop();
        k.push(a + b);
      } else if (s[i] == '-') {
        a = k.top();
        k.pop();
        b = k.top();
        k.pop();
        k.push(b - a);
      } else if (s[i] == '*') {
        a = k.top();
        k.pop();
        b = k.top();
        k.pop();
        k.push(a * b);
      } else
        k.push(s[i] - '0');
    }
    cout << k.top() << endl;
  }
  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string s;
  int a, b;
  stack<int> st;
  while (cin >> s) {
    if (s[0] == '+') {
      a = st.top();
      st.pop();
      b = st.top();
      st.pop();
      st.push(b + a);
    } else if (s[0] == '*') {
      a = st.top();
      st.pop();
      b = st.top();
      st.pop();
      st.push(b * a);
    } else if (s[0] == '-') {
      a = st.top();
      st.pop();
      b = st.top();
      st.pop();
      st.push(b - a);
    } else {
      st.push(atoi(s.c_str()));
    }
  }
  cout << st.top() << endl;
}

Error: 0
1
2
3


Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int MOD = 1e9 + 7;

int main() {
  string s;
  int a, b;
  stack<int> st;
  getline(cin, s);
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '+') {
      a = st.top();
      st.pop();
      b = st.top();
      st.pop();
      st.push(b + a);
    } else if (s[i] == '-') {
      a = st.top();
      st.pop();
      b = st.top();
      st.pop();
      st.push(b - a);
    } else if (s[i] == '*') {
      a = st.top();
      st.pop();
      b = st.top();
      st.pop();
      st.push(b * a);
    } else if (s[i] == ' ') {
      continue;
    } else {
      st.push(s[i] - '0');
    }
    cerr << st.top() << endl;
  }
  cout << st.top() << endl;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

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

#define N 101

class Stack {
public:
  void initialize();
  bool isEmpty();
  bool isFull();
  void push(const int);
  int pop();
  Stack(const int);
  ~Stack();

private:
  int top, STACKSIZE;
  vector<int> S;
};

void addition(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(a + b);
}

void subtraction(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(b - a);
}

void multiplication(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(b * a);
}

int main() {
  Stack _s(N);
  char s[N];

  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      addition(_s);
    }

    else if (s[0] == '-') {
      subtraction(_s);
    }

    else if (s[0] == '*') {
      multiplication(_s);
    }

    else {
      _s.push(atoi(s));
    }
  }

  cout << _s.pop() << endl;

  return 0;
}

Stack::Stack(const int s) : top(0) {
  STACKSIZE = s;
  vector<int> tmp(STACKSIZE, 0);
  S = tmp;
}

Stack::~Stack() {
  // cout << "STACK DELETED" << endl;
}

void Stack::initialize() { top = 0; }

bool Stack::isEmpty() { return (top == 0); }

bool Stack::isFull() { return (top >= STACKSIZE - 1); }

void Stack::push(const int x) {
  if (isFull()) {
    cout << "Overflow!" << endl;
    exit(1);
  }

  top += 1;
  S[top] = x;
}

int Stack::pop() {
  if (isEmpty()) {
    cout << "Underflow" << endl;
    exit(2);
  }

  top -= 1;
  return (S[top + 1]);
}


Error: 0

Fixed submission:

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

#define N 100

class Stack {
public:
  void initialize();
  bool isEmpty();
  bool isFull();
  void push(const int);
  int pop();
  Stack(const int);
  ~Stack();

private:
  int top, STACKSIZE;
  vector<int> S;
};

void addition(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(a + b);
}

void subtraction(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(b - a);
}

void multiplication(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(b * a);
}

int main() {
  Stack _s(N);
  char s[N];

  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      addition(_s);
    }

    else if (s[0] == '-') {
      subtraction(_s);
    }

    else if (s[0] == '*') {
      multiplication(_s);
    }

    else {
      _s.push(atoi(s));
    }
  }

  cout << _s.pop() << endl;

  return 0;
}

Stack::Stack(const int s) : top(0) {
  STACKSIZE = s;
  vector<int> tmp(STACKSIZE, 0);
  S = tmp;
}

Stack::~Stack() {
  // cout << "STACK DELETED" << endl;
}

void Stack::initialize() { top = 0; }

bool Stack::isEmpty() { return (top == 0); }

bool Stack::isFull() { return (top >= STACKSIZE - 1); }

void Stack::push(const int x) {
  if (isFull()) {
    cout << "Overflow!" << endl;
    exit(1);
  }

  top += 1;
  S[top] = x;
}

int Stack::pop() {
  if (isEmpty()) {
    cout << "Underflow" << endl;
    exit(2);
  }

  top -= 1;
  return (S[top + 1]);
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

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

int top, S[101];

void push(int x) { S[++top] = x; }

int pop() { return S[top--]; }

int main() {
  char str[10];
  int a, b;
  top = 0;

  while (scanf("%s", str) != EOF) {
    if (str[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (str[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
    } else if (str[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(str));
    }
  }
  cout << pop() << endl;
}

Error: 0

Fixed submission:

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

int top, S[100];

void push(int x) { S[++top] = x; }

int pop() { return S[top--]; }

int main() {
  char str[10];
  int a, b;
  top = 0;

  while (scanf("%s", str) != EOF) {
    if (str[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (str[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
    } else if (str[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(str));
    }
  }
  cout << pop() << endl;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  stack<int> st;

  string s;
  while (cin >> s) {
    if (s == "+") {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      st.push(a + b);
    } else if (s == "*") {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      st.push(a * b);
    } else if (s == "-") {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      st.push(b - a);
    } else {
      int x = stoi(s);
      st.push(x);
    }
  }
  cout << st.top() << endl;
}

Error: 0

Fixed submission:

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

int main() {

  stack<int> st;

  string s;
  int ans = 0;
  while (cin >> s) {
    if (s == "+") {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      ans += a + b;
    } else if (s == "*") {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      ans += a * b;
    } else if (s == "-") {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      ans += b - a;
    } else {
      int x = stoi(s);
      st.push(x);
    }
  }
  cout << ans << endl;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

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

#define dStackSize 500

int stack[dStackSize];
int top;

void initialize();
bool isFull();
bool isEmpty();
void Dump();
void Push(int x);
int Pop();

int main() {
  initialize();
  string buf;
  int a, b;

  while (cin >> buf) {
    if (buf[0] == '+') {
      a = Pop();
      b = Pop();
      Push(b + a);
      // cout << b << "+" << a << "=" << b + a << endl;
    } else if (buf[0] == '-') {
      a = Pop();
      b = Pop();
      Push(b - a);
    } else if (buf[0] == '*') {
      a = Pop();
      b = Pop();
      Push(b * a);
    } else {
      Push(stoi(buf));
    }
    // Dump();
  }
  cout << Pop() << endl;

  return 0;
}

void initialize() { top = -1; }
bool isFull() {
  if (top + 1 == dStackSize) {
    return true;
  }

  return false;
}

bool isEmpty() {
  if (top == -1) {
    return true;
  }

  return false;
}

void Dump() {
  for (int i = top; i >= 0; i--) {
    cout << i << ":" << stack[i] << endl;
  }
}

void Push(int x) {
  if (isFull() == true) {
    cout << "stack is full" << endl;
    exit(1);
  }
  top++;
  stack[top] = x;
}

int Pop() {
  if (isEmpty() == true) {
    return -1;
  }
  int pop = stack[top];
  top--;

  return pop;
}

Error: 1

Fixed submission:

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

#define dStackSize 500

int stack[dStackSize];
int top;

void initialize();
bool isFull();
bool isEmpty();
void Dump();
void Push(int x);
int Pop();

int main() {
  initialize();
  char buf;
  int a, b;

  while (cin >> buf) {
    if (buf == '+') {
      a = Pop();
      b = Pop();
      Push(b + a);
    } else if (buf == '-') {
      a = Pop();
      b = Pop();
      Push(b - a);
    } else if (buf == '*') {
      a = Pop();
      b = Pop();
      Push(b * a);
    } else {
      Push(atoi(&buf));
    }
  }
  cout << Pop() << endl;

  return true;
}

void initialize() { top = -1; }
bool isFull() {
  if (top + 1 == dStackSize) {
    return true;
  }

  return false;
}

bool isEmpty() {
  if (top == -1) {
    return true;
  }

  return false;
}

void Dump() {
  for (int i = top; i >= 0; i--) {
    cout << i << ":" << stack[i] << endl;
  }
}

void Push(int x) {
  if (isFull() == true) {
    cout << "stack is full" << endl;
    exit(1);
  }
  top++;
  stack[top] = x;
}

int Pop() {
  if (isEmpty() == true) {
    return -1;
  }
  char pop = stack[top];
  top--;

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

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

stack<int> s;

int main() {

  while (!s.empty()) {
    s.pop();
  }
  string tmp = "";
  while (getline(cin, tmp, ' ')) {
    stringstream ss;
    int a = 0;
    if (isdigit(tmp[0])) {
      ss << tmp;
      ss >> a;
      s.push(a);
    } else {
      string sign;
      sign = tmp;
      int left, right;
      right = s.top();
      s.pop();
      left = s.top();
      s.pop();
      switch (sign[0]) {
      case '+':
        s.push(left + right);
        break;
      case '-':
        s.push(left - right);
        break;
      case '*':
        s.push(left * right);
        break;
      case '/':
        s.push(left / right);
        break;
      default:
        break;
      }
      // cout << s.top() << endl;
    }
  }

  cout << s.top() << endl;

  return 0;
}

Error: -11

Fixed submission:

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

stack<int> s;

int main() {

  while (!s.empty()) {
    s.pop();
  }

  while (cin.eof()) {
    string tmp = "";
    cin >> tmp;
    stringstream ss;
    int a = 0;
    if (isdigit(tmp[0])) {
      ss << tmp;
      ss >> a;
      s.push(a);
    } else {
      string sign;
      sign = tmp;
      int left, right;
      right = s.top();
      s.pop();
      left = s.top();
      s.pop();
      switch (sign[0]) {
      case '+':
        s.push(left + right);
        break;
      case '-':
        s.push(left - right);
        break;
      case '*':
        s.push(left * right);
        break;
      case '/':
        s.push(left / right);
        break;
      default:
        break;
      }
      // cout << s.top() << endl;
    }
  }

  cout << s.top() << endl;

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

#include <stdio.h>
int main(void) {
  int p, r, a, b, i_p;
  int stack[100];
  char c;
  char input[399];
  int state; // 0 = normal 1 = number

  for (int i = 0; i < 399; i++) {
    input[i] = 0;
  }

  scanf("%399[^\\n]", input);

  p = 0;
  r = 0;
  i_p = 0;
  state = 0;
  for (int i = 0; i < 100; i++) {
    stack[i] = 0;
  }

  while (i_p <= 398 && input[i_p] != 0) {
    c = input[i_p++];
    switch (c) {
    case ' ':
      if (state == 1) {
        state = 0;
        stack[p++] = r;
      }
      break;
    case '+':
      a = stack[--p];
      b = stack[--p];
      stack[p++] = b + a;
      break;
    case '-':
      a = stack[--p];
      b = stack[--p];
      stack[p++] = b - a;
      break;
    case '*':
      a = stack[--p];
      b = stack[--p];
      stack[p++] = b * a;
      break;
    default:
      if (state == 0) {
        state = 1;
        r = c - '0';
      } else {
        r = (10 * r) + (c - '0');
      }

      break;
    }
  }

  printf("%d\\n", stack[--p]);
}


Error: 0

Fixed submission:

#include <stdio.h>
int main(void) {
  int p, r, a, b, i_p;
  int stack[50];
  char c;
  char input[199];
  int state; // 0 = normal 1 = number

  for (int i = 0; i < 199; i++) {
    input[i] = 0;
  }

  scanf("%199[^\\n]", input);

  p = 0;
  r = 0;
  i_p = 0;
  state = 0;
  for (int i = 0; i < 50; i++) {
    stack[i] = 0;
  }

  while (i_p <= 198 && input[i_p] != 0) {
    c = input[i_p++];
    switch (c) {
    case ' ':
      if (state == 1) {
        state = 0;
        stack[p++] = r;
      }
      break;
    case '+':
      a = stack[--p];
      b = stack[--p];
      stack[p++] = b + a;
      break;
    case '-':
      a = stack[--p];
      b = stack[--p];
      stack[p++] = b - a;
      break;
    case '*':
      a = stack[--p];
      b = stack[--p];
      stack[p++] = b * a;
      break;
    default:
      if (state == 0) {
        state = 1;
        r = c - '0';
      } else {
        r = (10 * r) + (c - '0');
      }

      break;
    }
  }

  printf("%d\\n", stack[--p]);
}
Problem p02263 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  stack<long long> st;
  string c;
  while (cin >> c) {
    if (c == "+") {
      long long a = st.top();
      st.pop();
      long long b = st.top();
      st.pop();
      st.push(a + b);
    } else if (c == "-") {
      long long a = st.top();
      st.pop();
      long long b = st.top();
      st.pop();
      st.push(b - a);
    } else if (c == "*") {
      long long a = st.top();
      st.pop();
      long long b = st.top();
      st.pop();
      st.push(a * b);
    } else {
      st.push(stoi(c));
    }
  }
  cout << st.top() << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

int main() {
  stack<long long> st;
  char c;
  while (scanf("%s", c) != EOF) {
    if (c == '+') {
      long long a = st.top();
      st.pop();
      long long b = st.top();
      st.pop();
      st.push(a + b);
    } else if (c == '-') {
      long long a = st.top();
      st.pop();
      long long b = st.top();
      st.pop();
      st.push(b - a);
    } else if (c == '*') {
      long long a = st.top();
      st.pop();
      long long b = st.top();
      st.pop();
      st.push(a * b);
    } else {
      st.push(atoi(&c));
    }
  }
  cout << st.top() << endl;
  return 0;
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

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

void add(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] += num[*pa];
  num[*pa] = 0;
}

void minus(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] -= num[*pa];
  num[*pa] = 0;
}

void multiply(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] *= num[*pa];
  num[*pa] = 0;
}

int main() {
  int i, n, j, k, num[200];
  char s[1000];
  int curnum = 0;
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      add(num, &curnum);
    } else if (s[0] == '-') {
      minus(num, &curnum);
    } else if (s[0] == '*') {
      multiply(num, &curnum);
    } else {
      sscanf(s, "%d", &num[curnum]);
      curnum++;
    }
  }
  printf("%d\\n", num[0]);
}


Error: -11

Fixed submission:

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

void add(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] += num[*pa];
  num[*pa] = 0;
}

void minus(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] -= num[*pa];
  num[*pa] = 0;
}

void multiply(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] *= num[*pa];
  num[*pa] = 0;
}

int main() {
  int i, n, j, k, num[200];
  char s[1000];
  int curnum = 0;
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      add(num, &curnum);
    } else if (s[0] == '-') {
      minus(num, &curnum);
    } else if (s[0] == '*') {
      multiply(num, &curnum);
    } else {
      sscanf(s, "%d", num[curnum]);
      curnum++;
    }
  }
  printf("%d\\n", num[0]);
}
Problem p02263 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
#include <string.h>
#include <string>
using namespace std;
stack<int> S1;
char opt[3] = {'+', '-', '*'};
bool isOpt(char a) {
  for (int i = 0; i < 3; i++) {
    if (a == opt[i])
      return true;
  }
  return false;
}
int main() {
  string temp;
  char a;
  while (cin >> temp) {
    if (temp.length() == 1) {
      a = temp[0];
    } else {
      a = '0';
    }
    if (isOpt(a)) { // 是操作码,弹出两个操作数，执行操作 s2 opt s1
      int x1 = S1.top();
      S1.pop();
      int x2 = S1.top();
      S1.pop();
      int y1;
      switch (a) {
      case '+':
        y1 = x1 + x2;
        S1.push(y1);
        break;
      case '-':
        y1 = x2 - x1;
        S1.push(y1);
        break;
      case '*':
        y1 = x1 * x2;
        S1.push(y1);
        break;
      }
    } else { // 是操作数
      int x = atoi(temp.c_str());
      S1.push(x);
    }
  }
  int ans = S1.top();
  S1.pop();
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

stack<string> S1;
char opt[3] = {'+', '-', '*'};
bool isOpt(char a) {
  for (int i = 0; i < 3; i++) {
    if (a == opt[i])
      return true;
  }
  return false;
}
int main() {
  string temp;
  char a;
  while (cin >> temp) {
    if (temp.length() == 1) {
      a = temp[0];
    }
    if (isOpt(a)) { // 是操作码,弹出两个操作数，执行操作 s2 opt s1
      string s1 = S1.top();
      S1.pop();
      string s2 = S1.top();
      S1.pop();
      int x1 = atoi(s1.c_str());
      int x2 = atoi(s2.c_str());
      int y1;
      string y2;
      switch (a) {
      case '+':
        y1 = x1 + x2;
        y2 = to_string(y1);
        S1.push(y2);
        break;
      case '-':
        y1 = x2 - x1;
        y2 = to_string(y1);
        S1.push(y2);
        break;
      case '*':
        y1 = x1 * x2;
        y2 = to_string(y1);
        S1.push(y2);
        break;
      }
    } else { // 是操作数
      S1.push(temp);
    }
  }
  string ans = S1.top();
  S1.pop();
  cout << ans << endl;
  return 0;
}