改进了下数据结构严蔚敏版3.22链栈实现表达式求值算法,将操作数从个位数扩展到多位整数或小数。
/***链栈实现表达式求值***/
#include<iostream>
using namespace std;
const char oprt[7] = { '+', '-', '*', '/', '(', ')', '#' };
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef union SElemType {
char value1;
float value2;
}SElemType;
typedef int Status;
typedef struct SNode {
SElemType elem;
struct SNode *next;
}SNode, *LinkStack;
Status InitStack(LinkStack &S) {
S = NULL;
return OK;
}
bool StackEmpty(LinkStack S) {
if (!S)
return true;
return false;
}
Status Push(LinkStack& S, SElemType e) {
SNode* p = new SNode;
if (!p) {
return OVERFLOW;
}
p->elem = e;
p->next = S;
S = p;
return OK;
}
Status Pop(LinkStack &S, SElemType &e) {
SNode *p;
if (!S)
return ERROR;
e = S->elem;
p = S;
S = S->next;
delete p;
return OK;
}
SElemType GetTop(LinkStack &S) {
if (S)
return S->elem;
}
bool isOPRT(char ch) {//判断ch是否为运算符
for (int i = 0; i < 7; i++) {
if (ch == oprt[i]) {
return true;
}
}
return false;
}
char Precede(char theta1, char theta2) {//判断运算符优先级
if ((theta1 == '(' && theta2 == ')') || (theta1 == '#' && theta2 == '#')) {
return '=';
} else if (theta1 == '(' || theta1 == '#' || theta2 == '(' || (theta1
== '+' || theta1 == '-') && (theta2 == '*' || theta2 == '/')) {
return '<';
} else
return '>';
}
float Operate(float num1, char theta,float num2) {//计算两数运算结果
switch (theta) {
case '+':
return num1 + num2 ;
case '-':
return num1 - num2 ;
case '*':
return num1 * num2 ;
case '/':
return num1 / num2;
}
return 0;
}
//算法3.22 表达式求值
float EvaluateExpression() {//算术表达式求值的算符优先算法,设OPTR和OPND分别为运算符栈和操作数栈
LinkStack OPTR, OPND;
SElemType ch,theta, num1, num2;
InitStack(OPND); //初始化OPND栈
InitStack(OPTR); //初始化OPTR栈
ch.value1 = '#';
Push(OPTR, ch); //将表达式起始符“#”压入OPTR栈
cin >> ch.value1;
while (ch.value1 != '#' || (GetTop(OPTR).value1!= '#')) //表达式没有扫描完毕或OPTR的栈顶元素不为“#”(未运算完毕)
{
if (!isOPRT(ch.value1)) {
int i = 0;
char str[MAXSIZE]={0};
while (ch.value1 >= '0' && ch.value1 <= '9' || ch.value1 == '.') {
str[i] = ch.value1;
i++;
cin >> ch.value1;
}
SElemType num;
num.value2 = atof(str);//通过c_str将string转换为char再转化为num
Push(OPND, num);//ch不是运算符则进OPND栈
}
else
switch (Precede(GetTop(OPTR).value1, ch.value1)) //比较OPTR的栈顶元素和ch的优先级
{
case '<':
Push(OPTR, ch);
cin >> ch.value1; //当前字符ch压入OPTR栈,读入下一字符ch
break;
case '>':
Pop(OPTR, theta); //弹出OPTR栈顶的运算符
Pop(OPND, num2);
Pop(OPND, num1); //弹出OPND栈顶的两个运算数
SElemType num3;
num3.value2=Operate(num1.value2, theta.value1, num2.value2);
Push(OPND, num3); //将运算结果压入OPND栈
break;
case '=': //OPTR的栈顶元素是“(”且ch是“)”
Pop(OPTR, ch);
cin >> ch.value1; //弹出OPTR栈顶的“(”,读入下一字符ch
break;
} //switch
} //while
return GetTop(OPND).value2; //OPND栈顶元素即为表达式求值结果
}
int menu() {
int c;
cout << "多项式计算" << endl;
cout << "1.计算" << endl;
cout << "0.退出\n" << endl;
cout << "选择:";
cin >> c;
return c;
}
void main() {
while (1) {
switch (menu()) {
case 1: {
cout << "请输入要计算的表达式(操作数可以是多位整数或小数,以#结束):" << endl;
float res = EvaluateExpression();//算法3.22 表达式求值
cout << "计算结果为" << res<< endl << endl;
}
break;
case 0:
cout << "退出成功\n" << endl;
exit(0);
default:
break;
}
}
}