forked from 0penBSD/LogicGate-Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogicgate.c
43 lines (36 loc) · 957 Bytes
/
logicgate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdbool.h>
#define AND(a, b) (a && b)
#define OR(a, b) (a || b)
#define NOT(a) (!a)
bool eval(char expr[], int *index) {
if (expr[*index] == 'N') {
(*index)++;
return NOT(eval(expr, index));
} else if (expr[*index] == 'O') {
(*index) += 2;
bool left = eval(expr, index);
(*index)++;
bool right = eval(expr, index);
(*index)++;
return OR(left, right);
} else if (expr[*index] == 'A') {
(*index) += 3;
bool left = eval(expr, index);
(*index)++;
bool right = eval(expr, index);
(*index)++;
return AND(left, right);
} else {
return expr[(*index)++] - '0';
}
}
int main() {
char expr[100];
printf("Escreva a operacao booleana: ");
scanf("%s", expr);
int index = 0;
bool result = eval(expr, &index);
printf("Resultado da expressao: %d\n", result);
return 0;
}