作者:天秤小果冻cici | 来源:互联网 | 2024-11-11 14:00
本文提出了一种基于栈结构的高效四则运算表达式求值方法。该方法能够处理包含加、减、乘、除运算符以及十进制整数和小括号的算术表达式。通过定义和实现栈的基本操作,如入栈、出栈和判空等,算法能够准确地解析并计算输入的表达式,最终输出其计算结果。此方法不仅提高了计算效率,还增强了对复杂表达式的处理能力。
要求从键盘输入一个算术表达式并输出它的结果,算术表达式可包含加、减、乘、除、十进制整数和小括号,利用栈实现。
首先我们需要定义两个栈的基本创建插入删除操作
数据栈:存放数据
操作符栈:存放操作符
#include
#include
typedef int ElemType;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{ ElemType *base;ElemType *top;int stacksize;}SqStack; typedef struct { char *base;char *top;int stacksize;}OpStack;
int InitStack(SqStack &s)
{ s.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));if(!s.base) return 0;s.top=s.base;s.stacksize=STACK_INIT_SIZE;return 1;} int Push(SqStack &s,ElemType e) { if(s.top-s.base>=s.stacksize){s.base=(ElemType *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(ElemType)); if(!s.base) return 0;s.top=s.base+s.stacksize;s.stacksize+=STACKINCREMENT;}*s.top++=e;return 1;}int Pop(SqStack &s,ElemType &e) { if(!s.base) return 0;e=*--s.top;return 1;}int InitOpStack(OpStack &s)
{ s.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));if(!s.base) return 0;s.top=s.base;s.stacksize=STACK_INIT_SIZE;return 1;} int OpPush(OpStack &s,char e) { if(s.top-s.base>=s.stacksize){s.base=(char *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(char)); if(!s.base) return 0;s.top=s.base+s.stacksize;s.stacksize+=STACKINCREMENT;}*s.top++=e;return 1;}int OpPop(OpStack &s,char &e) { if(!s.base) return 0;e=*--s.top;return 1;}
然后定义一个运算符比较表来告诉系统这些基本运算符的优先顺序。
char Precede(char a1,char a2) { switch(a1){case &#39;&#43;&#39;:switch(a2){case &#39;&#43;&#39;:return &#39;>&#39;;case &#39;-&#39;:return &#39;>&#39;;case &#39;*&#39;:return &#39;<&#39;;case &#39;/&#39;:return &#39;<&#39;;case &#39;(&#39;:return &#39;<&#39;;case &#39;)&#39;:return &#39;>&#39;;case &#39;#&#39;:return &#39;>&#39;;}case &#39;-&#39;:switch(a2){case &#39;&#43;&#39;:return &#39;>&#39;;case &#39;-&#39;:return &#39;>&#39;;case &#39;*&#39;:return &#39;<&#39;;case &#39;/&#39;:return &#39;<&#39;;case &#39;(&#39;:return &#39;<&#39;;case &#39;)&#39;:return &#39;>&#39;;case &#39;#&#39;:return &#39;>&#39;;}case &#39;*&#39;:switch(a2){case &#39;&#43;&#39;:return &#39;>&#39;;case &#39;-&#39;:return &#39;>&#39;;case &#39;*&#39;:return &#39;>&#39;;case &#39;/&#39;:return &#39;>&#39;;case &#39;(&#39;:return &#39;<&#39;;case &#39;)&#39;:return &#39;>&#39;;case &#39;#&#39;:return &#39;>&#39;; }case &#39;/&#39;:switch(a2){case &#39;&#43;&#39;:return &#39;>&#39;;case &#39;-&#39;:return &#39;>&#39;;case &#39;*&#39;:return &#39;>&#39;;case &#39;/&#39;:return &#39;>&#39;;case &#39;(&#39;:return &#39;<&#39;;case &#39;)&#39;:return &#39;>&#39;;case &#39;#&#39;:return &#39;>&#39;;}case &#39;(&#39;:switch(a2){case &#39;&#43;&#39;:return &#39;<&#39;;case &#39;-&#39;:return &#39;<&#39;;case &#39;*&#39;:return &#39;<&#39;;case &#39;/&#39;:return &#39;<&#39;;case &#39;(&#39;:return &#39;<&#39;;case &#39;)&#39;:return &#39;&#61;&#39;;case &#39;#&#39;:return 0;}case &#39;)&#39;:switch(a2){case &#39;&#43;&#39;:return &#39;>&#39;;case &#39;-&#39;:return &#39;>&#39;;case &#39;*&#39;:return &#39;>&#39;;case &#39;/&#39;:return &#39;>&#39;;case &#39;(&#39;:return 0;case &#39;)&#39;:return &#39;>&#39;;case &#39;#&#39;:return &#39;>&#39;;}case &#39;#&#39;:switch(a2){case &#39;&#43;&#39;:return &#39;<&#39;;case &#39;-&#39;:return &#39;<&#39;;case &#39;*&#39;:return &#39;<&#39;;case &#39;/&#39;:return &#39;<&#39;;case &#39;(&#39;:return &#39;<&#39;;case &#39;)&#39;:return 0;case &#39;#&#39;:return &#39;&#61;&#39;;}}}
定义函数&#xff08;对从栈顶取出的两个数进行操作&#xff09;
ElemType Operate(ElemType a,char theta,ElemType b)
{ ElemType sum;switch(theta) { case &#39;&#43;&#39;:sum&#61;a&#43;b;break;case &#39;-&#39;:sum&#61;a-b;break;case &#39;*&#39;:sum&#61;a*b;break;case &#39;/&#39;:sum&#61;a/b;break; }return sum;
}
然后通过表达式求值算法函数调用前面所说函数进行计算
int EvaluateExpression() { SqStack OPND;OpStack OPTR;int flag&#61;0; char c,e,theta; ElemType b,a; InitOpStack(OPTR);OpPush(OPTR,&#39;#&#39;); InitStack(OPND);printf("输入表达式(输入#结束)&#xff1a;\n");c&#61;getchar();while(c!&#61;&#39;#&#39;||*(OPTR.top-1)!&#61;&#39;#&#39;) { if(c>&#61;&#39;0&#39;&&c<&#61;&#39;9&#39;) { Push(OPND,c-48); if(flag!&#61;0) { Pop(OPND,b);Pop(OPND,a);Push(OPND,a*10&#43;b); flag&#61;0;}flag&#61;1;c&#61;getchar();}else {switch(Precede(*(OPTR.top-1),c)) { case &#39;<&#39;:OpPush(OPTR,c);flag&#61;0;c&#61;getchar();break;case &#39;&#61;&#39;:OpPop(OPTR,e);flag&#61;0;c&#61;getchar();break; case &#39;>&#39;:OpPop(OPTR,theta);flag&#61;0;Pop(OPND,b);Pop(OPND,a); Push(OPND,Operate(a,theta,b)); break;}}}printf("表达式的值&#61;:%d",*(OPND.top-1)); return 1;}
然后在主函数中调用&#xff0c;查看运行结果
int main(){ EvaluateExpression();return 0;}