Dear Expert
I am trying to solve karastooba multiplication using basic C commands. I have created a function to add two numbers which are stored as string.
void sum(a,b,c) will assign a+b to c.
I was able to get sum for any input when I used this function alone. But when I used this in recursive function i am getting some wrong results. I called this function in consecutive steps, but I am getting result only for one function call. What surprising me is that I got perfect result sometimes when I changed the name of variables. Will name of variable have any influence in program ?
I am doing all these with Code::Block
What I have tried:
#include<stdio.h>
#include<string.h>
void sum(char*,char*,char*);
void prod(char*,char*,char*);
void seperate(char *a,char *b, char *c, char *d, char *x, char *y);
void powercalculation(char*,char);
void difference(char*, char*,char*);
unsigned char pub=0;
int main()
{
char x[50],y[50],z[50];
gets(x);
gets(y);
prod(x,y,z);
puts(z);
return(0);
}
void prod(char *x, char *y, char *z)
{
char a[25],b[25],c[25],d[25],n;
char temp1[50],temp2[50],temp3[50],temp4[50],ac[50],bd[50];
if(strlen(x)>strlen(y))
n=strlen(x);
else
n=strlen(y);
if(strlen(x)==1||strlen(y)==1) {
strcpy(temp1,"0");
strcpy(temp2,"0");
if(strlen(x)==1) {
temp3[0]=x[0];
for(;temp3[0]>'0';temp3[0]--)
{
sum(temp1,y,temp2);
strcpy(temp1,temp2);
}
}
else {
temp3[0]=y[0];
for(;temp3[0]>'0';temp3[0]--)
{
sum(temp1,x,temp2);
strcpy(temp1,temp2);
}
}
strcpy(z,temp2); return;
}
else
{
seperate(a,b,c,d,x,y); sum(a,b,temp1); sum(c,d,temp2); puts(temp1);
puts(temp2);
prod(temp1,temp2,temp3); prod(a,c,ac); prod(b,d,bd); difference(temp3,ac,temp1); difference(temp1,bd,temp3); powercalculation(temp2,(2*n)); strcat(ac,temp2); sum(ac,bd,temp2); powercalculation(temp3,(n));
if(n%2==0) strcat(temp1,temp3);
else
prod(temp1,temp3,temp4); sum(temp4,temp2,z);
return;
}
}
void difference(char *a, char *b, char *c) {
char temp[10],temp1,temp2,i,k;
temp1=strlen(a)-1;
temp2=strlen(b)-1;
for(i=0;temp2>=0;temp2--,temp1--,i++) {
if(a[temp1]>=b[temp2])
temp[i]=a[temp1]-b[temp2]+48;
else {
temp[i]=a[temp1]+10-b[temp2]+48;
if(a[temp1-1]>48)
a[temp1-1]--;
else
{
for(k=temp1-2;a[k]=='0';k--);
a[k]--;
for(k+=1;k<=temp1-1;k++)
a[k]='9';
}
}
}
for(;temp1>=0;temp1--,i++) temp[i]=a[temp1];
for(i-=1;temp[i]=='0';i--); for(k=0;i>=0;k++,i--) c[k]=temp[i];
if(k>0)
c[k]='\0';
else
strcpy(c,"0");
return;
}
void powercalculation(char *a, char n) {
if(n%2==0) {
strcpy(a,"00000000000000000000000000000000000000000000000");
n/=2;
a[n+1]='\0';
}
else {
strcpy(a,"31622776601683793319988935444327"); n/=2;
a[n+1]='\0';
}
return;
}
void seperate(char *a,char *b, char *c, char *d, char *x, char *y)
{
char n,l1,l2,i,j,k,m,flag;
l1=strlen(x);
l2=strlen(y);
if(l1>l2)
n=l1;
else
n=l2;
if(l1==l2) if(n%2==0) {
a[n/2]=b[n/2]=c[n/2]=d[n/2]='\0';
for(i=n-1,j=(n/2)-1;i>=(n/2);i--,j--) {
a[j]=x[i-(n/2)];
b[j]=x[i];
c[j]=y[i-(n/2)];
d[j]=y[i];
}
}
else {
a[n/2]=c[n/2]='\0';
b[(n/2)+1]=d[(n/2)+1]='\0';
b[n/2]=x[n-1];
d[n/2]=y[n-1];
for(i=n-2,j=(n/2)-1;j>=0;i--,j--)
{
a[j]=x[i-(n/2)];
b[j]=x[i];
c[j]=y[i-(n/2)];
d[j]=y[i];
}
}
else if(l1>l2) if(l1%2==0) {
k=l2-(l1/2)-1;
a[l1/2]=b[l1/2]=d[l1/2]=c[k+1]='\0';
flag=1;
for(i=l1-1,j=(l1/2)-1,m=l2-1;i>=(l1/2);m--,i--,j--,k--)
{
a[j]=x[i-(l1/2)];
b[j]=x[i];
if(flag==1)
if(l2<=l1/2)
{
flag=0;
strcpy(d,y);
strcpy(c,"0");
}
if(flag==1)
{
d[j]=y[m];
if(k>=0)
c[k]=y[m-(l1/2)];
}
}
}
else {
a[l1/2]='\0';
b[(l1/2)+1]='\0';
b[l1/2]=x[l1-1];
k=l2-(l1/2)-2;
flag=1;
for(i=n-2,j=(n/2)-1,m=l2-2;j>=0;i--,j--,m--,k--)
{
a[j]=x[i-(n/2)];
b[j]=x[i];
if(flag==1)
if(l2<=(l1/2)+1)
{
flag=0;
strcpy(d,y);
strcpy(c,"0");
}
if(flag==1)
{
d[j]=y[m];
if(k>=0)
c[k]=y[m-(l1/2)];
}
}
if(flag==1)
{
d[(l1/2)+1]='\0';
d[l1/2]=y[l2-1];
c[l2-(l1/2)-1]='\0';
}
}
else if(l2%2==0) {
k=l1-(l2/2)-1;
c[l2/2]=d[l2/2]=b[l2/2]=a[k+1]='\0';
flag=1;
for(i=l2-1,j=(l2/2)-1,m=l1-1;i>=(l2/2);m--,i--,j--,k--)
{
c[j]=y[i-(l2/2)];
d[j]=y[i];
if(flag==1)
if(l1<=l2/2)
{
flag=0;
strcpy(b,x);
strcpy(a,"0");
}
if(flag==1)
{
b[j]=x[m];
if(k>=0)
a[k]=x[m-(l2/2)];
}
}
}
else {
c[l2/2]='\0';
d[(l2/2)+1]='\0';
d[l2/2]=y[l2-1];
k=l1-(l2/2)-2;
flag=1;
for(i=l2-2,j=(l2/2)-1,m=l1-2;j>=0;i--,j--,m--,k--)
{
c[j]=y[i-(l2/2)];
d[j]=y[i];
if(flag==1)
if(l1<=(l2/2)+1)
{
flag=0;
strcpy(b,x);
strcpy(a,"0");
}
if(flag==1)
{
b[j]=x[m];
if(k>=0)
a[k]=x[m-(l2/2)];
}
}
if(flag==1)
{
b[(l2/2)+1]='\0';
b[l2/2]=x[l1-1];
a[l1-(l2/2)-1]='\0';
}
}
return;
}
void sum(char *a, char *b, char *c) {
char temp1,temp2,flag=0,i;
temp1=strlen(a)-1; temp2=strlen(b)-1; if(temp1==temp2) {
c[temp1+2]='\0'; for(;temp1>=0;temp1--) {
c[temp1+1]=a[temp1]+b[temp1]-48; if(flag==1) {
c[temp1+1]++; flag=0;
}
if(c[temp1+1]>57) {
c[temp1+1]-=10;
flag=1;
}
}
if(flag==1) c[0]=49;
else
{
for(i=0;i c[i]=c[i+1];
}
}
else if(temp1>temp2) {
c[temp1+2]='\0';
for(;temp2>=0;temp1--,temp2--)
{
c[temp1+1]=a[temp1]+b[temp2]-48;
if(flag==1)
{
c[temp1+1]++;
flag=0;
}
if(c[temp1+1]>57)
{
c[temp1+1]-=10;
flag=1;
}
}
for(;temp1>=0;temp1--) {
c[temp1+1]=a[temp1]+flag;
if(c[temp1+1]>57)
{
c[temp1+1]-=10;
flag=1;
}
else
flag=0;
}
if(flag==1)
c[0]='1';
else
for(i=0;i<strlen(c);i++)>
c[i]=c[i+1];
}
else {
c[temp2+2]='\0';
for(;temp1>=0;temp1--,temp2--)
{
c[temp2+1]=a[temp1]+b[temp2]-48;
if(flag==1)
{
c[temp2+1]++;
flag=0;
}
if(c[temp2+1]>57)
{
c[temp2+1]-=10;
flag=1;
}
}
for(;temp2>=0;temp2--) {
c[temp2+1]=b[temp2]+flag;
if(c[temp2+1]>57)
{
c[temp2+1]-=10;
flag=1;
}
else
flag=0;
}
if(flag==1)
c[0]='1';
else
for(i=0;i<strlen(c);i++)>
c[i]=c[i+1];
}
return;
}