Logic Gates circuit solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
int myand(int a, int b);
int myor(int a, int b);
int mynand(int a, int b);
int mynor(int a, int b);
int myxor(int a, int b);
typedef int (*CallBack)(int a, int b);
int getinput();
typedef struct function {
CallBack f;
char ch[10];
} MyFunction;
typedef struct gate {
CallBack f;
struct gate * in1 ;
struct gate * in2 ;
} Gate;
Gate * creategate(CallBack f);
int eval(Gate *x);
int main() {
Gate *a_ptr, *a1_ptr, *a2_ptr, *b_ptr, *b1_ptr, *b2_ptr, *c_ptr, *c1_ptr, *c2_ptr, *d_ptr, *e_ptr, *f_ptr;
a_ptr = creategate(mynor);
a1_ptr = creategate(getinput);
a2_ptr = creategate(getinput);
a_ptr->in1 = a1_ptr;
a_ptr->in2 = a2_ptr;
printf("First gate's output: %d\n", eval(a_ptr));
b_ptr = creategate(myand);
b1_ptr = creategate(getinput);
b2_ptr = creategate(getinput);
b_ptr->in1 = b1_ptr;
b_ptr->in2 = b2_ptr;
printf("Second gate's output: %d\n", eval(b_ptr));
c_ptr = creategate(myor);
c1_ptr = creategate(getinput);
c2_ptr = creategate(getinput);
c_ptr->in1 = c1_ptr;
c_ptr->in2 = c2_ptr;
printf("Third gate's output: %d\n", eval(c_ptr));
d_ptr = creategate(mynand);
d_ptr->in1 = a_ptr;
d_ptr->in2 = b_ptr;
printf("Fourth gate's output: %d\n", eval(d_ptr));
e_ptr = creategate(myxor);
e_ptr->in1 = b_ptr;
e_ptr->in2 = c_ptr;
printf("Fifth gate's output: %d\n", eval(e_ptr));
f_ptr = creategate(myor);
f_ptr->in1 = d_ptr;
f_ptr->in2 = e_ptr;
printf("Circuit's output: %d\n", eval(f_ptr));
free(a_ptr);
free(a1_ptr);
free(a2_ptr);
free(b_ptr);
free(b1_ptr);
free(b2_ptr);
free(c_ptr);
free(c1_ptr);
free(c2_ptr);
free(d_ptr);
free(e_ptr);
free(f_ptr);
return 0;
}
int myand (int a, int b) {
return a * b;
}
int myor (int a, int b) {
return a + b>0;
}
int mynand (int a, int b) {
int result = a * b;
if (result == 0) {
result = 1;
}
else {
result = 0;
}
return result;
}
int mynor(int a, int b) {
int result = a + b>0;
if (result == 0) {
result = 1;
}
else {
result = 0;
}
return result;
}
int myxor(int a, int b) {
int a1=0, b1=0;
if (a==0) {
a1=1;
}
else if (a==1) {
a1=0;
}
if (b==0) {
b1=1;
}
else if (b==1) {
b1=0;
}
return ((a*b1) + (a1*b))>0;
}
int getinput() {
int x;
printf("Enter input(0 or 1): ");
scanf("%d", &x);
return x;
}
Gate * creategate(CallBack f) {
Gate * temp ;
temp = malloc(sizeof (Gate));
temp->f = f;
temp->in1 = NULL;
temp->in2 = NULL;
return temp;
}
int eval(Gate *x) {
int a, b;
if (x->in1 != NULL) {
a = eval(x->in1);
}
if (x->in2 != NULL) {
b = eval(x->in2);
}
if (x->in1==NULL && x->in2 == NULL) {
return (x->f)(0,0);
}
else {
return (x->f)(a,b);
}
}
I first make the user enter each gate inputs using the getinput function. After entering the first six inputs in the first three gates, i want the programm to take automatically the outputs from the previous gates without the user having to enter something. I tried many ways but i couldnt find a solution. I need to keep the dynamic memory allocation and also all the structures and i can add another function if necessary.
A number of issues ...
myand
,myor
, etc.) return a value instead of setting the output value into the struct.eval
function).Here is the refactored code. It is annotated. Unfortunately, I had to rewrite most of it.
Here is the sample input I used:
Here is the output: