Jump to content
xisto Community
LuciferStar

How to make 9 8 7 6 =24

Recommended Posts

#include "stdio.h"[br]#include "process.h"[/br]int fun0(int x,int y){return (x+y);}[br]int fun1(int x,int y){return (x-y);}[/br]int fun2(int x,int y){return (x*y);}[br]int fun3(int x,int y){return (x/y);}[/br]int (*assign[4])(int x,int y);[br]void assign_function(void)[/br]{[br]assign[0]=fun0;[/br]assign[1]=fun1;[br]assign[2]=fun2;[/br]assign[3]=fun3;[br]}[/br]//const int data[4]={9,8,7,1};[br]int data[4]={9,8,7,1};[/br]const int fetch[24][4]={[br]{3,2,1,0},{0,3,2,1},{1,0,3,2},{2,1,0,3},[/br]{3,1,2,0},{0,3,1,2},{2,0,3,1},{1,2,0,3},[br]{3,0,1,2},{2,3,0,1},{1,2,3,0},{0,1,2,3},[/br]{3,2,0,1},{1,3,2,0},{0,1,3,2},{2,0,1,3},[br]{3,0,1,2},{1,3,0,2},{2,1,3,0},{0,2,1,3},[/br]{3,1,0,2},{2,3,1,0},{0,2,3,1},{1,0,2,3}};[br]int calculator[4];[/br]void result(int x,int y,int r,int n)[br]{[/br]int a[4],b[4];[br]char  fomular[20],o[4]={'+','-','*','/'};[/br]for(int i=0;i<4;i++)[br]{[/br]a[i]=fetch[x][i];[br]b[i]=data[fetch[y][i]];//printf("a[%d]:%d  b[%d]:%d  o[%d]:%c  o[a[%d]]:%c\n",i,a[i],i,b[i],i,o[i],i,o[a[i]]);[br]}[/br]if((a[0]==0||a[0]==1)&&(a[1]==2||a[1]==3))[/br]   sprintf(fomular,"Method %d:  (%d%c%d)%c%d%c%d=%d\n",n,b[0],o[a[0]],b[1],o[a[1]],b[2],o[a[2]],b[3],r);[br]else if((a[0]==2||a[0]==3)&&(a[3]!=2||a[3]!=3))[/br]   sprintf(fomular,"Method %d:  %d%c%d%c%d%c%d=%d\n",n,b[0],o[a[0]],b[1],o[a[1]],b[2],o[a[2]],b[3],r);[br]else sprintf(fomular,"Method %d:  (%d%c%d%c%d)%c%d=%d\n",n,b[0],o[a[0]],b[1],o[a[1]],b[2],o[a[2]],b[3],r);[/br]//printf("\tResult:\n");[br]printf("\t%s",fomular);[/br][br]}[/br][br]void main(void)[/br]{  int r=24;[br]   //int temp;[/br]   int n=0;[br]assign_function();[/br]printf("\t\tAchieve 24 point\n\n\tBy Lucifer Star\n\tSingle Studio\n\t\tEMAIL:LetMeGetOne@hotmail.com\n\n");[br]printf("*****************************************\n");[/br]printf("\tPlease input 1st number:");[br]scanf("%d",&data[0]);[/br]printf("\tPlease input 2nd number:");[br]scanf("%d",&data[1]);[/br]printf("\tPlease input 3rd number:");[br]scanf("%d",&data[2]);[/br]printf("\tPlease input 4th number:");[br]scanf("%d",&data[3]);[/br]printf("\tWhat do you want to achieve(default:24):");[br]scanf("%d",&r);[/br]//scanf("%d",&temp);[br]//temp=getch();[/br]//if(temp!=0x0d) r=temp;[br][/br]printf("\n     ***********************************\n");[br]if(data[0]==0||data[1]==0||data[2]==0||data[3]==0) {printf("The 4 numbers shouldn't be 0!");exit(0);}[/br]for(int ii=0;ii<24;ii++)[br]{[/br]   for(int i=0;i<24;i++)[br]   {[/br]	int z=0;[br]	for(int j=0;j<4;j++)[/br]	{[br]  calculator[j]=data[fetch[i][j]];[/br]	}[br]	for(int k=0;k<3;k++)[/br]	{[br]  if(k==0)[/br]  {[br]     if(calculator[0]%calculator[1]!=0&&fetch[ii][0]==3)[/br]        {z=0;break;}[br]     else z=assign[fetch[ii][k]](calculator[k],calculator[k+1]);[/br]  }[br]  else[/br]  {  if(fetch[ii][k]==3)[br]       if(calculator[k+1]==0||z==0||(z%calculator[k+1])!=0)[/br] 	 {z=0;break;}[br]     z=assign[fetch[ii][k]](z,calculator[k+1]);[/br]  }[br]	}[/br]	if(z==r)[br]	{    n++;result(ii,i,r,n);   }[/br]    }[br]}[/br]printf("\n******************************************\n");[br]if(n==0) printf("Sorry,it's unable to do so!");[/br]else printf("Thanks for using my software!");}

Share this post


Link to post
Share on other sites

I prefer printf because it is fast and gives more power. Formatting can be done easily. Besides, looking at the above program, since iostream.h is not included, cout cannot be included. its a pure c program.

Share this post


Link to post
Share on other sites

don't be scared with c++. Every thing that you are doing in C++ you CAN do in C but in more complex way...

classes are invented to make peoples unsecured but it's not so hard...

 

To all of you...

I'm from Serbia. I'm new here on Xisto. I'm pleased to be with you folks, but you must forgive me on my "bad" english.. :(

Share this post


Link to post
Share on other sites

Now something I get to add here is the way the quoted text is posted. There are

tags in between the lines. You cannot directly copy the text into the c++ ide and you need to edit them. I would suggest that any such thing copied from another location must be edited so that they look organized. Look at the first quoted text, they just look junk and as a first time learner will find the codes are not very friendly. Any matter must be organized as I think the posts are done for some information and not wasting time over what is quoted and find that there are too many errors or logical problems with the program. All people who post their posts must be held responsible and asked to edit their posts for making them more informative and neat. Incentives may be given. And for badly posted posts there should be no myCent awarded until resolved by editing. I would not like too much mods/admin intervention but a neater look is definitely welcome.

Share this post


Link to post
Share on other sites

you can't use the cout function because the iostream.h is not included the program hmm anyway cout is more secure than printf but if its only a school project just use printf :lol:

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.