SHARE
TWEET

Untitled

a guest Oct 21st, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. void generate_truth_table(void *func(int *inputs), int n);
  4.  
  5. int main()
  6. {
  7.     /* Example function */
  8.     int f(int *v) {
  9.         return (v[0] & v[1]) | (v[2] & v[3]);
  10.     }
  11.  
  12.     generate_truth_table(&f, 4);
  13.    
  14.     return 0;
  15. }
  16.  
  17. void generate_truth_table(void *func(int *inputs), int n)
  18. {
  19.     void print_repeating(char *s, int n) {
  20.         for (; n > 0; n--) {
  21.             printf(s);
  22.         }
  23.         printf("\n");
  24.     }
  25.  
  26.     /* x1 |x2 |x3 |...|xn | */
  27.     int i, j;
  28.     for (i = 0; i < n; i++) {
  29.         printf(" x%d|", i);
  30.     }
  31.    
  32.     printf(" f ");
  33.     printf("\n");
  34.     /* ---+---+---+---+ */
  35.     print_repeating("---+", n + 1);
  36.     for (i = 0; i < (2 << (n-1)); i++) {
  37.         int v[n];
  38.         for (j = n-1; j >= 0; j--) {
  39.             v[j] = (i >> j) % 2;
  40.             printf(" %d |", v[j]);
  41.         }
  42.         printf(" %d |", (*func)(v));
  43.         printf("\n");
  44.         print_repeating("---+", n + 1);
  45.     }
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top