做了一整套高精度的内容,对高进度的理解也更深层次了一点下面把高精度的几种运算总结一下
一 .加法
这无非是所有高精度运算中最简单的一种运算,我们的处理方法是字符串存入,数组倒序,然后再模拟即可

1 // File Name: 加法.c 2 // Author: darkdream 3 // Created Time: 2013年01月28日 星期一 21时22分47秒 4 5 #include<stdio.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<time.h> 9 #include<math.h> 10 void add(char a[],char b[]) 11 { 12 int *x , *y ,i , j ,temp ,k ; 13 k = strlen(a) >strlen(b)? strlen(a)+2:strlen(b)+2; 14 x = (int*)malloc(sizeof(int)*k); 15 y = (int*)malloc(sizeof(int)*k); 16 memset(x,0,sizeof(x)); 17 memset(y,0,sizeof(y)); 18 for (i = 0 ; i < strlen(a) ; i ++) 19 x[i] = a[strlen(a) -i-1] -'0'; 20 for (i = 0 ; i < strlen(b) ; i ++) 21 y[i] = b[strlen(b) -i-1] -'0'; 22 temp = 0 ; //进位 23 for (i = 0 ; i < k ; i++) 24 { 25 int t = x[i] + y[i] + temp; 26 temp = t / 10; 27 x[i] = t % 10; 28 29 }//加法部分 30 printf("%d\n",x[k-1]); 31 for (j = k-1 ; j>= 1; j--) 32 if (x[j] != 0) 33 break; 34 for (; j>= 0; j--) 35 printf("%d",x[j]); 36 printf("\n");//输出 37 } 38 39 int main(){ 40 char a[100], b[100]; 41 gets(a); 42 gets(b); 43 add(a,b); 44 45 46 return 0 ; 47 }
二 .减法
这里我们要用大数减去小数然后输出时再加符号,也是简单模拟,代码如下!

1 // File Name: 减法.c 2 // Author: darkdream 3 // Created Time: 2013年01月28日 星期一 11时20分08秒 4 5 #include<stdio.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<time.h> 9 #include<math.h> 10 int cmp(char a[], char b[]) 11 { 12 if(strlen(a) > strlen(b)) 13 return 1; 14 if(strlen(a) == strlen(b)) 15 { if (strcmp(a,b) > 0 ) 16 return 1; 17 else if (strcmp(a,b) == 0) 18 return 0; 19 else return -1; 20 } 21 return -1; 22 } 23 void sub(char a[], char b[],int t ) 24 { 25 int i ; 26 int len = (strlen(a) > strlen(b))?strlen(a):strlen(b); 27 int *s; 28 s = (int*)malloc(sizeof(int)*(len+1)); 29 for(i = 0 ; i < strlen(a)/2; i++) 30 { 31 char temp; 32 temp = a[strlen(a) -i -1]; 33 a[strlen(a)-i-1] = a[i]; 34 a[i] = temp; 35 } 36 37 for(i = 0 ; i < strlen(b)/2; i++) 38 { 39 char temp; 40 temp = b[strlen(b) -i -1]; 41 b[strlen(b)-i-1] = b[i]; 42 b[i] = temp; 43 } 44 for (i = 0 ;i <= len; i++) 45 { 46 if (a[i] =='\0') 47 a[i] = '0'; 48 if (b[i] == '\0') 49 b[i] = '0'; 50 } 51 memset(s,0,sizeof(s)); 52 for (i = 0 ; i < len-1 ; i++) 53 { 54 s[i] = s[i] + a[i] - b[i]; 55 if (s[i] < 0 ) 56 { 57 s[i] += 10 ; 58 s[i+1]--; 59 } 60 61 } 62 s[i] = s[i]+ a[i] - b[i]; 63 if (!t) 64 printf("-"); 65 66 67 while (len > 1 && s[len-1] ==0 ) 68 len = len -1; 69 for (i = len - 1 ;i >=0; i--) 70 printf("%d",s[i]); 71 } 72 73 int main(){ 74 char a[100]; 75 char b[100]; 76 gets(a); 77 gets(b); 78 if (cmp(a,b) == 0) 79 printf("0\n"); 80 else if (cmp(a,b) > 0) 81 sub(a,b,1); 82 else sub(b,a,0); 83 return 0 ; 84 }
三 .乘法
乘法是比较麻烦的一部分也是模拟,但是中间值不用进位,乘后再进位!这里贴出网上看到的代码:http://sumile.blog.hexun.com/62509182_d.html

1 View Code 2 3 void multiply(char* a,char* b,char* c) 4 { 5 int i,j,ca,cb,*s; 6 ca=strlen(a); 7 cb=strlen(b); 8 s=(int*)malloc(sizeof(int)*(ca+cb)); 9 for (i=0;i<ca+cb;i++) 10 s[i]=0; 11 for (i=0;i<ca;i++) 12 for (j=0;j<cb;j++) 13 s[i+j+1]+=(a[i]-'0')*(b[j]-'0'); 14 for (i=ca+cb-1;i>=0;i--) 15 if (s[i]>=10) 16 { 17 s[i-1]+=s[i]/10; 18 s[i]%=10; 19 } 20 i=0; 21 while (s[i]==0) 22 i++; 23 for (j=0;i<ca+cb;i++,j++) 24 c[j]=s[i]+'0'; 25 c[j]='\0'; 26 free(s); 27 }
四 .除法
除法可以说是高精度运算中最困难的运算了,它分为 大数/小数 和 大数/大数 两种
1.大数/小数
还是模拟!

1 void op1(int b[], long long int n ) //b是倒序了的大数 2 { 3 int k = strlen(a); 4 long long int temp = b[0]; 5 6 int i , j = 0 , s[1000], p = 0;//p来判断第一个能除n的位置 7 for (i = 0 ;i < k ; i ++ ) 8 { 9 if (i >= 1) 10 temp = temp *10 + b[i]; 11 12 if (temp >= n || p) 13 { if (temp < n) 14 s[j++] = 0 ; 15 else 16 { 17 s[j++] = temp / n; 18 temp = temp % n ; 19 p = 1; 20 } 21 } 22 23 24 } 25 26 if (j == 0) 27 printf("0"); 28 else 29 for (i = 0; i < j ; i++) 30 printf("%d",s[i]); 31 32 }
2.大数/大数
正在学习中!