#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
using namespace std;
class VeryLongInt{
protected:
string a;
public:
VeryLongInt(){}
VeryLongInt(string x){
a.assign(x);
}
~VeryLongInt(){}
VeryLongInt operator + (const VeryLongInt &b);
VeryLongInt operator - (const VeryLongInt &b);
VeryLongInt operator * (const VeryLongInt &b);
VeryLongInt operator / (const VeryLongInt &b);
void display(){
cout << a <<endl;
}
int copy(int *a,int *b){
int i,j;
for(i = 0;i < 1000;i++)
a[i] = 0;
for(i = 999;b[i]==0;i--);
for(j = 0;j <= i;j++)
a[i] = b[i];
return i+1;
}
int compare(int *a,int *b){
int i,j;
for(i = 999;a[i] == 0;i--);
for(j = 999;b[j] == 0;j--);
if (i < j)
return -1;
else if(i > j)
return 1;
else {
for(;i >= 0;i--){
if (a[i] > b[i])
return 1;
else if(a[i] < b[i])
return -1;
}
return 0;
}
}
};
VeryLongInt VeryLongInt ::operator + (const VeryLongInt &b) {
VeryLongInt B;
int carry = 0;
string s1 = this->a;
string s2 = b.a;
int len1 = s1.size();
int len2 = s2.size();
int lenLong;
int lenShort;
if(len1 <= len2) {
lenLong = len2;
lenShort = len1;
}
else {
lenLong = len1;
lenShort = len2;
}
string s3(lenLong + 1,'0');
for(int i = 0;i <= lenShort;i ++) {
if ((s1[len1 - i] - '0') + (s2[len2 - i] - '0') + carry <= 9) {
s3[lenLong + 1 - i] = (s1[len1 - i] - '0' + s2[len2 - i] + carry);
carry = 0;
} else {
s3[lenLong + 1 - i] = (s1[len1 - i] - '0' + s2[len2 - i] - 10 + carry);
carry = 1;
}
}
if(len1 > len2){
for(int i = lenShort + 1;i <= lenLong ;i ++){
if(s1[len1-i] - '0' + carry <= 9){
s3[lenLong + 1 - i] = s1[len1-i] + carry;
carry = 0;
}
else{
s3[lenLong + 1 - i] = s1[len1-i] + carry - 10;
carry = 1;
}
}
if(carry == 1)
s3[0] = '1';
else
s3[0] = '0';
}
else if( len1 <len2 ){
for(int i = lenShort + 1;i <= lenLong ;i ++){
if(s2[len2-i] - '0' + carry <= 9){
s3[lenLong + 1 - i] = s2[len2-i] + carry;
carry = 0;
}
else{
s3[lenLong + 1 - i] = s2[len2-i] + carry - 10;
carry = 1;
}
}
if(carry == 1)
s3[0] = '1';
else
s3[0] = '0';
}
B.a.assign(s3);
return B;
}
VeryLongInt VeryLongInt ::operator - (const VeryLongInt &b){
VeryLongInt B;
int carry = 0;
string s1 = this->a;
string s2 = b.a;
int len1 = s1.size();
int len2 = s2.size();
int lenLong;
int lenShort;
if(len1 <= len2){
lenLong = len2;
lenShort = len1;
}
else {
lenLong = len1; lenShort = len2;
}
string s3(lenLong + 1,'0');
if( len1 == len2) {
int bigger = 1;
for (int i = 0; i <= len1; i++) {
if (s1[i] < s2[i]) {
bigger = 0;
break;
}
}
if (bigger) {
for (int i = 0; i <= len1; i++) {
if ((s1[len1 - i] - '0') - (s2[len2 - i] - '0') + carry >= 0) {
s3[lenLong + 1 - i] = (s1[len1 - i] + '0' - s2[len2 - i] + carry);
carry = 0;
} else {
s3[lenLong + 1 - i] = (s1[len1 - i] + '0' - s2[len2 - i] + 10 + carry);
carry = -1;
}
}
} else {
for (int i = 0; i <= len1; i++) {
if ((s2[len1 - i] - '0') - (s1[len2 - i] - '0') + carry >= 0) {
s3[lenLong + 1 - i] = (s2[len1 - i] + '0' - s1[len2 - i] + carry);
carry = 0;
} else {
s3[lenLong + 1 - i] = (s2[len1 - i] + '0' - s1[len2 - i] + 10 + carry);
carry = -1;
}
}
s3[0] = '-';
}
}
else if(len1 > len2){
for(int i = 0;i <= lenShort;i ++){
if((s1[len1-i] - '0') - (s2[len2-i] - '0') + carry >= 0){
s3[lenLong + 1 - i] = (s1[len1-i] + '0' - s2[len2-i] + carry);
carry = 0;
}
else {
s3[lenLong + 1 - i] = (s1[len1-i] + '0' - s2[len2-i] + 10 + carry);
carry = -1;
}
}
for(int i = lenShort + 1;i <= lenLong ;i ++){
if(s1[len1-i] - '0' + carry >= 0){
s3[lenLong + 1 - i] = s1[len1-i] + carry;
carry = 0;
}
else{
s3[lenLong + 1 - i] = s1[len1-i] + carry + 10;
carry = -1;
}
}
}
else if(len2 > len1){
for(int i = 0;i <= lenShort;i ++){
if((s2[len2-i] - '0') - (s1[len1-i] - '0') + carry >= 0){
s3[lenLong + 1 - i] = (s2[len2-i] + '0' - s1[len1-i] + carry);
carry = 0;
}
else {
s3[lenLong + 1 - i] = (s2[len2-i] + '0' - s1[len1-i] + 10 + carry);
carry = -1;
}
}
for(int i = lenShort + 1;i <= lenLong ;i ++){
if(s2[len2-i] - '0' + carry >= 0){
s3[lenLong + 1 - i] = s2[len2-i] + carry;
carry = 0;
}
else{
s3[lenLong + 1 - i] = s2[len2-i] + carry + 10;
carry = -1;
}
}
s3[0] = '-';
}
B.a.assign(s3);
return B;
}
VeryLongInt VeryLongInt ::operator * (const VeryLongInt &b){
VeryLongInt B;
int u[1000];
for(int i = 0;i < 1000;i++){
u[i] = 0; }
int v[1000];
for(int i = 0;i < 1000;i++){
v[i] = 0; }
int w[1000];
for(int i = 0;i < 1000;i++){
w[i] = 0; }
string str1 = this->a;
string str2 = b.a;
int i,j,k,carry = 0,tmp;
int len1 = str1.size();
int len2 = str2.size();
for(i = 0;i < len1;i++){
u[i] = str1[len1 - 1 - i] - '0';
}
for(i = 0;i < len2;i++){
v[i] = str2[len2 - 1 - i] - '0';
}
for(i = 0;i < len1;i++){
for(k = i,j = 0;j < len2;j++,k++){
w[k] += u[i] * v[j]; }
}
for(i = 0;i <= k;i++){
w[i] += carry;
tmp = w[i];
w[i] = tmp % 10;
carry = tmp/10;
}
for(i = k;w[i] == 0;i--);
for(;i >= 0;i--)
cout << w[i] ;
cout <<endl;
return B;
}
VeryLongInt VeryLongInt ::operator / (const VeryLongInt &b){
VeryLongInt B;
int u[1000];
for(int i = 0;i < 1000;i++){
u[i] = 0;
}
int v[1000];
for(int i = 0;i < 1000;i++){
v[i] = 0;
}
int w[1000];
for(int i = 0;i < 1000;i++){
w[i] = 0;
}
int i,j,tmp1,tmp2 = 0,borrow = 0;
int temp1[1000] = {0};
int temp2[1000] = {0};
string str1 = this->a;
string str2 = b.a;
int len1 = str1.size();
int len2 = str2.size();
for(i = 0;i < len1;i++)
u[i] = str1[len1 - 1 -i] - '0';
for(i = 0;i < len2;i++)
v[i] = str2[len2 - 1 -i] - '0';
if(compare(u,v) < 0){
cout << endl;
return B;
}
while(compare(u,v) >= 0){
tmp1 = len1 - len2;
if (tmp1 == tmp2&&tmp1 > 0)
tmp1--;
tmp2 = tmp1;
for(int i = 0;i < 1000;i++){
temp2[i] = 0;
}
for (i = len1 - 1; i >= tmp1; i--) {
temp2[i] = v[i - tmp1];
}
copy(temp1,u);
if(compare(temp1,temp2) < 0)
continue;
for (j = 1;;j++) {
borrow = 0;
for(i = tmp1;i < len1;i++) {
temp1[i] = u[i] - temp2[i] - borrow;
if(temp1[i] < 0){
borrow = 1;
temp1[i] += 10;
}
else
borrow = 0;
}
len1 = copy(u,temp1);
w[tmp1] = j;
if (compare(temp1,temp2) < 0)
break;
}
}
for(i = 999;w[i] == 0;i --);
for(;i >= 0;i--)
cout << w[i];
cout << endl;
}
int main(){
//string x;
//string y;
//cin >> x;
VeryLongInt A("666");
VeryLongInt B("111");
VeryLongInt result = A / B;
//result.display();
}
运行环境为clion,有无大神指出问题在哪