The program uses doubly linked lists to store the numbers where each node contains a single digit . Hence numbers of any length can be used as operands .
Program works only with +ve integers
The program uses doubly linked lists to store the numbers where each node contains a single digit . Hence numbers of any length can be used as operands .
Program works only with +ve integers
#include<stdio.h>
#include<stdlib.h>
typedef struct number
{
int a;
struct number *lptr,*rptr;
}node;
node *create();
node *findl(node *right);
node *findr(node *left);
void display(node *left);
node *add(node *fright,node *sright);
node *comp(node *right,int l);
node *sub(node *fright,node *sright);
void align(node *right);
node *multiply(node *fright,node *sright);
void *divide(node *firstr,node *sright);
node *create()
{
node *cur=NULL;
char ch,temp[2];
scanf("%c",&ch);
if(ch=='\n')
scanf("%c",&ch);
temp[0]=ch;
temp[1]='\0';
while(ch!='$')
{
if(cur==NULL)
{
cur=(node *)malloc(sizeof(node));
cur->lptr=NULL;
}
else
{
cur->rptr=(node *)malloc(sizeof(node));
cur->rptr->lptr=cur;
cur=cur->rptr;
}
cur->rptr=NULL;
cur->a=atoi(temp);
scanf("%c",&ch);
temp[0]=ch;
temp[1]='\0';
}
return cur;
}
node *findl(node *right)
{
node *left=right;
while(left->lptr!=NULL)
{
left=left->lptr;
}
return left;
}
node *findr(node *left)
{
node *right=left;
while(right->rptr!=NULL)
{
right=right->rptr;
}
return right;
}
void display(node *left)
{
while(left!=NULL)
{
printf("%d",left->a);
left=left->rptr;
}
printf("\n");
}
node *add(node *fright,node *sright)
{
node *fcur=fright,*scur=sright,*result=NULL,*rcur=NULL;
int temp=0;
while((fcur!=NULL)||(scur!=NULL))
{
if((fcur!=NULL)&&(scur!=NULL))
{
temp=temp+fcur->a+scur->a;
fcur=fcur->lptr;
scur=scur->lptr;
}
else
if((fcur!=NULL)&&(scur==NULL))
{
temp=temp+fcur->a;
fcur=fcur->lptr;
}
else
if((fcur==NULL)&&(scur!=NULL))
{
temp=temp+scur->a;
scur=scur->lptr;
}
if(result==NULL)
{
result=(node *)malloc(sizeof(node));
result->rptr=NULL;
rcur=result;
}
else
{
rcur->lptr=(node *)malloc(sizeof(node));
rcur->lptr->rptr=rcur;
rcur=rcur->lptr;
}
rcur->lptr=NULL;
rcur->a=temp%10;
temp=temp/10;
}
if(temp!=0)
{
rcur->lptr=(node *)malloc(sizeof(node));
rcur->lptr->rptr=rcur;
rcur=rcur->lptr;
rcur->lptr=NULL;
rcur->a=temp;
}
return result;
}
node *comp(node *right,int l)
{
node *cright=NULL,*ccur=NULL,*cur=right;
int temp=1,cl=0;
while(cur!=NULL)
{
if(cright==NULL)
{
cright=(node *)malloc(sizeof(node));
cright->rptr=NULL;
ccur=cright;
}
else
{
ccur->lptr=(node *)malloc(sizeof(node));
ccur->lptr->rptr=ccur;
ccur=ccur->lptr;
}
cl++;
ccur->lptr=NULL;
ccur->a=9-cur->a;
cur=cur->lptr;
}
while(cl<l)
{
ccur->lptr=(node *)malloc(sizeof(node));
ccur->lptr->rptr=ccur;
ccur=ccur->lptr;
ccur->lptr=NULL;
ccur->a=9;
cl++;
}
for(ccur=cright;ccur!=NULL;ccur=ccur->lptr)
{
temp+=ccur->a;
ccur->a=temp%10;
temp=temp/10;
if(temp==0)
break;
}
return cright;
}
node *sub(node *fright,node *sright)
{
int lf,ls,l,lr;
node *result=NULL,*rleft=NULL,*cur=NULL,*temp=NULL;
for(lf=0,cur=fright;cur!=NULL;cur=cur->lptr,lf++);
for(ls=0,cur=sright;cur!=NULL;cur=cur->lptr,ls++);
if((ls==1)&&(sright->a==0))
return fright;
l=(lf>ls?lf:ls);
sright=comp(sright,l);
result=add(fright,sright);
rleft=findl(result);
for(lr=0,cur=result;cur!=NULL;cur=cur->lptr,lr++);
if(lr>l)
{
temp=rleft;
rleft=rleft->rptr;
rleft->lptr=NULL;
free(temp);
temp=NULL;
}
else
{
result=comp(result,lr);
align(result);
rleft=findl(result);
rleft->a=-(rleft->a);
}
return result;
}
void align(node *right)
{
node *cur=right,*temp=NULL;
for(cur=right;cur->lptr!=NULL;cur=cur->lptr);
for(;(cur->a==0)&&(cur!=right);)
{
temp=cur;
cur=cur->rptr;
free(temp);
cur->lptr=NULL;
}
}
node *multiply(node *fright,node *sright)
{
node *result=NULL,*rright=NULL;
node *fcur=NULL,*scur=NULL,*rcur=NULL;
result=(node *)malloc(sizeof(node));
result->lptr=NULL;
result->rptr=NULL;
result->a=0;
rright=result;
for(fcur=fright;fcur!=NULL;fcur=fcur->lptr)
{
for(scur=sright,rcur=rright;scur!=NULL;scur=scur->lptr)
{
rcur->a=rcur->a+(fcur->a*scur->a);
if(rcur->lptr==NULL)
{
rcur->lptr=(node *)malloc(sizeof(node));
rcur->lptr->rptr=rcur;
rcur=rcur->lptr;
rcur->lptr=NULL;
rcur->a=0;
}
else
rcur=rcur->lptr;
}
rright=rright->lptr;
}
return result;
}
void *divide(node *firstr,node *sright)
{
node *fright=NULL,*fr=NULL,*fleft=NULL,*templ=NULL,*tempr=NULL,*temp=NULL,*rleft=NULL,*rcur=NULL,*cur=NULL,*cur2=NULL;
int count=0;
if((sright->lptr==NULL)&&(sright->a==0))
{
printf("\nIllegal Operation\n");
return;
}
// printf("\nStart copy fright");
for(cur=firstr;cur!=NULL;cur=cur->lptr)
{
if(fright==NULL)
{
fright=(node*)malloc(sizeof(node));
fright->rptr=NULL;
fleft=fright;
}
else
{
fleft->lptr=(node *)malloc(sizeof(node));
fleft->lptr->rptr=fleft;
fleft=fleft->lptr;
}
fleft->lptr=NULL;
fleft->a=cur->a;
}
// printf("\nStop copy fright");
cur=findl(sright);
// printf("\nStart calculating fr");
for(fr=fleft;(fr->rptr!=NULL)&&(cur->rptr!=NULL);fr=fr->rptr,cur=cur->rptr);
// printf("\nStart outer loop");
while(fr!=NULL)
{
count=0;
while(1)
{
tempr=sub(fr,sright);
templ=findl(tempr);
// printf("\ntemp = ");
// display(templ);
if(templ->a<0)
{
for(cur=templ;cur!=NULL;)
{
temp=cur;
cur=cur->rptr;
free(temp);
temp=NULL;
}
break;
}
else
{
count++;
for(cur=fr,cur2=tempr;(cur!=NULL)&&(tempr!=NULL);cur=cur->lptr,cur2=cur2->lptr)
cur->a=cur2->a;
for(cur=templ;cur!=NULL;)
{
temp=cur;
cur=cur->rptr;
free(temp);
temp=NULL;
}
// printf("\n count=%d ",count);
}
if(count>=10)
{getch();
exit(0);
}
}
if(rleft==NULL)
{
rleft=(node *)malloc(sizeof(node));
rleft->lptr=NULL;
rcur=rleft;
}
else
{
rcur->rptr=(node *)malloc(sizeof(node));
rcur->rptr->lptr=rcur;
rcur=rcur->rptr;
}
rcur->rptr=NULL;
rcur->a=count;
fr=fr->rptr;
}
printf("\nQuotient : ");
align(rcur);
display(rleft);
printf("\nRemainder : ");
align(fright);
fleft=findl(fright);
display(fleft);
}
int main()
{
node *fright=NULL,*fleft=NULL,*sright=NULL,*sleft=NULL,*rright=NULL,*rleft=NULL;
int ch;
printf("Enter The First Number ( type '$' at the end )\n");
fright=create();
align(fright);
printf("Enter The Second Number ( type '$' at the end )\n");
sright=create();
align(sright);
printf("\nFirst Number : ");
fleft=findl(fright);
display(fleft);
printf("\nSecond Number : ");
sleft=findl(sright);
display(sleft);
x:printf("\nChoice 1 : Addition\nChoice 2 : Subtraction\nChoice 3 : Multiplication\nChoice 4 : Division\nChoice 5 : Exit\nEnter The Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1 :
rright=add(fright,sright);
align(rright);
rleft=findl(rright);
printf("\nSum = ");
display(rleft);
break;
case 2 :
rright=sub(fright,sright);
align(rright);
printf("\nDifference = ");
rleft=findl(rright);
display(rleft);
break;
case 3 :
rright=multiply(fright,sright);
align(rright);
printf("\nProduct = ");
rleft=findl(rright);
display(rleft);
break;
case 4 :
divide(fright,sright);
break;
case 5 :
exit(0);
default :
printf("Wrong Choice\n");
}
printf("\n");
goto x;
getch();
return 0;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.