string postToInfix()
{
stack<string> s;
string token;
string a, b;
cin>>token;
while (token[0] != '=')
{
if (token[0] >= ’a’ && token[0] <= ’z’)
s.push(token);
else
switch (token[0])
{
case '+':
a=s.top();
s.pop();
b = s.top();
s.pop();
s.push("("+ a+"+"+b+")");
break;
case '-':
a=s.top();
s.pop();
b = s.top();
s.pop();
s.push("("+a+" - "+ b+")");
break;
case '*':
a=s.top();
s.pop();
b = s.top();
s.pop();
s.push("("+a+" * "+ b+")");
break;
case '/':
a=s.top();
s.pop();
b = s.top();
s.pop();
s.push("("+a+"/"+b+")");
break;
case '^':
a=s.top();
s.pop();
b = s.top();
s.pop();
s.push("("+a+"^"+b+")");
break;
}
cin>> token;
}
return s.top();
}
C++ 后缀转换中缀表达式
最新推荐文章于 2024-07-29 20:53:35 发布