intstoi(string str){ int num = 0, i = 0; if (str[0] == '-') ++i; for ( ; i < str.length(); ++i) { num = num * 10 + str[i] - '0'; } if (str[0] == '-') num = -num; return num; }
stringgetResult(string postfix[], int size){ Stack data; char num[MAX_SIZE]; memset(num, 0, MAX_SIZE * sizeof(char)); for (int i = 0; i < size; ++i) { if (postfix[i] == "+") { int right = stoi(data.pop()); int left = stoi(data.pop()); sprintf(num, "%d", left + right); data.push(string(num)); } elseif (postfix[i] == "-") { int right = stoi(data.pop()); int left = stoi(data.pop()); sprintf(num, "%d", left - right); data.push(string(num)); } elseif (postfix[i] == "/") { int right = stoi(data.pop()); int left = stoi(data.pop()); sprintf(num, "%d", left / right); data.push(string(num)); } elseif (postfix[i] == "*") { int right = stoi(data.pop()); int left = stoi(data.pop()); sprintf(num, "%d", left * right); data.push(string(num)); } else { data.push(postfix[i]); } memset(num, 0, MAX_SIZE * sizeof(char)); } return data.pop(); }
评论