#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",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; }
C语言超大数相加求和、加减乘除算法实现
精选 原创
©著作权归作者所有:来自51CTO博客作者mrc_elite的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
初识C语言
简述初识c语言的心得
初识c语言 -
实现加减乘除计算器。(c语言)
#include<stdio.h>#include
加减乘除 计算器 C语言 #include 运算符