1368 運算式轉換

來源:互聯網
上載者:User
 
描述

把中綴運算式轉化為尾碼運算式……

書上有詳細講解.

輸入

第一行為T,表示有T組資料;

以下T行每行包含一個字串,是中綴運算式。

輸出

對於每組測試資料資料尾碼運算式。

範例輸入
1
A+(B-C/D)*E
範例輸出
ABCD/-E*+

 

 

經典問題,用棧實現

#include   <iostream>     #include   <string>     #include   <stack>         using   namespace   std;             void   Convert(const   string   &   Infix,   string   &   Postfix);         bool   IsOperand(char   ch);         bool   TakesPrecedence(char   OperatorA,   char   OperatorB);             int   main(void)           {           char   Reply;          int number;        scanf("%d",&number);       while(number--)                    {                 string   Infix,   Postfix;       //   local   to   this   loop                                  cin   >>   Infix;                     Convert(Infix,   Postfix);                                  cout  <<   Postfix   <<   endl;                             }                      return   0;           }             /*   Given:     ch       A   character.           Task:       To   determine   whether   ch   represents   an   operand   (here   understood                           to   be   a   single   letter   or   digit).           Return:   In   the   function   name:   true,   if   ch   is   an   operand,   false   otherwise.     */     bool   IsOperand(char   ch)           {           if   (((ch   >=   'a')   &&   (ch   <=   'z'))   ||                 ((ch   >=   'A')   &&   (ch   <=   'Z'))   ||                 ((ch   >=   '0')   &&   (ch   <=   '9')))                 return   true;           else                 return   false;           }             /*   Given:     OperatorA         A   character   representing   an   operator   or   parenthesis.                           OperatorB         A   character   representing   an   operator   or   parenthesis.           Task:       To   determine   whether   OperatorA   takes   precedence   over   OperatorB.           Return:   In   the   function   name:   true,   if   OperatorA   takes   precedence   over                           OperatorB.     */     bool   TakesPrecedence(char   OperatorA,   char   OperatorB)           {           if   (OperatorA   ==   '(')                 return   false;           else   if   (OperatorB   ==   '(')                 return   false;           else   if   (OperatorB   ==   ')')                 return   true;           else   if   ((OperatorA   ==   '^')   &&   (OperatorB   ==   '^'))                 return   false;           else   if   (OperatorA   ==   '^')                 return   true;           else   if   (OperatorB   ==   '^')                 return   false;           else   if   ((OperatorA   ==   '*')   ||   (OperatorA   ==   '/'))                 return   true;           else   if   ((OperatorB   ==   '*')   ||   (OperatorB   ==   '/'))                 return   false;           else                 return   true;                           }             /*   Given:     Infix         A   string   representing   an   infix   expression   (no   spaces).           Task:       To   find   the   postfix   equivalent   of   this   expression.           Return:   Postfix     A   string   holding   this   postfix   equivalent.     */     void   Convert(const   string   &   Infix,   string   &   Postfix)           {           stack<char>   OperatorStack;           char   TopSymbol,   Symbol;           int   k;               for   (k   =   0;   k   <   Infix.size();   k++)                 {                 Symbol   =   Infix[k];                 if   (IsOperand(Symbol))                       Postfix   =   Postfix   +   Symbol;                 else                       {                       while   ((!   OperatorStack.empty())   &&                             (TakesPrecedence(OperatorStack.top(),   Symbol)))                             {                             TopSymbol   =   OperatorStack.top();                             OperatorStack.pop();                             Postfix   =   Postfix   +   TopSymbol;                             }                       if   ((!   OperatorStack.empty())   &&   (Symbol   ==   ')'))                             OperatorStack.pop();       //   discard   matching   (                       else                             OperatorStack.push(Symbol);                       }                 }               while   (!   OperatorStack.empty())                 {                 TopSymbol   =   OperatorStack.top();                 OperatorStack.pop();                 Postfix   =   Postfix   +   TopSymbol;                 }           }   

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.