#include<iostream>
#include<cstring>
#define MAX 65*65
using namespace std;
class doublelong{
char number[MAX];
public:
doublelong(const char *str = " ")
{
strcpy(number, str);
}
void show()
{
cout << number << endl;
}
void operator>>(doublelong a)
{
cin >> number;
}
friend istream & operator>>(istream &is, doublelong &ob);
friend ostream & operator<<(ostream &os, const doublelong &ob);
friend doublelong operator+(doublelong &ob1, doublelong &ob2);
friend doublelong operator-(doublelong &ob1, doublelong &ob2);
friend doublelong operator*(doublelong &ob1, doublelong &ob2);
friend doublelong operator/(doublelong &ob1, doublelong &ob2);
void operator+=(doublelong &ob);
void operator-=(doublelong &ob);
void operator/=(doublelong &ob);
};
void sub(char str1[], char str2[], char result[])
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1<len2)
{
char temp[MAX];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
int t = len1;
len1 = len2;
len2 = t;
}
int num1[MAX] = { 0 }, num2[MAX] = { 0 };
int i;
for (i = 0; i<len1; i++)
{
num1 = str1 - '0';
}
for (i = 0; i<len2; i++)
{
num2 = str2 - '0';
}
for (i = 0; i<len1 / 2; i++)
{
int temp = num1;
num1 = num1[len1 - 1 - i];
num1[len1 - 1 - i] = temp;
}
for (i = 0; i<len2 / 2; i++)
{
int temp = num2;
num2 = num2[len2 - 1 - i];
num2[len2 - 1 - i] = temp;
}
int len = len1>len2 ? len1 : len2;
int ans[10] = { 0 };
for (i = 0; i<len; i++)
{
if (num1 - num2<0)
{
ans = num1 + 10 - num2;
num1[i + 1] -= 1;
}
else
{
ans = num1 - num2;
}
}
for (i = len - 1; i >= 0; i--)
{
if (ans != 0)
break;
}
if (i == -1)
result[0] = '0';
else
{
int j;
if (result[0] != '-')
j = 0;
else
j = 1;
while (i >= 0)
{
result[j] = ans + '0';
j++;
i--;
}
}
}
void add(char str1[], char str2[], char result[])
{
int num1[MAX] = { 0 }, num2[MAX] = { 0 };
int len1 = strlen(str1);
int len2 = strlen(str2);
int i;
for (i = 0; i<len1; i++)
{
num1 = str1 - '0';
}
for (i = 0; i<len2; i++)
{
num2 = str2 - '0';
}
for (i = 0; i<len1 / 2; i++)
{
int temp = num1;
num1 = num1[len1 - 1 - i];
num1[len1 - 1 - i] = temp;
}
for (i = 0; i<len2 / 2; i++)
{
int temp = num2;
num2 = num2[len2 - 1 - i];
num2[len2 - 1 - i] = temp;
}
int len = len1>len2 ? len1 : len2;
int ans[MAX] = { 0 };
for (i = 0; i<len; i++)
{
ans += num1 + num2;
if (ans >= 10)
{
ans[i + 1] += ans / 10;
ans = ans % 10;
}
}
for (i = len; i >= 0; i--)
{
if (ans != 0)
break;
}
int j;
if (result[0] != '-')
j = 0;
else
j = 1;
while (i >= 0)
{
result[j] = ans + '0';
j++;
i--;
}
}
void multiply(char str1[], char str2[], char result[])
{
int num1[MAX] = { 0 };
int num2[MAX] = { 0 };
int ans[MAX] = { 0 };
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = len1>len2 ? len1 : len2;
int i, j;
for (j = 0, i = len1 - 1; i >= 0; i--)
num1[j++] = str1 - '0';
for (j = 0, i = len2 - 1; i >= 0; i--)
num2[j++] = str2 - '0';
for (i = 0; i<len1; i++)
{
for (j = 0; j<len2; j++)
ans[i + j] = ans[i + j] + num1 * num2[j];
}
for (i = 0; i<len * 2; i++)
{
if (ans >= 10)
{
ans[i + 1] = ans[i + 1] + ans / 10;
ans = ans % 10;
}
}
for (i = len * 2; i>0; i--)
{
if (ans != 0)
break;
}
if (result[0] != '-')
j = 0;
else
j = 1;
while (i >= 0)
{
result[j] = ans + '0';
j++;
i--;
}
}
int SubStract(int *p1, int *p2, int len1, int len2)
{
int i;
if (len1<len2)
return -1;
if (len1 == len2)
{
for (i = len1 - 1; i >= 0; i--)
{
if (p1>p2)
break;
else if (p1<p2)
return -1;
}
}
for (i = 0; i <= len1 - 1; i++)
{
p1 -= p2;
if (p1<0)
{
p1 += 10;
p1[i + 1]--;
}
}
for (i = len1 - 1; i >= 0; i--)
if (p1)
return i + 1;
return 0;
}
void division(char str1[], char str2[], char result[])
{
int i, j;
int nTimes;
int nTemp;
int num1[MAX] = { 0 };
int num2[MAX] = { 0 };
int ans[MAX] = { 0 };
int len1 = strlen(str1);
int len2 = strlen(str2);
for (j = 0, i = len1 - 1; i >= 0; j++, i--)
num1[j] = str1 - '0';
for (j = 0, i = len2 - 1; i >= 0; j++, i--)
num2[j] = str2 - '0';
if (len1<len2)
{
result[0] = '0';
return;
}
nTimes = len1 - len2;
for (i = len1 - 1; i >= 0; i--)
{
if (i >= nTimes)
num2 = num2[i - nTimes];
else
num2 = 0;
}
len2 = len1;
for (j = 0; j <= nTimes; j++)
{
while ((nTemp = SubStract(num1, num2 + j, len1, len2 - j)) >= 0)
{
len1 = nTemp;
ans[nTimes - j]++;
}
}
for (i = 9; i >= 0; i--)
{
if (ans != 0)
break;
}
if (result[0] != '-')
j = 0;
else
j = 1;
while (i >= 0)
{
result[j] = ans + '0';
j++;
i--;
}
}
istream & operator>>(istream &is, doublelong &ob)
{
is >> ob.number;
return is;
}
ostream & operator<<(ostream &os, const doublelong &ob)
{
os << ob.number;
return os;
}
doublelong operator+(doublelong &ob1, doublelong &ob2)
{
char result[MAX];
char str1[MAX], str2[MAX];
strcpy(str1, ob1.number);
strcpy(str2, ob2.number);
if (str1[0] == '-'&&str2[0] == '-')
{
result[0] = '-';
add(str1 + 1, str2 + 1, result);
}
else if (str1[0] == '-'&&str2[0] != '-')
{
int len1 = strlen(str1 + 1);
int len2 = strlen(str2);
if (len1 == len2)
{
if (strcmp(str1 + 1, str2)>0)
{
result[0] = '-';
sub(str1 + 1, str2, result);
}
else
{
sub(str2, str1 + 1, result);
}
}
else if (len1>len2)
{
result[0] = '-';
sub(str1 + 1, str2, result);
}
else
{
sub(str1 + 1, str2, result);
}
}
else if (str1[0] != '-'&&str2[0] == '-')
{
int len1 = strlen(str1);
int len2 = strlen(str2 + 1);
if (len1 == len2)
{
if (strcmp(str1, str2 + 1)>0)
{
sub(str1, str2 + 1, result);
}
else
{
result[0] = '-';
sub(str2 + 1, str1, result);
}
}
else if (len1<len2)
{
result[0]='-';
sub(str1, str2 + 1, result);
}
else
{
sub(str1, str2 + 1, result);
}
}
else if (str1[0] != '-'&&str2[0] != '-')
{
add(str1, str2, result);
}
return doublelong(result);
}
doublelong operator-(doublelong &ob1, doublelong &ob2)
{
char result[MAX];
char str1[MAX], str2[MAX];
strcpy(str1, ob1.number);
strcpy(str2, ob2.number);
if (str1[0] != '-'&&str2[0] == '-')
{
add(str1, str2 + 1, result);
}
else if (str1[0] == '-'&&str2[0] != '-')
{
result[0] = '-';
add(str1 + 1, str2, result);
}
else if (str1[0] != '-'&&str2[0] != '-')
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 == len2)
{
if (strcmp(str1, str2)>0)
{
sub(str1, str2, result);
}
else
{
result[0] = '-';
sub(str2, str1, result);
}
}
else if (len1>len2)
{
sub(str1, str2, result);
}
else
{
result[0] = '-';
sub(str1, str2, result);
}
}
else
{
int len1 = strlen(str1 + 1);
int len2 = strlen(str2 + 1);
if (len1 == len2)
{
if (strcmp(str2 + 1, str1 + 1) >= 0)
{
sub(str2 + 1, str1 + 1, result);
}
else
{
result[0] = '-';
sub(str1 + 1, str2 + 1, result);
}
}
else if (len1>len2)
{
result[0] = '-';
sub(str1 + 1, str2 + 1, result);
}
else
{
sub(str1 + 1, str2 + 1, result);
}
}
return doublelong(result);
}
doublelong operator*(doublelong &ob1, doublelong &ob2)
{
char result[MAX];
char str1[MAX], str2[MAX];
strcpy(str1, ob1.number);
strcpy(str2, ob2.number);
if (strcmp(str1, "0") == 0 || strcmp(str2, "0") == 0)
{
return doublelong("0");
}
if (str1[0] != '-'&&str2[0] == '-')
{
result[0] = '-';
multiply(str1, str2 + 1, result);
}
else if (str1[0] == '-'&&str2[0] != '-')
{
result[0] = '-';
multiply(str1 + 1, str2, result);
}
else if (str1[0] == '-'&&str2[0] == '-')
{
multiply(str1 + 1, str2 + 1, result);
}
else
{
multiply(str1, str2, result);
}
return doublelong(result);
}
doublelong operator/(doublelong &ob1, doublelong &ob2)
{
char result[MAX];
char str1[MAX], str2[MAX];
strcpy(str1, ob1.number);
strcpy(str2, ob2.number);
if (str1[0] != '-'&&str2[0] == '-')
{
result[0] = '-';
division(str1, str2 + 1, result);
}
else if (str1[0] == '-'&&str2[0] != '-')
{
result[0] = '-';
division(str1 + 1, str2, result);
}
else if (str1[0] == '-'&&str2[0] == '-')
{
division(str1 + 1, str2 + 1, result);
}
else
{
division(str1, str2, result);
}
return doublelong(result);
}
void doublelong::operator+=(doublelong &ob)
{
char result[MAX];
char str1[MAX], str2[MAX];
strcpy(str1, this->number);
strcpy(str2, ob.number);
if (str1[0] == '-'&&str2[0] == '-')
{
result[0] = '-';
add(str1 + 1, str2 + 1, result);
}
else if (str1[0] == '-'&&str2[0] != '-')
{
int len1 = strlen(str1 + 1);
int len2 = strlen(str2);
if (len1 == len2)
{
if (strcmp(str1 + 1, str2)>0)
{
result[0] = '-';
sub(str1 + 1, str2, result);
}
else
{
sub(str2, str1 + 1, result);
}
}
else if (len1>len2)
{
result[0] = '-';
sub(str1 + 1, str2, result);
}
else
{
sub(str1 + 1, str2, result);
}
}
else if (str1[0] != '-'&&str2[0] == '-')
{
int len1 = strlen(str1);
int len2 = strlen(str2 + 1);
if (len1 == len2)
{
if (strcmp(str1, str2 + 1)>0)
{
sub(str1, str2 + 1, result);
}
else
{
result[0] = '-';
sub(str2 + 1, str1, result);
}
}
else if (len1<len2)
{
result[0]='-';
sub(str1, str2 + 1, result);
}
else
{
sub(str1, str2 + 1, result);
}
}
else if (str1[0] != '-'&&str2[0] != '-')
{
add(str1, str2, result);
}
strcpy(this->number, result);
}
void doublelong::operator-=(doublelong &ob)
{
char result[MAX];
char str1[MAX], str2[MAX];
strcpy(str1, this->number);
strcpy(str2, ob.number);
if (str1[0] != '-'&&str2[0] == '-')
{
add(str1, str2 + 1, result);
}
else if (str1[0] == '-'&&str2[0] != '-')
{
result[0] = '-';
add(str1 + 1, str2, result);
}
else if (str1[0] != '-'&&str2[0] != '-')
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 == len2)
{
if (strcmp(str1, str2)>0)
{
sub(str1, str2, result);
}
else
{
result[0] = '-';
sub(str2, str1, result);
}
}
else if (len1>len2)
{
sub(str1, str2, result);
}
else
{
result[0] = '-';
sub(str1, str2, result);
}
}
else
{
int len1 = strlen(str1 + 1);
int len2 = strlen(str2 + 1);
if (len1 == len2)
{
if (strcmp(str2 + 1, str1 + 1) >= 0)
{
sub(str2 + 1, str1 + 1, result);
}
else
{
result[0] = '-';
sub(str1 + 1, str2 + 1, result);
}
}
else if (len1>len2)
{
result[0] = '-';
sub(str1 + 1, str2 + 1, result);
}
else
{
sub(str1 + 1, str2 + 1, result);
}
}
strcpy(this->number, result);
}
void doublelong::operator/=(doublelong &ob)
{
char result[MAX];
char str1[MAX], str2[MAX];
strcpy(str1, this->number);
strcpy(str2, ob.number);
if (str1[0] != '-'&&str2[0] == '-')
{
result[0] = '-';
division(str1, str2 + 1, result);
}
else if (str1[0] == '-'&&str2[0] != '-')
{
result[0] = '-';
division(str1 + 1, str2, result);
}
else if (str1[0] == '-'&&str2[0] == '-')
{
division(str1 + 1, str2 + 1, result);
}
else
{
division(str1, str2, result);
}
strcpy(this->number, result);
}
int main()
{
doublelong a = "100", b = "-7", c;
c = a*b;
cout << c << endl;
c = "0";
cout << c << endl;
return 0;
}
这个应该是把字符串变成数字运算