| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 596 人关注过本帖
标题:哪位高手帮帮忙啊!!!c++中缀转变为后缀 急啊~~ 谢谢!!
只看楼主 加入收藏
nccxq
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-1-8
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
哪位高手帮帮忙啊!!!c++中缀转变为后缀 急啊~~ 谢谢!!
中缀表示法变为后缀表示法
但是我看不懂题目是要我们做什么  
谁能为我解释一下各个步骤分别是什么意思吗???  万分感谢呀!!  请大家帮帮忙
程序可以变精简些吗?? 怎么改
#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;
      }
  }

搜索更多相关主题的帖子: 后缀 
2010-01-09 01:15
zj33
Rank: 2
等 级:论坛游民
帖 子:12
专家分:25
注 册:2008-10-2
收藏
得分:10 
  通常我们用于数学表达式的表示法称为中缀表示法,这种表示法把运算符放在操作数中间。例如,表达式
a+b中的运算符+就放在操作数a和b之间。在中缀表示法中,运算符是有优先级别的。也就是说,从左到右
计算表达式,乘法和除法要比加法和减法有更高的优先级。如果想以不同的顺序计算表达式,就必须加上
刮号。例如,对于表达式a+b*c,我们先对b和c进行*运算,然后再对a和b*c的结果进行+运算。
   在20世纪50年代,波兰数学家Lukasiewicz发现如果将运算符写在操作数之前(前缀表示或波兰表示,
如+ab),或者操作数之后(后缀表示或逆波兰表示,如ab+),则可以不必指定运算符优先级。例如,表达式
a+b*c的后缀式表示是abc*+
下面给出中缀表达式和与之对应的后缀表达式
      中缀表达式                                  对应的后缀表达式
       a + b                                          a b +
     a + b * c                                       a b c * +
     a * b + c                                       a b * c +
     (a + b) * c                                     a b + c *
   (a - b) * (c + d)                               a b - c d + *
   (a + b) * (c - d / e) + f                      a b + c d e / - * f +
该程序的功能是, 将中缀字符串Infix转换为后缀字符串Postfix。
程序的算法是, 将中缀字符串Infix依次扫描一遍。如果扫描的字符是数字或变量(这个功能由函数IsOperand实现),直接加入到后缀字符串Postfix中。如果是运算符,这时如果栈是空的,将运算符压入栈。如果栈不为空,则将该运算符和栈顶运算符比较优先级(该功能由函数
TakesPrecedence实现),如果该运算符的优先级大于栈顶运算符,将该运算符压栈。否则,将栈顶运算符出栈,加入后缀字符串Postfix中。而该运算符继续和新的栈顶运算符比较优先级。
一直到该运算符的优先级大于栈顶运算符或栈为空,将该运算符压入栈。
当中缀字符串扫描结束后,只要将栈中的运算符依次出栈,加入后缀字符串中即可。
2010-01-09 21:15
快速回复:哪位高手帮帮忙啊!!!c++中缀转变为后缀 急啊~~ 谢谢!!
数据加载中...
 
   



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

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