SoFunction
Updated on 2025-03-02

C language implements 24-point game source code

This article shares the specific code for implementing 24-point games in C language for your reference. The specific content is as follows

Reference article:C language implements classic 24-point algorithm

Change the algorithm implementation to C language and can be run on a linux server. Modify it to display all results.
Note: If the parameter is repeated, such as 4, 4, 7, 7, the duplicate result will be echoed and cannot be cleared for the time being.

#include <>
#include <>
#include <>
#include <> 

const double PRECISION = 1E-6; 
#define COUNT 4 
const int RESULT = 24; 
#define STRLEN 50
double number[COUNT] = {0}; //It is necessary to use double here,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 recursively if(1 == n){ 
 if(number[0] == RESULT)
 { 
  // Avoid outputting brackets before and after  for (i = 1; i < strlen(expression[0]) - 1; i++) 
  { 
  printf("%c", expression[0][i]); 
  } 
  printf("\n"); 
  cnt++;
  return; 
 } 
 else 
  return; 
 } 
 //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 the number[j] element and fill it with number[n-1]  number[j]=number[n-1]; 
  strcpy(expa, expression[i]);
  strcpy(expb, expression[j]);
  // Delete the expression[j] element and fill it with expression[n-1]  strcpy(expression[j], expression[n-1]);

  // Addition  len= strlen(expression[i]);
  snprintf(expression[i], STRLEN, "(%s+%s)", expa, expb);
  number[i]=a+b; 
  Test(n-1);
  //There are two cases of minus signs, a-b and 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); 
  }
  // Multiplication  len= strlen(expression[i]);
  snprintf(expression[i], STRLEN, "(%s*%s)", expa, expb); 
  number[i]=a*b; 
  Test(n-1); 
  //There are two different situations in division, a/b and 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); 
  } 
  //Restore 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

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.