...multiplication using stacks

• 10-08-2006
iiwhitexb0iii
...multiplication using stacks
this segment is proving to be the death of me...

i'm required to scan digits from an input file which are to long to record as int variables.

so i record them as strings and convert them to ints before pushing them onto stacks.

i have a function that adds the strings and reports the answer just fine. [add(num, x, y, z) where x and y represents the two stacks to add and z is the stack where the answer is to be stored.]

my problem lies with multiplication, for instance 75 * 36. i have two temp stacks and one answer stack for multiplication. the two temp stacks print out what they're supposed to have, but when i go to add them, it mucks up. here is my multiplication function.

Code:

```while(num[0].top != Empty && num[1].top != Empty)        {                 digit2 = pop(num, 1);                 while(num[0].top != Empty)        {                         digit = pop(num, 0);                                                 product = (digit * digit2) + carry;                                                 if(product >= 10)        {                                 carry = product / 10;                                 product = product % 10;                                 push(num, product, 3);                                 //printf("push 3\t%d\n", product);                         }                                                 else        {                                 carry = 0;                                 push(num, product, 3);                                 //printf("push 3\t%d\n", product);                         }                 }                 zeros++;                                 if(carry != 0)        {                         push(num, carry, 3);                         //printf("push 3\t%d\n", carry);                         carry = 0;                 }                                 for(i = 0; i < zeros; i++)        {                         push(num, 0, 4);                         //printf("push 4\t0\n");                 }                                 num[0].top = t1;                                 digit2 = pop(num, 1);                                 while(num[0].top != Empty)        {                         digit = pop(num, 0);                                                 product = (digit * digit2) + carry;                                                 if(product >= 10)        {                                 carry = product / 10;                                 product = product % 10;                                 push(num, product, 4);                                 //printf("push 4\t%d\n", product);                         }                                                 else        {                                 carry = 0;                                 push(num, product, 4);                                 //printf("push 4\t%d\n", product);                         }                 }                 if(carry != 0)        {                         push(num, carry, 3);                         //printf("push 4\t%d\n", carry);                         carry = 0;                 }                 zeros++;         }         add(num, 3, 4, 5);```
0 is the stack for the first number to multiply and
1 is the stack for the second number

3 is the first temp stack for multiplication and
4 is the second temp stack

5 is supposed to contain the result.

i hope i've given you enough information to point me in some sort of direction, i'll be monitering this thread so please feel free to ask absolutely any questions you have.