C语言超大数相加求和、加减乘除算法实现-创新互联
#include#include #include #include #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 = 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",num1, num2, division(num1,num2)); printf("%d 求余 %d = %d\n",num1, num2, Mod(num1,num2)); //printf("%d ^ %d = %d\n",num1,num2,Pow(num1,num2)); //addNum = add(num1,num2); //minNum = Min(num1,num2); //printf("%d + %d = %d\n",num1, num2, addNum); //printf("%d - %d = %d\n",num1, num2, minNum); //scanf("%d",&intNum); //printf("%d & 1 = %d %d & 0x01 = %d",num1,num1&1,num1,num1&0x01); //printf("%d\n",intNum >>=1); //convertBin(intNum); //printf("\n"); return 0; }
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的利辛网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!当前文章:C语言超大数相加求和、加减乘除算法实现-创新互联
当前链接:http://scjbc.cn/article/ddpese.html