大数加法的实现
自己写了一个大数相加的程序,
程序可以运行,但是遇到如下问题,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define DIGIT 100
char * AddOperation( char * a1, char * a2 )
{
int next = 0;
int i = 0;
int count1 = strlen( a1 );
int count2 = strlen( a2 );
char result[ DIGIT ];
while( *a1 != '\0' )
a1 ++;
while( *a2 != '\0' )
a2 ++;
a1 --;
a2 --;
while( ( count1 > 0 ) && ( count2 > 0 ) )
{
if( ( * a1 - 48 ) + ( * a2 - 48 ) + next > 9 )
{
result[ i ] = (char)( ( * a1 - 48 ) + ( * a2 - 48 ) + next - 10 ) + '0';
next = 1;
}
else
{
result[ i ] = (char)( ( * a1 - 48 ) + ( * a2 - 48 ) + next ) + '0';
next = 0;
}
count1 --;
a1 --;
count2 --;
a2 --;
i ++;
}
while( count1 > 0 )
{
if( ( * a1 - 48 ) + next > 9 )
{
result[ i ] = (char)( ( * a1 - 48 ) + next - 10 ) + '0';
next = 1;
}
else
{
result[ i ] = (char)( ( * a1 - 48 ) + next ) + '0';
next = 0;
}
count1 --;
a1 --;
i ++;
}
while( count2 > 0 )
{
if( ( * a2 - 48 ) + next > 9 )
{
result[ i ] = (char)( ( * a2 - 48 ) + next - 10 ) + '0';
next = 1;
}
else
{
result[ i ] = (char)( ( * a2 - 48 ) + next ) + '0';
next = 0;
}
count2 --;
a2 --;
i ++;
}
result[ i ] = '\0';
for( i = strlen( result ) - 1; i >= 0; i-- )
printf( "%c", result[ i ] );
printf( "\n" );
//printf( "%s\n", result );
return result;
}
int main( void )
{
char * add1;
char * add2;
char * result;
char adder1[ DIGIT ];
char adder2[ DIGIT ];
puts( "Please input adder1" );
add1 = gets_s( adder1, 100 );
puts( "Please input adder2" );
add2 = gets_s( adder2, 100 );
printf( "The result of %s + %s is\n", add1, add2 );
result = AddOperation( add1, add2 );
//printf( "%s\n", result );
system( "pause" );
return 0;
}
如果在主函数中通过注释掉的这一行来输出的话,当输入的加数位数大于3位时,会出现乱码情况,请问为什么?
目前是反向输出,还没改。

程序可以运行,但是遇到如下问题,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define DIGIT 100
char * AddOperation( char * a1, char * a2 )
{
int next = 0;
int i = 0;
int count1 = strlen( a1 );
int count2 = strlen( a2 );
char result[ DIGIT ];
while( *a1 != '\0' )
a1 ++;
while( *a2 != '\0' )
a2 ++;
a1 --;
a2 --;
while( ( count1 > 0 ) && ( count2 > 0 ) )
{
if( ( * a1 - 48 ) + ( * a2 - 48 ) + next > 9 )
{
result[ i ] = (char)( ( * a1 - 48 ) + ( * a2 - 48 ) + next - 10 ) + '0';
next = 1;
}
else
{
result[ i ] = (char)( ( * a1 - 48 ) + ( * a2 - 48 ) + next ) + '0';
next = 0;
}
count1 --;
a1 --;
count2 --;
a2 --;
i ++;
}
while( count1 > 0 )
{
if( ( * a1 - 48 ) + next > 9 )
{
result[ i ] = (char)( ( * a1 - 48 ) + next - 10 ) + '0';
next = 1;
}
else
{
result[ i ] = (char)( ( * a1 - 48 ) + next ) + '0';
next = 0;
}
count1 --;
a1 --;
i ++;
}
while( count2 > 0 )
{
if( ( * a2 - 48 ) + next > 9 )
{
result[ i ] = (char)( ( * a2 - 48 ) + next - 10 ) + '0';
next = 1;
}
else
{
result[ i ] = (char)( ( * a2 - 48 ) + next ) + '0';
next = 0;
}
count2 --;
a2 --;
i ++;
}
result[ i ] = '\0';
for( i = strlen( result ) - 1; i >= 0; i-- )
printf( "%c", result[ i ] );
printf( "\n" );
//printf( "%s\n", result );
return result;
}
int main( void )
{
char * add1;
char * add2;
char * result;
char adder1[ DIGIT ];
char adder2[ DIGIT ];
puts( "Please input adder1" );
add1 = gets_s( adder1, 100 );
puts( "Please input adder2" );
add2 = gets_s( adder2, 100 );
printf( "The result of %s + %s is\n", add1, add2 );
result = AddOperation( add1, add2 );
//printf( "%s\n", result );
system( "pause" );
return 0;
}
如果在主函数中通过注释掉的这一行来输出的话,当输入的加数位数大于3位时,会出现乱码情况,请问为什么?
目前是反向输出,还没改。

...全文
相关推荐
C++单链表实现大数加法
主要为大家详细介绍了C++单链表实现大数加法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
易语言实现大数加法计算示例程序
易语言是一款面向中文用户的编程语言,以其语法简洁、学习门槛低而受到广大编程初学者和中小型项目开发者的青睐。它采用图形化界面设计和自然语言风格的语法结构,使得开发者能够快速上手并构建功能完整的Windows应用程序。在数值处理方面,易语言提供了整型、长整型、浮点型等基本数据类型。然而,这些类型在处理极大整数(如超过10^18)时存在溢出风险,无法满足高精度计算需求。因此,在涉及大数加法的场景中,需要采用字符串模拟的方式实现加法运算。
用C++实现了大数阶乘及大数加法
用C++在控制台上写的。首先用链表实现了大数阶乘,在这基础上只要提供这样的两个链表就可以实现大数加法。想实现大数的乘法,但是失败了……
java实现大数加法(BigDecimal)的实例代码
之前写过用vector、string实现大数加法,现在用java的BigDecimal类,代码简单很多。但是在online-judge上,java的代码运行时间和内存大得多
大数加法与乘法c++实现
实现大数加法与乘法运算,大数减法与除法再提供


