编写一个程序,要求如下: 1) 设计并实现自然数(MyNaturalNumbers)及整数(MyInteger)类。 2) 自然数及整数类至少能表示及处理20位自然数或整数数据。 3) 自然数及整数类能表示及处理任意位数的自然数或整数数据。(选作。第五章内容) 4) 自然数及整数类的设计应体现体系结构(即应设计实现完整的继承关系)。 5) 自然数及整数类应提供数据赋值、数据输出、加法计算、减法计算功能。
时间: 2024-02-20 20:02:32 浏览: 79
好的,以下是一个简单的实现:
```
class MyNaturalNumbers {
private:
string value; // 自然数的值
public:
MyNaturalNumbers() {
value = "0";
}
MyNaturalNumbers(string _value) {
value = _value;
}
MyNaturalNumbers& operator=(const MyNaturalNumbers& other) {
value = other.value;
return *this;
}
friend ostream& operator<<(ostream& os, const MyNaturalNumbers& obj) {
os << obj.value;
return os;
}
MyNaturalNumbers operator+(const MyNaturalNumbers& other) const {
string result = "";
int carry = 0;
int i = value.size() - 1;
int j = other.value.size() - 1;
while (i >= 0 || j >= 0 || carry != 0) {
int x = (i >= 0) ? value[i] - '0' : 0;
int y = (j >= 0) ? other.value[j] - '0' : 0;
int sum = x + y + carry;
result += (sum % 10) + '0';
carry = sum / 10;
i--;
j--;
}
reverse(result.begin(), result.end());
return MyNaturalNumbers(result);
}
MyNaturalNumbers operator-(const MyNaturalNumbers& other) const {
string result = "";
int borrow = 0;
int i = value.size() - 1;
int j = other.value.size() - 1;
while (i >= 0 || j >= 0) {
int x = (i >= 0) ? value[i] - '0' : 0;
int y = (j >= 0) ? other.value[j] - '0' : 0;
int diff = x - y - borrow;
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
}
result += diff + '0';
i--;
j--;
}
reverse(result.begin(), result.end());
return MyNaturalNumbers(result);
}
};
class MyInteger : public MyNaturalNumbers {
private:
bool is_negative; // 是否为负数
public:
MyInteger() {
is_negative = false;
}
MyInteger(string _value) : MyNaturalNumbers(_value) {
is_negative = false;
}
MyInteger& operator=(const MyInteger& other) {
MyNaturalNumbers::operator=(other);
is_negative = other.is_negative;
return *this;
}
friend ostream& operator<<(ostream& os, const MyInteger& obj) {
if (obj.is_negative) {
os << "-";
}
os << obj.value;
return os;
}
MyInteger operator+(const MyInteger& other) const {
if (!is_negative && !other.is_negative) {
return MyInteger(MyNaturalNumbers::operator+(other).value);
} else if (is_negative && other.is_negative) {
MyInteger result(MyNaturalNumbers::operator+(other).value);
result.is_negative = true;
return result;
} else if (!is_negative && other.is_negative) {
MyInteger positive_other(other.value);
positive_other.is_negative = false;
return MyInteger(MyNaturalNumbers::operator-(positive_other).value);
} else {
MyInteger positive_this(value);
positive_this.is_negative = false;
return MyInteger(other.operator-(positive_this).value);
}
}
MyInteger operator-(const MyInteger& other) const {
if (!is_negative && !other.is_negative) {
if (*this >= other) {
return MyInteger(MyNaturalNumbers::operator-(other).value);
} else {
MyInteger result(other.operator-(MyNaturalNumbers::operator*(*this)).value);
result.is_negative = true;
return result;
}
} else if (is_negative && other.is_negative) {
if (*this >= other) {
MyInteger result(MyNaturalNumbers::operator-(other).value);
result.is_negative = true;
return result;
} else {
return MyInteger(other.operator-(MyNaturalNumbers::operator*(*this)).value);
}
} else if (!is_negative && other.is_negative) {
MyInteger positive_other(other.value);
positive_other.is_negative = false;
return MyInteger(MyNaturalNumbers::operator+(positive_other).value);
} else {
MyInteger positive_this(value);
positive_this.is_negative = false;
return MyInteger(positive_this.operator-(other).value);
}
}
bool operator>=(const MyInteger& other) const {
if (is_negative && !other.is_negative) {
return false;
} else if (!is_negative && other.is_negative) {
return true;
} else if (is_negative && other.is_negative) {
MyInteger positive_this(value);
MyInteger positive_other(other.value);
positive_this.is_negative = false;
positive_other.is_negative = false;
return positive_other >= positive_this;
} else {
if (value.size() > other.value.size()) {
return true;
} else if (value.size() < other.value.size()) {
return false;
} else {
int i = 0;
while (i < value.size() && value[i] == other.value[i]) {
i++;
}
if (i == value.size()) {
return true;
} else if (value[i] > other.value[i]) {
return true;
} else {
return false;
}
}
}
}
MyInteger operator-() const {
MyInteger result(*this);
result.is_negative = !is_negative;
return result;
}
};
```
这个实现中,`MyNaturalNumbers` 类表示自然数,`MyInteger` 类继承自 `MyNaturalNumbers`,表示整数。其中,`MyNaturalNumbers` 实现了自然数的加法和减法,`MyInteger` 实现了整数的加法、减法和取相反数操作。在加法和减法的实现中,我们需要根据两个操作数的正负情况分别进行处理。在 `MyInteger` 类中,我们使用了继承来实现体系结构。
阅读全文
相关推荐


















