-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path19.c
74 lines (64 loc) · 2.33 KB
/
19.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
// Function to identify the logic function based on outputs
void identifyFunction(int outputs[8]) {
int allZero = 1, allOne = 1;
int isAND = 1, isOR = 1, isXOR = 1, isNAND = 1, isNOR = 1, isXNOR = 1, isNOT = 1;
// Loop through each truth table entry
for (int i = 0; i < 8; i++) {
int x = (i & 4) >> 2; // Extract bit for x
int y = (i & 2) >> 1; // Extract bit for y
int z = (i & 1); // Extract bit for z
int f = outputs[i]; // Output for the combination (x, y, z)
// Check for all zero or all one
if (f != 0) allZero = 0;
if (f != 1) allOne = 0;
// Check common logic functions based on truth table entries
if (f != (x & y & z)) isAND = 0;
if (f != (x | y | z)) isOR = 0;
if (f != (x ^ y ^ z)) isXOR = 0;
if (f != !(x & y & z)) isNAND = 0;
if (f != !(x | y | z)) isNOR = 0;
if (f != !(x ^ y ^ z)) isXNOR = 0;
if (i == 0 && f != !z) isNOT = 0; // NOT only applies for one variable (like z)
}
// Identify the function based on flags
printf("Identified function: ");
if (allZero) {
printf("F(x, y, z) = 0 (Always False)\n");
} else if (allOne) {
printf("F(x, y, z) = 1 (Always True)\n");
} else if (isAND) {
printf("F(x, y, z) = x AND y AND z\n");
} else if (isOR) {
printf("F(x, y, z) = x OR y OR z\n");
} else if (isXOR) {
printf("F(x, y, z) = x XOR y XOR z\n");
} else if (isNAND) {
printf("F(x, y, z) = NOT (x AND y AND z)\n");
} else if (isNOR) {
printf("F(x, y, z) = NOT (x OR y OR z)\n");
} else if (isXNOR) {
printf("F(x, y, z) = NOT (x XOR y XOR z)\n");
} else if (isNOT) {
printf("F(x, y, z) = NOT z\n");
} else {
printf("Complex or custom function\n");
}
}
int main() {
int outputs[8];
// Input the output values for F(x, y, z)
printf("Enter the output values for F(x, y, z) for the following truth table:\n");
printf("x y z | F(x,y,z)\n");
printf("----------------\n");
for (int i = 0; i < 8; i++) {
int x = (i & 4) >> 2;
int y = (i & 2) >> 1;
int z = (i & 1);
printf("%d %d %d | ", x, y, z);
scanf("%d", &outputs[i]);
}
// Identify the function
identifyFunction(outputs);
return 0;
}