diff --git a/arith/Makefile b/arith/Makefile index b752a6e..c650405 100644 --- a/arith/Makefile +++ b/arith/Makefile @@ -1,4 +1,4 @@ -TARGETS = adder substractor +TARGETS = adder substractor multiplier divider negator comparator .PHONY: all clean @@ -10,4 +10,12 @@ clean: adder: gcc -I../common/ ../common/auxiliar.c adder.c -o adder substractor: - gcc -I../common/ ../common/auxiliar.c substractor.c -o substractor \ No newline at end of file + gcc -I../common/ ../common/auxiliar.c substractor.c -o substractor +multiplier: + gcc -I../common/ ../common/auxiliar.c multiplier.c -o multiplier +divider: + gcc -I../common/ ../common/auxiliar.c divider.c -o divider +negator: + gcc -I../common/ ../common/auxiliar.c negator.c -o negator +comparator: + gcc -I../common/ ../common/auxiliar.c comparator.c -o comparator \ No newline at end of file diff --git a/arith/comparator.c b/arith/comparator.c new file mode 100644 index 0000000..4b6108f --- /dev/null +++ b/arith/comparator.c @@ -0,0 +1,56 @@ +#include +#include +#include "auxiliar.h" + +int check_length(char **inputs, uint8_t num_inputs) +{ + int input_length = strlen(inputs[0]); + if(input_length != strlen(inputs[1])) + { + return -1; + } + return input_length; +} + +int main(int argc, char **argv) +{ + int i; + int pos; + 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,','); + if(num_inputs != 2) + { + error(); + } + int input_length = check_length(inputs,num_inputs); + int a,b; + a = bin2dec(inputs[0]); + b = bin2dec(inputs[1]); + if(a > b) + { + printf("1,0,0"); + } + else if(a < b) + { + printf("0,0,1"); + } + else if(a == b) + { + printf("0,1,0"); + } + free_mem(inputs,num_inputs); + return 0; +} diff --git a/arith/divider.c b/arith/divider.c new file mode 100644 index 0000000..163d93f --- /dev/null +++ b/arith/divider.c @@ -0,0 +1,66 @@ +#include +#include +#include "auxiliar.h" + +int check_length(char **inputs, uint8_t num_inputs) +{ + int input_length = strlen(inputs[0]); + int i; + for(i = 1; i < 3; i++) + { + if(input_length != strlen(inputs[i])) + { + return -1; + } + } + return input_length; +} + +int main(int argc, char **argv) +{ + int i; + int pos; + 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,','); + if(num_inputs != 3) + { + error(); + } + int input_length = check_length(inputs,num_inputs); + if(input_length == -1) + { + error(); + } + int a,b,c,rem; + char *upper = inputs[0]; + char *lower = inputs[1]; + char *op = (char*) malloc(sizeof(char)*(input_length+1)); + memset(op,0x00,input_length+1); + strcpy(op,upper); + strcat(op,lower); + a = bin2dec(op); + b = bin2dec(inputs[2]); + c = a/b; + rem = a%b; + char *result = dec2bin(c,input_length); + char *rem_bin = dec2bin(rem,input_length); + printf("%s,%s",result,rem_bin); + free(result); + free(rem_bin); + free(op); + free_mem(inputs,num_inputs); + return 0; +} diff --git a/arith/multiplier.c b/arith/multiplier.c new file mode 100644 index 0000000..487d40f --- /dev/null +++ b/arith/multiplier.c @@ -0,0 +1,57 @@ +#include +#include +#include "auxiliar.h" + +int check_length(char **inputs, uint8_t num_inputs) +{ + int input_length = strlen(inputs[0]); + int i; + for(i = 1; i < 3; i++) + { + if(input_length != strlen(inputs[i])) + { + return -1; + } + } + return input_length; +} + +int main(int argc, char **argv) +{ + int i; + int pos; + 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,','); + if(num_inputs != 3) + { + error(); + } + int input_length = check_length(inputs,num_inputs); + if(input_length == -1) + { + error(); + } + int a,b,carry_in,c; + a = bin2dec(inputs[0]); + b = bin2dec(inputs[1]); + carry_in = bin2dec(inputs[2]); + c = (a*b)+carry_in; + char *result = dec2bin(c,input_length*2); + printf("%s,%.*s",result+input_length,input_length,result); + free(result); + free_mem(inputs,num_inputs); + return 0; +} diff --git a/arith/negator.c b/arith/negator.c new file mode 100644 index 0000000..2166863 --- /dev/null +++ b/arith/negator.c @@ -0,0 +1,36 @@ +#include +#include +#include "auxiliar.h" + +int main(int argc, char **argv) +{ + int i; + int pos; + 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,','); + if(num_inputs != 1) + { + error(); + } + int input_length = strlen(inputs[0]); + int num = bin2dec(inputs[0]); + num *= -1; + char *result = dec2bin(num,input_length); + printf("%s",result); + free(result); + free_mem(inputs,num_inputs); + return 0; +} diff --git a/arith/substractor.c b/arith/substractor.c index f3071a2..a89f120 100644 --- a/arith/substractor.c +++ b/arith/substractor.c @@ -14,7 +14,7 @@ int check_length(char **inputs, uint8_t num_inputs) int main(int argc, char **argv) { - /*int i; + int i; int pos; if(argc != 2) { @@ -54,7 +54,5 @@ int main(int argc, char **argv) printf("%s,%c",result,borrow_out); free(result); free_mem(inputs,num_inputs); - return 0;*/ - printf("%d",bin2dec("11000000")); return 0; } diff --git a/common/auxiliar.c b/common/auxiliar.c index b6cb383..c2faea2 100644 --- a/common/auxiliar.c +++ b/common/auxiliar.c @@ -60,7 +60,7 @@ char** comma_separate(char *input) return NULL; } char **inputs; - inputs = malloc(sizeof(char*) * (input_num+1)); + inputs = (char**) malloc(sizeof(char*) * (input_num+1)); for(i = 0; i < input_num; i++) { next_pos += next_occurrence(input+pos,','); @@ -130,6 +130,7 @@ int bin2dec(char *bin) int length = strlen(bin); int pos = length - 1; char *aux = (char*) malloc(sizeof(char) * (length+1)); + memset(aux,0x00,length+1); strcpy(aux,bin); if(aux[0] == '1') { @@ -152,6 +153,7 @@ int bin2dec(char *bin) char* dec2bin(int dec, int length) { char *bin = (char*) malloc(sizeof(char) * (length + 1)); + memset(bin,0x00,length+1); int pos = length-1; int i; for(i = 0; i < length; i++) @@ -172,7 +174,8 @@ char* dec2bin(int dec, int length) void expand_input(char **input, int required_length) { - char *new_input = malloc(sizeof(char)*(required_length + 1)); + char *new_input = (char*) malloc(sizeof(char)*(required_length + 1)); + memset(new_input,0x00,required_length + 1); memset(new_input,'0',required_length+1); int offset = required_length - strlen(*input); strcpy(new_input + offset,*input); diff --git a/logic_gate/logic_gate.c b/logic_gate/logic_gate.c index d82fae4..862be8e 100644 --- a/logic_gate/logic_gate.c +++ b/logic_gate/logic_gate.c @@ -21,7 +21,7 @@ char* or(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * (input_length + 1)); + char *result = (char*) malloc(sizeof(char) * (input_length + 1)); memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { @@ -46,7 +46,7 @@ char* nor(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * (input_length + 1)); + char *result = (char*) malloc(sizeof(char) * (input_length + 1)); memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { @@ -71,7 +71,7 @@ char* xor(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * (input_length + 1)); + char *result = (char*) malloc(sizeof(char) * (input_length + 1)); memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { @@ -95,7 +95,7 @@ char* xnor(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * (input_length + 1)); + char *result = (char*) malloc(sizeof(char) * (input_length + 1)); memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { @@ -119,7 +119,7 @@ char* and(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * (input_length + 1)); + char *result = (char*) malloc(sizeof(char) * (input_length + 1)); memset(result, 0x01, input_length+1); for(i = 0; i < input_length; i++) { @@ -143,7 +143,7 @@ char* nand(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * (input_length + 1)); + char *result = (char*) malloc(sizeof(char) * (input_length + 1)); memset(result, 0x01, input_length+1); for(i = 0; i < input_length; i++) { @@ -167,7 +167,7 @@ char* not(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * (input_length + 1)); + char *result = (char*) malloc(sizeof(char) * (input_length + 1)); memset(result, 0x01, input_length+1); for(i = 0; i < input_length; i++) { diff --git a/plexers/bit_selector.c b/plexers/bit_selector.c index 44b8285..ad48c45 100644 --- a/plexers/bit_selector.c +++ b/plexers/bit_selector.c @@ -56,7 +56,7 @@ int main(int argc, char **argv) { expand_input(&inputs[0],required_length); } - char *result = malloc(sizeof(char) * (output_length + 1)); + char *result = (char*) malloc(sizeof(char) * (output_length + 1)); memset(result,0x00,output_length+1); int selected = bin2dec(inputs[1]); int pos = 0; diff --git a/todo b/todo index c593e23..960bb4f 100644 --- a/todo +++ b/todo @@ -1,9 +1,3 @@ -adder -substractor -multiplier -divider -negator -comparator shifter bit adder bit finder