HUST Online Judge WebBoard
Problem 3980 >> 答案错误55%
201803010212 @ 2019-07-18 09:41:16
[ Quote ] [ Edit ] [ Delete ] 1#
```
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define Size 200
typedef struct poly
{
char data[Size];
int top;
}po;
int push(po *&s,char e)
{
if(s->top ==Size-1)
return 0;
s->top ++;
s->data [s->top ]=e;
return 1;
}
int pop(po *&s,char &e)
{
if(s->top ==-1)
return 0;
e=s->data [s->top ];
s->top --;
return 1;
}
int poget(po *s,char &e)
{
if(s->top ==-1)
return 0;
e=s->data [s->top ];
return 1;
}
void Init(po *&s)
{
s=(po *)malloc(sizeof(po));
s->top =-1;
}
void Destroy(po *&s)
{
free(s);
}
int poempty(po *s)
{
if(s->top ==-1)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
char a[Size],exp[Size];
exp[0]='\0';
po *pa;
char t;
int i,k=0,m;
scanf("%s",a);
m=strlen(a);
Init(pa);
for(i=0;i<m;i++)
{
switch(a[i])
{
case '*':
case '/':
if(!poempty(pa))
{
poget(pa,t);
if(t=='*'||t=='/')
{
pop(pa,t);
exp[k]=t;
k++;
}
}
push(pa,a[i]);
break;
case '(':
push(pa,a[i]);
break;
case ')':
pop(pa,t);
if(t!='(')
{
exp[k]=t;
k++;
pop(pa,t);
}
break;
case '+':
case '-':
while(!poempty(pa))
{
poget(pa,t);
if(t!='(')
{
exp[k]=t;
k++;
pop(pa,t);
}
else
break;
}
push(pa,a[i]);
break;
default:
exp[k]=a[i];
k++;
}
}
while(!poempty(pa))
{
pop(pa,t);
exp[k]=t;
k++;
}
exp[k]='\0';
printf("%s",exp);
return 0;
}
```