大数加减乘模板

加法:

 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     if(flag) str3[len3++] = '1';
16     for(int i = 0; i < len3/2; i ++) {
17         swap(str3[i],str3[len3-i-1]);
18     }
19     return str3;
20 }

 

减法:

 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     while(str3[len3-1] == '0') {
29         str3[--len3] = '\0';
30     }
31     if(!flag) str3[len3++] = '-';
32     for(int i = 0; i < len3/2; i ++) swap(str3[i], str3[len3-i-1]);
33     return str3;
34 }

 

乘法:

 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 }

 

 1 std::string StringMul(const std::string& left, const std::string& right) { 
 2     bool flag1 = (left[0] == '-'), flag2 = (right[0] == '-');
 3     string s1, s2, result;
 4     if(flag1) s1 = left.substr(1,left.length());
 5     else s1 = left;
 6     if(flag2) s2 = right.substr(1,right.length());
 7     else s2 = right;
 8     int len1 = s1.length();
 9     int len2 = s2.length();
10     int len3 = len1+len2;
11     vector<int> num(len3+2, 0);
12     for(int i = 0; i < len1/2; i ++) swap(s1[i],s1[len1-i-1]);
13     for(int i = 0; i < len2/2; i ++) swap(s2[i],s2[len2-i-1]);
14     for(int i = 0; i < len1; i ++) {
15         for(int j = 0; j < len2; j ++) {
16             num[i+j] += (s1[i]-'0')*(s2[j]-'0');
17         }
18     }
19     for(int i = 0; i < len3; i ++) {
20         if(num[i] >= 10) {
21             num[i+1] += num[i]/10;
22             num[i] %= 10;
23         }
24     }
25     
26     while(num[len3] == 0 && len3 >= 0) len3--;
27     len3++;
28     for(int i = 0; i < len3/2; i ++) {
29         swap(num[i], num[len3-i-1]);
30     }
31     for(int i = 0; i < len3; i ++) {
32         result = result + char(num[i]+'0');
33     }
34     if(!len3) return "0";
35     if((flag1&&!flag2) || (!flag1&&flag2)) result = '-' + result;
36     return result;
37 }
View Code

 

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 }

 

posted @ 2018-04-19 21:46  starry_sky  阅读(251)  评论(0编辑  收藏  举报