手写栈版本
#include <iostream>
using namespace std;
typedef double ElemType;
const int MaxSize = 100;
const int inf = 0x3f3f3f3f;
typedef struct StackNode
{
ElemType data;
StackNode *next;
}SqStack;
void InitStack(SqStack *&s);
bool StackEmpty(SqStack *s);
bool GetTop(SqStack *s, ElemType &e);
bool Pop(SqStack *&s, ElemType &e);
void Push(SqStack *&s, ElemType e);
void DestroyStack(SqStack *&s);
void trans(char *exp, char postexp[]);
double compvalue(char *postexp);
int main(void)
{
char exp[MaxSize];
char postexp[MaxSize];
SqStack *s;
InitStack(s);/*
scanf("%s", exp);
trans(exp, postexp);
cout << postexp << endl;*/
scanf("%s", postexp);
cout << compvalue(postexp) << endl;
return 0;
}
void InitStack(SqStack *&s)
{
s = new SqStack;
s->next = NULL;
}
bool StackEmpty(SqStack *s)
{
return (s->next == NULL);
}
void Push(SqStack *&s, ElemType e)
{
SqStack *q = new SqStack;
q->data = e;
q->next = s->next;
s->next = q;
}
bool Pop(SqStack *&s, ElemType &e)
{
if (s->next == NULL)
return false;
else
{
e = s->next->data;
SqStack *p = s->next;
s->next = p->next;
delete p;
p = NULL;
return true;
}
}
bool GetTop(SqStack *s, ElemType &e)
{
if (s->next == NULL)
return false;
else
{
e = s->next->data;
return true;
}
}
void DestroyStack(SqStack *&s)
{
SqStack *p = s, *q = s->next;
while (q != NULL)
{
delete p;
p = q;
q = q->next;
}
delete p;
}
/*
void trans(char *exp, char postexp[])
{
int i = 0;
char e;
SqStack *Optr;
InitStack(Optr);
while (*exp != '\0')
{
switch (*exp)
{
case '(':
Push(Optr, *exp);
exp++;
break;
case ')':
Pop(Optr, e);
while (e != '(')
{
postexp[i++] = e;
Pop(Optr, e);
}
exp++;
break;
case '+':
case '-':
while (!StackEmpty(Optr))
{
GetTop(Optr, e);
if (e != '(')
{
Pop(Optr, e);
postexp[i++] = e;
}
else
break;
}
Push(Optr, *exp);
exp++;
break;
case '*':
case '/':
while (!StackEmpty(Optr))
{
GetTop(Optr, e);
if (e == '*' || e == '/')
{
Pop(Optr, e);
postexp[i++] = e;
}
else
break;
}
Push(Optr, *exp);
exp++;
break;
default:
while (*exp >= '0' && *exp <= '9')
{
postexp[i++] = *exp;
exp++;
}
postexp[i++] = '#';
}
}
while (!StackEmpty(Optr))
{
Pop(Optr, e);
postexp[i++] = e;
}
postexp[i] = '\0';
DestroyStack(Optr);
}
*/
double compvalue(char *postexp)
{
double d, a, b, c, e;
SqStack *Opnd;
InitStack(Opnd);
while (*postexp != '\0')
{
switch(*postexp)
{
case '+':
Pop(Opnd, b);
Pop(Opnd, a);
c = a+b;
Push(Opnd, c);
break;
case '-':
Pop(Opnd, b);
Pop(Opnd, a);
c = a-b;
Push(Opnd, c);
break;
case '*':
Pop(Opnd, b);
Pop(Opnd, a);
c = a*b;
Push(Opnd, c);
break;
case '/':
Pop(Opnd, b);
Pop(Opnd, a);
if (b != 0)
{
c = a/b;
Push(Opnd, c);
break;
}
else
{
cout << "除零错误!" << endl;
return inf;
}
default:
d = 0;
while (*postexp >= '0' && *postexp <= '9')
{
d = d*10+*postexp-'0';
postexp++;
}
Push(Opnd, d);
break;
}
postexp++;
}
GetTop(Opnd, e);
DestroyStack(Opnd);
return e;
}
STL栈版本
#include <iostream>
#include <stack>
using namespace std;
typedef double ElemType;
const int MaxSize = 100;
const int inf = 0x3f3f3f3f;
void trans(char *exp, char postexp[]);
double compvalue(char *postexp);
int main(void)
{
char exp[MaxSize];
char postexp[MaxSize];
cout << "中缀表达式:";
scanf("%s", exp);
trans(exp, postexp);
cout << "后缀表达式:" << postexp << endl;
cout << "表达式的值:" << compvalue(postexp) << endl;
return 0;
}
void trans(char *exp, char postexp[])
{
stack<char> Optr;
int i = 0;
char e;
while (*exp != '\0')
{
switch (*exp)
{
case '(':
Optr.push(*exp);
exp++;
break;
case ')':
e = Optr.top();
Optr.pop();
while (e != '(')
{
postexp[i++] = e;
e = Optr.top();
Optr.pop();
}
exp++;
break;
case '+':
case '-':
while (!Optr.empty())
{
if (Optr.top() != '(')
{
e = Optr.top();
postexp[i++] = e;
Optr.pop();
}
else
break;
}
Optr.push(*exp);
exp++;
break;
case '*':
case '/':
while (!Optr.empty())
{
if (Optr.top() == '*' || Optr.top() == '/')
{
e = Optr.top();
postexp[i++] = e;
Optr.pop();
}
else
break;
}
Optr.push(*exp);
exp++;
break;
default:
while (*exp >= '0' && *exp <= '9')
{
postexp[i++] = *exp;
exp++;
}
postexp[i++] = '#';
}
}
while (!Optr.empty())
{
e = Optr.top();
postexp[i++] = e;
Optr.pop();
}
postexp[i] = '\0';
}
double compvalue(char *postexp)
{
double d, a, b, c, e;
stack<double> Opnd;
while (*postexp != '\0')
{
switch (*postexp)
{
case '+':
b = Opnd.top();
Opnd.pop();
a = Opnd.top();
Opnd.pop();
c = a+b;
Opnd.push(c);
break;
case '-':
b = Opnd.top();
Opnd.pop();
a = Opnd.top();
Opnd.pop();
c = a-b;
Opnd.push(c);
break;
case '*':
b = Opnd.top();
Opnd.pop();
a = Opnd.top();
Opnd.pop();
c = a*b;
Opnd.push(c);
break;
case '/':
b = Opnd.top();
Opnd.pop();
a = Opnd.top();
Opnd.pop();
if (b == 0)
{
cout << "除零错误!" << endl;
return inf;
}
else
{
c = a/b;
Opnd.push(c);
}
break;
default:
d = 0;
while (*postexp >= '0' && *postexp <= '9')
{
d = d*10+*postexp-'0';
postexp++;
}
Opnd.push(d);
break;
}
postexp++;
}
e = Opnd.top();
Opnd.pop();
return e;
}