大数加法、减法、乘法、除法、求余、比较、乘方(代码简短(总计150)、易懂)
前言:
大数的操作大部分人都知道,而且网上也有许多代码,但是那些大多比较冗长,吓到了许多同学们。。。所以特意写了个比较简短易懂的代码,代码基本上没问题,不过只支持正整数的输入数据。
样例:
12345 12
add:12357
subtract:12333
multiply:148140
devide:1028 remainder:9
power:12528328468279800008275836185265394560488525390625
#include <cstdio>#include <iostream>#include <cstring>#include <cstdlib>using namespace std;#define SWAP(a,b) string c; c=a; a=b; b=c //交换字符串int compare(string a, string b){ //大数比较if(a.length()<b.length()) { return 0; } else if(a.length()==b.length()) { if(a<b) return 0; }return 1;}string add(string a, string b) { //大数加法 if(!compare(a,b)) {SWAP(a,b);} //保证a的长度大于b int *ad = new int[a.length()+1]; memset(ad,0,sizeof(int)*(a.length()+1)); //初始化 int i, j; for(i=0; i<a.length()-b.length(); i++) //将a长度大于b的部分传过去 ad[i+1] = a[i]-'0'; for(j=0; i<a.length(); i++, j++) //将a与b匹配的部分相加再传 ad[i+1] = a[i]-'0'+b[j]-'0'; for(i=a.length(); i>0; i--) { //进位操作 if(ad[i]>9) { ad[i-1] += ad[i]/10; ad[i] %= 10; } } char *c = new char[a.length()+1]; //定义一个字符数组保存结果 i = 0, j=0; while(ad[i]==0) i++; //去除前导0 while(i<a.length()+1) c[j++] = ad[i++]+'0'; c[j] = '\0'; if(c[0]=='\0') {c[1]='\0'; c[0]='0';} //如果结果为0 free(ad); //释放ad数组 string s(c); //将c转换成string类型 return s;}string subtract(string a, string b) { int flag = 0, i, j; if(!compare(a,b)){ //如果a<b,交换a、b并标识结果为负 flag = 1; SWAP(a,b); } int *su = new int[a.length()]; memset(su,0,sizeof(int)*a.length()); //初始化 for(i=0; i<a.length()-b.length(); i++) //将a长度大于b的部分传过去 su[i] = a[i]-'0'; for(j=0; i<a.length(); i++, j++) //将a与b匹配的部分相减再传 su[i] = a[i]-b[j]; for(i=a.length()-1; i>0; i--) { //进位操作 if(su[i]<0) { su[i-1]--; su[i] += 10; } } char *c = new char[a.length()+2]; i=0, j=0; if(flag) c[j++] = '-'; //如果是负数,c[0]='-' while(su[i]==0) i++; //后面同加法 while(i<a.length()) c[j++] = su[i++]+'0'; c[j] = '\0'; if(c[0]=='\0') {c[1]='\0'; c[0]='0';} free(su); string s(c); return s;}string multiply(string a, string b) { int *mu = new int[a.length()+b.length()]; memset(mu,0,sizeof(int)*(a.length()+b.length())); for(int i=0; i<a.length(); i++) //将数字两两相乘 for(int j=0; j<b.length(); j++) mu[i+j+1] += (a[i]-'0')*(b[j]-'0'); for(int i=a.length()+b.length()-1; i>0; i--) { //进位操作 if(mu[i]>9) { mu[i-1] += mu[i]/10; mu[i] %= 10; } } //后面同加法 char *c = new char[a.length()+b.length()]; int i = 0, j = 0; while(mu[i]==0) i++; while(i<a.length()+b.length()) c[j++] = mu[i++]+'0'; c[j] = '\0'; if(c[0]=='\0') {c[1]='\0'; c[0]='0';} free(mu); string s(c); return s;}string *devide(string a, string b) { string *de = new string[2]; de[0]="0"; de[1] = b; string one="1", ten="10"; if(!compare(a,b)) return de; while(a[0]!='-') { de[1] = a; a = subtract(a,b); de[0] = add(de[0],one); } de[0] = subtract(de[0],one); return de;}string power(string a, string b) { string c="1", one="1"; while(b[0]!='0') { c = multiply(c, a); b = subtract(b, one); } return c;}int main(int argc, char const *argv[]) { string a, b, c1, c2, c3, c5; string *c4; int cas; cin>>cas; while(cas--) { cin>>a>>b; c1 = add(a, b); c2 = subtract(a,b); c3 = multiply(a, b); c4 = devide(a, b); c5 = power(a, b); cout<<"add:"<<c1<<endl<<"subtract:"<<c2<<endl <<"multiply:"<<c3<<endl<<"devide:"<<c4[0]<<" remainder:"<<c4[1]<<endl <<"power:"<<c5<<endl; } return 0;}