Example #1
0
Value_type EvaluatePostfix()   /*计算后缀表达式值*/
{
	Kind_type type;
	int h;
	Value_type x,y;
	double St[MAXSTACK];
	int top=-1;
	postlength=-1;
	do
	{
		GetToken1(h);
		switch(type=Kind(h))
		{
		case OPERAND:
			top++;
			St[top]=GetValue(h);
			break;
		case UNARYOP:
			x=St[top];
			top--;
			top++;
			St[top]=DoUnary(h,x);
			break;
		case BINARYOP:
			y=St[top];top--;
			x=St[top];top--;
			top++;
			St[top]=DoBinary(h,x,y);
			break;
		case ENDEXPR:
			x=St[top];top--;
			if (top>-1)  /*栈不空,出错*/
				printf("  >>不正确的表达式\n");
			break;
		}
	} while (type!=ENDEXPR);
	return(x);
}
Example #2
0
/*---FUNCTION TO EVALUATE POSTFIX EXPRESSION---*/
float evaluate(List Postfix_l)
{
	int i,count;
	count = Lexicon.count;
	char a[20];
	float x,y,f;
	KindType type;

	Stack_f S;
	S = CreateStack_f();
	while(!IsEmpty(Postfix_l))
	{
		Gettoken(a,Postfix_l);
		switch(type = kind(a))
		{
			case OPERAND:
				i = get_count(a);
				if(i > count)
				{
					puts("var not declared");
					exit(1004);
				}
				if(Lexicon.Entry[i].str!=NULL)
				{
					puts("var has no number value");
					exit(1005);
				}

				f = Lexicon.Entry[i].info.val;
				Push_f(f,S);
				break;
			case UNARYOP:
				f = TopandPop_f(S);
				f = DoUnary(f,a);
				Push_f(f,S);
				break;
			case BINARYOP:
				y = TopandPop_f(S);
				x = TopandPop_f(S);

				f = DoBinary(x,y,a);
				Push_f(f,S);
				break;
		}
	}
	
	if(IsEmpty_f(S))
	{
		puts("INVALID EXPRESSION");
		exit(1005);
	}

	f = TopandPop_f(S);

	if(!IsEmpty_f(S))
	{
		puts("INVALID EXPRESSION");
		       exit(1005);
	}

		
	return f;
}