Blame view

logic_gate/logic_gate.c 4.4 KB
Imanol-Mikel Barba Sabariego authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#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;
Imanol-Mikel Barba Sabariego authored
24
	char *result = (char*) malloc(sizeof(char) * (input_length + 1));
Imanol-Mikel Barba Sabariego authored
25
	memset(result, 0x00, input_length+1);
Imanol-Mikel Barba Sabariego authored
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
	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;
Imanol-Mikel Barba Sabariego authored
49
	char *result = (char*) malloc(sizeof(char) * (input_length + 1));
Imanol-Mikel Barba Sabariego authored
50
	memset(result, 0x00, input_length+1);
Imanol-Mikel Barba Sabariego authored
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	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;
Imanol-Mikel Barba Sabariego authored
74
	char *result = (char*) malloc(sizeof(char) * (input_length + 1));
Imanol-Mikel Barba Sabariego authored
75
	memset(result, 0x00, input_length+1);
Imanol-Mikel Barba Sabariego authored
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
	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;
Imanol-Mikel Barba Sabariego authored
98
	char *result = (char*) malloc(sizeof(char) * (input_length + 1));
Imanol-Mikel Barba Sabariego authored
99
	memset(result, 0x00, input_length+1);
Imanol-Mikel Barba Sabariego authored
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	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;
Imanol-Mikel Barba Sabariego authored
122
	char *result = (char*) malloc(sizeof(char) * (input_length + 1));
Imanol-Mikel Barba Sabariego authored
123
	memset(result, 0x01, input_length+1);
Imanol-Mikel Barba Sabariego authored
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	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;
Imanol-Mikel Barba Sabariego authored
146
	char *result = (char*) malloc(sizeof(char) * (input_length + 1));
Imanol-Mikel Barba Sabariego authored
147
	memset(result, 0x01, input_length+1);
Imanol-Mikel Barba Sabariego authored
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
	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;
Imanol-Mikel Barba Sabariego authored
170
	char *result = (char*) malloc(sizeof(char) * (input_length + 1));
Imanol-Mikel Barba Sabariego authored
171
	memset(result, 0x01, input_length+1);
Imanol-Mikel Barba Sabariego authored
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
	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;
}