如何用C语言编程"输入4个数,通过加减乘除计算出24,显示计算过程,并提示成功信息。"
1个回答
2014-02-28
展开全部
#include "stdlib.h"
#include "stdio.h"
#include "string.h" typedef struct D_NODE
{
int data;
struct D_NODE *left,*right;
} NODE24;
NODE24 head;int res=24;int maketree(int *src,int *i, NODE24 *p)
{
int d=src[*i];
(*i)++;
switch (d)
{
case '+':
case '-':
case '*':
case '/':
p->left=new NODE24;
p->right=new NODE24;
p->data=d;
maketree(src,i,p->left);
maketree(src,i,p->right);
break;
default:
p->data=d;
p->left=NULL;
p->right=NULL;
}
return 0;
}int cmaketree(NODE24 *p)
{
int c;
c=getchar();
putchar(c);
switch (c)
{
case '+':
case '-':
case '*':
case '/':
p->left=new NODE24;
p->right=new NODE24;
p->data=c;
cmaketree(p->left);
cmaketree(p->right);
break;
default:
p->data=c-'0';
p->left=NULL;
p->right=NULL;
}
return 0;
}int work(struct D_NODE *d)
{
int res=0;
if (d->left==NULL&&d->right==NULL)
res=d->data;
else
{
int a,b;
a=work(d->left);
b=work(d->right);
switch (d->data)
{
case '+':
res=a+b;//work(d->left)+work(d->right);
break;
case '-':
res=a-b;//work(d->left)-work(d->right);
break;
case '*':
res=a*b;//work(d->left)*work(d->right);
break;
case '/':
if (b!=0)
res=(a%b==0)?a/b:-79;
else
res=-79;
//res=work(d->right)?work(d->left)/work(d->right):-79;
break;
}
}
return res;
}int destroy(struct D_NODE *d)
{
if (d->left==NULL&&d->right==NULL)
delete d;
else
{
destroy(d->left);
d->left=NULL;
destroy(d->right);
d->right=NULL;
if (d != &head)
delete d;
}
return 0;
}int show(struct D_NODE *d)
{
if (d->left==NULL && d->right==NULL)
printf("%d",d->data);
else
{
printf("(");
show(d->left);
printf("%c",d->data);
show(d->right);
printf(")");
}
return 0;
}/* int input()
{
//int buf[30]={'*','+','2','4','+','2','3'},idx=0;
//maketree(buf,&idx,&head); int buf[20],idx=0;
printf("\nPlease Input:");
for (idx=0;idx<20;idx++)
{
buf[idx]=getch();
printf("%c",buf[idx]);
}
idx=0;
maketree(buf,&idx,&head);
return 0;
} */#define test(p1,p2,p3,p4,p5,p6,p7) {exp[0]=(p1),exp[1]=(p2), \<br> exp[2]=(p3),exp[3]=(p4),exp[4]=(p5),exp[5]=(p6),exp[6]=(p7); \<br> idx=0; \<br> maketree(exp,&idx,&head); \<br> if (work(&head)==res) \<br> { \<br> found++;printf("%5d: ",found);show(&head);\<br> if (!(found%3)) printf("\n");\<br> } \
destroy(&head);\
}// printf("%d,%d,%d,%d,%d,%d,%d\n",p1,p2,p3,p4,p5,p6,p7); \int test24()
{
int num[4],opc[4]={'+','-','*','/'},exp[20];
int i1,i2,i3,i4,ic1,ic2,ic3,idx,found=0; char prompt[]="24点游戏分析\n";
printf("%s",prompt);
for (i1=0;i1<4;i1++)
{
printf("请输入第%d个数字: ",i1+1);
scanf("%d",num+i1);
//num[i1]=num[i1]%13+1;
}
printf("\n你已经输入了:%d,%d,%d,%d四个数字!\n",num[0],num[1],num[2],num[3]);
printf("\n请输入要计算的结果值,(当然二十四点就输入24啦!):");
scanf("%d",&res);
printf("OK! 按任意键就可以开始了!\n");
getchar();
for (i1=0;i1<4;i1++)
for (i2=0;i2<4;i2++)
if (i2!=i1)
for (i3=0;i3<4;i3++)
if (i3!=i1&&i3!=i2)
for (i4=0;i4<4;i4++)
if (i4!=i1&&i4!=i2&&i4!=i3)
for (ic1=0;ic1<4;ic1++)
for (ic2=0;ic2<4;ic2++)
for (ic3=0;ic3<4;ic3++)
{
test(opc[ic1],opc[ic2],opc[ic3],num[i1],num[i2],num[i3],num[i4]);
test(opc[ic1],opc[ic2],num[i1],opc[ic3],num[i2],num[i3],num[i4]);
test(opc[ic1],opc[ic2],num[i1],num[i2],opc[ic3],num[i3],num[i4]);
test(opc[ic1],num[i1],opc[ic2],opc[ic3],num[i2],num[i3],num[i4]);
test(opc[ic1],num[i1],opc[ic2],num[i2],opc[ic3],num[i3],num[i4]);
}
printf("\n共找到了 %d 条正确的计算方法!(很抱歉,我没有处理交换率*^_^*)\n",found);
return 0;
}int main()
{
// fflush(stdin);
// input();
// cmaketree(&head);
// printf("\n=%d\n",work(&head));
test24();
return 0;
}
#include "stdio.h"
#include "string.h" typedef struct D_NODE
{
int data;
struct D_NODE *left,*right;
} NODE24;
NODE24 head;int res=24;int maketree(int *src,int *i, NODE24 *p)
{
int d=src[*i];
(*i)++;
switch (d)
{
case '+':
case '-':
case '*':
case '/':
p->left=new NODE24;
p->right=new NODE24;
p->data=d;
maketree(src,i,p->left);
maketree(src,i,p->right);
break;
default:
p->data=d;
p->left=NULL;
p->right=NULL;
}
return 0;
}int cmaketree(NODE24 *p)
{
int c;
c=getchar();
putchar(c);
switch (c)
{
case '+':
case '-':
case '*':
case '/':
p->left=new NODE24;
p->right=new NODE24;
p->data=c;
cmaketree(p->left);
cmaketree(p->right);
break;
default:
p->data=c-'0';
p->left=NULL;
p->right=NULL;
}
return 0;
}int work(struct D_NODE *d)
{
int res=0;
if (d->left==NULL&&d->right==NULL)
res=d->data;
else
{
int a,b;
a=work(d->left);
b=work(d->right);
switch (d->data)
{
case '+':
res=a+b;//work(d->left)+work(d->right);
break;
case '-':
res=a-b;//work(d->left)-work(d->right);
break;
case '*':
res=a*b;//work(d->left)*work(d->right);
break;
case '/':
if (b!=0)
res=(a%b==0)?a/b:-79;
else
res=-79;
//res=work(d->right)?work(d->left)/work(d->right):-79;
break;
}
}
return res;
}int destroy(struct D_NODE *d)
{
if (d->left==NULL&&d->right==NULL)
delete d;
else
{
destroy(d->left);
d->left=NULL;
destroy(d->right);
d->right=NULL;
if (d != &head)
delete d;
}
return 0;
}int show(struct D_NODE *d)
{
if (d->left==NULL && d->right==NULL)
printf("%d",d->data);
else
{
printf("(");
show(d->left);
printf("%c",d->data);
show(d->right);
printf(")");
}
return 0;
}/* int input()
{
//int buf[30]={'*','+','2','4','+','2','3'},idx=0;
//maketree(buf,&idx,&head); int buf[20],idx=0;
printf("\nPlease Input:");
for (idx=0;idx<20;idx++)
{
buf[idx]=getch();
printf("%c",buf[idx]);
}
idx=0;
maketree(buf,&idx,&head);
return 0;
} */#define test(p1,p2,p3,p4,p5,p6,p7) {exp[0]=(p1),exp[1]=(p2), \<br> exp[2]=(p3),exp[3]=(p4),exp[4]=(p5),exp[5]=(p6),exp[6]=(p7); \<br> idx=0; \<br> maketree(exp,&idx,&head); \<br> if (work(&head)==res) \<br> { \<br> found++;printf("%5d: ",found);show(&head);\<br> if (!(found%3)) printf("\n");\<br> } \
destroy(&head);\
}// printf("%d,%d,%d,%d,%d,%d,%d\n",p1,p2,p3,p4,p5,p6,p7); \int test24()
{
int num[4],opc[4]={'+','-','*','/'},exp[20];
int i1,i2,i3,i4,ic1,ic2,ic3,idx,found=0; char prompt[]="24点游戏分析\n";
printf("%s",prompt);
for (i1=0;i1<4;i1++)
{
printf("请输入第%d个数字: ",i1+1);
scanf("%d",num+i1);
//num[i1]=num[i1]%13+1;
}
printf("\n你已经输入了:%d,%d,%d,%d四个数字!\n",num[0],num[1],num[2],num[3]);
printf("\n请输入要计算的结果值,(当然二十四点就输入24啦!):");
scanf("%d",&res);
printf("OK! 按任意键就可以开始了!\n");
getchar();
for (i1=0;i1<4;i1++)
for (i2=0;i2<4;i2++)
if (i2!=i1)
for (i3=0;i3<4;i3++)
if (i3!=i1&&i3!=i2)
for (i4=0;i4<4;i4++)
if (i4!=i1&&i4!=i2&&i4!=i3)
for (ic1=0;ic1<4;ic1++)
for (ic2=0;ic2<4;ic2++)
for (ic3=0;ic3<4;ic3++)
{
test(opc[ic1],opc[ic2],opc[ic3],num[i1],num[i2],num[i3],num[i4]);
test(opc[ic1],opc[ic2],num[i1],opc[ic3],num[i2],num[i3],num[i4]);
test(opc[ic1],opc[ic2],num[i1],num[i2],opc[ic3],num[i3],num[i4]);
test(opc[ic1],num[i1],opc[ic2],opc[ic3],num[i2],num[i3],num[i4]);
test(opc[ic1],num[i1],opc[ic2],num[i2],opc[ic3],num[i3],num[i4]);
}
printf("\n共找到了 %d 条正确的计算方法!(很抱歉,我没有处理交换率*^_^*)\n",found);
return 0;
}int main()
{
// fflush(stdin);
// input();
// cmaketree(&head);
// printf("\n=%d\n",work(&head));
test24();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询