采用面向对象程序设计方法实现对标准类型数据和复数类型数据进行加、减、乘、除、平方和立方(包括流插入运算符和流提取运算)运算的计算器
时间: 2023-12-26 13:04:19 浏览: 38
以下是一个采用面向对象程序设计方法实现的计算器类的示例代码。其中包括了对标准类型数据和复数类型数据进行加、减、乘、除、平方和立方运算的方法实现,以及流插入运算符和流提取运算符的重载。
```cpp
#include <iostream>
#include <cmath>
using namespace std;
// 复数类的定义
class Complex {
private:
double real; // 复数的实部
double imag; // 复数的虚部
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// 复数加法运算
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
// 复数减法运算
Complex operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
}
// 复数乘法运算
Complex operator*(const Complex& other) const {
return Complex(real * other.real - imag * other.imag,
real * other.imag + imag * other.real);
}
// 复数除法运算
Complex operator/(const Complex& other) const {
double den = other.real * other.real + other.imag * other.imag;
return Complex((real * other.real + imag * other.imag) / den,
(imag * other.real - real * other.imag) / den);
}
// 复数平方运算
Complex square() const {
return (*this) * (*this);
}
// 复数立方运算
Complex cube() const {
return (*this) * (*this) * (*this);
}
// 流插入运算符重载
friend ostream& operator<<(ostream& os, const Complex& c) {
os << c.real << (c.imag < 0 ? " - " : " + ") << abs(c.imag) << "i";
return os;
}
// 流提取运算符重载
friend istream& operator>>(istream& is, Complex& c) {
cout << "请输入复数的实部和虚部(以空格分隔):";
is >> c.real >> c.imag;
return is;
}
};
// 计算器类的定义
class Calculator {
public:
// 标准类型数据加法运算
template <typename T>
T add(const T& a, const T& b) const {
return a + b;
}
// 标准类型数据减法运算
template <typename T>
T sub(const T& a, const T& b) const {
return a - b;
}
// 标准类型数据乘法运算
template <typename T>
T mul(const T& a, const T& b) const {
return a * b;
}
// 标准类型数据除法运算
template <typename T>
T div(const T& a, const T& b) const {
return a / b;
}
// 标准类型数据平方运算
template <typename T>
T square(const T& a) const {
return a * a;
}
// 标准类型数据立方运算
template <typename T>
T cube(const T& a) const {
return a * a * a;
}
// 复数加法运算
Complex add(const Complex& a, const Complex& b) const {
return a + b;
}
// 复数减法运算
Complex sub(const Complex& a, const Complex& b) const {
return a - b;
}
// 复数乘法运算
Complex mul(const Complex& a, const Complex& b) const {
return a * b;
}
// 复数除法运算
Complex div(const Complex& a, const Complex& b) const {
return a / b;
}
// 复数平方运算
Complex square(const Complex& a) const {
return a.square();
}
// 复数立方运算
Complex cube(const Complex& a) const {
return a.cube();
}
};
int main() {
Calculator calc;
int a = 3, b = 4;
cout << a << " + " << b << " = " << calc.add(a, b) << endl;
cout << a << " - " << b << " = " << calc.sub(a, b) << endl;
cout << a << " * " << b << " = " << calc.mul(a, b) << endl;
cout << a << " / " << b << " = " << calc.div(a, b) << endl;
cout << a << " ^ 2 = " << calc.square(a) << endl;
cout << a << " ^ 3 = " << calc.cube(a) << endl;
Complex c1, c2;
cin >> c1 >> c2;
cout << c1 << " + " << c2 << " = " << calc.add(c1, c2) << endl;
cout << c1 << " - " << c2 << " = " << calc.sub(c1, c2) << endl;
cout << c1 << " * " << c2 << " = " << calc.mul(c1, c2) << endl;
cout << c1 << " / " << c2 << " = " << calc.div(c1, c2) << endl;
cout << c1 << " ^ 2 = " << calc.square(c1) << endl;
cout << c1 << " ^ 3 = " << calc.cube(c1) << endl;
return 0;
}
```
在上述代码中,我们定义了一个复数类 `Complex`,它包含了复数加、减、乘、除、平方和立方运算的实现,以及流插入运算符和流提取运算符的重载。同时,我们还定义了一个计算器类 `Calculator`,其中包含了对标准类型数据和复数类型数据进行加、减、乘、除、平方和立方运算的模板方法实现。
在主函数中,我们首先展示了对标准类型数据的运算,然后通过流提取运算符从用户输入获取了两个复数对象,并展示了对复数类型数据的运算结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)