示例#1
0
int infixToPostfix(char* exp)
{
	int i,k;
	struct Stack* stack = createStack(strlen(exp));
	if (!stack)
		return -1;
	for ( i=0, k=-1; exp[i]; ++i)
	{
		if(isOperand(exp[i]))
			exp[++k] = exp[i];
		else if (exp[i]== '(')
			push(stack,exp[i]);
		else if (exp[i]== ')')
		{
			while (!isEmpty(stack) && peek(stack)!='(' )
				exp[++k]=pop(stack);
			if (!isEmpty(stack) && peek(stack))
				return -1;
			else
				pop(stack);
		}
		else
		{
			while (!isEmpty(stack) && Precedence(exp[i]) <= Precedence(peek(stack)))
				exp[++k]=pop(stack);
			push(stack,exp[i]);			
		}
	}
	while (isEmpty(stack))
		exp[++k]=pop(stack);
	exp[++k]= '\0';
	printf("%s",exp);
}
示例#2
0
文件: hbppcalc.c 项目: xharbour/core
static double Reduce( PBIOP Exp )
{
   PBIOP Right;
   double dRet = 0;

   if( Exp->Operator[0] == 0 )
   {
      dRet = Exp->Left;

      free( (void *) Exp );

      return dRet;
   }

   if( Exp->Right->Operator[0] && Precedence( Exp ) >= Precedence( Exp->Right ) )
   {
      Right = Exp->Right;

      Exp->Right = BiOp();
      Exp->Right->Left = Right->Left;

      Right->Left = Reduce( Exp );

      return Reduce( Right );
   }

   //printf( "Operator: %s Left: %i Right: %i\n", Exp->Operator, (int) Exp->Left, (int) Reduce( Exp->Right ) );

   switch( Exp->Operator[0] )
   {
      case '+':
         dRet = Exp->Left + Reduce( Exp->Right );
         break;

      case '-':
         dRet = Exp->Left - Reduce( Exp->Right );
         break;

      case '*':
         dRet = Exp->Left * Reduce( Exp->Right );
         break;

      case '/':
         dRet = Exp->Left / Reduce( Exp->Right );
         break;

      case '&':
         if( Exp->Operator[1] )
         {
            if( Exp->Left )
            {
               dRet = Reduce( Exp->Right ) ? 1 : 0;
            }
            else
            {
               dRet = 0;
            }
         }
         else
         {
            dRet = (long) Exp->Left & (long) Reduce( Exp->Right );
         }
         break;

      case '|':
         if( Exp->Operator[1] )
         {
            if( Exp->Left )
            {
               dRet = 1;
            }
            else
            {
               dRet = Reduce( Exp->Right ) ? 1 : 0;
            }
         }
         else
         {
            dRet = (long) Exp->Left | (long) Reduce( Exp->Right );
         }
         break;

      case '=':
         dRet = (long) Exp->Left == (long) Reduce( Exp->Right );
         break;

      case '!':
         dRet = (long) Exp->Left != (long) Reduce( Exp->Right );
         break;

      case '<':
         if( Exp->Operator[1] )
         {
            dRet = (long) Exp->Left <= (long) Reduce( Exp->Right );
         }
         else
         {
            dRet = (long) Exp->Left < (long) Reduce( Exp->Right );
         }
         break;

      case '>':
         if( Exp->Operator[1] )
         {
            dRet = (long) Exp->Left >= (long) Reduce( Exp->Right );
         }
         else
         {
            dRet = (long) Exp->Left > (long) Reduce( Exp->Right );
         }
         break;
   }

   free( (void *) Exp );

   return dRet;
}
示例#3
0
void Expr::ReduceAndPush(Expr *n) {
    while(Precedence(n) <= Precedence(TopOperator())) {
        Reduce();
    }
    PushOperator(n);
}