/*================================= GET L.I.N. ====================================*/
void get_large_num()
{
get_1st_num();
get_2nd_num();
}
//devide get_large_num() into 2 methods, as i have 2 pointers with different names,
//if i need a better way, i should change the way i use the head && tail of two numbers!
void get_1st_num()
{
printf("Please input your first number:/n");
num1_tail=get_num_tail();
num1_head=get_num_head( num1_tail );
}
void get_2nd_num()
{
printf("Please input your second number:/n");
num2_tail=get_num_tail();
num2_head=get_num_head( num2_tail );
}
LIN* get_num_tail()
{
char temp;
LIN* tail='/0';
/*
*the 1st time create a struct need a special progress
*ONLY USE TAIL, as a devided metods,
*use tail pointer to gfet a new room && edit the first struct
*1st part's last && next = '/0'
*/
temp=getchar();
//get a new room
tail=(LIN*)malloc(sizeof(LIN));
//edit inner info.
tail->last='/0';
tail->next='/0';
tail->num=temp-48;
//return tail to up method
return tail;
}
LIN* get_num_head(LIN *tail)
{
char temp;
LIN *head,*pold,*pnew;
head=tail;
pnew=tail;
/*
*after the 1st struct
*record the numbers from tail to head
*so that can plus the number from the last position to the top position
*use 'pnew' as NEW STRUCT LEADER, use 'pold' record LAST ONE'S ADRESS
*E.G. -- head->pnew->pold->tail FOUR-STRCUT-EXAMPLE
*/
while( ( temp = getchar() ) != '/n')
{
//exchang commender for adress
pold = pnew;
//malloc new romm && create a new LIN
pnew = ( LIN* )malloc( sizeof(LIN) );
//edit front info.--head adress
head = pnew;
//edit behinde info.--last LIN's tail adress
pold->last = pnew;
//edit inner info. in 'pnew'
pnew->last = '/0';
pnew->num = temp - 48;
pnew->next = pold;
}
return head;
}
/*================================= END ===================================*/
/*=================================== P L U S ===================================*/
void plus_2_large_num()
{
result_head=(LIN*)malloc(sizeof(LIN));
result_tail=plus_tail(result_head,num1_head,num2_head);
}
LIN *plus_tail(LIN *head, LIN *p1, LIN *p2)
{
LIN *result_new,*result_old,*tail;
result_new=head;
tail=head;
result_new->last='/0';
result_new->next='/0';
result_new->num=single_plus( p1,p2 );
if(p1)
{
p1=p1->next;
}
if(p2)
{
p2=p2->next;
}
/*第一次以后的运算*/
while( p1||p2 )
{
result_old=result_new;
result_new=(LIN*)malloc(sizeof(LIN));
tail=result_new;
result_old->next=result_new;
result_new->last=result_old;
result_new->next='/0';
result_new->num=single_plus( p1,p2 );
if(p1)
{
p1=p1->next;
}
if(p2)
{
p2=p2->next;
}
}
return tail;
}
char single_plus(LIN *p1,LIN *p2)
{
static char temp;
char digit;
if(p1&&p2)
{
/*
*p1 p2 都是存在数的情况下,进行计算。
*temp先计算两数相加:
*1、如果是两位数(大于9):temp为1,记录进位的1。digit记录个位数,并返回。
*2、如果是一位数:temp赋值给digit,并返回。
*/
temp += p1->num + p2->num ;
if( temp > 9)
{
digit = temp - 10;
temp = 1;
}else
{
digit = temp;
temp = 0;
}
//最高位进位检查
if(p1->next=='/0'&&
p2->next=='/0'&&
temp == 1)
{
digit += 10;
}
}else if(p1)
{
/*
*p1 存在数、p2 为空的情况下,进行计算。
*1、如果 temp 中存有进位的值(只能是 1 ),判断p1 是不是9:
* 1、是 9 ,temp 保持 1 ,digit 赋值为 0 ,并返回。
* 2、不是 9 ,p1 加 1 ,赋值给 digit ,并返回。
*2、如果 temp 不是 1 即为 0 (说明上一位的计算没有进位):temp赋值给digit,并返回。
*/
digit=special_plus(p1,&temp);
}else
{
/*
*p1 为空、p2 存在数的情况下,进行计算。
*1、如果 temp 中存有进位的值(只能是 1 ),判断p2 是不是9:
* 1、是 9 ,temp 保持 1 ,digit 赋值为 0 ,并返回。
* 2、不是 9 ,p1 加 1 ,赋值给 digit ,并返回。
*2、如果 temp 不是 1 即为 0 (说明上一位的计算没有进位):temp赋值给digit,并返回。
*/
digit=special_plus(p2,&temp);
}
return digit;
}
char special_plus(LIN *p,char *temp)
{
char digit=0;
if(*temp == 1)
{
if( p->num == 9)
{
digit = 0;
}else
{
digit = p->num + 1;
*temp = 0;
}
}else
{
digit = p->num;
}
//最高位进位检查
if(p->next=='/0'&&
*temp ==1 )
{
digit += 10;
*temp=0;
}
return digit;
}
/*================================= END ===================================*/
/*================================= P R I N T ===================================*/
void print_result()
{
LIN *p;
p=result_tail;
printf("/n");
/*printf("%d",p->num);
do
{
p=p->last;
printf("%d",p->num);
}while(p->last!='/0');
*/
while (p)
{
printf("%d",p->num);
p=p->last;
}
printf("/n");
}
/*================================= END ===================================*/