计算后缀表达式的程序,但这个程序有一个奇怪的问题——它不给出输出,而是给出一个很长的错误

Program to evaluate postfix expressions but there is a wierd problem with this program-it does not gives output and instead gives a long error

我正在为后缀表达式求值编写代码,遇到了一个奇怪的错误。它显示了一个很大的错误,这对我来说真的很难理解代码中的错误。 如果您看一下并告诉我它有什么问题或我犯了什么错误,那将非常有帮助。 我列出了下面的代码看看。
提前谢谢你

这是代码:

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

using namespace std;

int evaluate(string expression);

bool Isdigit(char c);

bool IsOprator(char c);

int performcalc(char opration,int op2,int op1);

int main(){
    string expression;
    cout<<"Start of the program ! "<<endl;
    cout<<"Enter a postfix expression to evaluate: ";
    cin>>expression;
    int result = evaluate(expression);
    cout<<"Result ="<<result<<endl;
    cout<<"End of the program !!";
    return 0;
}

int evaluate(string expression){
    stack <char> S;
    
    for (int i = 0; i < expression.length(); i++)
    {
        if (expression[i] == ' ' || expression[i] == ',')
        {
            continue;
        }
        else if (Isdigit(expression[i]))
        {
            int a = stoi(expression[i]);
            S.push(a);
        }
        
        else if (IsOprator(expression[i]))
        {
            int op2 = S.top(); S.pop();
            int op1 = S.top(); S.pop();
            int result = performcalc(expression[i],op2,op1);
            S.push(result);
        }
    }
    return S.top();
}

bool Isdigit(char c){
    if (c >= '0' && c<='9')
    {
        return true;
    }
    else
    {
        return false;
    }
    
}

bool IsOprator(char c){
    if (c == '+' || c == '*' || c == '/' || c == '-' )
    {
        return true;
    }
    else
    {
        return false;
    }
    
}

int performcalc(char opration,int op2,int op1){
    if (opration == '+')
    {
        return op1+op2;
    }
    else if (opration == '-')
    {
        return op1-op2;
    }
    else if (opration == '*')
    {
        return op1*op2;
    }
    else if (opration == '/')
    {
        return op1/op2;
    }
    return -1;
}

错误:

Postfix_evaluation_using_stack.cpp: In function 'int evaluate(std::string)':
Postfix_evaluation_using_stack.cpp:37:39: error: no matching function for call to 'stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, ch
ar>::value_type&)'
   37 |             int a = stoi(expression[i]);
      |                                       ^
In file included from c:\ming\mingw\include\c++.2.0\string:55,
                 from c:\ming\mingw\include\c++.2.0\bits\locale_classes.h:40,
                 from c:\ming\mingw\include\c++.2.0\bits\ios_base.h:41,
                 from c:\ming\mingw\include\c++.2.0\ios:42,
                 from c:\ming\mingw\include\c++.2.0\ostream:38,
                 from c:\ming\mingw\include\c++.2.0\iostream:39,
                 from Postfix_evaluation_using_stack.cpp:1:
c:\ming\mingw\include\c++.2.0\bits\basic_string.h:6503:3: note: candidate: 'int std::__cxx11::stoi(const string&, std::size_t*, int)'
 6503 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
c:\ming\mingw\include\c++.2.0\bits\basic_string.h:6503:22: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<st
d::allocator<char>, char>::value_type' {aka 'char'} to 'const string&' {aka 'const std::__cxx11::basic_string<char>&'}
 6503 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |        ~~~~~~~~~~~~~~^~~~~
c:\ming\mingw\include\c++.2.0\bits\basic_string.h:6609:3: note: candidate: 'int std::__cxx11::stoi(const wstring&, std::size_t*, int)'
 6609 |   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
c:\ming\mingw\include\c++.2.0\bits\basic_string.h:6609:23: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<st
d::allocator<char>, char>::value_type' {aka 'char'} to 'const wstring&' {aka 'const std::__cxx11::basic_string<wchar_t>&'}
 6609 |   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)

你的问题是你试图比较 std::stringchar:

if (expression == ' ' || expression == ',')

改为:

if (expression == " " || expression == ",")

string class 不支持与单个字符的比较,因此出现错误。

另外,这是错误的:

int a = stoi(expression[i]);

stoi需要一个string,你正在传递一个char。将其更改为:

int a = stoi(expression);

你的堆栈是错误的:

stack <char> S;

应该是整数栈,不是char:

stack <int> S;