1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include <stdio.h>
#include <stdlib.h>
#include "my.h"
int check_error(char *str1, char *str2)
{
int i;
i = 0;
while (str1[i] != '\0')
{
if (my_isnum(str1[i]) == 0)
return (1);
else
i = i + 1;
}
i = 0;
while (str2[i] != '\0')
{
if (my_isnum(str2[i]) == 0)
return (1);
else
i = i + 1;
}
return (0);
}
int addition_2(int a, int b, char *result, char *str2, char *str1)
{
result = malloc(a + 2);
if (result == NULL)
return (1);
result[a] = '\0';
while (a != 0)
{
if (b <= 0)
str2[b - 1] = '0';
if (result[a - 1] = str2[b - 1] + str1[a - 1] - '0' > '9')
{
printf("a = "); // mes printf sont la pour m'aider a visualiser
printf("%d ", a);
printf("b = ");
printf("%d ", b);
printf("str1 = ");
printf("%s ", str1);
printf("str2 = ");
printf("%s ", str2);
if (a == 1 && b == 1) // ici j'ai essayer d'afficher le 1 sans trop de succès.
{
result[a - 1] = str2[b - 1] + str1[a - 1];
}
result[a - 1] = (str2[b - 1] + str1[a - 1] - '0' - 10);
str1[a - 2] = str1[a - 2] + 1;
}
else
result[a - 1] = str2[b - 1] + str1[a - 1] - 48;
a = a - 1;
b = b - 1;
}
my_putstr(result);
my_putchar('\n');
free(result);
return (0);
}
int addition(char *str1, char *str2)
{
int a;
int b;
char *str_tp1;
char *str_tp2;
char *result;
a = my_strlen(str1);
b = my_strlen(str2);
if (b > a)
{
my_swap(&a, &b);
str_tp1 = malloc(a * sizeof(char) + 1);
if (str_tp1 == NULL)
return (1);
str_tp2 = malloc(b * sizeof(char) + 1);
if (str_tp2 == NULL)
return (1);
str_tp1 = str1;
str_tp2 = str2;
str1 = str_tp2;
str2 = str_tp1;
}
addition_2(a, b, result, str2, str1);
}
int main(int argc, char **argv)
{
if (argc != 3)
{
write(2, "Bad enter, retry\n", 24);a
return (0);
}
if (check_error(argv[1], argv[2]) == 1)
{
write(2, "Bad enter, only numbers retry\n", 32);
return (0);
}
addition(argv[1], argv[2]);
return (0);
} |
Partager