用于计算后缀表达式的程序,但此程序存在wierd问题,它不给出输出,而是给出一个长错误



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

这是代码:

#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:mingmingwincludec++9.2.0string:55,
from c:mingmingwincludec++9.2.0bitslocale_classes.h:40,
from c:mingmingwincludec++9.2.0bitsios_base.h:41,
from c:mingmingwincludec++9.2.0ios:42,
from c:mingmingwincludec++9.2.0ostream:38,
from c:mingmingwincludec++9.2.0iostream:39,
from Postfix_evaluation_using_stack.cpp:1:
c:mingmingwincludec++9.2.0bitsbasic_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:mingmingwincludec++9.2.0bitsbasic_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:mingmingwincludec++9.2.0bitsbasic_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:mingmingwincludec++9.2.0bitsbasic_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类不支持使用单个字符进行比较,因此出现错误。

此外,这是错误的:

int a = stoi(expression[i]);

stoi需要string,您正在传递char。更改为:

int a = stoi(expression);

你的堆栈是错误的:

stack <char> S;

它应该是一个整数堆栈,而不是字符:

stack <int> S;

最新更新