C语言简单的大数字求和
#include<stdio.h>
#include<string.h>
#define N 100
/*
*Func:For two and a collapse
*date:2011-10-19
*author:huzhongzhong
*/
int main()
{
char m_a[N], m_b[N], m_c[N + 1];//m_a、m_b存相加的是机m_c存取结果
int m_flag = 0 , lengths, lengthb, m_fren, m_tem, m_div,M;
//内存做初始化工作
memset(m_a, 0 , N);
memset(m_b, 0, N);
memset(m_c, 0 ,N+1);
//输入
gets( m_a );
gets( m_b );
//求字符少的那个
lengths = strlen(m_a) > strlen(m_b) ? strlen(m_b) : strlen(m_a) ;
//求字符数多的
M = lengthb = strlen(m_a) > strlen(m_b) ? strlen(m_a) : strlen(m_b) ;
for(m_fren = 0 ,lengths--, lengthb--; m_fren < M; m_fren++)
{
if(lengths >= 0)
{
// m_tem = m_a[lengthb] - 48 + m_b[lengths] - 48 +m_flag;
//不能简单的m_a[lengthb],因为lengthb是最大的数,并不知道m_元素多大还是m_b元素多
m_tem = (strlen(m_a) >strlen(m_b) ?m_a[lengthb] : m_a[lengths]) - 48 +
(strlen(m_b) >strlen(m_a) ?m_b[lengthb] : m_b[lengths]) -48 +m_flag;
m_div = m_tem % 10;
m_flag = m_tem /10;
lengthb--;
lengths--;
}
else
{
//m_tem = m_a[lengthb] - 48 +m_flag;
m_tem = (strlen(m_a) >strlen(m_b) ? m_a[lengthb] :m_b[lengthb]) - 48 +m_flag;
m_div = m_tem % 10;
m_flag = m_tem /10;
lengthb--;
}
m_c[lengthb + 2] = m_div + 48;
}
m_c[0] = m_flag + 48;
puts(m_c);//打印结果
return 0;
}