求助~~24速算的C语言编程~~
发布网友
发布时间:2022-04-23 23:05
我来回答
共1个回答
热心网友
时间:2023-10-13 02:04
在VC6.0下可通过编译并运行
#include<stdio.h>
double ok(double n1,double n2,char c) /*计算两数运算结果*/
{
if(c=='+') return n1+n2;
if(c=='-') return n1-n2;
if(c=='*') return n1*n2;
if(c=='/') return n1/n2;
}
putout(double n1,double n2,double n3,double n4,char c1,char c2,char c3,char p)
{
switch(p)
{
case 12: printf("(%.0lf%c%.0lf)%c%.0lf%c%.0lf=24\n",n1,c1,n2,c2,n3,c3,n4);break;
case 23: printf("%.0lf%c(%.0lf%c%.0lf)%c%.0lf=24\n",n1,c1,n2,c2,n3,c3,n4);break;
case 34: printf("%.0lf%c%.0lf%c(%.0lf%c%.0lf)=24\n",n1,c1,n2,c2,n3,c3,n4);break;
case 13: printf("(%.0lf%c%.0lf%c%.0lf)%c%.0lf=24\n",n1,c1,n2,c2,n3,c3,n4);break;
case 24: printf("%.0lf%c(%.0lf%c%.0lf%c%.0lf)=24\n",n1,c1,n2,c2,n3,c3,n4);break;
default: printf("error\n");
}
}
main()
{
double t1,t2,t3;
double n1,n2,n3,n4;
int p,p1,p2,p3;
char s[5]={12,23,34,13,24};
char s1[4]="+-*/";
char s2[4]="+-*/";
char s3[4]="+-*/";
printf("input n1: ");scanf("%lf",&n1);
printf("input n2: ");scanf("%lf",&n2);
printf("input n3: ");scanf("%lf",&n3);
printf("input n4: ");scanf("%lf",&n4);
printf("\n");
for(p=0;p<=4;p++) /*遍虑括号的位置*/
for(p1=0;p1<=3;p1++) /*遍虑两数的运算方式*/
for(p2=0;p2<=3;p2++) /*遍虑两数的运算方式*/
for(p3=0;p3<=3;p3++) /*遍虑两数的运算方式*/
{
switch(s[p])
{
case 12: t1=ok(n1,n2,s1[p1]);
if((s2[p2]=='+'||s2[p2]=='-')&&(s3[p3]=='*'||s3[p3]=='/'))
{
t2=ok(n3,n4,s3[p3]);
t3=ok(t1,t2,s2[p2]);
}
else
{
t2=ok(t1,n3,s2[p2]);
t3=ok(t2,n4,s3[p3]);
}
if(t3==24) putout(n1,n2,n3,n4,s1[p1],s2[p2],s3[p3],s[p]);
break;
case 23: t1=ok(n2,n3,s2[p2]);
if((s1[p1]=='+'||s1[p1]=='-')&&(s3[p3]=='*'||s3[p3]=='/'))
{
t2=ok(t1,n4,s3[p3]);
t3=ok(n1,t2,s1[p1]);
}
else
{
t2=ok(n1,t1,s1[p1]);
t3=ok(t2,n4,s3[p3]);
}
if(t3==24) putout(n1,n2,n3,n4,s1[p1],s2[p2],s3[p3],s[p]);
break;
case 34: t1=ok(n3,n4,s3[p3]);
if((s1[p1]=='+'||s1[p1]=='-')&&(s2[p2]=='*'||s2[p2]=='/'))
{
t2=ok(n2,t1,s2[p2]);
t3=ok(n1,t2,s1[p1]);
}
else
{
t2=ok(n1,n2,s1[p1]);
t3=ok(t2,t1,s2[p2]);
}
if(t3==24) putout(n1,n2,n3,n4,s1[p1],s2[p2],s3[p3],s[p]);
break;
case 13:
if((s1[p1]=='+'||s1[p1]=='-')&&(s2[p2]=='*'||s2[p2]=='/'))
{
t1=ok(n2,n3,s2[p2]);
t2=ok(n1,t1,s1[p1]);
}
else
{
t1=ok(n1,n2,s1[p1]);
t2=ok(t1,n3,s2[p2]);
}
t3=ok(t2,n4,s3[p3]);
if(t3==24) putout(n1,n2,n3,n4,s1[p1],s2[p2],s3[p3],s[p]);
break;
case 24:
if((s2[p2]=='+'||s2[p2]=='-')&&(s3[p3]=='*'||s3[p3]=='/'))
{
t1=ok(n3,n4,s3[p3]);
t2=ok(n2,t1,s2[p2]);
}
else
{
t1=ok(n2,n3,s2[p2]);
t2=ok(t1,n4,s3[p3]);
}
t3=ok(n1,t2,s1[p1]);
if(t3==24) putout(n1,n2,n3,n4,s1[p1],s2[p2],s3[p3],s[p]);
break;
default: printf("error\n");
}
}
}
参考资料:http://post.baidu.com/f?kz=164157176