| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 640 人关注过本帖
标题:急需高手帮忙看下这个infix转换成Postfix 并运算
只看楼主 加入收藏
烈日拾荒者
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-8-18
收藏
 问题点数:0 回复次数:0 
急需高手帮忙看下这个infix转换成Postfix 并运算
#include <cctype>     // Provides isdigit
#include <cstdlib>    // Provides EXIT_SUCCESS
#include <cstring>    // Provides strchr
#include <iostream>   // Provides cout, cin, peek, ignore
#include <stack>      // Provides the stack template class
using namespace std;

// PROTOTYPES for functions used by this demonstration program:
void do_operation(char operation, stack<double>& s, bool& okay);
double evaluate_expression(istream& in, bool& okay);
void evaluate(istream& in);
bool is_operand(char ch);
bool takes_precedence(char operatorA, char operatorB);
void convert(istream& in);

string infix = "";
string postfix = "";
stack<char> operations;
stack<char> reverseOperations;
bool stackDone = false;

int main( )
{
    double answer;
 bool okay;


 do{
  cout << "================================================================" << endl;
  cout << "Type an infix arithmetic expression (Just type ENTER to quit!!):" << endl;
  cout << "Infix: ";
  if(cin.peek() == '\n' || cin.peek() == EOF)
   break;

  cout << "Postfix: ";
  answer = evaluate_expression(cin, okay);
  
  cout << endl;
  
  if(okay)
      cout << "Evaluation: " << answer << "." << endl;
  else
   cout << endl << "Invalid infix notation!!" << endl;
 }while(true);

   
    return EXIT_SUCCESS;
}

void do_operation(char operation, stack<double>& s, bool& okay)
// Pops two numbers from s, applies the operation, and pushes the answer
// back on the stack s. Sets okay to false if any errors are found,
// but leaves okay alone if there are no errors.
{
 double number1, number2;
    number2 = s.top();
    s.pop();
    number1 = s.top( );
    s.pop();
    switch (operation)
    {
        case '+':
   s.push(number1 + number2);
   break;
        case '-':
   s.push(number1 - number2);
            break;
        case '*':
   s.push(number1 * number2);
            break;
        case '/':
   s.push(number1 / number2);
            break;
  default:
   okay = false;
   break;
    }
 if (stackDone)
  reverseOperations.pop();
 else
  operations.pop();
}

double evaluate_expression(istream& in, bool& okay)
// Process each line for infix-to-postfix conversion and evaluation
{
 convert(in);
 
 const char DECIMAL = '.';
 const char LEFT_PARENTHESIS = '(';
 const char RIGHT_PARENTHESIS = ')';
 
 stack<double> numbers;
 stack<double>reverseNumbers;
 double number;
 char symbol;
 char hold;
 for(int i = 0; i < postfix.size(); i++)
 {
  hold = postfix[i];
  
  if(isdigit(postfix[i]) || (postfix[i] == DECIMAL))
  {
   number = hold-48;
   numbers.push(number);
  }

  else if(strchr("+-*/", postfix[i]))
  {
   symbol = hold;  
   operations.push(symbol);
  }
   
  else if(postfix[i] == '\n')
  {
   cin.ignore();
   //do_operation(symbol, numbers, okay);
  }
  
  else if (in.peek( ) == RIGHT_PARENTHESIS){  
            in.ignore( );
            while (!operations.empty()) {
    do_operation(operations.top(), numbers, okay);
   }
  }
        else {
            in.ignore( );
   okay = false;
  }
    }
 
 stackDone = true;
 
 while (!numbers.empty()) {
  reverseNumbers.push(numbers.top());
  numbers.pop();
 }
 
 while (!operations.empty()) {
  reverseOperations.push(operations.top());
  operations.pop();
 }
 
 
 
 while (!reverseOperations.empty()) {
  do_operation(reverseOperations.top(), reverseNumbers, okay);
 }
 cout << postfix << endl;
 
 return reverseNumbers.top();

}

void convert(istream& in) {
 in >> infix;

 stack<char> hold_symbol;
 char top_symbol;
 char symbol;
 
 for(int i = 0; i < infix.size(); ++i)
 {
  symbol = infix[i];
  if(is_operand(symbol))
   postfix = postfix + symbol;
  else
  {
   while((!hold_symbol.empty()) && (takes_precedence(hold_symbol.top(), symbol)))
   {
    top_symbol = hold_symbol.top();
    hold_symbol.pop();
    postfix = postfix + top_symbol;
   }
   if((!hold_symbol.empty()) && (symbol == ')'))
    hold_symbol.pop();
   else
    hold_symbol.push(symbol);
  }
 }
 while(!hold_symbol.empty())
 {
  top_symbol = hold_symbol.top();
  hold_symbol.pop();
  postfix = postfix + top_symbol;
 }
}

bool is_operand(char ch)
{
 if(((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')))
  return true;
 else
  return false;
}

bool takes_precedence(char operatorA, char operatorB)
{
 if(operatorA == '(')
  return false;
 else if(operatorB == '(')
  return false;
 else if(operatorB == ')')
  return true;
 else if((operatorA == '*') || (operatorA == '/'))
  return true;
 else if((operatorB == '*') || (operatorB == '/'))
  return false;
 else
  return true;
}

这是一个infix的简单表达式 转换成Postfix 并运算出来
这个可以运行
但是当我们输入错误的infix 表达式 例如 (1+2
的时候  显示的并不是  invalid infix notation
而是些其他的错误的提示
麻烦哪位高手修改下  


搜索更多相关主题的帖子: Postfix infix 运算 
2010-08-18 08:35
快速回复:急需高手帮忙看下这个infix转换成Postfix 并运算
数据加载中...
 
   



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

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