栈——表达式求值
//"SqStack.h" #include<iostream> using namespace std; #define SElemType_int int #define SElemType_char char #define MAXSIZE 100 typedef struct{ SElemType_int *base; SElemType_int *top; int stacksize; }SqStack_int; typedef struct{ SElemType_char *base; SElemType_char *top; int stacksize; }SqStack_char; string InitStack(SqStack_int &S){ S.base = new SElemType_int[MAXSIZE]; S.top = S.base; S.stacksize=MAXSIZE; return "OK"; } string InitStack(SqStack_char &S){ S.base = new SElemType_char[MAXSIZE]; S.top = S.base; S.stacksize=MAXSIZE; return "OK"; } string Push(SqStack_int &S,SElemType_int e){ if(S.top-S.base == S.stacksize) return "ERROR"; *S.top=e; S.top++; return "OK"; } string Push(SqStack_char &S,SElemType_char e){ if(S.top-S.base == S.stacksize) return "ERROR"; *S.top=e; S.top++; return "OK"; } string pop(SqStack_int &S,SElemType_int &e){ if(S.base == S.top) return "ERROE"; S.top--; e = *S.top; return "OK"; } string pop(SqStack_char &S,SElemType_char &e){ if(S.base == S.top) return "ERROE"; S.top--; e = *S.top; return "OK"; } SElemType_int GetTop(SqStack_int S){ if(S.top != S.base){ return *(S.top-1); } } SElemType_char GetTop(SqStack_char S){ if(S.top != S.base){ return *(S.top-1); } } int StackEmpty(SqStack_char S){ if(S.top == S.base) return 1; return 0; } int StackEmpty(SqStack_int S){ if(S.top == S.base) return 1; return 0; }
#include<iostream> #include"SqStack.h" using namespace std; /* 表达式求值 */ char Precede(char a,char b){ //比较行符间顺序 ,a在前,b在后 switch (a) { case '+': case '-': //栈底元素为'+'或'-'时 switch (b) { case '+': case '-': case ')': case '#': return '>'; break; case '*': case '/': case '(': return '<'; break; } break; case '*': case '/': switch (b) { case '+': case '-': case '*': case '/': case ')': case '#': return '>'; break; case '(': return '<'; break; } break; case '(': switch (b) { case '+': case '-': case '*': case '/': case '(': return '<'; break; case ')': return '='; break; } break; case ')': switch (b) { case '+': case '-': case '*': case '/': case ')': case '#': return '>'; break; } break; case '#': switch (b) { case '+': case '-': case '*': case '/': case '(': return '<'; break; case '#': return '='; break; } break; } } int Operate(int a,char Op,int b){ switch (Op) { case '+': return ((a)+(b)); break; case '-': return ((a)-(b)); break; case '*': return ((a)*(b)); break; case '/': return ((a)/(b)); break; default: break; } } int IsOPND(char a){ //判断是否为操作数 if(a >='!'&&a<='/') return 0; else return 1; } int main(){ SqStack_char OPTR ; //operator 运算符 SqStack_int OPND;// operand 操作数 InitStack(OPTR); InitStack(OPND); cout <<"Input the number to calculate # means the end"<<endl; char ch; cin >> ch; //先将#压入栈OPTR Push(OPTR,'#'); while(ch != '#' || GetTop(OPTR) != '#'){ //表达式未结束或OPTR栈顶元素不为#,则继续进行运算 if(IsOPND(ch)){ //若ch输入为操作数,则压入栈OPND,读入下一个字符 Push(OPND,(ch-48)); cin >> ch; }else { switch(Precede(GetTop(OPTR),ch)) //OPTR的栈顶元素与ch输入比较优先级 { case '<': //若<,则将ch压入栈OPTR,读入下一个字符 Push(OPTR,ch); cin >> ch; break; case '>': //若>,则对栈顶元素进行运算, char theta; //运算完成后,ch继续与下一个栈顶元素进行比较 int a,b; pop(OPTR,theta); pop(OPND,a);pop(OPND,b); Push(OPND,Operate(a,theta,b)); break; case '=': //只有()匹配才会产生=,匹配到,则表示栈顶元素为(,把(弹出 char x; //,读入下一个字符 cin >> ch; pop(OPTR,x); } } } cout << GetTop(OPND); system("pause"); return 0; }