| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 355 人关注过本帖
标题:请高手帮我解答一下 谢谢 急~~~
只看楼主 加入收藏
nccxq
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-1-8
结帖率:0
收藏
 问题点数:0 回复次数:1 
请高手帮我解答一下 谢谢 急~~~
中缀法转换为后缀法      但是我完全看不懂题目是要我们干吗???  
有没有人可以帮我解释一下每个步骤分别是什么意思呀!!!!!   
我在这先谢谢啦!!!  可以把程序变得精简点吗???   好长!!

#include <iostream>
#include <string>
#include <stack>
#include <cctype>   // to use the to lower function

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;

   do
      {
      string Infix, Postfix;   // local to this loop

      cout << "Enter an infix expression (e.g. (a+b)/c^2, with no spaces):"
         << endl;
      cin >> Infix;

      Convert(Infix, Postfix);
      cout << "The equivalent postfix expression is:" << endl
         << Postfix << endl;
      cout << endl << "Do another (y/n)? ";
      cin >> Reply;
      }
   while (tolower(Reply) == 'y');

   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;
      }
   }

看不懂 看不懂   谁帮帮我!!!!!  万分感谢







[ 本帖最后由 nccxq 于 2010-1-9 00:34 编辑 ]
搜索更多相关主题的帖子: 解答 
2010-01-08 15:32
nccxq
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-1-8
收藏
得分:0 
谁帮帮我呀!!!    都看不懂!!!
2010-01-09 00:35
快速回复:请高手帮我解答一下 谢谢 急~~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012498 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved