3*(4+6)-7 # 用代码实现这个运算表达式,仅包含逆波兰式部分,算术结果部分未写出
#include<vector>
#include<iostream>
#include<string.h>
using namespace std;
void pr(char *str)
{
cout<<"算式长度:"<<strlen(str);
vector<char> s1(strlen(str));
vector<char> s2(strlen(str));
int i=0;
while(str[i]!='\0')
{
if(str[i]>='0'&&str[i]<='9')
{
char ch=str[i];
s1.push_back(ch);
i++;
}
else if(str[i]==')')
{
while(s2.back()!='(')
{
char ch=s2.back();
s1.push_back(ch);
s2.pop_back();
}
s2.pop_back();
i++;
}
else if(str[i]=='+'||str[i]=='-')
{
if(s2.back()=='*'||s2.back()=='/')
{
s1.push_back(s2.back());
s2.pop_back();
s2.push_back(str[i]);
i++;
}
else{
s2.push_back(str[i++]);
}
}
else{
s2.push_back(str[i++]);
}
}
while(!s2.empty())
{
s1.push_back(s2.back());
s2.pop_back();
}
vector<char>::iterator it;
for(it=s1.begin();it<s1.end();it++)
{
cout<<*it<<" ";
}
}
int main()
{
char str[]="3*(4+6)-7";
pr(str);
return 0;
}
存在不足之处,考虑不周处望批评指正