Sample Input
X+A*(Y-B)-Z/F
A+BC+(DM-N)+(S-T)Y
A-BC+(D-E*F)/G
Sample Output
XAYB-+ZF/-
ABC+DMN-+ST-Y+
ABC*-DEF*-G/+
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct node{
char ch;//元素
int level;//优先级
}ElemType;//数据结构
typedef struct{
ElemType *base;
ElemType *top;
}LinkStack;//栈
int InitStack(LinkStack *L)//初始化
{
//这里只是为了A题 直接申请足够空间 未设置增加空间操作
L->base=(ElemType *)malloc(sizeof(ElemType)*10000);
L->top=L->base;
}
int push(LinkStack *L,ElemType elem)//压栈
{
L->top->ch=elem.ch;
char c=elem.ch;
L->top->level=elem.level;
L->top+=1;
}
int compare(LinkStack *S,ElemType elem)//比较运算符的优先级
{
S->top-=1;
int flag= (S->top->level);//将栈顶运算符优先级赋给flag
S->top+=1;
if( flag >= elem.level)
{
return 1;
}
if( flag < elem.level)
{
return 0;
}
}
int pop(LinkStack *L)//弹栈
{
while(L->base!=L->top)
{
printf("%c",L->base->ch);
L->base+=1;
}
}
int main()
{
char a[1000];
while(gets(a))//输入字符串
{
int l=strlen(a);
ElemType b[1000];
for(int i=0;i<l;i++)//给运算符划分优先等级
{
if(a[i]=='+'||a[i]=='-')
{
b[i].ch=a[i];
b[i].level=1;
}
else if(a[i]=='*'||a[i]=='/')
{
b[i].ch=a[i];
b[i].level=2;
}
else if(a[i]=='('||a[i]==')')
{
b[i].ch=a[i];
b[i].level=-1;
}
else
{
b[i].ch=a[i];
b[i].level=100;//操作数可以不赋优先级
}
}
LinkStack L,S;
InitStack(&L);//存放后缀表达式
InitStack(&S);//存放运算符
ElemType F;
F.ch='#';F.level=-2;//压入#号
push(&S,F);
ElemType r;//用来传递给函数的一个中间变量
for(int i=0;i<l;i++)
{
//若是运算符进行判断
if(b[i].ch=='+'||b[i].ch=='-'||b[i].ch=='*'||b[i].ch=='/'
||b[i].ch=='('||b[i].ch==')'||b[i].ch=='#')
{
if(b[i].ch=='(')//若是左括号 直接压栈
{
push(&S,b[i]);
continue;
}
if(b[i].ch==')')//若是右括号 将运算符弹出 放到操作数栈中
{
S.top-=1;
while(S.top->ch!='(')//直到遇到左括号 结束
{
r.ch=S.top->ch;
push(&L,r);
S.top-=1;
}
continue;
}
while(compare(&S,b[i]))//比较优先级 !
{
S.top-=1; //若a[i]优先级高于栈顶运算符 压栈
r.ch=S.top->ch; // 若不高于 则栈顶元素出栈 入操作数栈
push(&L,r); // 然后继续和前一个比较
} // 直到遇到比a[i]优先级低的 跳出循环
push(&S,b[i]); // a[i]入栈
}
else//操作数直接压入操作数栈
{
push(&L,b[i]);
}
}
//字符串扫描完 将剩余的运算符依次入操作数栈
S.top-=1;
r.ch=S.top->ch;
while(S.top!=S.base)
{
push(&L,r);
S.top-=1;
r.ch=S.top->ch;
}
//退栈输出后缀表达式
pop(&L);
printf("\n");
}
}