divider.c 1.18 KB
#include <stdio.h>
#include <string.h>
#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)
{
	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 = bin2udec(op);
	b = bin2udec(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;
}