目录
一.原理
1.将式子转换 按计算优先级排序为后缀表达式
2.计算后缀表达式
1.数据结构设计
//顺序栈
template <typename ElemType_stack>
struct stack {
ElemType_stack data[Maxstack];
int top;
};
2.栈的相关函数
//初始化栈:构造一个空栈S,分配内存空间;
template <typename ElemType_stack>
void InitStack(stack<ElemType_stack>*& S) {
S = new stack<ElemType_stack>;
S->top = -1;
}
// 判空: 断一个栈S是否为空,若S为空,则返回true, 否则返回false;
template <typename ElemType_stack>
bool StackEmpty(stack<ElemType_stack>* S) {
if (S->top == -1) {
return true;
}
else {
return false;
}
}
// 进栈:若栈S未满,则将x加入使其成为新栈顶;
template <typename ElemType_stack>
bool Push(stack<ElemType_stack>*& S, ElemType_stack x) {
if (S->top == Maxstack - 1) {
return false;//栈满
}
S->top = S->top + 1;
S->data[S->top] = x;
return true;
}
// 出栈:若栈S非空,则弹出(删除)栈顶元素,并用x返回;
template <typename ElemType_stack>
bool Pop(stack<ElemType_stack>*& S, ElemType_stack& x) {
if (S->top == -1) {
return false;//栈空
}
//只是逻辑上删除实际没有删除
x = S->data[S->top];
S->top = S->top - 1;
return true;
}
// 销毁栈:销毁并释放栈S所占用的内存空间;
template <typename ElemType_stack>
void DestroyStack(stack<ElemType_stack>*& S) {
delete S;
}
// 读取栈顶元素:若栈S非空,则用x - 返回栈顶元素;(栈的使用场景大多只访问栈顶元素);
template <typename ElemType_stack>
bool GetTop(stack<ElemType_stack>* S, ElemType_stack& x) {
if (S->top == -1) {
return false;
}
x = S->data[S->top];
return true;
}
3.计算函数设计
//转换为后缀表达式
void trans(char* exp, char postexp[]) {
char x;
stack<char>* optr;
InitStack(optr);
int i = 0;
while (*exp != '\0') {
switch (*exp) {
case'(':
Push(optr, *exp);
exp++;
break;
case')':
Pop(optr, x);
while (x != '(') {
postexp[i++] = x;
Pop(optr, x);
}
exp++;
break;
case'+':
case'-':
while (!StackEmpty(optr)) {
GetTop(optr, x);
if (x != '(') {
postexp[i++] = x;
Pop(optr, x);
}
else {
break;
}
}
Push(optr, *exp);
exp++;
break;
case'*':
case'/':
while (!StackEmpty(optr)) {
GetTop(optr, x);
if (x=='*'||x=='/') {
postexp[i++] = x;
Pop(optr, x);
}
else {
break;
}
}
Push(optr, *exp);
exp++;
break;
default:
while (*exp >= '0' && *exp <= '9') {
postexp[i++] = *exp;
exp++;
}
postexp[i++] = '#';
}
}
while (!StackEmpty(optr)) {
Pop(optr, x);
postexp[i++] = x;
}
postexp[i] = '\0';
DestroyStack(optr);
}
//计算后缀表达式
double commath(char* postexp) {
double a, b, c, d,e;
stack<double>* optr;
InitStack(optr);
while (*postexp != '\0') {
switch (*postexp) {
case'+':
Pop(optr, a);
Pop(optr, b);
c = a + b;
Push(optr, c);
break;
case'-':
Pop(optr, a);
Pop(optr, b);
c = b - a;
Push(optr, c);
break;
case'*':
Pop(optr, a);
Pop(optr, b);
c = a *b;
Push(optr, c);
break;
case'/':
Pop(optr, a);
Pop(optr, b);
if (a != 0) {
c = b / a;
Push(optr, c);
break;
}
else {
cout << "除0错误"<<endl;
exit(0);
}
break;
default:
d = 0;
while (*postexp >= '0' && *postexp <= '9') {
d = 10 * d + *postexp - '0';
postexp++;
}
Push(optr, d);
break;
}
postexp++;
}
GetTop(optr, e);
DestroyStack(optr);
return e;
}
4主函数
#include<iostream>
using namespace std;
int main() {
char exp[] = "(56-20)/(4+2)*2+(3-0)";
char postexp[Maxstack];
trans(exp, postexp);
double a=commath(postexp);
cout << exp << endl;
cout << postexp << endl;
cout << a;
}