#include<iostream>#include<string>#include<vector>usingnamespace std;
vector<int> A, B, C;voidinit(){
string a, b;
cin >> a >> b;for(int i = a.size()-1; i >=0;--i)
A.push_back(a[i]-'0');for(int i = b.size()-1; i >=0;--i)
B.push_back(b[i]-'0');}//大数加法voidadd(){int t =0;//进位for(int i =0; i < A.size()|| i < B.size();++i){if(i < A.size())
t += A[i];if(i < B.size())
t += B[i];
C.push_back(t %10);
t /=10;}if(t)
C.push_back(1);//如果最高位还有进位,在数组后面补1即可}//判断A、B大小boolisbig(){if(A.size()!= B.size())return A.size()> B.size();for(int i = A.size()-1; i >=0;--i)if(A[i]!= B[i])return A[i]> B[i];returntrue;}//大数减法voidsub()//A>=B{int t =0;//借位for(int i =0, t =0; i < A.size();++i){
t = A[i]- t;if(i < B.size())
t -= B[i];
C.push_back((t +10)%10);//若t<0,借位+10,若t>=0,+10余10仍是tif(t <0)
t =1;else
t =0;}while(C.size()>1&& C.back()==0)//去除前导零
C.pop_back();}voidprint(){for(int i = C.size()-1; i >=0;--i)
cout << C[i];
cout << endl;}signedmain(){init();add();print();
A.clear();B.clear();C.clear();init();if(isbig(A, B))sub(A, B);else{
cout <<"-";sub(B, A);}print();system("pause");return0;}
高精度*低精度and高精度/低精度
#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespace std;
string a;int b, r;
vector<int> A, C;voidinit(){
cin >> a >> b;for(int i = a.size()-1; i >=0;--i)
A.push_back(a[i]-'0');}//高精度*低精度voidmul(){int t =0;//进位for(int i =0; i < A.size()|| t;++i){if(i < A.size())
t += A[i]* b;//将b与A[i]相乘,而不是b的其中一位与A[i]相乘
C.push_back(t %10);//相乘后只取最后一位的数
t /=10;}}//高精度/低精度voiddiv(){
r =0;//余数for(int i = A.size()-1; i >=0;--i){
r = r *10+ A[i];
C.push_back(r / b);
r %= b;}reverse(C.begin(), C.end());//除法从最高位开始算的,所以最后要反转一下while(C.size()>1&& C.back()==0)
C.pop_back();}voidprint(){for(int i = C.size()-1; i >=0;--i)
cout << C[i];
cout << endl;}signedmain(){init();mul();print();
A.clear();C.clear();init();div();print();
cout <<"余数:"<< r << endl;system("pause");return0;}
高精度*高精度
#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespace std;
string num1, num2;int a[10000], b[10000], c[10000];int lena, lenb, lenc;voidinit(){
cin >> num1 >> num2;
lena = num1.size(), lenb = num2.size();for(int i = lena -1, j =0; i >=0;--i,++j)
a[j]= num1[i]-'0';for(int i = lenb -1, j =0; i >=0;--i,++j)
b[j]= num2[i]-'0';}//高精度*高精度voidmul(){for(int i =0; i < lena ;++i){for(int j =0; j < lenb;++j){
c[i + j]+= a[i]* b[j];
c[i + j +1]+= c[i + j]/10;//进位
c[i + j]%=10;//实际保留的值}}}voidprint(){
lenc = lena + lenb -1;//两数相乘最大位数是lena+lenb,-1是因为下标从0开始while(c[lenc]==0&& lenc >0)//去前导零
lenc--;for(int i = lenc; i >=0;--i)
cout << c[i];
cout << endl;}signedmain(){init();mul();print();system("pause");return0;}
高精度/高精度
#include<iostream>#include<string>#include<cstring>usingnamespace std;
string num1, num2;int a[105], b[105], c[105], tmp[105];voidinit(){
cin >> num1 >> num2;
a[0]= num1.size(), b[0]= num2.size();for(int i = a[0], j =0; i >0;--i,++j)
a[i]= num1[j]-'0';for(int i = b[0], j =0; i >0;--i,++j)
b[i]= num2[j]-'0';}voidcopyitmp(int len){for(int i =1; i <= b[0];++i)
tmp[i + len -1]= b[i];
tmp[0]= b[0]+ len -1;}intcmp(){if(a[0]> tmp[0])return1;if(a[0]< tmp[0])return-1;for(int i = a[0]; i >0;--i){if(a[i]> b[i])return1;if(a[i]< b[i])return-1;}return0;}voiddivision(){int cmp_res =cmp();if(cmp_res ==0)//二者相等{
a[0]=0;return;}if(cmp_res ==1)//做减法操作{for(int i =1; i <= a[0];++i){if(a[i]< tmp[i])//不够则向上借一位{
a[i +1]--;
a[i]+=10;}if(a[i]>= tmp[i])
a[i]-= tmp[i];}while((a[a[0]]==0)&& a[0]>0)//去前导零
a[0]--;return;}}voidfunc(){
c[0]= a[0]- b[0]+1;for(int i = c[0]; i >0;--i){memset(tmp,0,sizeof(tmp));copyitmp(i);//将b数组右移i位放到tmp数组中while(cmp()>=0){
c[i]++;division();}}while((c[c[0]]==0)&& c[0]>0)//去前导零
c[0]--;}voidprint(int arr[]){if(arr[0]==0)
cout <<0<< endl;else{for(int i = arr[0]; i >0;--i)
cout << arr[i];
cout << endl;}}intmain(){init();func();
cout <<"商为:";print(c);
cout <<"余数为:";print(a);system("pause");return0;}