繁体   English   中英

使用C ++将中缀转换为后缀(包括赋值运算符)

[英]converting infix to postfix (assignment operator included) using c++

我正在尝试将infix表达式转换为c ++中的后缀。 我已经用简单的运算符/,*,+,-完成了编码,但是我对赋值运算符(=)的逻辑感到困惑。 我知道必须给它最低的优先级,但是对于A = B = 4或A =(B = 2)* 2之类的输入,我如何确定优先级。 我的代码写在下面。 (没有赋值运算符的实现)。

#include <iostream>
#include <stack>
#include <string>
using namespace std;


// Simply determine if character is one of the four standard operators.
bool isOperator(char character) {
if (character == '+' || character == '-' || character == '*' || character == '/' || character=='=') {
    return true;
}
return false;
}


// If the character is not an operator or a parenthesis, then it is assumed to be an operand.
bool isOperand(char character) {
if (!isOperator(character) && character != '(' && character != ')') {
    return true;
}
return false;
}


// Compare operator precedence of main operators.
// Return 0 if equal, -1 if op2 is less than op1, and 1 if op2 is greater than op1.
int compareOperators(char op1, char op2) {
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) { return -1; }
else if ((op1 == '+' || op1 == '-') && (op2 == '*' || op2 == '/')) { return 1; }
return 0;
}


int main()
{
// Empty character stack and blank postfix string.
stack<char> opStack;
string postFixString = "";

char input[100];

// Collect input
cout << "Enter an expression: ";
cin >> input;

// Get a pointer to our character array.
char *cPtr = input;

// Loop through the array (one character at a time) until we reach the end of the string.
while (*cPtr != '\0') {
    // If operand, simply add it to our postfix string.
    // If it is an operator, pop operators off our stack until it is empty, an open parenthesis or an operator with less than or equal precedence.
    if (isOperand(*cPtr)) { postFixString += *cPtr; }
    else if (isOperator(*cPtr)) {
        while (!opStack.empty() && opStack.top() != '(' && compareOperators(opStack.top(), *cPtr) <= 0) {
            postFixString += opStack.top();
            opStack.pop();
        }
        opStack.push(*cPtr);
    }
    // Simply push all open parenthesis onto our stack
    // When we reach a closing one, start popping off operators until we run into the opening parenthesis.
    else if (*cPtr == '(') { opStack.push(*cPtr); }
    else if (*cPtr == ')') {
        while (!opStack.empty()) {
            if (opStack.top() == '(') { opStack.pop(); break; }
            postFixString += opStack.top();
            opStack.pop();
        }
    }

    // Advance our pointer to next character in string.
    cPtr++;
}

// After the input expression has been ran through, if there is any remaining operators left on the stack
// pop them off and put them onto the postfix string.
while (!opStack.empty()) {
    postFixString += opStack.top();
    opStack.pop();
}


// Show the postfix string at the end.
cout << "Postfix is: " << postFixString << endl;
return 0;
}

尝试给它与操作数相同的优先级,然后看看会得到什么:

// Simply determine if character is one of the four standard operators.
bool isOperator(char character) {
if (character == '+' || character == '-' || character == '*' || character == '/') {
    //|| character=='='
    return true;
}
return false;
}

// If the character is not an operator or a parenthesis, then it is assumed to be an operand.
bool isOperand(char character) {
//for =, treat it wth the same rules as the operands; push to output
if (character == '='){
    return true;
}
if (!isOperator(character) && character != '(' && character != ')') {
    return true;
}
return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM