Commit 131e732db87bbb413e2f1c4ac222b30f03e77887
1 parent
8a0a8236
--no commit message
Showing
9 changed files
with
218 additions
and
24 deletions
common/auxiliar.c
@@ -120,4 +120,44 @@ void normalize(char **inputs, int num_inputs) | @@ -120,4 +120,44 @@ void normalize(char **inputs, int num_inputs) | ||
120 | } | 120 | } |
121 | } | 121 | } |
122 | } | 122 | } |
123 | +} | ||
124 | + | ||
125 | +int bin2dec(char *bin) | ||
126 | +{ | ||
127 | + int i; | ||
128 | + int num = 0; | ||
129 | + int length = strlen(bin); | ||
130 | + int pos = length - 1; | ||
131 | + char *aux = (char*) malloc(sizeof(char) * length); | ||
132 | + strcpy(aux,bin); | ||
133 | + normalize(&aux,1); | ||
134 | + for(i = 0; i < length; i++) | ||
135 | + { | ||
136 | + if(aux[pos--]) | ||
137 | + { | ||
138 | + num += pow(2,i); | ||
139 | + } | ||
140 | + } | ||
141 | + free(aux); | ||
142 | + return num; | ||
143 | +} | ||
144 | + | ||
145 | +char* dec2bin(int dec, int length) | ||
146 | +{ | ||
147 | + char *bin = (char*) malloc(sizeof(char) * length + 1); | ||
148 | + int pos = length-1; | ||
149 | + int i; | ||
150 | + for(i = 0; i < length; i++) | ||
151 | + { | ||
152 | + if(dec & 0x1) | ||
153 | + { | ||
154 | + bin[pos--] = '1'; | ||
155 | + } | ||
156 | + else | ||
157 | + { | ||
158 | + bin[pos--] = '0'; | ||
159 | + } | ||
160 | + dec >>= 1; | ||
161 | + } | ||
162 | + return bin; | ||
123 | } | 163 | } |
124 | \ No newline at end of file | 164 | \ No newline at end of file |
common/auxiliar.h
@@ -13,3 +13,5 @@ void error(); | @@ -13,3 +13,5 @@ void error(); | ||
13 | void free_mem(char **inputs, int num_inputs); | 13 | void free_mem(char **inputs, int num_inputs); |
14 | 14 | ||
15 | void normalize(char **inputs, int num_inputs); | 15 | void normalize(char **inputs, int num_inputs); |
16 | +int bin2dec(char *bin); | ||
17 | +char* dec2bin(int dec, int length); | ||
16 | \ No newline at end of file | 18 | \ No newline at end of file |
plexers/Makefile
1 | -TARGETS = multiplexor | 1 | +TARGETS = multiplexor demultiplexor encoder decoder |
2 | 2 | ||
3 | .PHONY: all clean | 3 | .PHONY: all clean |
4 | 4 | ||
@@ -9,3 +9,11 @@ clean: | @@ -9,3 +9,11 @@ clean: | ||
9 | 9 | ||
10 | multiplexor: | 10 | multiplexor: |
11 | gcc -I../common/ ../common/auxiliar.c multiplexor.c -o multiplexor -lm | 11 | gcc -I../common/ ../common/auxiliar.c multiplexor.c -o multiplexor -lm |
12 | + | ||
13 | +demultiplexor: | ||
14 | + gcc -I../common/ ../common/auxiliar.c demultiplexor.c -o demultiplexor -lm | ||
15 | + | ||
16 | +encoder: | ||
17 | + gcc -I../common/ ../common/auxiliar.c encoder.c -o encoder -lm | ||
18 | +decoder: | ||
19 | + gcc -I../common/ ../common/auxiliar.c decoder.c -o decoder -lm |
plexers/decoder.c
0 โ 100644
1 | +#include <stdio.h> | ||
2 | +#include <string.h> | ||
3 | +#include "auxiliar.h" | ||
4 | +#include <math.h> | ||
5 | + | ||
6 | +int main(int argc, char **argv) | ||
7 | +{ | ||
8 | + int i; | ||
9 | + if(argc != 2) | ||
10 | + { | ||
11 | + error(); | ||
12 | + } | ||
13 | + char *input = argv[1]; | ||
14 | + if(!strcmp(input,"error")) | ||
15 | + { | ||
16 | + error(); | ||
17 | + } | ||
18 | + char **inputs = comma_separate(input); | ||
19 | + if(inputs == NULL) | ||
20 | + { | ||
21 | + error(); | ||
22 | + } | ||
23 | + int num_inputs = num_occurrences(input,','); | ||
24 | + if(num_inputs != 1) | ||
25 | + { | ||
26 | + error(); | ||
27 | + } | ||
28 | + int selected = bin2dec(inputs[0]); | ||
29 | + int num_outputs = pow(2,strlen(inputs[0])); | ||
30 | + for(i = 0; i < num_outputs; i++) | ||
31 | + { | ||
32 | + if(i == selected) | ||
33 | + { | ||
34 | + printf("1"); | ||
35 | + } | ||
36 | + else | ||
37 | + { | ||
38 | + printf("0"); | ||
39 | + } | ||
40 | + if(i != (num_outputs-1)) | ||
41 | + { | ||
42 | + printf(","); | ||
43 | + } | ||
44 | + } | ||
45 | + free_mem(inputs,num_inputs); | ||
46 | + return 0; | ||
47 | +} |
plexers/demultiplexor.c
0 โ 100644
1 | +#include <stdio.h> | ||
2 | +#include <string.h> | ||
3 | +#include "auxiliar.h" | ||
4 | +#include <math.h> | ||
5 | + | ||
6 | +int main(int argc, char **argv) | ||
7 | +{ | ||
8 | + int i; | ||
9 | + int j; | ||
10 | + if(argc != 2) | ||
11 | + { | ||
12 | + error(); | ||
13 | + } | ||
14 | + char *input = argv[1]; | ||
15 | + if(!strcmp(input,"error")) | ||
16 | + { | ||
17 | + error(); | ||
18 | + } | ||
19 | + char **inputs = comma_separate(input); | ||
20 | + if(inputs == NULL) | ||
21 | + { | ||
22 | + error(); | ||
23 | + } | ||
24 | + int num_inputs = num_occurrences(input,','); | ||
25 | + if(num_inputs != 2) | ||
26 | + { | ||
27 | + error(); | ||
28 | + } | ||
29 | + int input_length = strlen(inputs[0]); | ||
30 | + int selected = bin2dec(inputs[1]); | ||
31 | + int num_outputs = pow(2,strlen(inputs[1])); | ||
32 | + for(i = 0; i < num_outputs; i++) | ||
33 | + { | ||
34 | + if(i == selected) | ||
35 | + { | ||
36 | + printf("%s",inputs[0]); | ||
37 | + } | ||
38 | + else | ||
39 | + { | ||
40 | + for(j = 0; j < input_length; j++) | ||
41 | + { | ||
42 | + printf("0"); | ||
43 | + } | ||
44 | + } | ||
45 | + if(i != (num_outputs-1)) | ||
46 | + { | ||
47 | + printf(","); | ||
48 | + } | ||
49 | + } | ||
50 | + free_mem(inputs,num_inputs); | ||
51 | + return 0; | ||
52 | +} |
plexers/encoder.c
0 โ 100644
1 | +#include <stdio.h> | ||
2 | +#include <string.h> | ||
3 | +#include "auxiliar.h" | ||
4 | +#include <math.h> | ||
5 | + | ||
6 | +int check_length(char **inputs, uint8_t num_inputs) | ||
7 | +{ | ||
8 | + int i; | ||
9 | + for(i = 0; i < num_inputs; i++) | ||
10 | + { | ||
11 | + if(strlen(inputs[i]) != 1) | ||
12 | + { | ||
13 | + return -1; | ||
14 | + } | ||
15 | + } | ||
16 | + int max_value = 1; | ||
17 | + i = 0; | ||
18 | + while(max_value < num_inputs) | ||
19 | + { | ||
20 | + max_value = pow(2,++i); | ||
21 | + } | ||
22 | + return i; | ||
23 | +} | ||
24 | + | ||
25 | +int main(int argc, char **argv) | ||
26 | +{ | ||
27 | + int i; | ||
28 | + int pos; | ||
29 | + if(argc != 2) | ||
30 | + { | ||
31 | + error(); | ||
32 | + } | ||
33 | + char *input = argv[1]; | ||
34 | + if(!strcmp(input,"error")) | ||
35 | + { | ||
36 | + error(); | ||
37 | + } | ||
38 | + char **inputs = comma_separate(input); | ||
39 | + if(inputs == NULL) | ||
40 | + { | ||
41 | + error(); | ||
42 | + } | ||
43 | + int num_inputs = num_occurrences(input,','); | ||
44 | + pos = num_inputs - 1; | ||
45 | + int sel_length = check_length(inputs,num_inputs); | ||
46 | + if(sel_length == -1) | ||
47 | + { | ||
48 | + error(); | ||
49 | + } | ||
50 | + int selected = -1; | ||
51 | + for(i = 0; i < num_inputs; i++) | ||
52 | + { | ||
53 | + if(inputs[i][0] == '1') | ||
54 | + { | ||
55 | + selected = i; | ||
56 | + break; | ||
57 | + } | ||
58 | + } | ||
59 | + if(selected == -1) | ||
60 | + { | ||
61 | + error(); | ||
62 | + } | ||
63 | + char *result = dec2bin(selected,sel_length); | ||
64 | + printf("%s",result); | ||
65 | + free(result); | ||
66 | + free_mem(inputs,num_inputs); | ||
67 | + return 0; | ||
68 | +} |
plexers/multiplexor deleted
No preview for this file type
plexers/multiplexor.c
@@ -3,26 +3,6 @@ | @@ -3,26 +3,6 @@ | ||
3 | #include "auxiliar.h" | 3 | #include "auxiliar.h" |
4 | #include <math.h> | 4 | #include <math.h> |
5 | 5 | ||
6 | -int bin2dec(char *bin) | ||
7 | -{ | ||
8 | - int i; | ||
9 | - int num = 0; | ||
10 | - int length = strlen(bin); | ||
11 | - int pos = length - 1; | ||
12 | - char *aux = (char*) malloc(sizeof(char) * length); | ||
13 | - strcpy(aux,bin); | ||
14 | - normalize(&aux,1); | ||
15 | - for(i = 0; i < length; i++) | ||
16 | - { | ||
17 | - if(aux[pos--]) | ||
18 | - { | ||
19 | - num += pow(2,i); | ||
20 | - } | ||
21 | - } | ||
22 | - free(aux); | ||
23 | - return num; | ||
24 | -} | ||
25 | - | ||
26 | int check_length(char **inputs, uint8_t num_inputs) | 6 | int check_length(char **inputs, uint8_t num_inputs) |
27 | { | 7 | { |
28 | int i; | 8 | int i; |