# 数据结构之"逆波兰树"求解算式 [a + b * (c - d) - e / f]

C思想下的操作：

-+ann*bnn-cnndnn/ennfnn

a 1
b 2
c 3
d 4
e 6
f 3

abcd-*+ef/-

C++思想下的操作：

-+ann*bnn-cnndnn/ennfnn

a 1
b 2
c 3
d 4
e 6
f 3

abcd-*+ef/-

Process returned 0 (0x0)   execution time : 39.540 s
Press any key to continue.

``````#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a));
#define For(a,b) for(int i = a;i<b;i++)
#define LL long long
#define MAX_N 100010
using namespace std;
typedef struct treen *treenode;
typedef int Status;
struct treen
{
treenode left;
treenode right;
treenode father;
char data;
};
map<char,int> mp;
{
stack<int> ch;
stack<treenode> sta;
treenode p;
printf("该波兰树的后序优先次序、也就是逆波兰式为：\n\n");
while(p != nullptr || !sta.empty())
{
while(p != nullptr)
{
sta.push(p);
p = p->left ? p->left : p->right;
}
p = sta.top();
sta.pop();
printf("%c",p->data);
if(isalpha(p->data))
{
char e = p->data;
int num = mp[e];
ch.push(num);
}
else
{
int getc = 0;
int num1 = ch.top();
ch.pop();
int num2 = ch.top();
ch.pop();
if(p->data == '+')
getc = num1 + num2;
else if(p->data == '-')
getc = num2 - num1;
else if(p->data == '*')
getc = num2 * num1;
else if(p->data == '/')
getc = num2 / num1;
ch.push(getc);
}
if(!sta.empty() && sta.top()->left == p)
p = sta.top()->right;
else
p = nullptr;
}
printf("\n\n");
*ans = ch.top();
ch.pop();
return ;
}
Status Get_bolantree_C(treenode *p)
{
char s_c;
scanf("%c",&s_c);
//cout<<s_c<<endl;
if(s_c == 'n') *p = nullptr;
else
{
*p = (treenode)malloc(sizeof(treen));
if(!(*p)) return 0;
(*p)->data = s_c;
Get_bolantree_C(& ((*p)->left));
Get_bolantree_C(& ((*p)->right));
}
return 0;
}
Status Get_bolantree_CPP(treenode &q)
{
char s;
scanf("%c",&s);
/*
本来想着c与c++写两个程序来着。
觉得麻烦，就放在了一起。我要感谢这个输出1的操作。。
刚开始输出所输入的字符还没发现错误。（纠结的我都去打游戏了，蓝瘦啊）。
改成输出1以后，我发现还没有输入字符呢，她自己就输出了一个1。
让我想到了检验上面C的样例之后的   回车。回车。回车 ......
香菇......
*/
//cout<<1<<endl;
if(s == 'n') q = nullptr;
else
{
q = (treenode)malloc(sizeof(treen));
if(!q) return 0;
q->data = s;
Get_bolantree_CPP(q->left);
Get_bolantree_CPP(q->right);
}
return 1;
}
{
stack<treenode> st;
if(p == nullptr) return false;
else
st.push(p);
while(!st.empty())
{
treenode dele = st.top();
st.pop();
if(dele->left != nullptr)
{
st.push(dele->left);
}
if(dele->right != nullptr)
{
st.push(dele->right);
}
delete dele;
}
if(st.empty()) return true;
return false;
}
int main()
{

printf("C思想下的操作：\n\n");
printf("请输入构建[a + b * (c - d) - e / f]波兰树的字符串:\n\n");
printf("\n");
char ss_c[5];
int val_c= 0;
printf("请输入字母及其所代表的数值:\n\n");
for(int i = 1; i<=6; i++)
{
scanf("%s %d",ss_c,&val_c);
mp[ss_c[0]] = val_c;
}
printf("\n");
int res_c;
printf("最终结果为: %d\n\n",res_c);
cout<<"内存已被成功释放。"<<endl<<endl;
else
cout<<"内存没有被成功释放或者说没有内存的占用。"<<endl<<endl;
mp.clear();
getchar();//吃上面剩下的回车。。不然下面多了一个'\n'没法检验...
printf("C++思想下的操作：\n\n");
printf("请输入构建[a + b * (c - d) - e / f]波兰树的字符串:\n\n");
printf("\n");
char ss_cpp[5];
int val_cpp = 0;
printf("请输入字母及其所代表的数值:\n\n");
for(int i = 1; i<=6; i++)
{
scanf("%s %d",ss_cpp,&val_cpp);
mp[ss_cpp[0]] = val_cpp;
}
printf("\n");
int res_cpp;
printf("最终结果为: %d\n",res_cpp);
return 0;
}

/*

2016.10.28

-+ann*bnn-cnndnn/ennfnn

a 1
b 2
c 3
d 4
e 6
f 3

*/
``````

原文作者：B树
原文地址: https://blog.csdn.net/sxtopc/article/details/52957986
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。