题目描述
定义大整数类CBigInteger(假设最长支持1000位),数据成员为字符串类型(用字符指针或string都可以)。成员函数或友元函数包括:
1)带参构造函数,为字符指针动态分配1001个字符空间。根据参数初始化大整数。
2) 无参构造函数,为字符指针动态分配1001个字符空间。
3)重载运算符+,实现两个大整数的加法。
4)重载运算符-,实现两个大整数的减法。
5)重载运算符*,实现两个大整数的乘法。
6)重载输入,输入大整数。
7)重载输出,输出大整数。
8)析构函数,如果字符指针,释放分配的空间,不可屏蔽。
主函数输入大整数,完成大整数的加、减、乘。主函数代码如下,请补充CBigInteger类。
输入
测试次数
每组测试数据一行: 大整数1 运算符 大整数2
输出
对每组测试数据输出表达式和计算结果,具体格式见样例。
样例
输入
3
100 * -100
-123456789 + 123456
0001 - -123
输出
100 * (-100) = (-10000)
(-123456789) + 123456 = (-123333333)
1 - (-123) = 124
AC代码
//补充CBigInteger类和头文件
/********** Write your code here! **********/
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
class CBigInteger {
private:
char* p;
public:
CBigInteger(char* s) {
p = new char[1001];
strcpy(p, s);
}
CBigInteger() {
p = new char[1001];
}
CBigInteger(const CBigInteger& c) {
p = new char[1001];
strcpy(p, c.p);
}
CBigInteger operator + (const CBigInteger& rhs) {
CBigInteger res, c1(p), c2(rhs.p);
int f = 1;
if (p[0] == '-' && rhs.p[0] == '-') {
f = 0;
strcpy(c1.p, c1.p + 1);
strcpy(c2.p, c2.p + 1);
}
else if (p[0] != '-' && rhs.p[0] == '-') {
strcpy(c2.p, c2.p + 1);
return c1 - c2;
}
else if (p[0] == '-' && rhs.p[0] != '-') {
strcpy(c1.p, c1.p + 1);
return c2 - c1;
}
int len1 = strlen(c1.p), len2 = strlen(c2.p), t = 0;
int len = 0, i, j;
for (i = len1 - 1, j = len2 - 1; i >= 0 || j >= 0; i--, j--) {
int n1 = i >= 0 ? (c1.p[i] - '0') : 0;
int n2 = j >= 0 ? (c2.p[j] - '0') : 0;
res.p[len] = (n1 + n2 + t) % 10 + '0';
len++;
t = (n1 + n2 + t) / 10;
}
if (t != 0) {
res.p[len] = t + '0';
len++;
}
while (res.p[len - 1] == '0' && len > 1) {
len--;
}
if (f == 0) {
if (len != 1) {
res.p[len] = '-';
len++;
}
else if (res.p[0] != '0') {
res.p[len] = '-';
len++;
}
}
res.p[len] = '\0';
reverse(res.p, res.p + len);
return res;
}
CBigInteger operator-(const CBigInteger& rhs) {
CBigInteger res, c1(p), c2(rhs.p);
int f = 1, len = 0,css=666;
if (p[0] != '-' && rhs.p[0] != '-') {
if (c1 < c2)f = 0;
int len1 = strlen(c1.p), len2 = strlen(c2.p);
int t = 0, i, j;
for (i = len1 - 1, j = len2 - 1; i >= 0 || j >= 0; i--, j--) {
int n1 = i >= 0 ? (c1.p[i] - '0') : 0, n2 = j >= 0 ? (c2.p[j] - '0') : 0;
if (f == 1) {
if (n1 - n2 - t >= 0) {
res.p[len] = n1 - n2 - t + '0';
len++;
t = 0;
}
else {
res.p[len] = n1 + 10 - n2 - t + '0';
len++;
t = 1;
}
}
else {
if (n2 - n1 - t >= 0) {
res.p[len] = n2 - n1 - t + '0';
len++;
t = 0;
}
else {
res.p[len] = n2 + 10 - n1 - t + '0';
len++;
t = 1;
}
}
}
}
else if (p[0] == '-' && rhs.p[0] == '-') {
strcpy(c1.p, c1.p + 1);
strcpy(c2.p, c2.p + 1);
return c2 - c1;
}
else if (p[0] != '-' && rhs.p[0] == '-') {
strcpy(c2.p, c2.p + 1);
return c1 + c2;
}
else if (p[0] == '-' && rhs.p[0] != '-') {
strcpy(c2.p + 1, c2.p);
c2.p[0] = '-';
return c1 + c2;
}
while (res.p[len - 1] == '0' && len != 1) {
len--;
}
if (f == 0) {
res.p[len] = '-';
len++;
}
res.p[len] = '\0';
reverse(res.p, res.p + len);
return res;
}
CBigInteger operator*(const CBigInteger& rhs) {
CBigInteger res, c1(p), c2(rhs.p);
int f = 1;
if (p[0] == '0' || rhs.p[0] == '0') {
res.p[0] = '0';
res.p[1] = '\0';
return res;
}
if (p[0] == '-' && rhs.p[0] == '-') {
strcpy(c1.p, c1.p + 1);
strcpy(c2.p, c2.p + 1);
}
else if (p[0] == '-' && rhs.p[0] != '-') {
f = 0;
strcpy(c1.p, c1.p + 1);
}
else if (p[0] != '-' && rhs.p[0] == '-') {
f = 0;
strcpy(c2.p, c2.p + 1);
}
int len1 = strlen(c1.p), len2 = strlen(c2.p);
int len = len1 + len2;
for (int i = 0; i < len; i++) {
res.p[i] = '0';
}
res.p[len] = '\0';
for (int i = len1 - 1; i >= 0; i--) {
int t = 0;
for (int j = len2 - 1; j >= 0; j--) {
int mul = (c1.p[i] - '0') * (c2.p[j] - '0');
int sum = res.p[i + j + 1] - '0' + t + mul;
res.p[i + j + 1] = sum % 10 + '0';
t = sum / 10;
}
res.p[i] += t;
}
while (res.p[0] == '0' && len != 1) {
for (int i = 0; i < len; i++)
res.p[i] = res.p[i + 1];
len--;
}
if (f == 0) {
strcpy(res.p + 1, res.p);
res.p[0] = '-';
}
return res;
}
bool operator<(const CBigInteger& rhs) {//只能比较正数
if (strlen(p) != strlen(rhs.p)) {
return strlen(p) < strlen(rhs.p);
}
else {
for (int i = 0; i < strlen(p); i++) {
if (p[i] != rhs.p[i])return p[i] < rhs.p[i];
}
return false;
}
}
friend istream& operator>>(istream& in, const CBigInteger& c) {
in >> c.p;
while (c.p[0] == '0' && c.p[1] != '\0') {
strcpy(c.p, c.p + 1);
}
if (c.p[0] == '-') {
while (c.p[1] == '0' && c.p[2] != '\0') {
strcpy(c.p + 1, c.p + 2);
}
}
if (c.p[0] == '-' && c.p[1] == '0' && c.p[2] == '\0') {
c.p[0] = '0';
c.p[1] = '\0';
}
return in;
}
friend ostream& operator<<(ostream& out, const CBigInteger& c) {
if (c.p[0] == '-') {
out << '(' << c.p << ')';
}
else out << c.p;
return out;
}
~CBigInteger() {
delete[]p;
}
};
/*******************************************/
int main()
{
int t;
char op;
CBigInteger bigNum1;
CBigInteger bigNum2;
cin >> t;
while (t--)
{
cin >> bigNum1 >> op >> bigNum2;
cout << bigNum1 << " " << op << " " << bigNum2 << " = ";
if (op == '+')
cout << bigNum1 + bigNum2 << endl;
else if (op == '-')
cout << bigNum1 - bigNum2 << endl;
else if (op == '*')
cout << bigNum1 * bigNum2 << endl;
}
return 0;
}