有瑕疵
#include<stdio.h>
#include<stdlib.h>
#define N 100
//char map[100];
typedef struct {
char node[N];
int top;
}SeqStack;
SeqStack seq;
void Init(SeqStack *s)
{
s=(SeqStack *)malloc(sizeof(SeqStack));
s->top=-1;
}
int IsEmpty(SeqStack *s)
{
if(s->top==-1)
return 1;
return 0;
}
int Push(SeqStack *s, char e)
{
if(s->top==N-1)
return 0;
s->top++;
s->node[s->top]=e;
return 1;
}
int Pop(SeqStack *s, char *e)
{
if(s->top==-1)
return 0;
*e=s->node[s->top];
s->top--;
return 1;
}
char GetTop(SeqStack *s)
{
// if(s->top==-1)
// return 0;
return s->node[s->top];
}
void print(char map[])
{
Init(&seq);
int i=0;
char ch;
while(map[i]!='\0')
{
if(map[i]>='a' && map[i]<='z')
{
printf("%c", map[i]);
}
else if(map[i]=='+' || map[i]=='-')
{
if(IsEmpty(&seq)==0)
{
ch=GetTop(&seq);
while(ch!='(' && IsEmpty(&seq)==0)
{
Pop(&seq, &ch);
printf("%c", ch);
ch=GetTop(&seq);
}
}
Push(&seq, map[i]);
}
else if(map[i]=='*' || map[i]=='/')
{
if(IsEmpty(&seq)==0)
{
ch=GetTop(&seq);
while(ch!='(' && IsEmpty(&seq)==0)
{
Pop(&seq, &ch);
printf("%c", ch);
ch=GetTop(&seq);
}
}
Push(&seq, map[i]);
}
else if(map[i]=='(')
{
Push(&seq, map[i]);
}
else if(map[i]==')')
{
Pop(&seq, &ch);
while(ch!='(')
{
printf("%c", ch);
Pop(&seq, &ch);
}
}
i++;
}
/* Init(&seq);
int i=0;
while(map[i]!='\0')
{
if(map[i]>='a' && map[i]<='z')
{
printf("%c", map[i]);
}
if(map[i]=='+' || map[i]=='-')
{
if(IsEmpty(&seq)==1)
{
Push(&seq, map[i]);
}
else
{
char ch;
ch=GetTop(&seq);
while(ch!='(' && 0==IsEmpty(&seq))
{
Pop(&seq, &ch);
printf("%c", ch);
ch=GetTop(&seq);
}
Push(&seq, map[i]);
/* char ch;
do{
Pop(&seq, &ch);
if(ch=='(')
Push(&seq, ch);
else
printf("%c", ch);
}while(IsEmpty(&seq) && ch!='(');
Push(&seq, map[i]);
}
}
else if(map[i]== '*' || map[i]=='/')
{
if(IsEmpty(&seq)==0)
{
char ch;
ch=GetTop(&seq);
while('*'==ch || '/'==ch)
{
Pop(&seq, &ch);
printf("%c", ch);
ch=GetTop(&seq);
}
}
Push(&seq, map[i]);
}
else if(map[i]=='(')
{
Push(&seq, map[i]);
}
else if(map[i]==')')
{
char ch;
Pop(&seq, &ch);
while(ch!='(')
{
printf("%c", ch);
Pop(&seq, &ch);
}
}
i++;
}*/
while(IsEmpty(&seq)==0)
{
// char ch;
Pop(&seq, &ch);
printf("%c", ch);
}
}
int main()
{
char map[N];
scanf("%s", map);
print(map);
}