酷代码 AI
菜单
服务商

#include <iostream> #define MAXSIZE 100 #define OK 1 #define ERROR 0 #define OVERFLOW -2 using namespace std; typedef struct {//符号栈 char* base; char* top; int stacksize; }SqStack1; int InitStack1(SqStack1& S) {//符号栈初始化 return OK; } int Push1(SqStack1& S, char e) {//符号栈入栈 return OK; } int Pop1(SqStack1& S) {//符号栈出栈 return OK; } char GetTop1(SqStack1 S) {//符号栈取栈顶元素 return ERROR; } typedef struct {//数字栈 double* base; double* top; int stacksize; }SqStack2; int InitStack2(SqStack2&

```cpp #include <iostream> #include <iomanip> #include <cstring> #include <cmath> #define MAXSIZE 100 #define OK 1 #define ERROR 0 #define OVERFLOW -2 using namespace std; typedef struct { char* base; char* top; int stacksize; } SqStack1; int InitStack1(SqStack1& S) { S.base = new char[MAXSIZE]; if (!S.base) return OVERFLOW; S.top = S.base; S.stacksize = MAXSIZE; return OK; } int Push1(SqStack1& S, char e) { if (S.top - S.base == S.stacksize) return ERROR; *S.top++ = e; return OK; } int Pop1(SqStack1& S) { if (S.top == S.base) return ERROR; S.top--; return OK; } char GetTop1(SqStack1 S) { if (S.top == S.base) return ERROR; return *(S.top - 1); } typedef struct { double* base; double* top; int stacksize; } SqStack2; int InitStack2(SqStack2& S) { S.base = new double[MAXSIZE]; if (!S.base) return OVERFLOW; S.top = S.base; S.stacksize = MAXSIZE; return OK; } int Push2(SqStack2& S, double e) { if (S.top - S.base == S.stacksize) return ERROR; *S.top++ = e; return OK; } int Pop2(SqStack2& S) { if (S.top == S.base) return ERROR; S.top--; return OK; } double GetTop2(SqStack2 S) { if (S.top == S.base) return ERROR; return *(S.top - 1); } double Calculate(double a, char op, double b) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': if (fabs(b) < 1e-6) { cout << "除数不能为0" << endl; exit(0); } return a / b; default: return 0; } } char Precede(char a, char b) { if (a == '+' || a == '-') { if (b == '+' || b == '-' || b == ')' || b == '=') return '>'; else return '<'; } else if (a == '*' || a == '/') { if (b == '(') return '<'; else return '>'; } else if (a == '(') { if (b == ')') return '='; else if (b == '=') return ' '; else return '<'; } else if (a == ')') { if (b == '(') return ' '; else return '>'; } else if (a == '=') { if (b == '=') return '='; else if (b == ')') return ' '; else return '<'; } return ' '; } int main() { string str; while (getline(cin, str)) { if (str == "=") break; SqStack1 OPTR; // 运算符栈 SqStack2 OPND; // 操作数栈 InitStack1(OPTR); InitStack2(OPND); Push1(OPTR, '='); int i = 0; int flag = 0; // 负数标志 int decimal = 0; // 小数标志 double num = 0; double factor = 0.1; while (i < str.length()) { if (str[i] == ' ') { i++; continue; } // 处理数字 if ((str[i] >= '0' && str[i] <= '9') || (str[i] == '-' && (i == 0 || str[i-1] == '('))) { if (str[i] == '-') { flag = 1; i++; } num = 0; decimal = 0; factor = 0.1; while ((str[i] >= '0' && str[i] <= '9') || str[i] == '.') { if (str[i] == '.') { decimal = 1; i++; continue; } if (!decimal) { num = num * 10 + (str[i] - '0'); } else { num = num + (str[i] - '0') * factor; factor *= 0.1; } i++; } if (flag) { num = -num; flag = 0; } Push2(OPND, num); } // 处理运算符 else { char theta; char priority = Precede(GetTop1(OPTR), str[i]); switch (priority) { case '<': Push1(OPTR, str[i]); i++; break; case '=': Pop1(OPTR); i++; break; case '>': theta = GetTop1(OPTR); Pop1(OPTR); double b = GetTop2(OPND); Pop2(OPND); double a = GetTop2(OPND); Pop2(OPND); double result = Calculate(a, theta, b); Push2(OPND, result); break; } } } cout << fixed << setprecision(2) << GetTop2(OPND) << endl; } return 0; } ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 处理负数时要注意区分减号和负号,根据上下文判断 2. 浮点数比较要使用精度判断,避免直接使用等号 3. 栈操作时要检查栈空栈满情况,防止越界访问 [2025-11-14 14:57:43 | AI写代码神器 | 1360点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多