#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Maxsize 1000
typedef char Elementtype;
struct node
{
Elementtype * data;
int top;
int maxsize;
};
int Initstack(struct node * Stack)
{
Stack->data = (Elementtype *) malloc( Maxsize * sizeof(Elementtype) );
if(Stack->data == NULL) return 0;
Stack->maxsize = Maxsize;
Stack->top = -1;
return 1;
}
int Full(struct node * Stack)
{
return (Stack->top == (Stack->maxsize - 1));
}
int Push(Elementtype k, struct node * Stack)
{
if(Full(Stack))
{
printf("The stack is full.\n");
return 0;
}
else
{
Stack->data[++Stack->top] = k;
}
return 1;
}
int IsEmpty(struct node * Stack)
{
return (Stack->top == -1);
}
Elementtype Pop(struct node * Stack)
{
if(IsEmpty(Stack))
{
printf("The stack is empty.\n");
return 0;
}
else
{
return Stack->data[Stack->top--];
}
}
Elementtype GetTop(struct node * Stack)
{
return Stack->data[Stack->top];
}
void Deletestack(struct node * Stack)
{
free(Stack->data);
Stack->top = -1;
Stack->maxsize = -1;
}
int main()
{
struct node S1, S2;
char c[1001];
Initstack(&S1);
Initstack(&S2);
gets(c);
int i, len = strlen(c);
for(i = len - 2; i >= 0; i--)
{
switch(c[i])
{
case '(':
char d;
while(1)
{
d = Pop(&S1);
if(d == ')') break;
Push(d, &S2);
}
break;
case '-':
case '+'://前缀++ab
while(GetTop(&S1) != ')' && !IsEmpty(&S1) && GetTop(&S1) != '+' && GetTop(&S1) != '-')
{
Push(Pop(&S1), &S2);
}
Push(c[i], &S1);
break;
case '*':
case '/':
case ')':
Push(c[i], &S1);
break;
default:
Push(c[i], &S2);
}
}
while(!IsEmpty(&S1))
{
char d = Pop(&S1);
Push(d, &S2);
}
while(!IsEmpty(&S2))
{
putchar(Pop(&S2));
}
putchar('\n');
Deletestack(&S1);
Deletestack(&S2);
for(i = 0; c[i] != '#'; i++)
{
if(c[i] != '(' && c[i] != ')')
putchar(c[i]);
}
putchar('\n');
Initstack(&S1);
for(i = 0; c[i] != '#'; i++)
{
switch(c[i])
{
case ')':
char d;
while(1)
{
d = Pop(&S1);
if(d == '(') break;
putchar(d);
}
break;
case '+':
case '-':
while(GetTop(&S1) != '(' && !IsEmpty(&S1))
{
putchar(Pop(&S1));
}
Push(c[i], &S1);
break;
case '*':
case '/':
if(!IsEmpty(&S1) && GetTop(&S1) != '(' && GetTop(&S1) != '+' && GetTop(&S1) != '-')
{putchar(GetTop(&S1)); Pop(&S1);}
case '(':
Push(c[i], &S1);
break;
default:
putchar(c[i]);
}
}
while(!IsEmpty(&S1))
{
putchar(Pop(&S1));
}
putchar('\n');
return 0;
}