在 C++ 中解析表达式时出错
Error parsing an expression in C++
谁能指出我的代码中出现分段错误的原因。我正在尝试将优先级由“()”决定的算术表达式转换为后缀形式,然后求解该表达式。
#include<iostream>
#include<string>
#include<stack>
using namespace std;
string post(string exp)
{
cout<<"reached post";
stack<char> s2;
string new_exp="";
int length=exp.length();
for(int i=0; i<length; i++)
{
if(exp[i]=='(')
{
s2.push(exp[i]);
}
else if(exp[i]=='+'|| exp[i]=='-' || exp[i]=='*' || exp[i]=='/')
{
new_exp+=' ';
s2.push(exp[i]);
}
else if(exp[i]>='0'&& exp[i]<='9')
{
new_exp+=exp[i];
}
else if(exp[i]==')')
{
new_exp+=' ';
new_exp+=s2.top();
s2.pop();
s2.pop();
}
}
if(!s2.empty())
{
while(!s2.empty())
{
new_exp+=' ';
new_exp+=s2.top();
s2.pop();
}
}
return(new_exp);
}
int operation(char op, char op1, char op2)
{
if(op == '+') return(op1+op2);
else if(op=='-') return(op1-op2);
else if(op=='*') return(op1*op2);
else if(op=='/') return(op1/op2);
}
int solve(string expression)
{
cout<<"\nreached solve";
string postfix=post(expression);
stack<char> s;
int res;
int length=postfix.length();
for(int i=0; i<length; i++)
{
if(postfix[i]==' ')
{
continue;
}
else if(postfix[i]=='+'|| postfix[i]=='-' || postfix[i]=='*' || postfix[i]=='/')
{
char op2=s.top();
s.pop();
char op1=s.top();
s.pop();
res=operation(postfix[i],op1,op2);
s.push(res);
}
else if(postfix[i]>='0' && postfix[i]<=9)
{
int operand=0;
while(postfix[i]!=' ' || i!=length)
{
operand=(operand*10)+(postfix[i]-'0');
i++;
}
i--;
s.push(operand);
}
}
return(res);
}
int main(void)
{
string exp;
int result;
cout<<"Enter expression: ";
getline(cin,exp);
result=solve(exp);
cout<<"\nResult= "<<result;
return 0;
}
我收到以下错误消息:
cav@cav-VirtualBox:~/src/cpp$ ./infix_postfix
Enter expression: 10+3
Segmentation fault (core dumped)
我至少可以看到两个错误。首先,
else if(postfix[i]>='0' && postfix[i]<=9)
您需要比较字符 '9'
,而不是整数 9
,因为这里有一个字符串。应该是:
else if(postfix[i]>='0' && postfix[i]<='9')
^ ^
第二个问题在这里:
while(postfix[i]!=' ' || i!=length)
你在这里的意思是和操作&&
,而不是||
。当它是 ||
时,除了 i
超出长度之外,所有字符基本上都是正确的。另外 i != length
应该在 postfix[i] != ' '
之前测试,因为 i == length
postfix[i]
将超出范围。这一行应该是:
while(i!=length && postfix[i]!=' ')
由于这两个错误,您没有正确地将值推送到堆栈,在不同时间获取错误值,这会导致分段错误。
谁能指出我的代码中出现分段错误的原因。我正在尝试将优先级由“()”决定的算术表达式转换为后缀形式,然后求解该表达式。
#include<iostream>
#include<string>
#include<stack>
using namespace std;
string post(string exp)
{
cout<<"reached post";
stack<char> s2;
string new_exp="";
int length=exp.length();
for(int i=0; i<length; i++)
{
if(exp[i]=='(')
{
s2.push(exp[i]);
}
else if(exp[i]=='+'|| exp[i]=='-' || exp[i]=='*' || exp[i]=='/')
{
new_exp+=' ';
s2.push(exp[i]);
}
else if(exp[i]>='0'&& exp[i]<='9')
{
new_exp+=exp[i];
}
else if(exp[i]==')')
{
new_exp+=' ';
new_exp+=s2.top();
s2.pop();
s2.pop();
}
}
if(!s2.empty())
{
while(!s2.empty())
{
new_exp+=' ';
new_exp+=s2.top();
s2.pop();
}
}
return(new_exp);
}
int operation(char op, char op1, char op2)
{
if(op == '+') return(op1+op2);
else if(op=='-') return(op1-op2);
else if(op=='*') return(op1*op2);
else if(op=='/') return(op1/op2);
}
int solve(string expression)
{
cout<<"\nreached solve";
string postfix=post(expression);
stack<char> s;
int res;
int length=postfix.length();
for(int i=0; i<length; i++)
{
if(postfix[i]==' ')
{
continue;
}
else if(postfix[i]=='+'|| postfix[i]=='-' || postfix[i]=='*' || postfix[i]=='/')
{
char op2=s.top();
s.pop();
char op1=s.top();
s.pop();
res=operation(postfix[i],op1,op2);
s.push(res);
}
else if(postfix[i]>='0' && postfix[i]<=9)
{
int operand=0;
while(postfix[i]!=' ' || i!=length)
{
operand=(operand*10)+(postfix[i]-'0');
i++;
}
i--;
s.push(operand);
}
}
return(res);
}
int main(void)
{
string exp;
int result;
cout<<"Enter expression: ";
getline(cin,exp);
result=solve(exp);
cout<<"\nResult= "<<result;
return 0;
}
我收到以下错误消息:
cav@cav-VirtualBox:~/src/cpp$ ./infix_postfix
Enter expression: 10+3
Segmentation fault (core dumped)
我至少可以看到两个错误。首先,
else if(postfix[i]>='0' && postfix[i]<=9)
您需要比较字符 '9'
,而不是整数 9
,因为这里有一个字符串。应该是:
else if(postfix[i]>='0' && postfix[i]<='9')
^ ^
第二个问题在这里:
while(postfix[i]!=' ' || i!=length)
你在这里的意思是和操作&&
,而不是||
。当它是 ||
时,除了 i
超出长度之外,所有字符基本上都是正确的。另外 i != length
应该在 postfix[i] != ' '
之前测试,因为 i == length
postfix[i]
将超出范围。这一行应该是:
while(i!=length && postfix[i]!=' ')
由于这两个错误,您没有正确地将值推送到堆栈,在不同时间获取错误值,这会导致分段错误。