malloc()
#include <ctype.h>
#include <stdlib.h>
struct bignum
{
char dig ;
struct bignum * next ;
}
* LIST ;
void my_malloc( LIST * );
void output( LIST );
LIST add( LIST ,LIST );
void my_free( LIST );
void move( LIST * );
int get_dig( LIST );
void put( int , LIST * );
{
LIST p_num1 , p_num2 ;
LIST p_sum ;
puts("輸入一個整數:");
p_num1 = get_num();
puts("輸入一個整數:");
p_num2 = get_num();
output(p_sum);
putchar('\n');
my_free(p_sum);
my_free(p_num1);
my_free(p_num2);
return 0;
}
{
if ( ( * p = malloc( sizeof ( * * p ) )) == NULL )
exit(1);
}
LIST get_num( void )
{
LIST p = NULL ;
int c ;
while ( ( c = getchar() ) && isdigit( c ) )
{
LIST tmp ;
my_malloc ( &tmp );
tmp -> dig = c - '0';
tmp ->next = p ;
p = tmp ;
}
return p;
}
{
if ( p->next != NULL )
free( p->next );
}
{
if ( p == NULL )
return 0;
return p ->dig ;
}
{
if ( *pp == NULL )
return ;
*pp = (*pp) -> next ;
}
{
my_malloc( pp );
(*pp)->dig = n ;
(*pp)->next= NULL;
}
{
LIST p_res = NULL ;
LIST * pp_res = & p_res ;
char low , high = 0 ;
while ( p1 != NULL || p2 != NULL )
{
low = high ; //high為前一次的進位
low += get_dig( p1 ) + get_dig( p2 ) ;
high = low / 10 ; //記下進位
low %= 10 ;
put( low , pp_res ); //將和放入新結點
pp_res = & (*pp_res)->next ;
move( &p1 ) , move( &p2 ) ;
}
if ( high != 0 ) //處理最高位的進位
put( high , pp_res );
}
void output( LIST p )
{
if ( p->next != NULL )
output( p->next );
putchar( p -> dig + '0' );
}