#include #include #include #include "auxiliar.h" int check_length(char **inputs, uint8_t num_inputs) { int i; int length = strlen(inputs[0]); for(i = 1; i < num_inputs; i++) { if(strlen(inputs[i]) != length) { return -1; } } return length; } char* or(char **inputs, int num_inputs, int input_length) { int i; int j; char *result = malloc(sizeof(char) * (input_length + 1)); memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) { result[i] |= inputs[j][i]; } if(result[i]) { result[i] = '1'; } else { result[i] = '0'; } } return result; } char* nor(char **inputs, int num_inputs, int input_length) { int i; int j; char *result = malloc(sizeof(char) * (input_length + 1)); memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) { result[i] |= inputs[j][i]; } if(result[i]) { result[i] = '0'; } else { result[i] = '1'; } } return result; } char* xor(char **inputs, int num_inputs, int input_length) { int i; int j; char *result = malloc(sizeof(char) * (input_length + 1)); memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) { result[i] ^= inputs[j][i]; } if(result[i]) { result[i] = '1'; } else { result[i] = '0'; } } return result; } char* xnor(char **inputs, int num_inputs, int input_length) { int i; int j; char *result = malloc(sizeof(char) * (input_length + 1)); memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) { result[i] ^= inputs[j][i]; } if(result[i]) { result[i] = '0'; } else { result[i] = '1'; } } return result; } char* and(char **inputs, int num_inputs, int input_length) { int i; int j; char *result = malloc(sizeof(char) * (input_length + 1)); memset(result, 0x01, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) { result[i] &= inputs[j][i]; } if(result[i]) { result[i] = '1'; } else { result[i] = '0'; } } return result; } char* nand(char **inputs, int num_inputs, int input_length) { int i; int j; char *result = malloc(sizeof(char) * (input_length + 1)); memset(result, 0x01, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) { result[i] &= inputs[j][i]; } if(result[i]) { result[i] = '0'; } else { result[i] = '1'; } } return result; } char* not(char **inputs, int num_inputs, int input_length) { int i; int j; char *result = malloc(sizeof(char) * (input_length + 1)); memset(result, 0x01, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) { result[i] = inputs[j][i]; } if(result[i]) { result[i] = '0'; } else { result[i] = '1'; } } return result; } int main(int argc, char **argv) { if(argc != 2) { error(); } char *input = argv[1]; if(!strcmp(input,"error")) { error(); } char **inputs = comma_separate(input); if(inputs == NULL) { error(); } int num_inputs = num_occurrences(input,','); int input_length = check_length(inputs,num_inputs); if(input_length == -1) { error(); } normalize(inputs,num_inputs); char *result; char *command_name; if(last_occurrence(argv[0],'/') == -1) { command_name = argv[0]; } else { command_name = substring(argv[0],last_occurrence(argv[0],'/')+1,strlen(argv[0])); } if(!strcmp(command_name,"or")) { result = or(inputs,num_inputs,input_length); } else if(!strcmp(command_name,"nor")) { result = nor(inputs,num_inputs,input_length); } else if(!strcmp(command_name,"xor")) { result = xor(inputs,num_inputs,input_length); } else if(!strcmp(command_name,"xnor")) { result = xnor(inputs,num_inputs,input_length); } else if(!strcmp(command_name,"and")) { result = and(inputs,num_inputs,input_length); } else if(!strcmp(command_name,"nand")) { result = nand(inputs,num_inputs,input_length); } else if(!strcmp(command_name,"not")) { result = not(inputs,num_inputs,input_length); } else { error(); } free_mem(inputs,num_inputs); printf("%s",result); free(result); return 0; }