大数加减乘模板

Posted starry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数加减乘模板相关的知识,希望对你有一定的参考价值。

加法:

 1 char * add(char *s, char *ss) {
 2     memset(str3, 0, sizeof(str3));
 3     int len1 = strlen(s)-1;
 4     int len2 = strlen(ss)-1;
 5     int len3 = 0, x, y, flag = 0;
 6     while(len1 >= 0 || len2 >= 0) {
 7         if(len1 >= 0) x = s[len1] - 0;
 8         else x = 0;
 9         if(len2 >= 0) y = ss[len2] - 0;
10         else y = 0;
11         str3[len3++] = (x+y+flag)%10 + 0;
12         flag = (x+y+flag)/10;
13         len2--;len1--;
14     }
15     for(int i = 0; i < len3/2; i ++) {
16         swap(str3[i],str3[len3-i-1]);
17     }
18     return str3;
19 }

减法:

 1 char *sub(char *s, char *ss) {
 2     memset(str3, 0, sizeof(str3));
 3     int len1 = strlen(s)-1, len2 = strlen(ss)-1;
 4     int flag = 1, len3 = 0, x, y, cnt = 0;
 5     if(strcmp(s,ss) == 0){
 6         str3[len3++] = 0;
 7         return str3;
 8     }
 9     if(len1 < len2 || (len1==len2&&strcmp(s,ss)<0)) {
10         swap(len1,len2);
11         swap(s,ss);
12         flag = 0;
13     }
14     while(len1 >= 0 || len2 >= 0) {
15         if(len1 >= 0) x = s[len1--] - 0;
16         else x = 0;
17         if(len2 >= 0) y = ss[len2--] - 0;
18         else y = 0;
19         int ans = x-y+cnt;
20         if(ans < 0) {
21             str3[len3++] = ans+10+0;
22             cnt = -1;
23         } else {
24             str3[len3++] = ans+0;
25             cnt = 0;
26         }
27     }
28     if(!flag) str3[len3++] = -;
29     for(int i = 0; i < len3/2; i ++) swap(str3[i], str3[len3-i-1]);
30     return str3;
31 }

乘法:

 1 int mul(char *s, char *ss,int *num) {
 2     int len1 = strlen(s);
 3     int len2 = strlen(ss);
 4     int len3 = len1+len2;
 5     for(int i = 0; i < len1/2; i ++) swap(s[i],s[len1-i-1]);
 6     for(int i = 0; i < len2/2; i ++) swap(ss[i],ss[len2-i-1]);
 7     for(int i = 0; i < len1; i ++) {
 8         for(int j = 0; j < len2; j ++) {
 9             num[i+j] += (s[i]-0)*(ss[j]-0);
10         }
11     }
12     for(int i = 0; i < len3; i ++) {
13         if(num[i] >= 10) {
14             num[i+1] += num[i]/10;
15             num[i] %= 10;
16         }
17     }
18     while(num[len3] == 0 && len3 >= 0) len3--;
19     len3++;
20     for(int i = 0; i < len3/2; i ++) {
21         swap(num[i], num[len3-i-1]);
22     }
23     if(!len3) len3++;
24     return len3;
25 }

 

Java的加减乘法:

 1 import java.util.Scanner;
 2 import java.math.BigDecimal;
 3 import java.math.BigInteger;
 4 
 5 public class Main{
 6     public static void main(String[] args) {
 7         Scanner cin = new Scanner(System.in);
 8         BigDecimal a = cin.nextBigDecimal();
 9         BigDecimal b = cin.nextBigDecimal();
10         BigInteger aa = cin.nextBigInteger();
11         BigInteger bb = cin.nextBigInteger();
12         System.out.println("a+b:"+a.add(b));
13         System.out.println("a-b:"+a.subtract(b));
14         System.out.println("a*b:"+a.multiply(b));
15         System.out.println("a/b:"+aa.divide(bb));
16         System.out.println("a^10:"+a.pow(10));
17     }
18 }

 

以上是关于大数加减乘模板的主要内容,如果未能解决你的问题,请参考以下文章

大数加减乘模板

大数模板(加减乘除幂次开方)

大数运算(加减乘除)

大数(高精度)加减乘除

大数运算实现加减乘除四则运算

c++实现大数加减乘除四则运算