简单的大数加法
昨天写了个大数加法,说下原理。
数字保存肯定是得用字符串了,然后逐位从char转换成int型,接着进行运算,把运算的结果保存到一个字符串中。
然后对字符串进行判断是一位还是两位,如果是一位,就直接修改结构体中的字符串,如果是两位,就向保存的进位数字的数组中加1(我是用分配两个大小相同的数组来做的,一个用来存储要运算的数,一个保存要进位的数,其实不用这样,给个临时变量就行了,不过这是后来想明白的,呵呵)
不多说了,看代码吧:
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#define NUM_LEN 500
#define ctoi(c) (c-48)
//#define max(a,b) (((a) > (b)) ? (a) : (b))
//itoa
struct _big_num;
typedef _big_num big_num;
typedef big_num* pbig_num;
struct _big_num
{
//用来标识字符串中共有多少位的数字
int index;
//标识最大支持多少位
int maxsize;
char *buff;
//用来表示进位
char *nums;
};
//将整形
/*int ctoi(int c)
{
return (c-48);
}*/
//创建大数结构体,并分配内存,清零
pbig_num big_num_create(char *num)
{
pbig_num p = NULL;
p = (pbig_num)malloc(sizeof(*p));
assert(p);
p->buff = (char*)malloc(NUM_LEN);
p->nums = (char*)malloc(NUM_LEN);
//memset(p)
memset(p->buff,0,NUM_LEN);
memset(p->nums,0,NUM_LEN);
p->maxsize = NUM_LEN;
p->index = 0;
if (num != NULL)
{
strncpy(p->buff,num,NUM_LEN);
p->index = strlen(num);
}
return p;
}
//释放资源
void big_num_free(pbig_num *b)
{
assert(b);
free((*b)->buff);
free((*b)->nums);
free((*b));
}
//前面要添加零,需要重新计算
int aligh_num(char *s1,int size)
{
int n = strlen(s1);
//声明并清零
char *tmp = (char*)malloc(n);
memset(tmp,0,n);
//赋值给临时变量
strncpy(tmp,s1,n);
memset(s1,'0',size);
memcpy(s1+size,tmp,n);
free(tmp);
return 0;
}
//进行计算
//s为一保存数字的字符串
pbig_num big_num_add2(pbig_num pb,char *s)
{
static char tmp[3] = {0};
int n = strlen(s);
//p用来操作需要增加0的字符串
char *p;
char *num2 = (char*)malloc(NUM_LEN);
memset(num2,0,NUM_LEN);
strncpy(num2,s,n);
if (pb->index > n)
{
//num2需要增加0
p = num2;
aligh_num(p,pb->index - n);
}
else if (n > pb->index)
{
p = pb->buff;
aligh_num(p,n - pb->index);
}
int i;
//添加0之后,重新计算长度
n = strlen(num2);
pb->index = strlen(pb->buff);
for (i = n - 1; i >= 0; i--)
{
memset(tmp,0,3);
int x = 0;
x = ctoi(num2[i]) + (ctoi(pb->buff[i])) + (pb->nums[i]);
sprintf(tmp,"%d",x);
if (strlen(tmp) == 2 && i != 0)
{
pb->nums[i-1] = ctoi(tmp[0]);
pb->buff[i] = tmp[1];
}
else
{
pb->buff[i] = tmp[0];
}
}
free(num2);
return pb;
}
int _tmain(int argc, _TCHAR* argv[])
{
pbig_num pbn = big_num_create("1234");
big_num_add2(pbn,"123431");
puts(pbn->buff);
fwrite(pbn->buff,pbn->index,1,stdout);
getchar();
big_num_free(&pbn);
return 0;
}