Advertisement
simeonvarbanov

Untitled

Oct 27th, 2012
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAXINPUT 10
  3.  
  4. typedef enum {IN0 = 0, IN1 = 1, ANDG, ORG, XORG, NANDG, NORG, INVG} GateType;
  5. typedef struct Gate* GatePtr;
  6.  
  7. struct Gate {
  8.   GateType type;
  9.   GatePtr input1;
  10.   GatePtr input2;
  11.   GatePtr input3;
  12.   GatePtr input4;
  13. };
  14.  
  15. int initialiseInputArray (GatePtr gateArray[], int count)
  16. {
  17.   int i = 0;
  18.   while (i < count) {
  19.     GatePtr g = malloc(sizeof(struct Gate));
  20.     g->input1 = NULL;
  21.     g->input2 = NULL;
  22.     g->type = IN0;
  23.     gateArray[i] = g;
  24.     ++i;
  25.   }
  26.   gateArray[i] = NULL;
  27. }  
  28.  
  29. int resetInput (GatePtr gateArray[])
  30. {
  31.   /* Must set all inputgates to type IN0 */
  32.   GatePtr g = *gateArray;
  33.   while (g != NULL) {
  34.     g->type = IN0;
  35.     g = *(++gateArray);
  36.   }
  37. }
  38.  
  39. int applyNextInput (GatePtr gateArray[])
  40. {  
  41.   GatePtr g = *gateArray;
  42.   int done = 0;
  43.   int overflow = 1;
  44.   while (!done) {
  45.     if (g->type == IN0) {
  46.       // Add one and return
  47.       g->type = IN1;
  48.       done = 1;
  49.       overflow = 0;
  50.     }
  51.     else {
  52.       // Reset and carry:
  53.       g->type = IN0;
  54.       g = *(++gateArray);
  55.     }
  56.     if (g == NULL)
  57.       done = 1;
  58.   }
  59.   return overflow;
  60. }
  61.  
  62. int eval(GatePtr gate) {
  63.   if(gate->type == IN0) {
  64.     return 0;
  65.   } else if(gate->type == IN1) {
  66.     return 1;
  67.   } else if(gate->type == ANDG) {
  68.     return eval(gate->input1) & eval(gate->input2)
  69.     & eval(gate->input3)& eval(gate->input4);
  70.   } else if (gate->type == ORG) {
  71.     return eval(gate->input1) | eval(gate->input2);
  72.   } else if(gate->type == XORG) {
  73.     return eval(gate->input1) ^ eval(gate->input2);
  74.   } else if (gate->type == NANDG) {
  75.     return !(eval(gate->input1) & eval(gate->input2));
  76.   } else if (gate->type == NORG) {
  77.     return !(eval(gate->input1) | eval(gate->input2));
  78.   } else if (gate->type == INVG) {
  79.     return !(eval(gate->input1));
  80.   }
  81. }
  82.  
  83. void printTruthTable(GatePtr inputGates[], int results[]) {
  84.   GatePtr gate = *inputGates;
  85.   while (gate != NULL) {
  86.     if(gate->type == IN0) {
  87.       printf("|\t0\t|");
  88.     } else if (gate->type == IN1) {
  89.        printf("|\t1\t|");
  90.     }
  91.     gate = *(++inputGates);
  92.   }
  93.   int * result = results;
  94.   while(*result != -1) {
  95.     printf("|\t%d\t|", *result);
  96.     result = ++results;
  97.   }
  98.   printf("\n");
  99. }
  100.  
  101. int main(void) {
  102.   GatePtr inputGates[MAXINPUT+1];
  103.   initialiseInputArray (inputGates, 4);
  104.  
  105.   GatePtr andgate = malloc(sizeof(struct Gate));
  106.   andgate->type = ANDG;
  107.   andgate->input1 = inputGates[0];
  108.   andgate->input2 = inputGates[1];
  109.   andgate->input3 = inputGates[2];
  110.   andgate->input4 = inputGates[3];
  111.   resetInput(inputGates);
  112.   do {
  113.     int out = eval(andgate);
  114.     int results[4] = {out, -1};
  115.     printTruthTable(inputGates, results);
  116.   } while (!applyNextInput(inputGates));
  117.  
  118.   return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement