简单计算器:
将输入的一个中缀表达式通过计算机计算得到结果:
1.将中缀转后缀(用到一个队列一个栈)
2.计算值(用到一个栈)
void change()
{
node temp;
for(int i=0;i<str.length();) 【这句for(int i=0;i<str.length(); I++ )就是错的,这个逻辑会一直跳过运算符】
{
if(str[i]>='0'&&str[i]<='9')
{
temp.flag=true;
temp.num=str[i++]-'0';
while(str[i]>='0'&&str[i]<='9'){
temp.num=temp.num*10+(str[i++]-'0');
}
q.push(temp);
}
else
{
temp.flag=false;
while(!s.empty()&&op[s.top().op]>=op[str[i]])
{
q.push(s.top());
s.pop();
}
temp.op=str[i];
s.push(temp);
i++;
}
}
while(!s.empty())
{
q.push(s.top());
s.pop();
}
}
将输入的一个中缀表达式通过计算机计算得到结果:
1.将中缀转后缀(用到一个队列一个栈)
2.计算值(用到一个栈)
void change()
{
node temp;
for(int i=0;i<str.length();) 【这句for(int i=0;i<str.length(); I++ )就是错的,这个逻辑会一直跳过运算符】
{
if(str[i]>='0'&&str[i]<='9')
{
temp.flag=true;
temp.num=str[i++]-'0';
while(str[i]>='0'&&str[i]<='9'){
temp.num=temp.num*10+(str[i++]-'0');
}
q.push(temp);
}
else
{
temp.flag=false;
while(!s.empty()&&op[s.top().op]>=op[str[i]])
{
q.push(s.top());
s.pop();
}
temp.op=str[i];
s.push(temp);
i++;
}
}
while(!s.empty())
{
q.push(s.top());
s.pop();
}
}