定义一个整数计算类Integer,实现短整数+、-、*、/基本算术运算。 要求:可以进行数据范围检查(-32768~32767),数据溢出时显示错误信息。 输入数据格式:短整数1 运算符 短整数2,数字与符号之间有一个空格,例如:2 + 2。假设为除法运算时,不会出现除以0的情况。 计算结果,如果结果在-32768~32767范围内,则直接输出,否则输出"Overflow"。 样例输入: 5 + 234 32762 * 10 0 / 98 样例输出: 239 Overflow 0 提示:请使用运算符重载实现。
时间: 2024-03-17 20:44:55 浏览: 35
好的,下面是根据您的要求实现的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Integer {
private:
int num;
public:
Integer(int n = 0) : num(n) {}
friend Integer operator+(const Integer &a, const Integer &b);
friend Integer operator-(const Integer &a, const Integer &b);
friend Integer operator*(const Integer &a, const Integer &b);
friend Integer operator/(const Integer &a, const Integer &b);
void print();
};
Integer operator+(const Integer &a, const Integer &b) {
int res = a.num + b.num;
if ((a.num > 0 && b.num > 0 && res <= 0) || (a.num < 0 && b.num < 0 && res >= 0)) {
cout << "Overflow" << endl;
return Integer(0);
}
return Integer(res);
}
Integer operator-(const Integer &a, const Integer &b) {
int res = a.num - b.num;
if ((a.num > 0 && b.num < 0 && res <= 0) || (a.num < 0 && b.num > 0 && res >= 0)) {
cout << "Overflow" << endl;
return Integer(0);
}
return Integer(res);
}
Integer operator*(const Integer &a, const Integer &b) {
int res = a.num * b.num;
if (a.num != 0 && res / a.num != b.num) {
cout << "Overflow" << endl;
return Integer(0);
}
return Integer(res);
}
Integer operator/(const Integer &a, const Integer &b) {
if (b.num == 0) {
cout << "Error" << endl;
return Integer(0);
}
int res = a.num / b.num;
if (res < -32768 || res > 32767) {
cout << "Overflow" << endl;
return Integer(0);
}
return Integer(res);
}
void Integer::print() {
cout << num << endl;
}
int main() {
string s;
int num1, num2;
char op;
while (cin >> num1 >> op >> num2) {
Integer a(num1), b(num2), res;
switch (op) {
case '+':
res = a + b;
break;
case '-':
res = a - b;
break;
case '*':
res = a * b;
break;
case '/':
res = a / b;
break;
default:
break;
}
res.print();
}
return 0;
}
```
代码中重载了加减乘除四个运算符,实现了数据范围检查,并且可以输出错误信息。在`main`函数中,读入两个整数和一个运算符,然后根据运算符进行相应的计算并输出结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)