从头开始搭建算式表达式解析器,第五部分,结束

这个就是计算和现实了,很简单的递归法(后序遍历)

void ShowAtom(Atom *atm)
{
	if(atm)
	{
		ShowAtom(atm->Lsun);
		ShowAtom(atm->Rsun);
		switch(atm->type)
		{
			case Is_Value:
				printf("[%.3lf]",atm->data->vlu);
			break;
			case Is_Lquot:
			case Is_Comma:
			case Is_Rquot:
			case Is_Opr1p:
			case Is_Opr2p:
			case Is_Setvr:
				printf("[%s]",atm->data->opr);
			break;
			case Is_Param:
				printf("[%s]",GetParamLabel(atm->data->adr));
			break;
			case Is_Fun1p:
				printf("[%s]",GetFun1pLabel(atm->data->fc1));
			break;
			case Is_Fun2p:
				printf("[%s]",GetFun2pLabel(atm->data->fc2));
			break;
			default:break;
		}
	}
}
double ExecAtom(Atom *root)
{
	if(root)
	{
		switch(root->type)
		{
			case Is_Value:
			case Is_Param:
				return root->data->get();
			break;
			case Is_Fun1p:
			case Is_Opr1p:
				return root->data->run(ExecAtom(root->Lsun));
			break;
			case Is_Fun2p:
			case Is_Opr2p:
				return root->data->run(ExecAtom(root->Lsun),ExecAtom(root->Rsun));
			break;
			case Is_Setvr:
				return root->data->set(root->Lsun->data->adr,ExecAtom(root->Rsun));
			break;
			default:break;
		}
	}
	return 0;
}
void ShowLequ(Lequ *ary)
{
	while(ary)
	{
		ShowAtom(ary->equ);
		ary=ary->nxt;
	}
}
void ExecLequ(Lequ *ary,Addrs adr)
{
	while(ary)
	{
		*adr=ExecAtom(ary->equ);
		ary=ary->nxt;
		adr++;
	}
}

你可能感兴趣的:(从头开始搭建算式表达式解析器,第五部分,结束)