算术表达式求值
利用栈求解的一个典型的问题是算术表达式求值,例如:“3+4*2-(1+1)#”,这样的表达式计算,在计算过程中,不是读到一个运算就立即计算,而是要与后面的运算符进行优先级比较,以决定先算哪一个。
为解决问题,首先要进行一些约定。表达式由操作数、运算符和界限符组成。操作符和运算符是表达式的主要组成部分,界限符标志一个表达式的结束。表达式分为3类,及算术表达式、关系表达式、逻辑表达式这里只讨论算术表达式,并对表达式做如下简化:
(1)假定所有算数都是整数。
(2)所有算数符都是整数的二元操作,且都用一个字符表示。
其次,确定加减乘除四则远算的规则:先乘除后加减;从左向右计算,先算括号内,再算括号外。基于此,相邻的前后运算符之间优先级关系如表所示,其中x1代表前面的运算符,x2代表当前的运算符。例如,当x1为-,x2为+时,对应的优先级关系为“>”,表示x1的优先级更高要先计算,优先级表为’0’的表示不会出现此项。
x2 | + | - | * | \ | ( | ) | # | |
---|---|---|---|---|---|---|---|---|
x1 | ||||||||
+ | > | > | < | < | < | > | > | |
- | > | > | < | < | < | > | > | |
* | > | > | > | > | < | > | > | |
/ | > | > | > | > | < | > | > | |
( | < | < | < | < | < | = | 0 | |
) | > | > | > | > | 0 | > | > | |
# | < | < | < | < | < | 0 | = |
根据上面的约定,利用操作数(OPND栈)和运算符(OPTR栈),设计对应的计算方法:
(1)初始化:置OPND栈为空,OPTR栈的栈底元素为表达式起始符‘#’;
(2)执行过程:
依次读入表达式中的每个字符:
若为操作数,则转换为对应的数值压入OPND栈;
否则为运算符,和OPTR栈的栈顶运算符进行优先级比较;
若栈顶运算符优先级低于当前运算符,则当前运算符压栈;
若栈顶运算符优先级高于当前运算符,则OPTR栈弹出运算符、OPND栈弹出两个操作数,进行相应的计算,将计
算结果压入OPND栈;
若栈顶运算符优先级等于当前运算符,则当前运算符为右括号,将对应的左括号从OPTR栈中弹出。
执行结束,将OPND栈栈顶元素作为运算结果返回。
在程序中我实现栈的基本操作,以及操作数字符向十进制数的转换还有优先级表的定义,所以程序较长:
以上介绍的方法直接通过中缀表达式求解,另外,算术表达式的求解也可以用后缀表达式。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<locale>
//以下是自己写的关于链式栈的基本操作,之后再进行表达式求值算法
#define OK 1
#define ERROR 0
typedef int Status;
typedef char ElemType1;
typedef struct LNode //栈元素节点
{
ElemType1 data; //数据
struct LNode* next; //指向下一个节点
}LNode, * LinkStack;
Status InitStack(LinkStack& S) //链式栈初始化
{
S = (LNode*)malloc(sizeof(LNode));
if (S == NULL) exit(0);
S->next = NULL;
return OK;
}
Status DestroyStack(LinkStack& S) //链式栈销毁
{
LNode* p = S;
LNode* q = p;
while (p)
{
q = p;
p = p->next;
free(q);
}
free(q);
return OK;
}
Status ClearStack(LinkStack& S) //链式栈清空
{
if (S->next == NULL) return OK;
LNode* p = S->next;
LNode* q;
while (p)
{
q = p;
p = p->next;
free(q);
}
S->next = NULL;
return OK;
}
Status StackEmpty(LinkStack S) //链式栈判空
{
if (S->next == NULL) return OK;
return ERROR;
}
Status StackLength(LinkStack S) //链式栈元素个数
{
LNode* p = S;
int i = 0;
while (p->next)
{
i++;
p = p->next;
}
return i;
}
Status GetTop(LinkStack S, ElemType1& e) //链式栈获取栈顶元素
{
if (S->next == NULL) return ERROR;
e = S->next->data;
return OK;
}
Status StackTraverse(LinkStack S) //链式栈遍历
{
if (S->next == NULL) return ERROR;
LNode* p = S->next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}
Status Push(LinkStack& S, ElemType1 e) //链式栈进栈
{
LNode* p = (LNode*)malloc(sizeof(LNode));
if (p == NULL) exit(0);
p->data = e;
p->next = S->next;
S->next = p;
return OK;
}
Status Pop(LinkStack& S, ElemType1& e) //链式栈出栈
{
if (S->next == NULL) return ERROR;
e = S->next->data;
LNode* p = S->next;
S->next = p->next;
free(p);
return OK;
}
typedef int ElemType2;
typedef struct LNode2 //栈元素节点
{
ElemType2 data; //数据
struct LNode2* next; //指向下一个节点
}LNode2, * LinkStack2;
Status InitStack2(LinkStack2& S) //链式栈初始化
{
S = (LNode2*)malloc(sizeof(LNode2));
if (S == NULL) exit(0);
S->next = NULL;
return OK;
}
Status DestroyStack2(LinkStack2& S) //链式栈销毁
{
LNode2* p = S;
LNode2* q = p;
while (p)
{
q = p;
p = p->next;
free(q);
}
free(q);
return OK;
}
Status ClearStack2(LinkStack2& S) //链式栈清空
{
if (S->next == NULL) return OK;
LNode2* p = S->next;
LNode2* q;
while (p)
{
q = p;
p = p->next;
free(q);
}
S->next = NULL;
return OK;
}
Status StackEmpty2(LinkStack2 S) //链式栈判空
{
if (S->next == NULL) return OK;
return ERROR;
}
Status StackLength2(LinkStack2 S) //链式栈元素个数
{
LNode2* p = S;
int i = 0;
while (p->next)
{
i++;
p = p->next;
}
return i;
}
Status GetTop2(LinkStack2 S, ElemType2& e) //链式栈获取栈顶元素
{
if (S->next == NULL) return ERROR;
e = S->next->data;
return OK;
}
Status StackTraverse2(LinkStack2 S) //链式栈遍历
{
if (S->next == NULL) return ERROR;
LNode2* p = S->next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}
Status Push2(LinkStack2& S, ElemType2 e) //链式栈进栈
{
LNode2* p = (LNode2*)malloc(sizeof(LNode2));
if (p == NULL) exit(0);
p->data = e;
p->next = S->next;
S->next = p;
return OK;
}
Status Pop2(LinkStack2& S, ElemType2& e) //链式栈出栈
{
if (S->next == NULL) return ERROR;
e = S->next->data;
LNode2* p = S->next;
S->next = p->next;
free(p);
return OK;
}
char ComparePriority(ElemType1 e1, ElemType1 e2) //当前操作符与前一个操作符的比较
{
int i, j;
//设计优先级表
switch (e1) //不同的操作符对应不同的下标
{
case '+':
i = 0;
break;
case '-':
i = 1;
break;
case '*':
i = 2;
break;
case '/':
i = 3;
break;
case '(':
i = 4;
break;
case ')':
i = 5;
break;
case '#':
i = 6;
break;
default:
break;
}
switch (e2)
{
case '+':
j = 0;
break;
case '-':
j = 1;
break;
case '*':
j = 2;
break;
case '/':
j = 3;
break;
case '(':
j = 4;
break;
case ')':
j = 5;
break;
case '#':
j = 6;
break;
default:
break;
}
char prior[7][7]; //用二维数组存放当前元素和栈顶元素优先级比较的结果
prior[0][0] = '>'; prior[0][1] = '>'; prior[0][2] = '<'; prior[0][3] = '<'; prior[0][4] = '<'; prior[0][5] = '>'; prior[0][6] = '>';
prior[1][0] = '>'; prior[1][1] = '>'; prior[1][2] = '<'; prior[1][3] = '<'; prior[1][4] = '<'; prior[1][5] = '>'; prior[1][6] = '>';
prior[2][0] = '>'; prior[2][1] = '>'; prior[2][2] = '>'; prior[2][3] = '>'; prior[2][4] = '<'; prior[2][5] = '>'; prior[2][6] = '>';
prior[3][0] = '>'; prior[3][1] = '>'; prior[3][2] = '>'; prior[3][3] = '>'; prior[3][4] = '<'; prior[3][5] = '>'; prior[3][6] = '>';
prior[4][0] = '<'; prior[4][1] = '<'; prior[4][2] = '<'; prior[4][3] = '<'; prior[4][4] = '<'; prior[4][5] = '='; prior[4][6] = '0';
prior[5][0] = '>'; prior[5][1] = '>'; prior[5][2] = '>'; prior[5][3] = '>'; prior[5][4] = '0'; prior[5][5] = '>'; prior[5][6] = '>';
prior[6][0] = '<'; prior[6][1] = '<'; prior[6][2] = '<'; prior[6][3] = '<'; prior[6][4] = '<'; prior[6][5] = '0'; prior[6][6] = '=';
return prior[i][j]; //返回栈顶元素和当前元素的优先级比较结果
}
//将字符数字转换为十进制数的函数
int Atoi(char* str, int len)
{
assert(str != NULL);
int i = 0;
int flag = 1;//符号位
int tmp = 0;
for (; i < len; i++)
{
if (str[i] == '-')
{
flag = -1;
}
if (isdigit(str[i]))
{
str[i] -= '0';
tmp = tmp * 10 + str[i];
}
}
return tmp * flag;
}
Status Operate(int a, char theta, int b) //根据操作符theta对两个数进行运算
{
int m; //保存计算结果
switch (theta)
{
case '+':
m = a + b;
break;
case '-':
m = a - b;
break;
case '*':
m = a * b;
break;
case '/':
m = a / b;
break;
default:
break;
}
return m;
}
Status CalculateExpression(char* str) //表达式求值算法
{
//利用OPND和OPTR栈进行算数表达式求值
LinkStack OPTR;
LinkStack2 OPND;
InitStack2(OPND);
InitStack(OPTR);
Push(OPTR, '#'); //将表达式结束符'#'压入OPTR栈
//依次读入表达式的每个字符
int i = 0;
int j = 0;
char str1[20];
ElemType1 theta1;
ElemType1 theta;
GetTop(OPTR, theta1);
int data;
int flag = 0;
int a, b;
char s;
while (str[i] != '#' || theta1 != '#')
{
//如果是操作数压入OPND栈
if (str[i] >= '0' && str[i] <= '9')
{
str1[j] = str[i];
j++;
i++; //i移动到下一个位置
if (str[i] < '0' || str[i] > '9')
{
flag = 1;
}
if (flag == 1 && j > 0)
{
data = Atoi(str1, j);
Push2(OPND, data);
flag = 0;
j = 0;
}
}
else
{
GetTop(OPTR, theta1); //查看OPTR运算符theta1
//按照优先级表对栈顶元素theta1和当前元素进行比较
s = ComparePriority(theta1, str[i]);
switch (s)
{
case '<': //栈顶运算优先级低,则当前运算压栈
Push(OPTR, str[i]);
i++;
break;
case '>': //栈顶运算优先级高,则进行计算,计算结果压入OPND栈
Pop(OPTR, theta);
Pop2(OPND, b);
Pop2(OPND, a);
Push2(OPND, Operate(a, theta, b));
break;
case '=':
Pop(OPTR, theta);
i++;
break;
default:
break;
}
}
GetTop(OPTR, theta1);
}
int result;
GetTop2(OPND, result);
return result;
}
int main()
{
char str[20];
printf("输入算术表达式(以‘#’结尾):");
gets(str);
int result;
result = CalculateExpression(str);
printf("结果为:%d\n", result);
return 0;
}
运行结果如下: