Thread: longer numbers?
View Single Post
Old 08-28-2003, 06:40 PM   #13
welcor
Member
 
Join Date: Apr 2002
Location: Aarhus, Denmark, Europe
Posts: 59
welcor is on a distinguished road
Send a message via ICQ to welcor
The large number calculator I mentioned in the earlier post, worked on strings (arrays of char), dynamically allocated to fit the size wanted - pseudo code:
[code]
string _add(string num1, string num2)
{
int i, j, s = 0;
string result, t1, t2;

if (strlen(num1) < strlen(num2)) {
t1 = num2; /* longest */
i = strlen(num2);
t2 = num1; /* shortest */
j = strlen(num1);
} else {
t1 = num1; /* longest */
i = strlen(num1);
t2 = num2; /* shortest */
j = strlen(num2);
}

for (;i >= 0 || s;i--, j--) {
if (j>= 0)
s = t1[j] + t2[i] + s;
else if (i >= 0)
s = t2[i] + s;

result[i] = s % 10;
s = s / 10; /*transfer extra */
}

return result;
}
[/quote]

This method (or something similar - the above is mailer code) can be used for all wanted calculations. To output this string afterwards, just print it with a %s conversion.

Welcor
welcor is offline   Reply With Quote