Blame view

arith/comparator.c 1.21 KB
Imanol-Mikel Barba Sabariego authored
1
2
3
4
#include <stdio.h>
#include <string.h>
#include "auxiliar.h"
Imanol-Mikel Barba Sabariego authored
5
6
7
#define SIGNED 	 1
#define UNSIGNED 0
Imanol-Mikel Barba Sabariego authored
8
9
10
11
12
13
14
15
16
17
18
19
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)
{
Imanol-Mikel Barba Sabariego authored
20
21
	uint8_t signedness;
	if(argc != 3)
Imanol-Mikel Barba Sabariego authored
22
23
24
25
26
27
28
29
	{
		error();
	}
	char *input = argv[1];
	if(!strcmp(input,"error"))
	{
		error();
	}
Imanol-Mikel Barba Sabariego authored
30
31
32
33
34
35
36
37
38
39
40
41
	if(!strcmp("unsigned", argv[2]))
	{
		signedness = UNSIGNED;
	}
	else if(!strcmp("signed", argv[2]))
	{
		signedness = SIGNED;
	}
	else
	{
		error();
	}
Imanol-Mikel Barba Sabariego authored
42
43
44
45
46
47
48
49
50
51
52
53
	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;
Imanol-Mikel Barba Sabariego authored
54
55
56
57
58
59
60
61
62
63
	if(signedness == UNSIGNED)
	{
		a = bin2udec(inputs[0]);
		b = bin2udec(inputs[1]);
	}
	else if(signedness == SIGNED)
	{
		a = bin2dec(inputs[0]);
		b = bin2dec(inputs[1]);
	}
Imanol-Mikel Barba Sabariego authored
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	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;
}