11234 - Expressions

#include<iostream>
#include<cstdio>
#include<string>
#include<stack>
#include<queue>
#include<cstdlib>
using namespace std;
struct node{
char value;
node *lchild;
node *rchild;
node *add;
};
int main()
{
int n;
while(cin>>n)
{
getchar();
string str;
for(int c=0;c<n;c++)
{
cin>>str;
stack<node> s;
node *p=(node*)malloc(sizeof(node));
p->add=p;
p->lchild=NULL;
p->rchild=NULL;
p->value=str[0];
s.push(*p);
int i;
for(i=1;i<str.length();i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
node *temp1,*temp2;
temp1=s.top().add;
s.pop();
temp2=s.top().add;
s.pop();
p=(node*)malloc(sizeof(node));
p->add=p;
p->value=str[i];
p->lchild=temp2;
p->rchild=temp1;
s.push(*p);
}
else
{
p=(node*)malloc(sizeof(node));
p->add=p;
p->value=str[i];
p->lchild=NULL;
p->rchild=NULL;
s.push(*p);
}
}
p=s.top().add;
queue<node*> a;
char out[10000];
i=0;
while(p)
{
out[i++]=p->value;
if(p->lchild)
a.push(p->lchild);
if(p->rchild)
a.push(p->rchild);
if(a.empty())
break;
p=a.front();
a.pop();
}
int len=i;
for(i=len-1;i>=0;i--)
cout<<out[i];
cout<<endl;
}
}
return 0;
}

  

posted @ 2013-03-31 00:02  sooflow  阅读(95)  评论(0)    收藏  举报