demultiplexor.c 813 Bytes
#include <stdio.h>
#include <string.h>
#include "auxiliar.h"

int main(int argc, char **argv)
{
	int i;
	int j;
	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 = strlen(inputs[0]);
	int selected = bin2dec(inputs[1]);
	int num_outputs = 0x1 << strlen(inputs[1]);
	for(i = 0; i < num_outputs; i++)
	{
		if(i == selected)
		{
			printf("%s",inputs[0]);
		}
		else
		{
			for(j = 0; j < input_length; j++)
			{
				printf("0");
			}
		}
		if(i != (num_outputs-1))
		{
			printf(",");
		}
	}
	free_mem(inputs,num_inputs);
	return 0;
}