C language to achieve 24 point game source code

  • 2020-06-19 11:23:52
  • OfStack

This article shares the specific code of C language to realize 24 points game for your reference. The specific content is as follows

C language to achieve the classical 24-point algorithm

Change the algorithm implementation to C language and run on the linux server. Also modified to display all results.
Note: if the pass is repeated, such as 4,4,7,7, it will echo the repeat result and cannot be cleaned temporarily.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> 

const double PRECISION = 1E-6; 
#define COUNT 4 
const int RESULT = 24; 
#define STRLEN 50
double number[COUNT] = {0}; // Here, 1 Need to use double,
char expression[COUNT][STRLEN] = {0}; // Save expression  

#define TRUE 1
#define FALSE 0

int cnt = 0;

void Test(int n)
{ 
 int i = 0;
 int j = 0;
 int len = 0;
 // End of the recursive  
 if(1 == n){ 
 if(number[0] == RESULT)
 { 
  //  Avoid parentheses before and after output  
  for (i = 1; i < strlen(expression[0]) - 1; i++) 
  { 
  printf("%c", expression[0][i]); 
  } 
  printf("\n"); 
  cnt++;
  return; 
 } 
 else 
  return; 
 } 
 // A recursive process  
 for(i=0;i<n;i++){ 
 for(j=i+1;j<n;j++){ 
  double a,b; 
  char expa[STRLEN] = {0};
  char expb[STRLEN] = {0};
  a=number[i]; 
  b=number[j]; 
  //  delete number[j] Elements, number[n-1] fill  
  number[j]=number[n-1]; 
  strcpy(expa, expression[i]);
  strcpy(expb, expression[j]);
  //  delete expression[j] Elements, expression[n-1] fill  
  strcpy(expression[j], expression[n-1]);

  //  add  
  len= strlen(expression[i]);
  snprintf(expression[i], STRLEN, "(%s+%s)", expa, expb);
  number[i]=a+b; 
  Test(n-1);
  // There are two ways to do it ,a-b with b-a 
  len= strlen(expression[i]);
  snprintf(expression[i], STRLEN, "(%s-%s)", expa, expb);
  number[i]=a-b; 
  Test(n-1); 
  if(a != b)
  {
  len= strlen(expression[i]);
  snprintf(expression[i], STRLEN, "(%s-%s)", expb, expa); 
  number[i]=b-a; 
  Test(n-1); 
  }
  //  The multiplication  
  len= strlen(expression[i]);
  snprintf(expression[i], STRLEN, "(%s*%s)", expa, expb); 
  number[i]=a*b; 
  Test(n-1); 
  // There are two ways of dividing ,a/b with b/a 
  if(b!=0){ 
  len= strlen(expression[i]);
  snprintf(expression[i], STRLEN, "(%s/%s)", expa, expb);
  number[i]=a/b; 
  Test(n-1);
  } 
  if((a!=0) && (a != b)){ 
  len= strlen(expression[i]);
  snprintf(expression[i], STRLEN, "(%s/%s)", expb, expa);
  number[i]=b/a; 
  Test(n-1); 
  } 
  // Return an array  
  number[i]=a; 
  number[j]=b; 
  strcpy(expression[i], expa);
  strcpy(expression[j], expb);
 } 
 } 
 return; 
} 
int main(int argc, char **argv)
{ 
 int i = 0;

 if(5 != argc)
 {
 printf("arg err\n");
 return 0;
 }

 for(i=0;i<COUNT;i++)
 { 
 char buffer[20]; 
 number[i] = atoi(argv[i + 1]);
 strcpy(expression[i], argv[i + 1]);
 } 

 Test(COUNT);

 if(0 != cnt) 
 {
 printf("Total[%d], Success\n", cnt); 
 }
 else 
 {
 printf("Fail\n"); 
 }
 return 0;
} 

The operation results are as follows:


andy@ubuntu14:~/work$ ./test 5 6 7 8
((5+7)-8)*6
(5+7)*(8-6)
8/((7-5)/6)
(6/(7-5))*8
6/((7-5)/8)
(8/(7-5))*6
(6*8)/(7-5)
((5-8)+7)*6
(7-(8-5))*6
(5+7)*(8-6)
(6*8)/(7-5)
(5+(7-8))*6
(5-(8-7))*6
Total[13], Success
andy@ubuntu14:~/work$ ./test 7 7 7 7
Fail

Related articles: