Advertisement
Guest User

test_countdown.c

a guest
Sep 23rd, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. // number of tabs with numbers
  8. #define NBTAB 6
  9. // number of operations (+-*/)
  10. #define NBOP 4
  11.  
  12. #define DIFF(a, b)       (((a) > (b))? ((a) - (b)):((b) - (a)))
  13. // I need to printf in my string the complete operation to keep track of it, to prinf it later in main
  14. #define SPRINT_OP(a, b, ope, result) {strcmp(tmp_res, res); sprintf(res,"%d %c %d = %d\n%s",(((a)>(b))?(a):(b)),c_op[ope],(((a)>(b))?(b):(a)),result,tmp_res);}
  15.  
  16.  
  17. int best = 0;
  18. int best_diff = 999;
  19. char res[255];
  20. char tmp_res[255];
  21.  
  22. int add(int *a, int b)
  23. {
  24.     return(*a += b);
  25. }
  26.  
  27. int sub(int *a, int b)
  28. {
  29.     return(*a = DIFF(*a, b));
  30. }
  31.  
  32. int mult(int *a, int b)
  33. {
  34.     return(*a *= b);
  35. }
  36.  
  37. int divi(int *a, int b)
  38. {
  39.     if( *a > b )
  40.     {
  41.         if(((*a && b) != 0) && (b != 1) && ((*a % b) == 0))
  42.             return(*a = *a / b);
  43.     }
  44.     if( b > *a)
  45.     {
  46.         if(((*a && b) != 0) && (*a != 1) && ((b % *a) == 0))
  47.             return(*a = b / *a);
  48.     }
  49.     return 0;
  50. }
  51.  
  52. // now I need an array of if int containing all the possible operations
  53. int(*op[])(int* ,int) = {add, sub, mult, divi};
  54. // now I need an array of char for the 4 operators
  55.  
  56. char c_op[] ="+-*/";
  57.  
  58. int compute(int* tab, int nb, int total)
  59. {
  60.     int i, j, k; // counters!!
  61.     int t[NBTAB]; // array of int of length the number "given" on the tabs
  62.     // now i will go from 0 to n-1 and j from i+1 to n, regular search nested for loops
  63.     for(i = 0; i < (nb - 1); i++)
  64.     {
  65.         for(j = (i + 1); j < nb; j++)
  66.         {
  67.             // k is the inner loop, for each i and j, we do the 4 op aka NBOP
  68.             for(k = 0; k < NBOP; k++)
  69.             {
  70.                 memcpy(t, tab, sizeof(int) * NBTAB); // pointers yay!!
  71.                 if((*op[k])(&t[i], t[j]))
  72.                 {
  73.                     if(t[i] == total)
  74.                         return(SPRINT_OP(tab[i], tab[j], k, t[i]));
  75.                     if(DIFF(t[i], total) < best_diff)
  76.                     {
  77.                         best = t[i];
  78.                         best_diff = DIFF(best, total);
  79.                     }
  80.                     t[j] = t[nb - 1];
  81.                     if(compute(tab, nb - 1, total))
  82.                         return(SPRINT_OP(tab[i], tab[j], k, t[i]));
  83.                 }
  84.             }
  85.         }
  86.     }
  87.     return 0;
  88. }
  89.  
  90. int main (void)
  91. {
  92.     //res = {NULL};
  93.     int i, num, total = 0, t[NBTAB];
  94.     int draw[] = {1,2,3,4,5,6,7,8,9,10,25,50,75,100};
  95.     for (i = 0; i < NBTAB; i++)
  96.     {
  97.         num = draw[rand() % 14];
  98.         printf("number %d : %d\n",(i+1), num);
  99.         t[i] = num;
  100.     }
  101.     total = (100+(rand() % 899));
  102.     printf("total: %d\n", total);
  103.     if(!compute(t, NBTAB, total))
  104.         (compute(t, NBTAB, best));
  105.     else
  106.         printf("We have an answer!\n");
  107.         //printf("%s",res);
  108.         return (!printf(res));
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement