### 给出5个数字，前4个数字做四则运算…

#include
#include
using namespace std;
char fuhao[4]={'+','-','*','/'};
bool caculate(int f[3],double shuzi[5]);
int main()
{
int i,j,k;
double shuzi[5];
int f[3];
while(1)
{
cout<<"请输入五个数字"<<endl;
bool flag=0;
for(i=0;i<5;i++)
cin>>shuzi[i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
for(k=0;k<4;k++)
{
f[0]=i;
f[1]=j;
f[2]=k;
if(caculate(&f[0],&shuzi[0])==1)
{
flag=1;
printf("%.2lf%c%.2lf%c%.2lf%c%.2lf = %.2lf\n",shuzi[0],fuhao[i],shuzi[1],fuhao[j],shuzi[2],fuhao[k],shuzi[3],shuzi[4]);
}
}
if(flag==0)
cout<<"无法组成四则运算"<<endl;
}
}
bool caculate(int f[3],double shuzi[5])
{
double num[5];
int i=0,j=0;
bool flag=0;
memset(num,'\0',sizeof(num));
num[0]=shuzi[0];
while(i<3)
{
//fuhao[f[i]];
//shuzi[i+1];
if(fuhao[f[i]]=='/' && shuzi[i+1]==0)
{
cout<<"除数不能为0"<<endl;
flag=1;
break;
}
switch(fuhao[f[i]])
{
case '*':num[j]*=shuzi[i+1];break;
case '/':num[j]/=shuzi[i+1];break;
case '+':num[++j]=shuzi[i+1];break;
case '-':num[++j]=-shuzi[i+1];
}
i++;
}

if(flag==0)
{
double ans=0;
for(j;j>=0;j--)
ans+=num[j];
if(ans==shuzi[4])
return 1;
else
return 0;
}
else
return 0;
}