《数据结构与算法》上的一道作业题答案,本程序设计二叉树的构建,遍历以及打印等函数,能够实现自动分辨所输入表达式属于什么类型。
Inn(bt->rightchild);
cout << ")"; //考虑root与root->leftchild操作符的优先级,如果root的优先级较大则需要添加小括号;
}
else
Inn(bt->rightchild);
}
}
}
string Exp_turn(string s){
queue <char> bQ;
stack <char> bS;
int k = 0;
while (s[k] != '\0'){
if (!isOP(s[k])){
bQ.push(s[k]);
}
else
{
bS.push(s[k]);
}
k++;
}
char ss[30];
int g = 0;
while (!bQ.empty())
{
ss[g] = bQ.front();
bQ.pop();
g++;
if (!bS.empty()){
ss[g] = bS.top();
bS.pop();
g++;
}
}
ss[g] = '\0';
return ss;
}
string Exp_turn_and_turn(string s){
queue <char> bOP;
queue <char> bNum;
int k = 0;
while (s[k] != '\0'){
if (!isOP(s[k])){
bNum.push(s[k]);
}
else
{
bOP.push(s[k]);
}
k++;
}
char ss[30];
int g = 0;
while (!bNum.empty())
{
ss[g] = bNum.front(); //实现了数字与操作符的分离 //实现了后缀表达式转换成中缀表达式; //实现了数字与操作符的分离