My code is working fine on my lappy.But it's giving WRONG ANSWER here.I am unable to find out what's the error.Please somebody help me out.I am attaching my code.Please some1 check it.....thanks in advance..
include
include
include
include
void reverse(char str[], int length);
char* itoa1(int num, char* str, int base);
int main()
{
char str1[501]; char str2[502];
char **expr;char *ws;
char *ptr;char *ptr1;char *ptr2;char temp[2];
char *mult_str;
char first[2];char second[2];
char res[3];
int **mult_arr;char **fin_str;int *fin_prod;
int count,sum,diff,carry,i,j,first_len,second_len,n,m,k,pos,max,flag,tot_len,temp1;
int white_space;
scanf("%d",&n);
if(n>1000)
exit(0);
expr=(char *)malloc(nsizeof(char *));
for(i=0;i {
expr[i]=(char )malloc(1002sizeof(char));
scanf("%s",expr[i]);
}
for(i=0;i {
//check if space is present
white_space=0;
if(strlen(expr[i])>1001)
continue;
ws=expr[i];
while((*ws)!='\0')
{
if((*ws)==' ')
{
white_space=1;
break;
}
ws++;
}
if(white_space==1)
continue;
//find out which operator is present
ptr=strstr(expr[i],"+");
ptr1=strstr(expr[i],"-");
ptr2=strstr(expr[i],"*");
//logic for '+' operator
if(ptr!=NULL && ptr1==NULL && ptr2==NULL)
{
printf("\n");
pos=ptr-expr[i];
first_len=pos;
second_len=strlen(expr[i])-pos;
if(first_len>500 || second_len>501)
continue;
str1[0]='\0';str2[0]='\0';
strncat(str1,expr[i],first_len);
strncat(str2,expr[i]+pos,second_len);
if(str1[0]=='0' || str2[1]=='0')
continue;
diff=abs(first_len-second_len);
if(first_len<second_len)
{
max=second_len;
diff=strlen(str1)+diff;
printf("%*s\n",diff,str1);
printf("%s\n",str2);
}
else
{
max=first_len;
diff=strlen(str2)+diff;
printf("%s\n",str1);
printf("%*s\n",diff,str2);
}
m=0;
while(m++!=max)
printf("-");
printf("\n");
carry=0;
for(k=pos-1,j=strlen(expr[i])-1;k>=0 && j>pos;k--,j--)
{
first[0]=*(expr[i]+k);first[1]='\0';
second[0]=*(expr[i]+j);second[1]='\0';
sum=carry+atoi(first)+atoi(second);
printf("%*d\r",max--,sum%10);
carry=sum/10;
}
if(k>=0)
{
while(k>=0)
{
first[0]=*(expr[i]+k);first[1]='\0';
sum=atoi(first)+carry;
printf("%*d\r",max--,sum%10);
carry=sum/10;
k--;
}
}
else if(j>pos)
{
while(j>pos)
{
first[0]=*(expr[i]+j);first[1]='\0';
sum=atoi(first)+carry;
printf("%*d\r",max--,sum%10);
carry=sum/10;
j--;
}
}
if(carry!=0)
printf("%*d",max--,carry);
printf("\n");
}
//logic for '-' operator
else if(ptr1!=NULL && ptr==NULL && ptr2==NULL)
{
printf("\n");
pos=ptr1-expr[i];
first_len=pos;
second_len=strlen(expr[i])-1-pos;
if(first_len>500 || second_len>501)
continue;
if(first_len<second_len)
continue;
else if(first_len==second_len)
{
int flag=0;
for(j=0,k=pos+1;j<pos && k<strlen(expr[i]);j++,k++)
{
if(expr[i][j]<expr[i][k])
{
flag=1;
break;
}
else if(expr[i][j]>expr[i][k])
break;
}
if(flag==1)
continue;}
second_len++;
str1[0]='\0';str2[0]='\0';
strncat(str1,expr[i],first_len);
strncat(str2,expr[i]+pos,second_len);
if(str1[0]=='0' || str2[1]=='0')
continue;
diff=abs(first_len-second_len);
if(first_len<second_len)
{
max=second_len;
diff=strlen(str1)+diff;
printf("%*s\n",diff,str1);
printf("%s\n",str2);
}
else
{
max=first_len;
diff=strlen(str2)+diff;
printf("%s\n",str1);
printf("%*s\n",diff,str2);
}
k=0;
while(k++!=max)
printf("-");
printf("\n");
for(j=pos-1,k=strlen(expr[i])-1;j>=0 && k>pos;j--,k--)
{
first[0]=expr[i][j];first[1]='\0';
second[0]=expr[i][k];second[1]='\0';
if(atoi(first)>=atoi(second))
printf("%*d\r",max--,atoi(first)-atoi(second));
else
{
res[0]='1';res[1]=expr[i][j];res[2]='\0';
first[0]=expr[i][j-1];first[1]='\0';
itoa1(atoi(first)-1,temp,10);
expr[i][j-1]=temp[0];
printf("%*d\r",max--,atoi(res)-atoi(second));
}
}
if(j!=-1)
{
while(j>=0)
{
first[0]=expr[i][j];first[1]='\0';
printf("%*d\r",max--,atoi(first));
j--;
}
}
printf("\n");
}
//logic for * operator
else if(ptr2!=NULL && ptr==NULL && ptr1==NULL)
{
printf("\n");
pos=ptr2-expr[i];
mult_arr=NULL;fin_str=NULL;fin_prod=NULL;mult_str=NULL;
free(mult_arr);free(fin_str);free(fin_prod);free(mult_str);
first_len=pos;
second_len=strlen(expr[i])-pos;
if(first_len>500 || second_len>501)
continue;
str1[0]='\0';str2[0]='\0';
strncat(str1,expr[i],first_len);
strncat(str2,expr[i]+pos,second_len);
if(str1[0]=='0' || str2[1]=='0')
continue;
/*case when first number is 0*/
if(first_len==1 && str1[0]=='0')
{
printf("%*s\n",second_len,str1);
printf("%s\n",str2);
i=0;
while(i++!=second_len)
printf("-");
printf("\n%*c",second_len,'0');
continue;
}
mult_arr=(int **)malloc((second_len-1)*sizeof(int *));
fin_str=(char **)malloc((second_len-1)*sizeof(char *));
tot_len=first_len+second_len-2;
flag=0;
for(m=0;m<second_len-1;m++)
{
mult_arr[m]=(int *)malloc((tot_len+1)*sizeof(int));
for(j=0;j<tot_len+1;j++)
mult_arr[m][j]=0;
}
for(m=second_len-1;m>0;m--)
{
first[0]=str2[m];first[1]='\0';
if(atoi(first)==0)
{
fin_str[second_len-1-m]=(char *)malloc(2*sizeof(char));
fin_str[second_len-1-m][0]='0';fin_str[second_len-1-m][1]='\0';
flag++;
continue;
}
temp1=flag;
carry=0;
for(j=first_len-1;j>=0;j--)
{
second[0]=str1[j];second[1]='\0';
sum=carry+atoi(first)*atoi(second);
mult_arr[second_len-1-m][tot_len-temp1]=sum%10;
carry=sum/10;
temp1++;
}
if(carry!=0)
{
mult_arr[second_len-1-m][tot_len-temp1]=carry;
temp1++;
}
fin_str[second_len-1-m]=(char *)malloc((temp1-flag)*sizeof(char));
fin_str[second_len-1-m][0]='\0';
for(k=temp1-1;k>=flag;k--)
strcat(fin_str[second_len-1-m],itoa1(mult_arr[second_len-1-m][tot_len-k],temp,10));
flag++;
}
if(second_len!=2)
{
carry=0;
fin_prod=(int *)malloc((tot_len+2)*sizeof(int));
memset(fin_prod,0,sizeof(fin_prod));
count=tot_len+2;
for(m=tot_len+1;m>0;m--)
{
sum=0;
for(j=0;j<(second_len-1);j++)
sum=sum+mult_arr[j][m-1];
sum=sum+carry;
fin_prod[m]=sum%10;
if(m==1 && fin_prod[m]==0)
count--;
carry=sum/10;
}
if(carry!=0)
fin_prod[0]=carry;
else
count--;
mult_str=(char *)malloc(count*sizeof(char));
mult_str[0]='\0';
for(m=tot_len+2-count;m<tot_len+2;m++)
strcat(mult_str,itoa1(fin_prod[m],temp,10));
}
else
count=strlen(fin_str[0]);
diff=(second_len==2 && str2[1]=='0'?first_len:count>second_len?count:second_len);
printf("%*s\n",diff,str1);
printf("%*s\n",diff,str2);
max=(second_len!=2?first_len>second_len?first_len:second_len:str2[1]=='0'?first_len:count);
m=0;first[0]='-';first[1]='\0';
printf("%*s",1+diff-max,first);
while(m++!=max-1)
printf("-");
printf("\n");
flag=0;
for(m=0;m<second_len-1;m++)
printf("%*s\n",diff-flag++,fin_str[m]);
if(second_len!=2)
{
m=0;
while(m++!=count)
printf("-");
printf("\n%s",mult_str);
}
printf("\n");
}
}
return 0;
}
void reverse(char str[], int length)
{
int start = 0,temp;
int end = length -1;
while (start < end)
{
temp=*(str+start);
(str+start)=(str+end);
*(str+end)=temp;
start++;
end--;
}
}
// Implementation of itoa()
char* itoa1(int num, char* str, int base)
{
int i = 0;
int isNegative = 0;
/* Handle 0 explicitely, otherwise empty string is printed for 0 */
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}
// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
if (num < 0 && base == 10)
{
isNegative = 1;
num = -num;
}
// Process individual digits
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
// If number is negative, append '-'
if (isNegative)
str[i++] = '-';
str[i] = '\0'; // Append string terminator
// Reverse the string
reverse(str, i);
return str;
}