站长网 大数据 C语言超大数相加求和、加减乘除算法实现

C语言超大数相加求和、加减乘除算法实现

#include?stdio.h#include?stdbool.h#include?string.h#include?stdlib.h#define?MAXLEN?20int?arr1[MAXLEN];int?arr2[MAXLEN];char?str1[MAXLEN];char?str2[MAXLEN];void?convertBin(int?intNum)?{???static?int?bitSize?=?32;???int?modBin?=?intNum??1;

#include?<stdio.h>
#include?<stdbool.h>
#include?<string.h>
#include?<stdlib.h>

#define?MAXLEN?20
int?arr1[MAXLEN];
int?arr2[MAXLEN];
char?str1[MAXLEN];
char?str2[MAXLEN];


void?convertBin(int?intNum)?{

???static?int?bitSize?=?32;
???int?modBin?=?intNum?&?1;
???int?modNum?=?intNum?>>?1;
???
???if(bitSize?--)
??????convertBin(modNum);
???printf("%d",modBin);
}

int?add(int?a,int?b)?{
???return?b???add(a^b,(a&b)<<1)?:?a;
}

int?complementNum(int?b)?{

???return?add(~b,1);
}
int?Min(int?a,?int?b)?{
???return?add(a,complementNum(b));
}

int?Mul(int?a,int?b)?{
???
???int?sum?=?0;
???int?isNeg?=?(?a?>?0)?^?(?b?>?0);
???unsigned?int?num?=?(?a?>?0)???a?:?complementNum(a);
???unsigned?int?numAdd?=?(?b?>?0?)???b?:?complementNum(b);

???while(numAdd)?{
??????if(numAdd?&?1)
?????????sum?=?add(num,sum);
??????numAdd?=?numAdd?>>?1;
??????num?=?num?<<?1;
???}
???return?isNeg???complementNum(sum)?:?sum;
}

int?Pow(int?a,?int?b)?{

???int?sum?=?1,?q?=?a;
???while(b)?{
??????if(b?&?1)
?????????sum?=?Mul(sum,q);
??????b?>>=?1;
??????q?=?Mul(q,q);
???}
???return?sum;
}

int?division(int?a,?int?b)?{

???int?num?=?0;
???int?isNeg?=?(?a?>?0)?^?(?b?>?0);
???unsigned?int?num1?=?a?>?0???a?:?complementNum(a);
???unsigned?int?num2?=?b?>?0???b?:?complementNum(b);
???while(num1)?{
??????if(?num1?>=?num2)?{
?????????num1?=?Min(num1,num2);
?????????num?=?add(1,num);
??????}?else
?????????break;
???}
???
???return?isNeg???complementNum(num)?:?num;
}

int?Mod(int?a,int?b){
???int?isNeg?=?(?a?>?0)?^?(?b?>?0);
???unsigned?int?num1?=?a?>?0???a?:?complementNum(a);
???unsigned?int?num2?=?b?>?0???b?:?complementNum(b);
???int?modNum;
???while(num1)?{
??????modNum?=?num1;
??????if(num1?>=?num2)?{
?????????num1?=?Min(num1,num2);
??????}?else?{
?????????break;
??????}
???}
???return?isNeg???complementNum(modNum)?:?modNum;
}

int?main(void)?{

???//大数求和有待封装
???printf("Please?Input?Str1:\n");
???scanf("%s",&str1);
???printf("Please?Input?Str2:\n");
???scanf("%s",&str2);

???int?strLen1?=?strlen(str1);
???int?strLen2?=?strlen(str2);

???int?i,k;
???int?j?=?0,?l?=?0;
???for(i?=?strLen1?-?1;?i>=0;i--)?{
??????arr1[j++]?=?str1[i]?-?'0';
???}

???for(k?=?strLen2?-?1;?k>=0;k--)?{
??????arr2[l++]?=?str2[k]?-?'0';
???}



???for(i=0;i<MAXLEN;i++)?{
??????arr1[i]?+=?arr2[i];
??????if(arr1[i]?>=?10)?{
?????????arr1[i]?-=?10;
?????????arr1[i+1]?++;
??????}
???}

???_Bool?skipFlag?=?false;
???for(i=MAXLEN?-1;i>=0;i--)?{
??????if(skipFlag)?{
?????????printf("%d",arr1[i]);
??????}?else?if(arr1[i])?{
?????????printf("%d",arr1[i]);
?????????skipFlag?=?true;
??????}
???}

???if(!skipFlag)
??????printf("0");
???
???return?0;
???int?intNum,addNum,minNum,num1,num2;
???printf("Please?Input?an?Integer:\n");
???scanf("%d",&num1);
???printf("Please?Input?another?Integer:\n");
???scanf("%d",&num2);
???printf("%d?/?%d?=?%d\n",?num2,?division(num1,num2));
???printf("%d?求余?%d?=?%d\n",?Mod(num1,num2));
???//printf("%d?^?%d?=?%d\n",num2,Pow(num1,num2));
???//addNum?=?add(num1,num2);
???//minNum?=?Min(num1,num2);
???//printf("%d?+?%d?=?%d\n",?addNum);
???//printf("%d?-?%d?=?%d\n",?minNum);
???//scanf("%d",&intNum);
???//printf("%d?&?1?=?%d??%d?&?0x01?=?%d",num1&1,num1&0x01);
???//printf("%d\n",intNum?>>=1);
???//convertBin(intNum);
???//printf("\n");
???return?0;
}

本文来自网络,不代表站长网立场,转载请注明出处:https://www.zwzz.com.cn/html/shuju/2021/0527/6940.html

作者: dawei

【声明】:站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部