问题描述
- C语言中,我想依次读入两个非常大的数,用字符串存,
-
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)
解决方案二:
一开始读入的时候就以字符串的形式来读。但是你这个问题,两个整数非常大,那他的和不是更大?你可以试试bigint这个类型。
解决方案三:
struct biginteger
{
int digit[500];
int len;
}
读进去之后转换一下,存进biginteger类型的变量里。用数组高精度计算的方法做
解决方案四:
可以用整形数组来实现
解决方案六:
用数组来存啊,例如这样:10000=0*1+0*10+0* 100+0*1000+1*10000
时间: 2024-09-27 14:08:54