输入中缀或后缀表达式,输出值。
思路: 如果是中缀表达式,则先转化成后缀表达式。定义两个栈,一个操作符栈,根据优先级决定是否入栈,另一个栈,将操作数和运算符转化成后缀式存储,进行计算。
如果是后缀表达式,直接进行运算。
#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <cstdlib>
using namespace std;
stack<char> st;//存储操作符
int priority(char a)//运算符优先级
{
switch(a)
{
case '+':
case '-':
return 1;
break;
case '*':
case '/':
return 2;
break;
case '(':
return 0;
break;
case ')':
return 3;
break;
}
return -1;
}
vector<char> s;//后序存储栈
void change(string str)
{
int i=0;
while(i!=str.size())
{
if(str[i]>='0'&&str[i]<='9')//数字入栈
s.push_back(str[i]);
else
{
if(str[i]=='(')//第一个为左括号入操作符栈
st.push(str[i]);
else if(st.size()==0)//运算过程中栈中无字符
st.push(str[i]);
else if(priority(st.top())<priority(str[i]))//优先级的比较
{
if(str[i]==')')
{
while(st.top()!='(')//先将括号里面的运算符入栈
{
char b=st.top();
st.pop();
s.push_back(b);
}
st.pop();//左括号出栈
}
else
st.push(str[i]);
}
else
{
while(priority(st.top())>=priority(str[i]))
{
if(st.top()=='(')
{
st.pop();
continue;
}
char b=st.top();
st.pop();
s.push_back(b);
if(st.size()==0)
{
st.push(str[i]);
break;
}
}
}
}
++i;
}
while(!st.empty())
{
char t=st.top();
st.pop();
s.push_back(t);
}
}
int fun(string str)
{
stack<char> temp;
int result=0;
int m,n;
for(int i=0;i<s.size();++i)
{
if(isdigit(s[i]))//是数字
{
n=s[i]-'0';
temp.push(n);
}
else
{
m=temp.top();//运算符
temp.pop();
switch(s[i])
{
case '+':
result=temp.top()+m;
break;
case '-':
result=temp.top()-m;
break;
case '*':
result=temp.top()*m;
break;
case '/':
result=temp.top()/m;
break;
}
temp.pop();
temp.push(result);
}
}
return result;
}
int fun1(string str)
{
stack<char> temp;
int result=0;
int m,n;
for(int i=0;i<str.size();++i)
{
if(isdigit(str[i]))
{
n=str[i]-'0';
temp.push(n);
}
else
{
m=temp.top();
temp.pop();
switch(str[i])
{
case '+':
result=temp.top()+m;
break;
case '-':
result=temp.top()-m;
break;
case '*':
result=temp.top()*m;
break;
case '/':
result=temp.top()/m;
break;
}
temp.pop();
temp.push(result);
}
}
return result;
}
int main()
{
while(1)
{
int key;
while(1)
{
cout<<"请输入:1.中缀表达式;2.后缀表达式;3.退出"<<endl;
cin>>key;
string str;
if(key==1)
{
cout<<"请输入中缀表达式:"<<endl;
cin>>str;
change(str);
cout<<"运算结果:"<<fun(str)<<endl;
}
else if(key==2)
{
cout<<"请输入后缀表达式:"<<endl;
cin>>str;
cout<<"运算结果:"<<fun1(str)<<endl;
}
else
return 0;
}
}
return 0;
}
输入表达式,输出前序遍历
输入:a+b*(c-d)-e/f
输出:-+a*b-cd/ef
#include <iostream>
#include <stack>
#include <vector>
#include <string>
using namespace std;
typedef struct no
{
char data;
struct no *lchild,*rchild;
}*node;
stack<char> st;
int priority(char a)
{
switch(a)
{
case '+':
case '-':
return 1;
break;
case '*':
case '/':
return 2;
break;
case '(':
return 0;
break;
case ')':
return 3;
break;
}
return -1;
}
vector<char> s;
string change(string str)//转为后缀表达式,即后续遍历
{
int i=0;
string post="";
while(i!=str.size())
{
if(str[i]>='a'&&str[i]<='z')
s.push_back(str[i]);
else
{
if(str[i]=='(')
{
st.push(str[i]);
}
else if(st.size()==0)
st.push(str[i]);
else if(priority(st.top())<priority(str[i]))
{
if(str[i]==')')
{
while(st.top()!='(')
{
char b=st.top();
st.pop();
s.push_back(b);
}
st.pop();
}
else
st.push(str[i]);
}
else
{
while(priority(st.top())>=priority(str[i]))
{
if(st.top()=='(')
{
st.pop();
continue;
}
char c=st.top();
st.pop();
s.push_back(c);
if(st.size()==0)
{
st.push(str[i]);
break;
}
}
}
}
++i;
}
while(!st.empty())
{
char t=st.top();
st.pop();
s.push_back(t);
}
for(i=0;i<s.size();++i)
{
//cout<<s[i]<<" ";
post+=s[i];
}
return post;
}
node create(string post)///根据后序遍历,建树
{
node tree;
stack<node>st;
for(int i=0; i<post.length(); i++)
{
if(post[i]!='+'&&post[i]!='-'&&post[i]!='/'&&post[i]!='*')///一定是叶子结点
{
tree=new no();
tree->data=post[i];
tree->lchild=tree->rchild=NULL;
}
else///非叶子结点,赋值其左右子孩子
{
tree=new no();
tree->data=post[i];
tree->rchild=st.top();
st.pop();
tree->lchild=st.top();
st.pop();
}
st.push(tree);
}
return st.top();
}
void pre(node &sa)
{
if(sa!=NULL)
{
cout<<sa->data;
pre(sa->lchild);
pre(sa->rchild);
}
}
int main()
{
cout<<"请输入中缀表达式:"<<endl;
string inorder,postorder;
node head;
head=new no();
cin>>inorder;
//cout<<in<<endl;
cout<<"转换为后缀表达式为:"<<endl;
postorder=change(inorder);
cout<<postorder<<endl;
//cout<<"构建表达式树"<<endl;
head=create(postorder);
cout<<"前序遍历:"<<endl;
pre(head);
cout<<endl;
return 0;
}
/*
a+b*(c-d)-e/f
*/