Advertisement
simeonvarbanov

Untitled

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