急需高手帮忙看下这个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
而是些其他的错误的提示
麻烦哪位高手修改下