C语言高手大大 帮忙注释下这段程序 谢谢了

2024年11月17日 03:29
有1个网友回答
网友(1):

#include
#include
int enumerate(int ans);

char op[]={'+','-','*','/'};
char *model[]=
{"((AxB)yC)zD",
"(Ay(BxC))zD",
"Az(By(CxD))",
"Az((BxC)yD)",
"(AxB)z(CyD)"};
int *A, *B, *C, *D;
char *x, *y ,*z;
int ***opd;
char ***opr;
char *sample;
int **oprand[]={&A,&B,&C,&D};
char **oprator[]={&x,&y,&z};
int a[4];
int main(void)
{int i;
printf("Enter %d numbers.\n",4);
for(i=0;iscanf("%d",a+i);
if(enumerate(24)==0)
printf("No solution!\n");
return 0;}
int calculate()
{int v1,v2;
char op;
if(*sample=='A'||*sample=='B'||*sample=='C'||*sample=='D')
v1=***opd++;
else if(*sample=='('){
sample++;
v1=calculate();}
sample++;
while(*sample=='x'||*sample=='y'||*sample=='z'){
op=***opr++;
sample++;
if(*sample=='A'||*sample=='B'||*sample=='C'||*sample=='D')
v2=***opd++;
else if(*sample=='('){
sample++;
v2=calculate();}
sample++;
//clrscr();
switch(op)
{case '+': v1+=v2; break;
case '-' : if(v1 else v1-=v2;break;
case '*': v1*=v2; break;
case '/': if(v2==0||v1%v2!=0)
return -1000;
else v1/=v2; break;}}
return v1;}
int enumerate(int ans)
{int k;
for(A=a;Afor(B=a;Bif(B==A) continue;
for(C=a;Cif(C==A||C==B)
continue;
D=a+6-(A-a)-(B-a)-(C-a);
for(x=op;xfor(y=op;yfor(z=op;zfor(k=0;k<5;k++){
opd=oprand;
opr=oprator;
sample=model[k];
if(calculate()==ans)
{opd=oprand;
opr=oprator;
sample=model[k];
while(*sample){
switch(*sample)
{case 'A':
case 'B':
case 'C':
case 'D':printf("%d",***opd++); break;
case 'x':
case 'y':
case 'z':printf("%c",***opr++);break;
case '(':
case ')':printf("%c",*sample);break;}
*sample++;}
printf("=24\n\n");
return 1;}}}}
return 0;}