How would one go about doing this? It's like doing second grade multiplication, like:
Code:
1234
11
____
1234
12340
+
13574
like that. I'm trying alot of different things but ive managed to come up with some really buggy code. When responding, don't just give me code, but suggestions on what to do change/do. Heres my function. It makes use of push, pop, etc.
Code:
stack multiply(stack stackOne, stack stackTwo)
{
stack copyStack;
copyStack.top = -1;
stack carry;
carry.top = -1;
stack runOne;
runOne.top = -1;
int x, y, limitOne, limitTwo, lhs, rhs, test, quotient, carryInt, total = 0, digit;
int limitThree, z;
limitOne = stackOne.top;
limitTwo = stackTwo.top;
for(x = 0; x <= limitTwo; x++)
{
copyStack = stackOne;
pop(&stackTwo, &rhs);
for(y = 0; y <= limitOne; y++)
{
if(test = isEmpty(©Stack) != 1)
lhs = 1;
else
pop(©Stack, &lhs);
if(test = isEmpty(&carry) != 1)
carryInt = 0;
else
pop(&carry, &carryInt);
quotient = (lhs * rhs) + carryInt;
if(quotient > 10)
{
digit = quotient % 10;
push(&runOne, digit);
carryInt = (quotient - digit) / 10;
push(&carry, carryInt);
}
else if(quotient == 10)
{
push(&runOne, 0);
push(&carry, 1);
}
else if(quotient < 10)
{
push(&runOne, quotient);
}
if(y == stackTwo.top-1)
{
limitThree = carry.top;
for(z = 0; z <= limitThree; z++)
pop(&carry, &carryInt);
push(&runOne, carryInt);
}
}
}
return runOne;
}