感觉自己正在慢慢走向真正的代码之路..比较笨拙写了好久才写出来,课本上的思路又借鉴了几行别人的代码。即使比较坎坷起码还是把它写出来了..加油加油!
#includeusing namespace std; #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE - 1 #define OVERFLOW - 2 #define MAXSIZE 100 #define SIZE 4 typedef int Status; typedef struct { char* base; char* top; int sizestack; }OPTRStack; Status InitOPTRStack(OPTRStack& S) { S.base = new char[MAXSIZE]; if (!S.base) exit(OVERFLOW); S.top = S.base; S.sizestack = MAXSIZE; return OK; } typedef struct { double* base; double* top; int sizestack; }OPNDStack; Status InitOPNDStack(OPNDStack& S) { S.base = new double[MAXSIZE]; if (!S.base) exit(OVERFLOW); S.top = S.base; S.sizestack = MAXSIZE; return OK; } Status Push(OPTRStack& S,char e) { if (S.top - S.base == S.sizestack) return ERROR; *S.top++ = e; return OK; } Status OPNDPush(OPNDStack& S, double e) { if (S.top - S.base == S.sizestack) return ERROR; *S.top++ = e; return OK; } Status Pop(OPTRStack& S, char& e) { if (S.base == S.top) return ERROR; e = *--S.top; return OK; } Status OPNDpop(OPNDStack& S,double& e) { if (S.base == S.top) return ERROR; e = *--S.top; return OK; } char* OPTRGetTop(OPTRStack& S) { if (S.base == S.top) return NULL; return (S.top - 1); } double* OPNDGetTop(OPNDStack& S) { if (S.base == S.top) return NULL; return (S.top - 1); } char Precede(char a, char b) { if ((a == '+' || a == '-' || a == '*' || a == '/') && (b == '+' || b == '-' || b == '=' || b == ')')) return '>'; else if ((a == '*' || a == '/') && (b == '*' || b == '/')) return '>'; else if ((a == '(') && (b == '+' || b == '-' || b == '*' || b == '/')) return '<'; else if ((a == '+' || a == '-') && (b == '*' || b == '/')) return '<'; else if ((a == '+' || a == '-' || a == '*' || a == '/') && b == '(') return '<'; else return '='; } double Operate(double a, char x, double b) { switch (x) { case'+': return a + b; case'-': return a - b; case'*': return a * b; case'/': return a / b; } } Status IsOPTREmpty(OPTRStack& S) { if (S.base != S.top) return FALSE; else return TRUE; } Status IsOPNDEmpty(OPNDStack& S) { if (S.base != S.top) return FALSE; else return TRUE; } double evaluateexpression(char str[],OPTRStack& OPTR, OPNDStack& OPND) { InitOPTRStack(OPTR); InitOPNDStack(OPND); double n = 0; for (int i = 0; i < strlen(str); i++) { if (str[i] >= '0' && str[i] <= '9') { while (i < strlen(str)) { n *= 10; n += (str[i] - '0'); if (str[i + 1] > '0' && str[i + 1] < '9') i++; else break; } OPNDPush(OPND, n); n = 0; } else { if (IsOPTREmpty(OPTR)) Push(OPTR, str[i]); else switch (Precede(*OPTRGetTop(OPTR), str[i])) { case'<': Push(OPTR, str[i]); break; case'>': { while (!IsOPTREmpty(OPTR)) { if (Precede(*OPTRGetTop(OPTR), str[i]) == '>') { char x = '0'; Pop(OPTR, x); double a = 0, b = 0; OPNDpop(OPND, a); OPNDpop(OPND, b); OPNDPush(OPND, Operate(b, x, a)); } else if (Precede(*OPTRGetTop(OPTR), str[i]) == '=') { char x = '0'; Pop(OPTR, x); break; } else { Push(OPTR, str[i]); break; } } } } } } return *OPNDGetTop(OPND); } void main() { OPTRStack OPTR; OPNDStack OPND; char str[MAXSIZE]; cin >> str; double res=evaluateexpression(str, OPTR, OPND); cout << res; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)