栈的应用
Q1:简单表达式求值
限定的简单表达式求值问题是用户输入一个包含+、-、*、/、正整数和圆括号的合法算术表达式,计算该表达式的结果。
思路:(1)将算术表达式转换成后缀表达式
(2)后缀表达式求值
具体执行代码:
#include <iostream>
using namespace std;
#define MaxSize 50
typedef char ELemType; //存储后缀表达式的运算符栈,char类型
typedef struct {
ELemType data[MaxSize];
int top;
}SqStack;
void initStack(SqStack* &s) {
s = (SqStack*)malloc(sizeof(SqStack));
s->top = -1;
}
bool Pop(SqStack* s, ELemType &e) {
if (s->top == -1) return false;
e = s->data[s->top];
s->top--;
return true;
}
bool Push(SqStack* s, ELemType e) {
if (s->top == MaxSize - 1) return false;
s->top++;
s->data[s->top] = e;
return true;
}
bool StackNotEmpty(SqStack* &s) {
return s->top != -1;
}
ELemType GetTop(SqStack* s) {
return s->data[s->top];
}
typedef double ELemType1; //存储运算数据的栈,最后剩下的栈中元素即为运算结果
typedef struct { //为double类型,具体实现完全同上
ELemType1 data[MaxSize];
int top;
}SqStack1;
void initStack(SqStack1* &s) {
s = (SqStack1*)malloc(sizeof(SqStack1));
s->top = -1;
}
bool Pop(SqStack1* s, ELemType1 &e) {
if (s->top == -1) return false;
e = s->data[s->top];
s->top--;
return true;
}
bool Push(SqStack1* s, ELemType1 e) {
if (s->top == MaxSize - 1) return false;
s->top++;
s->data[s->top] = e;
return true;
}
bool StackNotEmpty(SqStack1* &s) {
return s->top != -1;
}
ELemType1 GetTop(SqStack1* s) {
return s->data[s->top];
}
void trans(char exp[], char postexp[]) { //把算式转换为后缀表达式,存储在char型数组postexp中
SqStack* Optr;
initStack(Optr);
int i = 0, j=0;
while (exp[i] != '\0') {
switch (exp[i]) {
case '+':
case '-':
while (StackNotEmpty(Optr) && (GetTop(Optr) != '(')) {
ELemType temp;
Pop(Optr, temp);
postexp[j] = temp;
j++;
}
Push(Optr, exp[i]);
i++;
break;
case '*':
case '/':
while (StackNotEmpty(Optr) && (GetTop(Optr) != '(')
&& (GetTop(Optr) != '+') && (GetTop(Optr) != '-')) {
ELemType temp;
Pop(Optr, temp);
postexp[j] = temp;
j++;
}
Push(Optr, exp[i]);
i++;
break;
case ')':
while (StackNotEmpty(Optr) && (GetTop(Optr) != '(')) {
ELemType temp;
Pop(Optr, temp);
postexp[j] = temp;
j++;
}
if (GetTop(Optr) == '(') {
ELemType temp;
Pop(Optr, temp);
}
i++;
break;
case '(':
Push(Optr, exp[i]);
i++;
break;
default:
postexp[j] = exp[i];
j++;
i++;
if (exp[i]<'0' || exp[i]>'9') {
postexp[j] = '#';
j++;
}
break;
}
}
while (StackNotEmpty(Optr)) {
ELemType temp;
Pop(Optr, temp);
postexp[j] = temp;
j++;
}
}
double compvalue(char postexp[], int n) { //对后缀表达式进行求值
SqStack1* s1;
initStack(s1);
for (int i = 0; i < n; i++) {
double d = 0;
if (postexp[i] >= '0'&&postexp[i] <= '9') {
while (postexp[i]!='#') {
d = 10 * d + postexp[i] - '0'; i++;
}
Push(s1, d);
}
else if (postexp[i] == '*'|| postexp[i] == '/' || postexp[i] == '+' || postexp[i] == '-') {
double a, b;
Pop(s1, a);
Pop(s1, b);
switch (postexp[i]) {
case '*':
Push(s1, a*b);
break;
case '/':
Push(s1, b/a);
break;
case '+':
Push(s1, a+b);
break;
case '-':
Push(s1, b-a);
break;
}
}
}
double result;
Pop(s1, result);
return result;
}
int main() {
char exp[] = "(56-20)/(4+2)";
char postexp[13];
trans(exp, postexp);
for (int i = 0; i < 13; i++) {
cout << postexp[i];
}
cout << endl << endl;
cout << compvalue(postexp, 13) << endl;
system("pause");
}