假条,今天要写编译原理,来不及更新啦(1/2)
//:
//
#include“stdafx.h“
#include<iostreaess“;
}
else{
cout<“error:fail!“;
flag=0;
b
}
}
cout<
if(ty())b
if(n>0)
{p->child=(0).i
node*temp=p->child->i
for(inti=1;i<n;i++)
{
temp->bro=(i).i
temp=temp->bro;
}
}
n=0;
}
}
/********************************************************************/
/*语义分析*/
voidsemanti_ana(my_stack<node>&stack,node*s)//主体部分,对后序遍历得到的后缀树进行栈操作,得出最后结果,放在后序遍历循环体中
{
stringout=““;
boolhasundefine=f
if(s->id>=0&&s->id<19)
{
if(s->id==10)
{
cout<left<setw(40)<““;
cout<“returntop[0].val“;
cout<
cout<“结果:“;
if(hasundefine)
cout<s;
else
cout<setw(40)<w();
}
elseif(s->id==0)
{
if(s->index->hasval)
().index->val=sin(().index->val);
else
{
out+=“sin(“+().index->text+“)“;
().index->text=out;
().index->hasval=f
}
cout<setw(40)<w();
cout<“top[0].text=“<().index->
cout<“,top[0].val=sin(top[0].val)“;
}
elseif(s->id==1)
{
if(s->index->hasval)
().index->val=cos(().index->val);
else
{
out=“cos(“+().index->text+“)“;
().index->text=out;
().index->hasval=f
}
cout<setw(40)<w();
cout<“top[0].text=“<().index->
cout<“,top[0].val=cos(top[0].val)“;
}
elseif(s->id==2)
{
if(s->index->hasval)
().index->val=tan(().index->val);
else
{
out=“tan(“+().index->text+“)“;
().index->text=out;
().index->hasval=f
}
cout<setw(40)<w();
cout<“top[0].text=“<().index->
cout<“,top[0].val=tg(top[0].val)“;
}
elseif(s->id==3)
{
if(s->index->hasval)
().index->val=tan(1.57-().index->val);
else
{
out=“cot(“+().index->text+“)“;
().index->text=out;
().index->hasval=f
}
cout<setw(40)<w();
cout<“top[0].text=“<().index->
cout<“,top[0].val=ctg(top[0].val)“;
}
elseif(s->id==4)
{
if(s->index->hasval)
().index->val=log(().index->val);
else
{
out=“log(“+().index->text+“)“;
().index->text=out;
().index->hasval=f
}
cout<setw(40)<w();
cout<“top[0].text=“<().index->
cout<“,top[0].val=log(top[0].val)“;
}
elseif(s->id==5)
{
if(s->index->hasval)
().index->val=log10(().index->val);
else
{
out=“lg(“+().index->text+“)“;
().index->text=out;
().index->hasval=f
}
cout<setw(40)<w();
cout<“top[0].text=“<().index->
cout<“,top[0].val=lg(top[0].val)“;
}
elseif(s->id==6)
{
if(s->index->hasval)
().index->val=log(().index->val);
else
{
out=“ln(“+().index->text+“)“;
().index->text=out;
().index->hasval=f
}
cout<setw(40)<w();
cout<“top[0].text=“<().index->
cout<“,top[0].val=ln(top[0].val)“;
}
elseif(s->id==11)
{
if((0).index->hasval&&(1).index->hasval)
{
doubletemp=().index->val;
();
().index->val+=
().index->text=to_string(().index->val);
}
else{
out=“(“+(1).index->text+“+“+(0).index->text+“)“;
(1).index->text=out;
(1).index->hasval=f
();
}
cout<setw(40)<w();
cout<“top[1].val+=top[0].val,pop()“;
}
elseif(s->id==12)
{
if((0).index->hasval&&(1).index->hasval)
{
doubletemp=().index->val;
();
().index->val-=
().index->text=to_string(().index->val);
}
else{
out=“(“+(1).index->text+“-“+(0).index->text+“)“;
(1).index->text=out;
(1).index->hasval=f
();
}
cout<setw(40)<w();
cout<“top[1].val-=top[0].val,pop()“;
}
elseif(s->id==13)
{
if((0).index->hasval&&(1).index->hasval)
{
doubletemp=().index->val;
();
().index->val*=
().index->text=to_string(().index->val);
}
else{
out=(1).index->text+“*“+(0).index->
(1).index->text=out;
(1).index->hasval=f
();
}
本章未完,点击下一页继续阅读。