Reputation: 1
I am writing a program to evaluate a boolean algebra equation such as A'B + C'
Ive tried changing how the temparray is read or created and im not sure what im missing
Im relativey new to this so it might be very obvious and thanks for any help
Is it a problem with how im assigning values to the tempArray? because the regular array works just fine, i need the temp array so it can take the terms between the + and evaluate them first then evaluate the + after
my input is 6
and then A'B+C'
where '
is negate of the previous variable and it gives me
RESULT IN EVAL TERM FUNCTION: 1
Term Result: 1
REAL ARRAY
A'B+C'
TEMP ARRAY
☺ ' ☺ 2 ⌂
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool evalTerm(int A, int B, int C, char *term);
int main() {
int s;
int A = true;
int B = true;
int C = true;
printf("Enter The Length of Your Function: ");
scanf("%d", &s);
char arr[s + 1];
printf("Enter a sum-of-products: ");
scanf("%s", arr);
char tempArray[s+1];
for(int i = 0; i<strlen(arr); i++) {
if (arr[i] == '+' || arr[i] == '\0'){
for(int j = 0; j<i; j++) {
if (arr[j] == 'A') {
tempArray[j] = A;
}
if (arr[j] == 'B') {
tempArray[j] = B;
}
if (arr[j] == 'C') {
tempArray[j] = C;
}
if (arr[j] == '\'') {
tempArray[j] = '\'';
}
}
bool termResult = evalTerm(A,B,C,tempArray);
printf("Term Result: %d\n", termResult);
}
}
//get everything before first + and copy it to tep array, then evaluate and put in result array then get next terms and then OR result array at end
printf("REAL ARRAY\n");
for (int i = 0; i < s; i++) {
printf("%c", arr[i]);
}
printf("\n\n");
printf("TEMP ARRAY\n");
for (int i = 0; i < s; i++) {
printf(" %c", tempArray[i]);
}
}
bool evalTerm(int A, int B, int C, char *term){
for (int i = 0; i < strlen(term); i++) {
if (term[i] == '\'') {
switch (term[i - 1]) {
case 'A':
A = !A;
break;
case 'B':
B = !B;
break;
case 'C':
C = !C;
break;
}
}
}
bool result = A && B && C;
printf("RESULT IN EVAL TERM FUNCTION: %d\n", result);
return result;
}
Upvotes: 0
Views: 54