设计一个求两个大整数四则运算的类。要求如下。 (1)类中私有数据成员为2个运算数及位数、运算结果。 (2)公有成员函数包括:构造函数和析构函数;接收数据的函数;和函数;差函数; 积函数;除函数;显示函数;清零函数等。 程序运行结果如下所示: Please enter one: 1234567 please enter two: 2345678 one+two=3580245 one-two=-1111111 one*two=2895896651426 one/two=52631742743。c++
时间: 2023-06-19 21:04:22 浏览: 107
以下是一个可能的实现:
```
#include <iostream>
#include <cstring>
using namespace std;
class BigNumArith {
private:
int num1[100], num2[100], res[200];
int len1, len2, resLen;
public:
BigNumArith() {
memset(num1, 0, sizeof(num1));
memset(num2, 0, sizeof(num2));
memset(res, 0, sizeof(res));
len1 = len2 = resLen = 0;
}
~BigNumArith() {}
void input() {
char str1[100], str2[100];
cout << "Please enter one: ";
cin >> str1;
cout << "please enter two: ";
cin >> str2;
len1 = strlen(str1);
len2 = strlen(str2);
for (int i = 0; i < len1; i++) {
num1[i] = str1[len1-1-i] - '0';
}
for (int i = 0; i < len2; i++) {
num2[i] = str2[len2-1-i] - '0';
}
}
void add() {
int carry = 0;
resLen = max(len1, len2);
for (int i = 0; i < resLen; i++) {
int sum = num1[i] + num2[i] + carry;
res[i] = sum % 10;
carry = sum / 10;
}
if (carry > 0) {
res[resLen++] = carry;
}
}
void sub() {
int borrow = 0;
resLen = max(len1, len2);
for (int i = 0; i < resLen; i++) {
int diff = num1[i] - num2[i] - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
res[i] = diff;
}
while (resLen > 0 && res[resLen-1] == 0) {
resLen--;
}
}
void mul() {
resLen = len1 + len2;
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
res[i+j] += num1[i] * num2[j];
res[i+j+1] += res[i+j] / 10;
res[i+j] %= 10;
}
}
while (resLen > 0 && res[resLen-1] == 0) {
resLen--;
}
}
void div() {
int divisor[100], quotient[100];
memcpy(divisor, num2, sizeof(num2));
memset(quotient, 0, sizeof(quotient));
resLen = len1 - len2 + 1;
for (int i = len1-1; i >= len2-1; i--) {
int q = 0;
while (compare(num1+i-len2+1, divisor, len2) >= 0) {
substract(num1+i-len2+1, divisor, len2);
q++;
}
quotient[i-len2+1] = q;
}
while (resLen > 0 && quotient[resLen-1] == 0) {
resLen--;
}
memcpy(res, quotient, sizeof(quotient));
}
void display() {
cout << "one+two=";
for (int i = resLen-1; i >= 0; i--) {
cout << res[i];
}
cout << endl;
}
void clear() {
memset(num1, 0, sizeof(num1));
memset(num2, 0, sizeof(num2));
memset(res, 0, sizeof(res));
len1 = len2 = resLen = 0;
}
private:
int compare(int* a, int* b, int len) {
for (int i = len-1; i >= 0; i--) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
}
}
return 0;
}
void substract(int* a, int* b, int len) {
int borrow = 0;
for (int i = 0; i < len; i++) {
int diff = a[i] - b[i] - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
a[i] = diff;
}
}
};
int main() {
BigNumArith bna;
bna.input();
bna.add();
bna.display();
bna.clear();
bna.input();
bna.sub();
bna.display();
bna.clear();
bna.input();
bna.mul();
bna.display();
bna.clear();
bna.input();
bna.div();
bna.display();
bna.clear();
return 0;
}
```
说明:
1. 本实现中,使用了三个私有函数:`compare`、`substract`和`clear`。其中,`compare`函数用于比较两个整数数组的大小,返回1表示第一个大于第二个,返回-1表示第一个小于第二个,返回0表示相等;`substract`函数用于计算两个整数数组的差,并将结果保存在第一个数组中;`clear`函数用于将所有成员变量清零,以便进行下一轮计算。
2. 在进行加、减、乘、除运算时,需要考虑进位和借位的情况,需要注意不同位数的处理方式不同。
3. 由于本题中的数据非常大,因此需要使用数组来保存数据,同时需要注意数组的长度需要足够大。
4. 在除法运算中,需要用到两个数组的值,因此需要使用memcpy函数进行拷贝。
阅读全文