![在这里插入图片描述](https://img-blog.csdnimg.cn/954d28c9bedc4b869b7be88157f01d20.png)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int a;
vector<int>vc;
vector<int>::const_iterator it;
cout<<"请输入一串整数,以0为结束"<<endl;
while(cin>>a)
{
if(a==0)
break;
vc.push_back(a);
}
cout<<"当前数据为:";
for(it=vc.begin();it!=vc.end();it++)
cout<<*it<<' ';
cout<<endl;
char op;
while(1)
{
cin>>op;
if(op=='a')
{
int t;
cin>>t;
vc.push_back(t);
cout<<"当前数据为:";
for(it=vc.begin();it!=vc.end();it++)
cout<<*it<<' ';
cout<<endl;
}
else if(op=='c')
{
char a,b;
int i,j;
cin>>a>>i>>b>>j;
replace(vc.begin(),vc.end(),i,j);
cout<<"当前数据为:";
for(it=vc.begin();it!=vc.end();it++)
cout<<*it<<' ';
cout<<endl;
}
else if(op=='d')
{
int t;
cin>>t;
vector<int>::const_iterator it1;
for(it=vc.begin();it!=vc.end();it++)
if(*it==t)
{
vc.erase(it);
it--;
}
cout<<"当前数据为:";
for(it=vc.begin();it!=vc.end();it++)
cout<<*it<<' ';
cout<<endl;
}
else if(op=='s')
{
sort(vc.begin(),vc.end());
cout<<"当前数据为:";
for(it=vc.begin();it!=vc.end();it++)
cout<<*it<<' ';
cout<<endl;
}
else
break;
}
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/d4b796547c20485d9171ed4f2caf6f6b.png)
#include<iostream>
#include<string>
#include<stack>
#include<iomanip>
using namespace std;
int fun_yxj(char c)
{
int yxj;
switch(c)
{
case '+':yxj=2;break;
case '-':yxj=2;break;
case '*':yxj=3;break;
case '/':yxj=3;break;
}
return yxj;
}
double value(double op1,double op2,char c)
{
switch(c)
{
case '+':return op1+op2;break;
case '-':return op1-op2;break;
case '*':return op1*op2;break;
case '/':return (op1*1.00)/op2;break;
}
}
void z_to_h(string str)
{
stack<char>s;
stack<char>op;
char str1[str.length()];
for(int i=0;i<str.length();i++)
{
if(str[i]>='0'&&str[i]<='9')
s.push(str[i]);
else if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
{
while(true)
{
if(op.empty()||op.top()=='(')
{
op.push(str[i]);
break;
}
else if(fun_yxj(str[i])>fun_yxj(op.top()))
{
op.push(str[i]);
break;
}
else
{
char a=op.top();
op.pop();
s.push(a);
}
}
}
else
{
if(str[i]=='(')
op.push(str[i]);
else
{
while(op.top()!='(')
{
char b=op.top();
op.pop();
s.push(b);
}
op.pop();
}
}
}
while(!op.empty())
{
char c=op.top();
op.pop();
s.push(c);
}
while(!s.empty())
{
char d=s.top();
s.pop();
op.push(d);
}
cout<<"该表达式的后缀表达式为:";
stack<double>st;
while(!op.empty())
{
char m=op.top();
cout<<m;
if(m>='0'&&m<='9')
st.push(m-'0');
else
{
double op2=st.top();
st.pop();
double op1=st.top();
st.pop();
double temp=value(op1,op2,m);
st.push(temp);
}
op.pop();
}
cout<<endl<<"该表达式的值为:";
cout<<fixed<<setprecision(2)<<st.top()<<endl;
}
double h_to_s(string str)
{
stack<double>st;
for(int i=0;i<str.length();i++)
{
if(str[i]>='0'&&str[i]<='9')
st.push(str[i]-'0');
else
{
double op2=st.top();
st.pop();
double op1=st.top();
st.pop();
double temp=value(op1,op2,str[i]);
st.push(temp);
}
}
return st.top();
}
int main()
{
int op;
cout<<"请输入操作选项,1表示输入中缀表达式,2表示输入后缀表达式。"<<endl;
while(cin>>op)
{
if(op==1)
{
string str;
cin>>str;
z_to_h(str);
cout<<"请输入操作选项,1表示输入中缀表达式,2表示输入后缀表达式。"<<endl;
}
else if(op==2)
{
string str;
cin>>str;
cout<<"该表达式的值为:";
cout<<fixed<<setprecision(2)<<h_to_s(str)<<endl;
cout<<"请输入操作选项,1表示输入中缀表达式,2表示输入后缀表达式。"<<endl;
}
else
break;
}
return 0;
}