#include "stdio.h"
#include "math.h"
#include "time.h"
void main()
{
void first();
void second();
int third();
time_t t;
int close=0;
char get;
clrscr();
srand((unsigned) time(&t));
for(;;)
{
clrscr();
printf("24 POINTS SYSTEM v2.2\n");
printf("MADE BY DANTE WANG\n");
printf("Jul.28th 2006\n\n");
printf("Please choose mode!\n\n");
printf("1. Game Mode\n");
printf("2. Count Mode\n");
printf("3. Quit\n\n");
get=getch();
switch(get)
{
case '1': first(); break;
case '2': second();break;
case '3': close=third(); break;
default: printf("Please input 1, 2 or 3!\n"); getch();
}
if(close)
break;
}
}
void first()
{
int jdgsgn(int a,int b,int c,int d);
int n[4],i;
char ch[100],ch1[100];
int check(char *p);
int exam2(char *p,int a,int b,int c,int d);
void sign(int a,int b,int c,int d);
double figure(char *p);
void draw(int *p);
for(;;)
{
for(i=0;i<4;i++)
{
n[i]=rand()%13+1;
}
draw(n);
if(!jdgsgn(n[0],n[1],n[2],n[3]))
{
printf("No answer!");
getch();
continue;
}
gets(ch);
if(!strcmp(strlwr(ch),"quit"))
break;
else
{
strcpy(ch1,ch);
if(!check(ch)&&!exam2(ch,n[0],n[1],n[2],n[3])&&fabs(figure(ch1)-24)<=0.000001)
{
printf("\nGood job!\n");
getch();
}
else
{
printf("\nWrong!\n\nAnswer: ");
sign(n[0],n[1],n[2],n[3]);
getch();
}
}
}
}
void second()
{
int n[4],i;
char c[4][10];
int exam(char *p);
void sign(int a,int b,int c,int d);
double recog(char *p);
clrscr();
printf("Input 0 to leave.\n\n");
for(;;)
{
for(i=0;i<4;i++)
{
scanf("%s",&c[i]);
if(exam(c[i]))
{
printf("GRAMMER ERROR!\n");
i--;
continue;
}
n[i]=(int)recog(c[i]);
if(n[i]==0)
break;
}
if(n[0]&&n[1]&&n[2]&&n[3])
sign(n[0],n[1],n[2],n[3]);
else
break;
}
}
int third()
{
printf("Thanks for using this software!\n\n");
printf("If you have any advices or suggestions,\n\n");
printf("please call my QQ:331349451\n\n");
getch();
return(1);
}
void sign(int a,int b,int c,int d)
{
char *change(int x);
double figure(char *p);
int i[4],n,n1,n2,n3,n4,s1,s2,s3,t,flag=0;
char s[4][2]={"+","-","*","/"},p[64]={0},p1[64]={0};
i[0]=a;
i[1]=b;
i[2]=c;
i[3]=d;
for(n1=0;n1<4;n1++)
{
for(n2=0;n2<4;n2++)
{
if(n2==n1)
continue;
for(n3=0;n3<4;n3++)
{
if(n3==n2||n3==n1)
continue;
for(n4=0;n4<4;n4++)
{
if(n4==n3||n4==n2||n4==n1)
continue;
for(s1=0;s1<4;s1++)
{
for(s2=0;s2<4;s2++)
{
for(s3=0;s3<4;s3++)
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
if(s1/2==1&&(s2/2==0||s3/2==0))
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,"(");
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcat(p,")");
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
if(s1/2==0&&s2/2==1)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,")");
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
if(s2/2==0&&s1/2==1)
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,"(");
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,")");
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
if((s1/2==0||s2/2==0)&&s3/2==1)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,")");
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
if(s1/2==0&&s2/2==1&&s3/2==0)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,")");
strcat(p,s[s2]);
strcat(p,"(");
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcat(p,")");
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
printf("%s\n",p);
flag++;
break;
}
*p='\0';
}
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag==0)
printf("No answer!\n");
}
char *change(int x)
{
static char a[64];
char *c;
c=a+63;
*c='\0';
c--;
for(;;c--)
{
if(x>=10)
{
*c=x%10+'1'-1;
x=x/10;
}
else
{
*c=x+'1'-1;
break;
}
}
return(c);
}
double figure(char *p)
{
int judge(char *p);
int judge2(char *p);
void add(char *p);
void pick(char *p);
double recog(char *p);
char *c=p,*c1,c2[64];
int sign,kuoh=0,flag2=0;
double result,chu;
add(p);
for(sign=0;*c!='\0';c++)
{
if(*c=='+'||*c=='-'||*c=='*'||*c=='/'||*c=='('||*c==')')
{
sign++;
break;
}
}
if(sign==0)
return(recog(p));
else
{
c=p;
for(;*c!='\0';c++)
{
if(*c=='(')
kuoh++;
if(*c==')')
kuoh--;
if(!judge2(c))
{
if(*c=='+'&&kuoh==0)
{
*c='\0';
result=figure(p)+figure(c+1);
flag2++;
break;
}
if(*c=='-'&&kuoh==0)
{
*c='\0';
result=figure(p)-figure(c+1);
flag2++;
break;
}
}
if(!judge(c))
{
if(*c=='*'&&kuoh==0)
{
*c='\0';
result=figure(p)*figure(c+1);
flag2++;
break;
}
if(*c=='/'&&kuoh==0)
{
*c='\0';
chu=figure(c+1);
if(chu==0)
{
result=99999;
}
else
{
result=figure(p)/chu;
}
flag2++;
break;
}
}
}
if(flag2)
return(result+0);
else
{
pick(p);
result=figure(p);
return(result+0);
}
}
}
int judge(char *p)
{
char *c=p;
int kuoh=0,flag=0;
for(c++;*c!='\0';c++)
{
if(*c=='(')
kuoh++;
if(*c==')')
kuoh--;
if(kuoh==0&&(*c=='+'||*c=='-'||*c=='*'||*c=='/'))
{
flag++;
break;
}
}
return(flag);
}
int judge2(char *p)
{
char *c=p;
int kuoh=0,flag=0;
for(c++;*c!='\0';c++)
{
if(*c=='(')
kuoh++;
if(*c==')')
kuoh--;
if(kuoh==0&&(*c=='+'||*c=='-'))
{
flag++;
break;
}
}
return(flag);
}
void add(char *p)
{
char *c=p,b[64];
int flag=0;
for(;*c!='\0';c++)
{
if((*c=='-'||*c=='+')&&(*(c+1)>='0'&&*(c+1)<='9'))
flag++;
if((*c>='0'&&*c<='9'||*c=='.')&&(*(c+1)=='*'||*(c+1)=='/'||*(c+1)=='+'||*(c+1)=='-'))
{
flag++;
break;
}
if((*c>='0'&&*c<='9'||*c=='.')&&*(c+1)==')')
{
break;
}
}
if(flag==2)
flag=1;
else
flag=0;
if(flag)
{
c=p;
for(;*c!='\0';c++)
{
if(*(c+1)>='0'&&*(c+1)<='9')
{
if(*c=='+'||*c=='-')
{
strcpy(b,c);
*c='(';
strcpy(c+1,b);
}
break;
}
}
c+=2;
for(;*c!='\0';c++)
{
if((*c<'0'||*c>'9')&&*c!='.')
{
strcpy(b,c);
*c=')';
strcpy(c+1,b);
break;
}
}
}
}
void pick(char *p)
{
char *c=p;
if(*c=='('&&*(c+strlen(c)-1)==')')
{
strcpy(c,c+1);
*(c+strlen(c)-1)='\0';
}
}
double recog(char *p)
{
char *c=p;
int flag=0,number;
double quan,ans=0;
for(;;c++)
{
if(*c=='.')
{
flag++;
number=c-p;
}
if(*c!='.'&&(*c<'0'||*c>'9'))
break;
}
if(flag==0)
{
for(quan=1,c--;c>=p;c--,quan*=10)
ans+=(*c-'1'+1)*quan;
}
else
{
for(c--,quan=pow(10,number-(c-p));c>=p;c--)
{
if(*c=='.')
continue;
else
{
ans+=(*c-'1'+1)*quan;
quan*=10;
}
}
}
return(ans);
}
int exam(char *p)
{
double recog(char *p);
char *c=p;
int number=0;
for(;*c!='\0';c++)
{
if(*c>'9'||*c<'0')
number++;
if(number)
break;
}
if(!number)
{
if(recog(p)>13||recog(p)<0)
number++;
}
return(number);
}
void draw(int *p)
{
time_t t;
int n;
clrscr();
printf("Press \"quit\" to leave.");
srand((unsigned) time(&t));
for(n=0;n<4;n++)
{
gotoxy(1+7*n,3);
printf("\xda\xc4\xc4\xc4\xc4\xbf");
gotoxy(1+7*n,4);
printf("\xb3%c \xb3",rand()%4+3);
gotoxy(1+7*n,5);
printf("\xb3 \xb3");
gotoxy(1+7*n,6);
switch(*(p+n))
{
case 1:
printf("\xb3 A\xb3");
break;
case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:
printf("\xb3%4d\xb3",*(p+n));
break;
case 11:
printf("\xb3 J\xb3");
break;
case 12:
printf("\xb3 Q\xb3");
break;
case 13:
printf("\xb3 K\xb3");
}
gotoxy(1+7*n,7);
printf("\xc0\xc4\xc4\xc4\xc4\xd9");
gotoxy(1,9);
}
}
int jdgsgn(int a,int b,int c,int d)
{
char *change(int x);
double figure(char *p);
int i[4],n,n1,n2,n3,n4,s1,s2,s3,t,flag=0;
char s[4][2]={"+","-","*","/"},p[64]={0},p1[64]={0};
i[0]=a;
i[1]=b;
i[2]=c;
i[3]=d;
for(n1=0;n1<4;n1++)
{
for(n2=0;n2<4;n2++)
{
if(n2==n1)
continue;
for(n3=0;n3<4;n3++)
{
if(n3==n2||n3==n1)
continue;
for(n4=0;n4<4;n4++)
{
if(n4==n3||n4==n2||n4==n1)
continue;
for(s1=0;s1<4;s1++)
{
for(s2=0;s2<4;s2++)
{
for(s3=0;s3<4;s3++)
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
if(s1/2==1&&(s2/2==0||s3/2==0))
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,"(");
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcat(p,")");
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
if(s1/2==0&&s2/2==1)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,")");
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
if(s2/2==0&&s1/2==1)
{
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,"(");
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,")");
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
if((s1/2==0||s2/2==0)&&s3/2==1)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,s[s2]);
strcat(p,change(i[n3]));
strcat(p,")");
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
if(s1/2==0&&s2/2==1&&s3/2==0)
{
strcat(p,"(");
strcat(p,change(i[n1]));
strcat(p,s[s1]);
strcat(p,change(i[n2]));
strcat(p,")");
strcat(p,s[s2]);
strcat(p,"(");
strcat(p,change(i[n3]));
strcat(p,s[s3]);
strcat(p,change(i[n4]));
strcat(p,")");
strcpy(p1,p);
if(fabs(figure(p1)-24)<=0.000001)
{
flag++;
break;
}
*p='\0';
}
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
return(flag);
}
int exam2(char *p,int a,int b,int c,int d)
{
double recog(char *p);
int number=0,num[4],flag=0,n,m;
num[0]=a;
num[1]=b;
num[2]=c;
num[3]=d;
for(m=0;*p!='\0';p++,m++)
{
if(*p>='0'&&*p<='9'&&(*(p+1)>'9'||*(p+1)<'0'))
number++;
if(m>0&&*p>='0'&&*p<='9'&&*(p-1)>='0'&&*(p-1)<='9')
continue;
if(*p<='9'&&*p>='0')
{
if(recog(p)>13||recog(p)<1)
{
flag++;
break;
}
else
{
for(n=0;n<4;n++)
{
if(recog(p)==num[n])
{
num[n]=0;
break;
}
}
}
}
}
if(!flag)
{
if(number!=4)
flag++;
else
if(num[1]||num[2]||num[3]||num[0])
flag++;
}
return(flag);
}
int check(char *p)
{
int kuoh=0,flag=0;
for(;*p!='\0';p++)
{
if(*p==' ')
{
flag++;
break;
}
if(kuoh<0)
{
flag++;
break;
}
if(*p=='(')
kuoh++;
if(*p==')')
kuoh--;
if((*p=='+'||*p=='-'||*p=='*'||*p=='/')&&(*(p+1)=='*'||*(p+1)=='/'||*(p+1)=='+'||*(p+1)=='-'))
{
flag++;
break;
}
if(*p=='('&&(*(p+1)>'9'||*(p+1)<'0'&&*(p+1)!='-'))
{
flag++;
break;
}
if(*p==')'&&*(p+1)!='+'&&*(p+1)!='-'&&*(p+1)!='*'&&*(p+1)!='/'&&*(p+1)!='\0')
{
flag++;
break;
}
}
if(!flag)
{
if(kuoh!=0)
flag++;
}
return(flag);
}