大整数加法的源代码合集!

来源:互联网 发布:java 打war包 编辑:程序博客网 时间:2024/05/01 21:15

首先引用 http://bbs.bccn.net/thread-291142-1-1.html 的 #9的一句话:

其实就是用字符串来模拟数的相加,先判定输入的数的位数,对齐后个位相加(注意ASCII码的变化),然后要进位的进位就行了··懂了后就完善下自己写的代码咯···可以是负数,有小数。


然后 下面的源码是 “让字符串 num1 = "9876543210",字符串 num2 = "1234567890",结果保存在字符串result中。要求编程实现上述高精度的十进制加法。” 来自于 http://www.nowamagic.net/librarys/veda/detail/417

#include<stdio.h>#include<string.h>int NumAdd(const char *first,const char *second,char *result, int resultlen){int numlen[2]; numlen[0] = strlen(first); numlen[1] = strlen(second);int maxlen; maxlen=  numlen[0]> numlen[1] ?  numlen[0] : numlen[1] ;      if (resultlen< maxlen + 1)       return -1;  int n; int byteValue[2]; int curByteResult;  int addByteResult;   curByteResult=addByteResult=0;//从左到右进行循环 for(n = 0; n <maxlen; n++) { --numlen[0];  --numlen[1]; if (numlen[0] >= 0) byteValue[0] = first[n] - '0' ; else  byteValue[0] = 0 ;     if (numlen[1] >= 0) byteValue[1] = second[n]- '0' ; else  byteValue[1] = 0 ;    curByteResult = byteValue[0] +  byteValue[1]; if (curByteResult>=10) { curByteResult -= 10; addByteResult = 1;  if (n==0) { result[0] = '1'; ++result; } else { ++result[n-1]; //处理进位 } result[n] = '0'+curByteResult; } else { result[n] = '0'+curByteResult ; addByteResult = 0; } } result[n] =0;return 1;}int main( ){char szstr1[]="9876543210";char szstr2[]="1234567890";char result[100]; NumAdd(szstr1,szstr2,result,100);    printf("result is %s ",result);return 0;}
我觉得可以改一下然后直接用 恩 妥儿妥儿的~


再然后就是 “先将两个字符串逆置,之后再逐位相加并存入另一个字符串中(相加过程中要检查是否有进位),最后将得到的字符串逆置就可以得到结果(其实可以从字符串末尾开始倒着计算,不过逆置之后再求值感觉更顺畅些,思维惯性使然)。

来源于 http://www.cnblogs.com/liangchao/archive/2012/09/13/2683716.html

#include<stdio.h>#include<string.h>void reverse( char *s )        /*将字符串逆置*/{    int length;    int i = 0;    char temp;    length = strlen( s );    while( i < length - i - 1 )    {        temp = s[i];        s[i] = s[length - i - 1];        s[length - i - 1] = temp;        i++;    }}void AddBigNum( char* s1, char* s2, char* result ){    int len1 = strlen( s1 );    int len2 = strlen( s2 );    int acc = 0, temp, i;        /*acc为进位标记*/    if( s1 == NULL || s2 == NULL || result == NULL )    {        return;    }    reverse( s1 );    reverse( s2 );    for( i = 0; i < len1 && i < len2; i++ )    {        temp = s1[i] - '0' + s2[i] - '0' + acc;        /*计算每位的实际和*/        result[i] = temp % 10 + '0';        /*通过求余数来确定每位的最终值*/        if( temp >= 10 )        /*通过这个if..else..条件来判断是否有进位,并设置进位值*/            acc = 1;        else            acc = 0;    }    if( i < len1 )        /*两个加数位数不同*/    {        for( ; i < len1; i++ )        {            temp = s1[i] - '0' + acc;        /*依旧要考虑进位,比如9999 + 1的情况*/            result[i] = temp % 10 + '0';            if( temp >= 10 )                        acc = 1;            else                acc = 0;        }    }    if( i < len2 )    {        for( ; i < len2; i++ )        {            temp = s2[i] - '0' + acc;            result[i] = temp % 10 + '0';            if( temp >= 10 )                acc = 1;            else                acc = 0;        }    }    if( acc == 1 )        /*考虑如:123 + 911 = 1034的情况,如果不增加这个条件会得到结果为034,进位被舍弃*/        result[i++] = ‘1’;    result[i] = '\0';    reverse( result );}main(){    char s1[] = "138999129837281929381892";    char s2[] = "121212123172837184345";    char result[100];    AddBigNum( s1, s2, result );    printf( "%s\n", result );}

最后隆重推荐一种 利用我喜欢的位移的方法~因为玩单片机久了难免有偏爱嘛

来自于http://hi.baidu.com/lazync/item/4d7c20cf698c83d9ee183ba9 (别人07年写的 ╮(╯▽╰)╭ 现在隔了七年了被我翻粗来。。。)


#include<string.h>#include<stdio.h>#include<math.h>#include<iostream.h>#include<assert.h>//大整数的加法#define MAX 20 //MAX -1 位大整数 struct Integer{    char st[MAX];// [0,100]     int begin ; };void print(struct Integer &a){    cout<<a.st<<endl;}void input(struct Integer *a){    cin>>a->st;        int len = strlen(a->st);    assert(len <= MAX - 1);    a->begin = MAX- 1 -len;    for(int i = MAX-2; i>= a->begin;i--){        a->st[i] = a->st[len -1 + i - MAX + 2];                    }    i = 0 ;    while(i++<a->begin) a->st[i-1] = '0' ;    a->st[MAX -1 ] = 0 ;    }void add(struct Integer *a ,struct Integer *b){ // a+b再存在b里面    int carry = 0 ;    int begin = a->begin > b->begin ? b->begin:a->begin ;    for(int i=MAX-2; i>=begin; i--){       int tmp =a->st[i]-'0'+b->st[i]-'0';         b->st[i] = (tmp + carry)%10 + '0';         carry = (tmp + carry)/10;    }            b->begin = i+1 ;    if(carry>0 ){        if(i>=0) {            b->st[i] = carry + '0';            b->begin = i;        }        else cout<<"Overflow ..."<<endl;    }    }int main(){        while(1){        struct Integer sa ,sb ;        input(&sa);        input(&sb);        cout<<"The two number are:"<<endl;            print(sa);        print(sb);        add(&sa,&sb);        cout<<"Their sum is :"<<endl;        print(sb);        cout<<"________________________________"<<endl;    }    return 0;}





//大概就是这些了 编程之美资格赛的第二题感觉就是考大整数存储方面的╮(╯_╰)╭ 恩 比较解题思路很简单嘛 还是数据结构和算法为核心
//↖(^ω^)↗ 司空徵

0 0
原创粉丝点击