C语言中,我想依次读入两个非常大的整数,用字符串存,应该怎么写读入语句,并且想计算他们的和,并输出结果,应该怎么办(这两个整数非常大,long int也不能存)
6条回答
- qq1223386926 2015-10-08 16:49关注
/********** 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; } ![图片说明](https://img-ask.csdn.net/upload/201510/09/1444322973_353727.png)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 iqoo11 如何下载安装工程模式
- ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
- ¥15 本题的答案是不是有问题
- ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
- ¥15 C++使用Gunplot
- ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
- ¥15 matlab数字图像处理频率域滤波
- ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
- ¥15 ELGamal和paillier计算效率谁快?
- ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题