expression evaluation and conversion algorithm

Source: Internet
Author: User

Postfix expression Evaluation algorithm

stack operands;  //the arithmetic stack while (not to the end of expression) {    scanf ("an Op object op");     if (op is  operand)         operands.push (OP);     else if (op is  operator)     {         operand_right = operands.pop ();         Operand_left = operands.pop ();        result =  Operand_left op operand_right;        operands.push (result);     }    else    {         printf ("suffix expression is invalid !");     }}if (Operands.size ()  == 1)     return () ; else    printf ("suffix expression&Nbsp;is invalid! "); 

Evaluation algorithm of prefix expression

Algorithm one: Start processing from the end of the expression (right-to-left)

Pseudo Code Description:

stack operands;   //operand stack while (not reaching the first expression) {    scanf ("an operand op");     if (Op is operand)         operands.push (OP) ;     else if (Op is operator)     {         operand_left = operands.pop ();         operand_right = operands.pop ();         result  = operand_left op operand_right;         Operands.push (Result);    }    else    {         printf ("prefix expression is invalid!");     }}if (Operands.size ()  == 1) {    return ( );} else    printf ("prefix  expression is invalid! "); 

Algorithm two: Start processing from the expression header (left to right)

The stack operations;  //operand stack, which can be either a cloud algorithm or an operand while (not to the end of an expression) {    scanf (" An Op object op ");     if (Op is operator)     {         operations.push (OP);    }    else  if (Op is operand)     {        if ( ()  is operator)         {             operations.push (OP);         }        else         {            while ( ()  is  operand)             {                 operand_right = op;                 operand_left = operations.pop ();                 operator =  Operations.pop ();                 op = operand_left operator operand_right;             }             Operations.push (OP);        }    }     else    {        printf ("Prefix  Expression is invalid! ");     }}if (Operations.size ()  == 1)     return operations.tOP (); else    printf ("prefix expression is invalid!"); 

infix expression converted to postfix expression algorithm

stack operators;   //operator Stack while (not to end of expression) {    scanf ("an operand op");     if (Op is operand)     {         printf (OP);     }    else if (Op is Operator)     {        if (op  priority greater than ()  && op is not  closing brackets)              operator.push (OP);         else if (Op is   Closing brackets)         {             do            {                 tmp =  Operators.pop ();    &nbsP;            if (tmp is not  corresponds to opening parenthesis)                 {                      printf (TMP);                 }                 else                 {                     break;                 }                 if (Operators.empty ())                 {                     printf ("Infix  Expression is invalid! ");                 }             }while (1);         }        else         {            do             {                 tmp = operators.pop ();                 PRINTF (TMP);        & nbSp;    }while (op  priority is less than ());             operators.push (OP);        }     }    else    {         printf ("infix expression is invalid!");     }}while (!operators.empty ()) {    tmp = operators.pop ();     PRINTF (TMP);}

infix expression conversion to prefix expression algorithm

Start processing from the end of an expression

The stack operators;  //operator stack stack operations;  //an operand stack, which can be either an operator or an operand while  ( Not to expression first) {    scanf ("an operand op");    if  (Op is operand)     {        operations.push (OP);     }    else if  (Op is operator)     {         if  (op  priority is greater than ()  &&  op is not  opening brackets)              Operators.push (OP);        else if  (op is  left parenthesis)         {             do            {          &nBsp;      tmp = operators.pop ();                 if  (tmp is not  corresponds to closing parenthesis)                  {                      Operations.push (TMP);                 }                 else                {                      break;                }                 if  (Operators.empty ())                  {                     printf ("Infix  Expression is invalid! ");                 }             }while (1);         }        else         {            do             {                 tmp = operators.pop ();       &nBsp;         operations.push (TMP);             }while (op  priority is less than ());             operators.push (OP);         }    }    else    {         printf ("infix expression is invalid!");     }}while (!operators.empty ()) {    tmp = operators.pop ();     operations.push (TMP);} while (!operations.empty ()) {    tmp = operations.pop ();     printf (TMP);}

expression evaluation and conversion algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.