代码
#include<iostream>
#include<cmath>
using namespace std;
double math[100001];
char chp[100001];
bool nop[100001];
void packo(const int n) //更新
{
for(int i=n;i>=1;i--)
{
if(chp[i]=='^')
{
math[i]=pow(math[i],math[i+1]);
math[i+1]=0;
nop[i]=1;
}
}
}
void packt(const int n)
{
for(int i=1;i<=n;i++)
{
if(chp[i]=='^') math[i+1]=math[i],math[i]=0;
if(chp[i]=='*')
{
math[i+1]*=math[i];
math[i]=0;
}
if(chp[i]=='/')
{
math[i+1]=math[i]/math[i+1];
math[i]=0;
}
}
}
double packp(const int n) //更新
{
double sum=0;
char ch='+';
for(int i=1;i<=n+1;i++)
{
if(chp[i-1]=='+'||chp[i-1]=='-') ch=chp[i-1];
if(ch=='+') sum+=math[i];
if(ch=='-') sum-=math[i];
}
return sum;
}
int main()
{
int j=0;
char cch=0;
double lj=0;
while(cch!='=')
{
cin>>math[++j];
cch=getchar();
chp[j]=cch;
}
math[j+1]=lj;
packo(j);
packt(j);
cout<<packp(j);
return 0;
}
更新:
加入^(幂运算)。修复加法错误。