I am trying to represent a string that holds any number to infinity. This number must be an integer and return its value as a result. Right now, my goal is to do this representation but for division only. So lets say we have 125 as our numerator, and 5 as our denominator. The answer should be 25 right? However, I need to represent this number as a linked list of strings. Currently the way to approach this problem is doing "division by subtracting" because I can iterate the number over and over without worrying its remainder and the size of the number. The iteration must be with a base number of 10, meaning it has to read '0' to '9'.
My division consists with operands A and B, and incrementing counter (which will be end result), and subtraction these operands. Here is the logic/pseudocode that I found from Wikipedia and my partner:
while N >= D do
then N := N - D
counter++
end
return N
In our previous example, the 125/5 will result a counter of 25 because we are iterating our N every time and subtracting it with 5. Once N is not greater or equal to D, then we can finally finish and return our result according to the counter. This counter will be placed in a new list and added to our current list to return its final value.
I had to make separate functions to come with the logic, but I think something is not right structured.
Linked lists
typedef struct s_list
{
void *content;
size_t content_size;
struct s_list *next;
} t_list;
typedef struct s_BigNumber
{
char *base;
int base_size;
char *exp;
int exp_size;
} t_BigNumber;
List add function
void lstadd(t_list **alst, t_list *new)
{
if (new)
{
new->next = *alst;
*alst = new;
}
}
Incrementor
t_list *increment(t_bistro *bistro, t_list *oper)
{
t_list *incr;
t_list *counter;
incr = NULL;
lstadd(&incr, ft_lstnew(&(bistro->base[1]), 1));
counter = addition(bistro, oper, incr);
del_num(oper);
free(incr->content);
free(incr);
return (counter);
}
My subtraction function is pretty big and requires other functions to works, so I will skip the code of it.
This is my actual code with everything mentioned from above:
Division
t_list *division(t_list *big, t_list *oper1, t_list *oper2)
{
t_list *result;
t_list *tmp;
result = NULL;
lstadd(&result, lstnew(&(big->base[0]), 1)); //Creates a new list with the result to the first index of the new list
while (oper2->content >= oper1->content)
{
tmp = result;
result = subtract(big, result, oper1);
del_num(tmp);
oper2 = increment(big, oper2);
}
return (result);
}
Printing the value
void digit_printer(char *num)
{
char c;
c = *num;
putchar(c);
}
void digitizer(t_list *num)
{
if (num->next)
digitizer(num->next);
digit_printer(num->content);
}
Main
int main()
{
t_list *op1;
t_list *op2;
t_list *result;
t_BigNumber *big;
char *str = "125";
char *str1 = "5";
op1 = NULL;
op2 = NULL;
while (*str)
{
lstadd(&op1, lstnew(str, 1));
str++;
}
while (*str1)
{
lstadd(&op2, lstnew(str1, 1));
str1++;
}
big = malloc(sizeof(t_BigNumber));
big->base = "0123456789";
big->base_size = 10;
result = division(big, op1, op2);
digitizer(result);
printf("\n");
}
Expected output: 25
I'm looking for a open discussion and willing to give suggestions. It is up to you to come with a solution. Some guidance and examples will help me :). Sorry for my English overall.