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)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 求指导ADS低噪放设计
- ¥15 CARSIM前车变道设置
- ¥50 三种调度算法报错 有实例
- ¥15 关于#python#的问题,请各位专家解答!
- ¥200 询问:python实现大地主题正反算的程序设计,有偿
- ¥15 smptlib使用465端口发送邮件失败
- ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
- ¥15 对于squad数据集的基于bert模型的微调
- ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
- ¥20 steam下载游戏占用内存