Code:
/*
This code is designed to multiply two numbers as *strings* not integers.
The largest unsigned long long int is supposedly 18,446,744,073,709,551,615, around 2^64 – 1
This should be able to handle larger integers written as strings.
Numbers larger than 18 digits.
It was written with tiny c 927 and is designed for standard c.
Microsoft says max string length in c is 2k ansi standard is 509.
*/
#include <stdlib.h> // used by atoi and malloc
#include <stdio.h> // printf library
#include <string.h> // library manipulates strings.
// subtract 48 do mult and mul[j]='\0';
// 1000 length for a string is 2k.
// ANSI compatibility requires a compiler to accept up to 509
int main(void)
{
char primep1[100]="87875285724157870424814863206395626902470222951900326730049585044816226015610272"; //part1
// these are test numbers1
// these are test numbers2
char mtest1[]="762777762777762777762777762777762777762777762777";
char mtest2[]="438777762377762777762773762777762777762777762777";
char ansreg[500]; // array of the calculated answer without trailing zeros
char anspad[500]; // array of the with added zeros in front to make all entries same length.
char anssum[500]; // array of string2array line to sum string for addition.
char anstot[500]; // array of the string2array totals reversed and then *final*
char ch[10]; // this is used to store integer digits as strings.
/******************************* string2array[count1] = (char *)malloc(strlen(string3array)+1); *******************************/
char *string2array[400]; // REQUIRED number of string LINES array where the ascii values are stored.
int num1=0, num2=0, num3=0, num4=0, num5=0, num6=0;
int i=0, j=0, t=0, v=0, n=0;
int sizestr1=0, sizestr2=0, sizestrtot=0;
int rm1=0, carry1=0;
int mcount1=0;
int length1=0, length2=0;
sizestr2=strlen(mtest2); // length of number to evalute 1
sizestr1=strlen(mtest1); // length of number to evalute 1
sizestrtot=sizestr2+sizestr1; // length of both strings together
/************************************This section does the multiplication ************************************/
for (i=0; i<300; i++ ) {string2array[i] = (char *)malloc(sizestrtot+4);} // initializes 300 string arrays dynamically based on strlen
for ( i=strlen(mtest2)-1; i >= 0; i-- )
{
// printf("\ni:%d\n",i);
for ( n=strlen(mtest1)-1; n >= 0; n-- )
{
// printf("\nn:%d\n",n);
num1=0;
num1=((mtest1[n]-48)*(mtest2[i]-48)+carry1);
// printf("%d*%d", (mtest1[n]-48), (mtest2[i]-48));
// printf("\nnum1:%d\n",num1);
rm1=num1%10;
itoa (rm1, ch, 10);
strcat(ansreg, ch);
// printf("\n rm1:%d\n",rm1);
// printf("\n ansreg:%s", ansreg);
carry1=(num1/10);
// printf("\n carry1:%d\n",carry1);
// printf("\nn:%d\n", n);
if ( n == 0 )
{
itoa (carry1, ch, 10);
strcat(ansreg, ch);
strrev(ansreg); // reverses string with string.h library
// printf("\n ansreg:%s", ansreg);
}
}
for (v=0; v<mcount1; v++) { strcat ( ansreg, "0"); } // this pads the answer with trailing zeros.
// printf("\nmcount1:%d",mcount1);
for ( v=strlen(ansreg); v<= sizestrtot+1; v++) { strcat(anspad, "0"); } // this pads the answer with leading zeros.
strcat(anspad, ansreg);
// printf("\npadded:%s", anspad);
strcpy(string2array[mcount1], anspad);
strcpy(anspad, ""); // erases the string before another loop.
strcpy(ansreg, ""); // erases the string before another loop.
// printf("\nlined:%s",string2array[mcount1]); // test print
carry1=0;
rm1=0;
mcount1++;
}
/***************************************does the addition************************************************************/
for (t=0; t<mcount1; t++)
if ((strlen(string2array[0]))!=(strlen(string2array[t])))
printf("\nERROR incorrect length at %s", string2array[t]);
length2=strlen(string2array[0]);
strcpy(anstot, "");
for (v=length2-1; v>=0; v--)
{
for (t=0; t<mcount1; t++) // test print of the full pointer array.
{
// printf("\nstring2arrayed:%s",string2array[t]);
strcpy(anssum, string2array[t]);
num6=(anssum[v]-48);
// printf("\nnum6:%d",num6);
// printf("\nnum5:%d\n", num5);
// printf("\n%d+%d+%d\n",carry1, num5, num6);
num5=carry1+num5+num6;
carry1=0;
}
rm1=num5%10;
// printf("\n rm1:%d\n",rm1);
carry1=(num5/10);
// printf("\n carry1:%d\n",carry1);
itoa (rm1, ch, 10);
strcat(anstot, ch);
// printf("\n\n%s", anssum);
// printf("\nfirstnum:%d", rm1);
// printf("\n\nANSWER%s", anstot);
num5=0;
}
for (t=0; t<mcount1; t++)
printf("\nstring2arrayed:%s",string2array[t]);
// printf("\n\nANSWER:%s", anstot);
strrev(anstot);
printf("\n\nANSWER:%s", anstot);
strcpy(anstot, "");
num6=0;
free(string2array);
return 0;
}