Пример #1
0
void  main(void)
{
     double opn1, opn2;       /* working operands         */
     char   s[LINE_SIZE];     /* input line buffer        */
     char   opr;              /* working operator         */
     char   *p;               /* buffer cursor            */

     printf("\nPrefix Form Evaluator");
     printf("\n=====================");
     printf("\n\nInput --> ");
     gets(s);

     initial();               /* initial stack            */
     for (p = s; *p != '\0'; p++)  /* for each character  */
          if (is_opr(*p))     /* is is an operator ?      */
               push_opr(*p);  /* YES, push.               */
          else if (isdigit(*p)) {  /* is it an operand ?  */
               opn2 = *p - '0'; /* YES, convert to double */
               while (stack_top() == OPERAND) { 
                    opn1 = pop_opn();  /* for all of its  */
                    opr  = pop_opr();  /* buddy, compute  */
                    opn2 = compute(opr, opn1, opn2);
               }
               push_opn(opn2);  /* then push the result   */
          }
     printf("\nResult = %lf", pop_opn());
}
Пример #2
0
real calc_check(int check,const char * exp)
{
	real	opd_stack[STACK_SIZE];
	int		opr_stack[STACK_SIZE];
	int		opd_size = 0,opr_size = 0;
	int		state = 1/*,i*/;
	extern VAR * find_var (const char *);

	if (check)
	{
		match_exp(exp);
		l_get_token();
		if (token_type!=TT_LINE_END)
		{
			merror_msg("illegal expr");
		}
	}

	pline = exp;

	while(1)
	{
		l_get_token();//puts(token);SHOW_STACK;
		// exit
		if (token_type==TT_LINE_END)
		{
			break;
		}
		else if (token_type==TT_ID)
		{
			VAR * v;ARRAY * a;
			if (state!=1 && !check)
			{
				l_put_back();
				break;
			}
			v = find_var(token);
			if (v!=NULL)
			{
				push_opd(v->value);
				move2(2);
				continue;
			}
			a = find_array (token);
			if (a!=NULL)
			{
				int index;
				l_get_token();//skip (
				index = (int)calc_check(FALSE,pline);
				// delete this line,calc_check call skip ( automatically
				//l_get_token();//skip )
				push_opd(get_element(a,index));
				move2(2);
				continue;
			}
			merror_msg("Unrecognized identifier '%s'",token);
		}
		// constant
		else if (token_type==TT_INT || token_type==TT_FLOAT)
		{
			push_opd((real)atof(token));
			move2(2);
		}
		// operator
		else if (IS_OPR(token_type))
		{
			// neg "-" judgement
			if (token_type==OPR_SUB && state==1)
			{
				token_type = OPR_NEG;
			}
			//
			if (opr_size>0 &&
				opr_stack[opr_size-1]!=TT_LBK &&
				priority(token_type) <= priority(opr_stack[opr_size-1]))
			{
				real result;
				result = calc_pop(opd_stack,opr_stack,&opd_size,&opr_size);
				push_opd(result);
			}
			push_opr(token_type);
			move2(1);
		}
		// process '(' and ')'
		else if (token_type==TT_LBK)
		{
			push_opr(token_type);
		}
		else if (token_type==TT_RBK)
		{
			while(opr_stack[opr_size-1] /* top */ != TT_LBK && opr_size > 0)
			{
				real result;
				result = calc_pop(opd_stack,opr_stack,&opd_size,&opr_size);
				push_opd(result);
			}
			if (opr_stack[opr_size-1] /* top */ == TT_LBK)
			{
				opr_size--; // pop '('
			}
			else // funcition call end
			{
				break;
			}
		}
		// built-in function call
		else if (IS_RESWORD(token_type))
		{
			int			argc = token_ext,i;
			real		result;
			FUNCTION	func = get_func(token_type);
			//printf("call <%s>\n",token);
			// skip '('
			l_get_token ();
			// get all arg and push them in stack
			for (i=0;i<argc;++i)
			{
				result = calc_check(FALSE,pline);
				push_opd(result);
				//printf("[%d]  |%.4lf|\n",i,result);
			}
			// call func
			result = func(opd_stack + opd_size - argc);
			// pop args
			opd_size -= argc;
			// push result
			push_opd(result);
			//printf("call end\n");
		}
		else
		{
			if (check)
				merror_illegal_token();
			else
			{
				l_put_back();
				break;
			}
		}
	}
	// pop up all
	while(opr_size > 0)
	{
		real result;
		result = calc_pop(opd_stack,opr_stack,&opd_size,&opr_size);
		push_opd(result);
	}
	if (opd_size != 1)
	{
		merror_msg("calc:unknown error!");
	}
	return opd_stack[0];
}