7-20 表达式转换
分数 25
作者 DS课程组
单位 浙江大学
算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。
输入格式:
输入在一行中给出不含空格的中缀表达式,可包含+
、-
、*
、\
以及左右括号()
,表达式不超过20个字符。
输出格式:
在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。
输入样例:
2+3*(7-4)+8/4
输出样例:
2 3 7 4 - * + 8 4 / +
并且题目中还有可能存在小数,这点需要特别注意;也还好这只是练习题,每个样例会给出提示,这个样例有什么内容,但是考试的时候,我们又怎么知晓呢?故还得继续练。
——好好看,好好学,真正的大师永远怀着一颗好学的心。
first:
#include <iostream>
#include <map>
#include <stack>
#include <string>
using namespace std;
stack<char> op;
int flag=1;
void get_beh(string &str)
{
map<char,int> pr{{'+',1},{'-',1},{'*',2},{'/',2},{'(',3}};
for(int i=0;i<str.size();++i)
{ //判断运算数之前是不是有符号
if( (i==0 || str[i-1]=='(') && (str[i]=='+' || str[i]=='-') )
{
if(str[i] == '+') //如果符号是正数直接剔除
continue;
else //如果符号是负数,作为运算数的一部分
{
int j=i+1,cnt=1;
// eg:-2.36
while(j<str.size() && (isdigit(str[j]) || str[j]=='.') )
{
++j;
++cnt;
}
double db = stod(str.substr(i,cnt));
if(flag)
flag = 0;
else
cout << ' ';
cout << db;
//num.push(db);
i=j-1;
}
}
else if(isdigit(str[i]))
{ // eg:255.5689
int j=i,cnt=0;
while(j<str.size() && (isdigit(str[j]) || str[j]=='.') )
{
++j;
++cnt;
}
double db = stod(str.substr(i,cnt));
if(flag)
flag = 0;
else
cout << ' ';
cout << db;
i=j-1;
}
else if(str[i] == '(') //'('直接如运算符栈
op.push('(');
else if(str[i] == ')') //')'将括号中的数出栈进行运算
{
while(op.top() != '(')
{
if(flag)
flag = 0;
else
cout << ' ';
cout << op.top();
op.pop();
}
op.pop(); //'(' 出栈
}
else
{
while(op.size() && pr[op.top()] >= pr[str[i]] && op.top()!='(')
{
if(flag)
flag = 0;
else
cout << ' ';
cout << op.top();
op.pop();
} //栈中的运算符优先级不低于str[i],进行运算
op.push(str[i]); //将str[i]入栈
}
}
while(op.size()) //如果运算符栈中还有元素
{
if(flag)
flag = 0;
else
cout << ' ';
cout << op.top();
op.pop();
}
}
int main()
{
string str;
cin >> str;
get_beh(str);
return 0;
}
second:
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <string>
using namespace std;
struct Node
{
double d;
char op;
int flag;
};
int main()
{
map<char,int> op = {{'+',1},{'-',1},{'*',2},{'/',2},{'(',3}};
string str;
cin >> str;
stack<char> stk;
queue<Node> q;
for(int i=0;i<str.size();++i)
{
Node tm;
if( (i==0 || str[i-1]=='(') && (str[i]=='+' || str[i]=='-'))
{
if(str[i] == '+')
continue;
else
{
int j= i+1,num=1;
while(j<str.size() && (isdigit(str[j]) || str[j]=='.'))
{
++j;
++num;
}
tm.flag = 1;
double db = stod(str.substr(i,num));
tm.d = db;
q.push(tm);
i=j-1;
}
}
else if(isdigit(str[i]))
{
int j= i,num=0;
while(j<str.size() && ( isdigit(str[j]) || str[j]=='.') )
{
++j;
++num;
}
tm.flag = 1;
double db = stod(str.substr(i,num));
tm.d = db;
q.push(tm);
i=j-1;
}
else
{
while(stk.size() && op[str[i]] <= op[stk.top()] && stk.top()!='(')
{
tm.flag=0;
tm.op=stk.top();
q.push(tm);
stk.pop();
}
stk.push(str[i]);
if(str[i] == ')')
{
stk.pop();
while(stk.top() != '(')
{
tm.flag=0;
tm.op=stk.top();
q.push(tm);
stk.pop();
}
stk.pop();
}
}
}
while(stk.size())
{
Node tm;
tm.flag=0;
tm.op=stk.top();
stk.pop();
q.push(tm);
}
int flag = 1;
while(q.size())
{
if(flag)
flag = 0;
else
cout << ' ';
Node tm = q.front();
if(tm.flag)
cout << tm.d;
else
cout << tm.op;
q.pop();
}
return 0;
}