C語言中,我想依次讀入兩個非常大的整數,用字符串存,應該怎麼寫讀入語句,並且想計算他們的和,並輸出結果,應該怎麼辦(這兩個整數非常大,long int也不能存)
/********** main function *********/
/*
** FILE: tbigint.c
** NOTE: 2015-10-08 created by Jack Liu
*/
#include<stdio.h>
#include<string.h>
#include"bigint.h"
int
main( void )
{
char caOper1[ 100 ] = { 0 };
char caOper2[ 100 ] = { 0 };
st_bigint stOper1;
st_bigint stOper2;
st_bigint stResult;
stOper1.iFlag = POSITIVE;
stOper1.iUsed = 0;
memset( stOper1.iaValue, 0x00, sizeof( stOper1.iaValue ) );
memcpy( &stOper2, &stOper1, sizeof( st_bigint ) );
memcpy( &stResult, &stOper1, sizeof( st_bigint ) );
printf( "input operand 1: " );
scanf( "%s", caOper1 );
printf( "input operand 2: " );
scanf( "%s", caOper2 );
str2bigint( caOper1, &stOper1 );
str2bigint( caOper2, &stOper2 );
printf( "first operand : " );
print_bigint( &stOper1 );
printf( "\n" );
printf( "second operand: " );
print_bigint( &stOper2 );
printf( "\n" );
add_bigint( &stOper1, &stOper2, &stResult );
printf( "result output : " );
print_bigint( &stResult );
printf( "\n" );
return 0;
}
/************************************/
/************** detail **************/
/***********************************/
/*
** FILE: bigint.h
** NOTE: 2015-10-08 created by Jack Liu
*/
#ifndef JACK_LIU_COM_BIGINT_H
#define JACK_LIU_COM_BIGINT_H
#define DIGITS_NUM 100
#define NEGATIVE -1
#define POSITIVE 1
typedef struct _st_bigint
{
int iaValue[ DIGITS_NUM + 1 ];
int iUsed;
int iFlag;
} st_bigint;
int get_digits_of_int( unsigned int iInteger );
int str2bigint( const char *ccpStr, st_bigint *stpBigint );
int add_bigint( st_bigint *stpOper1, st_bigint *stpOper2, st_bigint *stpResult );
int print_bigint( st_bigint *stpResult );
#endif
/*
** FILE: bigint.c
** NOTE: 2015-10-08 created by Jack Liu
*/
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include"bigint.h"
int
get_digits_of_int( unsigned int iInteger )
{
int iCnt = 1;
iInteger = iInteger > 0 ? iInteger : -iInteger;
while( ( iInteger /= 10 ) > 0 )
++iCnt;
return iCnt;
}
int
str2bigint( const char *ccpStr, st_bigint *stpBigint )
{
int iStrLen = strlen( ccpStr );
int iIntLen = get_digits_of_int( INT_MAX );
int iTmpInt = 0;
int iTmpPos = 0;
int i = 0;
if( ccpStr[ 0 ] == '-' )
stpBigint->iFlag = NEGATIVE;
else
{
stpBigint->iFlag = POSITIVE;
iTmpInt = *( ccpStr ) - '0';
iTmpPos++;
}
for( i = 1; i < iStrLen; ++i )
{
iTmpInt = iTmpInt * 10 + *( ccpStr + i ) - '0';
if( ++iTmpPos == iIntLen - 1 )
{
stpBigint->iaValue[ stpBigint->iUsed++ ] = iTmpInt;
iTmpInt = 0;
iTmpPos = 0;
}
}
if( iTmpInt > 0 )
stpBigint->iaValue[ stpBigint->iUsed++ ] = iTmpInt;
return 0;
}
static int
get_int_carry( unsigned int uiResult, int iLimLen, int *ipRemain )
{
int i = 0;
unsigned int uiTmpResult = uiResult;
unsigned int uiTmpConvert = 0;
/*
** after for loop, uiTmpResult contain the carry value
*/
for( i = 0; i < iLimLen; ++i )
uiTmpResult /= 10;
/*
** build a tmp value with the carry value
*/
uiTmpConvert = uiTmpResult;
for( i = 0; i < iLimLen; ++i )
uiTmpConvert *= 10;
/*
** caculate the remain without the carry part
*/
if( ipRemain != NULL )
{
*ipRemain = uiResult - uiTmpConvert;
}
return ( int )uiTmpResult;
}
int
add_bigint( st_bigint *stpOper1, st_bigint *stpOper2, st_bigint *stpResult )
{
unsigned int uiTmpResult = 0;
int iIntLen = get_digits_of_int( INT_MAX );
int iTmpCarry = 0;
int iCalcuCnt = stpOper1->iUsed > stpOper2->iUsed ? stpOper2->iUsed : stpOper1->iUsed;
int iCarryCnt = stpOper1->iUsed > stpOper2->iUsed ? stpOper1->iUsed : stpOper2->iUsed;
if( stpOper1->iFlag == POSITIVE && stpOper2->iFlag == POSITIVE )
{
int i = 0;
int j = 0;
int iTmpResultLen = 0;
stpResult->iUsed = iCarryCnt + 1;
for( i = iCalcuCnt; i > 0; --i )
{
uiTmpResult = stpOper1->iaValue[ stpOper1->iUsed - 1 - j ] + stpOper2->iaValue[ stpOper2->iUsed - 1 - j ] + iTmpCarry;
iTmpCarry = 0;
iTmpResultLen = get_digits_of_int( uiTmpResult );
if( iTmpResultLen >= iIntLen )
iTmpCarry = get_int_carry( uiTmpResult, iIntLen, &( stpResult->iaValue[ stpResult->iUsed - 1 - j ] ) );
else
stpResult->iaValue[ stpResult->iUsed - 1 - j ] = uiTmpResult;
j++;
}
for( i = stpOper1->iUsed - iCalcuCnt, j = 0; i > 0; --i )
{
uiTmpResult = stpOper1->iaValue[ stpOper1->iUsed - 1 - iCalcuCnt - j ] + iTmpCarry;
iTmpCarry = 0;
iTmpResultLen = get_digits_of_int( uiTmpResult );
if( iTmpResultLen >= iIntLen )
iTmpCarry = get_int_carry( uiTmpResult, iIntLen, &( stpResult->iaValue[ stpResult->iUsed - 1 - iCalcuCnt - j ] ) );
else
stpResult->iaValue[ stpResult->iUsed - 1 - iCalcuCnt - j ] = uiTmpResult;
j++;
}
for( i = stpOper2->iUsed - iCalcuCnt, j = 0; i > 0; --i )
{
uiTmpResult = stpOper1->iaValue[ stpOper2->iUsed - 1 - iCalcuCnt - j ] + iTmpCarry;
iTmpCarry = 0;
iTmpResultLen = get_digits_of_int( uiTmpResult );
if( iTmpResultLen >= iIntLen )
iTmpCarry = get_int_carry( uiTmpResult, iIntLen, &( stpResult->iaValue[ stpResult->iUsed - 1 - iCalcuCnt - j ] ) );
else
stpResult->iaValue[ stpResult->iUsed - 1 - iCalcuCnt - j ] = uiTmpResult;
j++;
}
if( iTmpCarry > 0 )
stpResult->iaValue[ 0 ] = iTmpCarry;
else
{
for( i = 1; i < stpResult->iUsed; ++i )
stpResult->iaValue[ i - 1 ] = stpResult->iaValue[ i ];
stpResult->iUsed--;
}
}
return 0;
}
int print_bigint( st_bigint *stpResult )
{
int i = 0;
if( stpResult->iFlag == NEGATIVE )
printf( "-" );
for( i = 0; i < stpResult->iUsed; ++i )
{
printf( "%d", stpResult->iaValue[ i ] );
}
return 0;
}
![圖片說明](http://img.ask.csdn.net/upload/201510/09/1444322973_353727.png)