commit
b3ca9337a2
@ -0,0 +1,34 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "convert_base.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *nbr;
|
||||
char *base_from;
|
||||
char *base_to;
|
||||
|
||||
nbr = (char *)malloc(33 * sizeof(char));
|
||||
base_from = (char *)malloc(50 * sizeof(char));
|
||||
base_to = (char *)malloc(50 * sizeof(char));
|
||||
if(!nbr || !base_from || !base_to)
|
||||
{
|
||||
write(2, "Erreur memoire, abandon", 24);
|
||||
return 1;
|
||||
}
|
||||
printf("Rentrez votre nombre\n");
|
||||
scanf("%32s", nbr);
|
||||
fflush(stdin);
|
||||
printf("Rentrez la base d'entrée de votre nombre\n");
|
||||
scanf("%49s", base_from);
|
||||
fflush(stdin);
|
||||
printf("Rentrez la base de sortie désirée\n");
|
||||
scanf("%49s", base_to);
|
||||
fflush(stdin);
|
||||
puts(convert_base(nbr, base_from, base_to));
|
||||
free(nbr);
|
||||
free(base_from);
|
||||
free(base_to);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
#include <stdlib.h>
|
||||
#include "convert_base.h"
|
||||
|
||||
long decode(char *nbr, char *base_from)
|
||||
{
|
||||
int i;
|
||||
long result;
|
||||
|
||||
i = 0;
|
||||
result = 0;
|
||||
while (nbr[i] == 32 || (nbr[i] >= 9 && nbr[i] <= 13)
|
||||
|| nbr[i] == 43 || nbr[i] == 45)
|
||||
i++;
|
||||
while (nbr[i] != '\0' && is_base(nbr[i], base_from))
|
||||
{
|
||||
result = (result * check(base_from)) + unchar(nbr[i], base_from);
|
||||
i++;
|
||||
}
|
||||
if (nbr [i] != '\0' && !is_base(nbr[i], base_from))
|
||||
abort();
|
||||
return (result);
|
||||
}
|
||||
|
||||
void convert(long medium, char *base_to,
|
||||
char *unestring, int *p_i)
|
||||
{
|
||||
long l_base;
|
||||
|
||||
l_base = check(base_to);
|
||||
if (medium >= 0 && medium < l_base)
|
||||
{
|
||||
unestring[*p_i] = base_to[medium];
|
||||
*p_i = *p_i + 1;
|
||||
}
|
||||
if (medium >= l_base)
|
||||
{
|
||||
convert(medium / l_base, base_to, unestring, p_i);
|
||||
convert(medium % l_base, base_to, unestring, p_i);
|
||||
}
|
||||
unestring[*p_i] = '\0';
|
||||
}
|
||||
|
||||
char *jonemar(char *cepafini, char *celafin, int i)
|
||||
{
|
||||
int j;
|
||||
|
||||
j = 0;
|
||||
while (cepafini[j])
|
||||
{
|
||||
celafin[i] = cepafini[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
celafin[i] = '\0';
|
||||
return (celafin);
|
||||
}
|
||||
|
||||
char *recode(long medium, char *base_to, int posi)
|
||||
{
|
||||
char cepafini[50];
|
||||
char *celafin;
|
||||
int i;
|
||||
int *p_i;
|
||||
|
||||
i = 0;
|
||||
p_i = &i;
|
||||
convert(medium, base_to, cepafini, p_i);
|
||||
if (posi < 0)
|
||||
celafin = (char *)malloc(sizeof(char) * (*p_i + 3));
|
||||
else
|
||||
celafin = (char *)malloc(sizeof(char) * (*p_i + 2));
|
||||
if (!(celafin))
|
||||
return (0);
|
||||
if (posi < 0 && medium != 0)
|
||||
{
|
||||
i = 1;
|
||||
celafin[0] = '-';
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
return (jonemar(cepafini, celafin, i));
|
||||
}
|
||||
|
||||
char *convert_base(char *nbr, char *base_from, char *base_to)
|
||||
{
|
||||
if (!(check(base_from)) || !(check(base_to)))
|
||||
return (NULL);
|
||||
return (recode(decode(nbr, base_from), base_to, start(nbr)));
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
long decode(char *nbr, char *base_from);
|
||||
void convert(long medium, char *base_to, char *unestring, int *p_i);
|
||||
char *jonemar(char *cepafini, char *celafin, int i);
|
||||
char *recode(long medium, char *base_to, int posi);
|
||||
char *convert_base(char *nbr, char *base_from, char *base_to);
|
||||
int check(char *base);
|
||||
int is_base(char k, char *base);
|
||||
int unchar(char k, char *base);
|
||||
int start(char *str);
|
@ -0,0 +1,69 @@
|
||||
int check(char *base)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (base[i] != '\0')
|
||||
{
|
||||
j = i + 1;
|
||||
while (base[j] != '\0')
|
||||
{
|
||||
if (base[j] == base[i])
|
||||
return (0);
|
||||
j++;
|
||||
}
|
||||
if (base[i] == 43 || base[i] == 45)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
if (i < 2)
|
||||
return (0);
|
||||
return (i);
|
||||
}
|
||||
|
||||
int is_base(char k, char *base)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (base[i])
|
||||
{
|
||||
if (k == base[i])
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int unchar(char k, char *base)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (base[i])
|
||||
{
|
||||
if (k == base[i])
|
||||
return (i);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int start(char *str)
|
||||
{
|
||||
int posi;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
posi = 1;
|
||||
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
|
||||
i++;
|
||||
while (str[i] == '-' || str[i] == '+')
|
||||
{
|
||||
if (str[i] == '-')
|
||||
posi = posi * -1;
|
||||
i++;
|
||||
}
|
||||
return (posi);
|
||||
}
|
Loading…
Reference in new issue