Written by Robert_Wang in Southwest University of Science And Technology.
#include<iostream>
#define MaxSize 100
using namespace std;
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
typedef struct LinkNode
{
ElemType data;
struct LinkNode *next;
}LinkStack;
void InitStack(SqStack *&s)
{
s = new SqStack;
s->top = -1;
}
void InitStack(LinkStack *&s)
{
s = new LinkStack;
s->next = NULL;
}
void DestroyStack(SqStack *&s)
{
delete s;
}
void DestroyStack(LinkStack *&s)
{
LinkStack *p, *pre;
pre = s;
p = s->next;
while (p)
{
delete pre;
pre = p;
p = p->next;
}
delete pre;
}
bool StackEmpty(SqStack *&s)
{
return s->top == -1;
}
bool StackEmpty(LinkStack *&s)
{
return s->next == NULL;
}
bool Push(SqStack *& s, ElemType e)
{
if (s->top == MaxSize-1) return false;
s->data[++s->top] = e;
return true;
}
void Push(LinkStack *&s, ElemType& e)
{
LinkStack *p = new LinkStack;
p->data = e;
p->next = s->next;
s->next = p;
}
bool Pop(SqStack *&s, ElemType &e)
{
if (StackEmpty(s)) return false;
e = s->data[s->top--];
return true;
}
bool Pop(LinkStack *&s, ElemType &e)
{
if (StackEmpty(s)) return false;
e = s->next->data;
LinkStack *p = s->next;
s->next = p->next;
delete p;
return true;
}
bool GetTop(SqStack*s, ElemType& e)
{
if (StackEmpty(s)) return false;
e = s->data[s->top];
return true;
}
bool GetTop(LinkStack*s, ElemType& e)
{
if (StackEmpty(s)) return false;
e = s->next->data;
return true;
}
//中缀表达式变为后缀表达式
void trans(char *exp, char postexp[])
{
char e;
int i = 0;
SqStack *s;
InitStack(s);
while (*exp)
{
switch (*exp)
{
case '(': Push(s, *exp); exp++; break;
case ')':
Pop(s, e);
while (e != '(')
{
postexp[i++] = e;
Pop(s, e);
}
exp++;
break;
case '+':
case '-':
while (!StackEmpty(s))
{
GetTop(s, e);
if (e !='(')//将 ( 和 +之间包括+ 存入postexp
{
postexp[i++] = e;
Pop(s, e);
}
else break;
}
Push(s, *exp);//
exp++;
break;
case '*':
case '/':
while (!StackEmpty(s))
{
GetTop(s, e);
if (e == '*' || e == '/')
{
postexp[i++] = e;
Pop(s, e);
}
else break;
}
Push(s, *exp);
exp++;
break;
default:
while (*exp >= '0' && *exp <= '9')
{
postexp[i++] = *exp;
exp++;
}
postexp[i++] = '#';
}
}
while (!StackEmpty(s))
{
Pop(s, e);
postexp[i++] = e;
}
postexp[i] = '\0';
DestroyStack(s);
}
double compvalue(char *postexp)
{
char a,b,c,d,e;
SqStack *s;
InitStack(s);
while (*postexp != '\0')
{
switch (*postexp)
{
case '+':Pop(s, a); Pop(s, b);
e = a + b;
Push(s, e); break;
case '-':Pop(s, a); Pop(s, b);
e = b-a;
Push(s, e); break;
case '*':Pop(s, a); Pop(s, b);
e = a * b;
Push(s, e); break;
case '/':Pop(s, a); Pop(s, b);
if (!a) {
cout << "除0错误" << endl;
exit(0);//异常退出
}
else
{
e = b / a;
Push(s, e);
break;
}
default://将连续的数字转化为对应的数字
d = 0;
while (*postexp >= '0' && *postexp <= '9')
{
d = 10 * d + *postexp - '0';//int char操作时,会自动转化为int ,如'9'+2 = 11;然后又转化为char
postexp++;
}
Push(s, d);
break;
}
postexp++;
}
GetTop(s, e);
DestroyStack(s);
return e;
}
int main()
{
char s[100] = "(56-20)/(4+2)", s1[100];
trans(s, s1);
cout << "中缀表达式为:"<<s << endl;
cout << "后缀表达式"<<s1 << endl;
cout << "表达式的值为:" << compvalue(s1) << endl;
return 0;
}