|
|
|
|
![]() ![]() |
Aug 10 2004, 07:44 AM
Post
#1
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 114 Joined: 9-August 04 From: Suzhou Jiangsu China Member No.: 743 |
CODE #include "stdio.h"
#include "process.h" int fun0(int x,int y){return (x+y);} int fun1(int x,int y){return (x-y);} int fun2(int x,int y){return (x*y);} int fun3(int x,int y){return (x/y);} int (*assign[4])(int x,int y); void assign_function(void) { assign[0]=fun0; assign[1]=fun1; assign[2]=fun2; assign[3]=fun3; } //const int data[4]={9,8,7,1}; int data[4]={9,8,7,1}; const int fetch[24][4]={ {3,2,1,0},{0,3,2,1},{1,0,3,2},{2,1,0,3}, {3,1,2,0},{0,3,1,2},{2,0,3,1},{1,2,0,3}, {3,0,1,2},{2,3,0,1},{1,2,3,0},{0,1,2,3}, {3,2,0,1},{1,3,2,0},{0,1,3,2},{2,0,1,3}, {3,0,1,2},{1,3,0,2},{2,1,3,0},{0,2,1,3}, {3,1,0,2},{2,3,1,0},{0,2,3,1},{1,0,2,3}}; int calculator[4]; void result(int x,int y,int r,int n) { int a[4],b[4]; char fomular[20],o[4]={'+','-','*','/'}; for(int i=0;i<4;i++) { a[i]=fetch[x][i]; 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]]); } if((a[0]==0||a[0]==1)&&(a[1]==2||a[1]==3)) 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); else if((a[0]==2||a[0]==3)&&(a[3]!=2||a[3]!=3)) 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); 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); //printf("\tResult:\n"); printf("\t%s",fomular); } void main(void) { int r=24; //int temp; int n=0; assign_function(); printf("\t\tAchieve 24 point\n\n\tBy Lucifer Star\n\tSingle Studio\n\t\tEMAIL:LetMeGetOne@hotmail.com\n\n"); printf("*****************************************\n"); printf("\tPlease input 1st number:"); scanf("%d",&data[0]); printf("\tPlease input 2nd number:"); scanf("%d",&data[1]); printf("\tPlease input 3rd number:"); scanf("%d",&data[2]); printf("\tPlease input 4th number:"); scanf("%d",&data[3]); printf("\tWhat do you want to achieve(default:24):"); scanf("%d",&r); //scanf("%d",&temp); //temp=getch(); //if(temp!=0x0d) r=temp; printf("\n ***********************************\n"); if(data[0]==0||data[1]==0||data[2]==0||data[3]==0) {printf("The 4 numbers shouldn't be 0!");exit(0);} for(int ii=0;ii<24;ii++) { for(int i=0;i<24;i++) { int z=0; for(int j=0;j<4;j++) { calculator[j]=data[fetch[i][j]]; } for(int k=0;k<3;k++) { if(k==0) { if(calculator[0]%calculator[1]!=0&&fetch[ii][0]==3) {z=0;break;} else z=assign[fetch[ii][k]](calculator[k],calculator[k+1]); } else { if(fetch[ii][k]==3) if(calculator[k+1]==0||z==0||(z%calculator[k+1])!=0) {z=0;break;} z=assign[fetch[ii][k]](z,calculator[k+1]); } } if(z==r) { n++;result(ii,i,r,n); } } } printf("\n******************************************\n"); if(n==0) printf("Sorry,it's unable to do so!"); else printf("Thanks for using my software!"); } |
|
|
|
Sep 1 2004, 02:18 PM
Post
#2
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 38 Joined: 31-August 04 Member No.: 967 |
don't use printf use cout its more secure. Using print may lead to bufferover flows and hackers can easily gain control of the computer by injecting some extra code themselves
|
|
|
|
Sep 1 2004, 06:07 PM
Post
#3
|
|
|
Administrator ![]() Group: Admin Posts: 1,479 Joined: 11-June 04 From: Somewhere in Time & Space. Member No.: 1 |
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.
|
|
|
|
Sep 3 2004, 11:40 AM
Post
#4
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 114 Joined: 9-August 04 From: Suzhou Jiangsu China Member No.: 743 |
I've learned C for more than 5 years,
and I found it's hard for me to learn C++,though I can understand it. CLASS is hard to me. the output of cout is variable,uncontrolable,I think. |
|
|
|
Sep 24 2004, 01:53 PM
Post
#5
|
|
|
Newbie ![]() Group: Members Posts: 7 Joined: 24-September 04 Member No.: 1,253 |
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 Trap17. I'm pleased to be with you folks, but you must forgive me on my "bad" english.. |
|
|
|
Sep 25 2004, 11:48 AM
Post
#6
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 114 Joined: 9-August 04 From: Suzhou Jiangsu China Member No.: 743 |
QUOTE(goranche @ Sep 24 2004, 01:53 PM) 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 Trap17. I'm pleased to be with you folks, but you must forgive me on my "bad" english.. I 'm from China. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 12th October 2008 - 07:30 AM |