繁体   English   中英

中缀到后缀的转换:堆栈无法识别错误

[英]Conversion of infix to postfix: stack is not recognized error

我是使用堆栈的初学者,所以我一直在尝试对其进行不同的练习。 我正在尝试转换中缀-> 后缀。 xcode 调试器说“使用类模板‘堆栈’需要模板参数’。这是我的代码。

#include<iostream>
#include<stack>
#include<string>

using namespace std;


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

bool IsOperator(char C)
{
    if (C == '+' || C == '-' || C == '*' || C == '/' || C == '^') {
        return true;
    }
    return false;
}
bool IsLeftParenthesis(char ch)
{
    if (ch == '(') {
        return true;
    }
    return false;
}

bool IsRightParenthesis(char ch)
{
    if (ch == ')') {
        return true;
    }
    return false;
}

bool Flag(char ch)
{
    if (!IsOperand(ch) || !IsOperator(ch) || !IsLeftParenthesis(ch) || !IsRightParenthesis(ch)) {
        return false;
    }
    return true;
}

int IsRightAssociative(char op)
{
    if (op == '^') {
        return true;
    }
    return false;
}
int GetOperatorWeight(char op)
{
    int weight = -1;
    switch (op) {
        case '+':
        case '-':
            weight = 1;
            break;
        case '*':
        case '/':
            weight = 2;
            break;
        case '^':
            weight = 3;
            break;
    }
    return weight;
}

bool HasHigherPrecedence(char op1, char op2)
{
    int op1Weight = GetOperatorWeight(op1);
    int op2Weight = GetOperatorWeight(op2);
    // If operators have equal precedence, return true if they are left associative.
    // BUT REMEMBER...return false, if right associative.
    // if operator is left-associative, left one should be given priority.
    if (op1Weight == op2Weight) {
        if (IsRightAssociative(op1)) {
            return false;
        }
        else {
            return true;
        }
    }
    return op1Weight > op2Weight ? true : false;
}

string InfixToPostfix(string expression)
{

    stack S;
    string postfix = "";
    for (auto& elem : expression) {
        if (Flag(elem)) {
            continue;
        }
        // If character is operator, pop two elements from stack, perform operation and push the result back.
        else if (IsOperator(elem)) {
            while (!S.empty() && S.top() != '(' && HasHigherPrecedence(S.top(), elem)) {
                postfix += S.top();
                S.pop();
            }
            S.push(elem);
        }
        else if (IsOperand(elem)) {
            postfix += elem;
        }
        else if (elem == '(') {
            S.push(elem);
        }
        else if (elem == ')') {
            while (!S.empty() && S.top() != '(') {
                postfix += S.top();
                S.pop();
            }
            S.pop();
        }
    }

    while (!S.empty()) {
        postfix += S.top();
        S.pop();
    }

    return postfix;
}

int main()
{
    // std::string expression = "54/(5^2)+(6^2^3)";
    std::string expression = "A+(BC-(D/E^F)G)H";
    std::string postfix = InfixToPostfix(expression);
    std::cout << "Output = " << postfix << "\n";
}

这里特别是发生错误的地方。

string InfixToPostfix(string expression)
{

    stack S;

它说

Stack S -> 使用类模板'stack'需要模板参数'

Stack 是一个容器,您需要指定容器的类型,例如:

stack <int> S;

或者在你的情况下它是一堆char

stack <char> S;

暂无
暂无

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

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