| #include <iostream> |
| using namespace std; |
| class Complex |
| { |
| protected: |
| double real; |
| double imag; |
| public: |
| Complex (double r = 0.0, double i = 0.0): real(r), imag(i) {}; |
| Complex operator + (const Complex& c) |
| { |
| return Complex(real + c.real, imag + c.imag); |
| } |
| Complex operator - (const Complex& c) |
| { |
| return Complex(real - c.real, imag - c.imag); |
| } |
| Complex operator * (const Complex& c) |
| { |
| return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); |
| } |
| Complex operator / (const Complex& c) |
| { |
| return Complex((real * c.real + imag * c.imag) / (c.real * c.real + c.imag * c.imag), (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag)); |
| } |
| friend ostream& operator << (ostream& os, const Complex& c) |
| { |
| os << c.real << " + " << c.imag << "i"; |
| return os; |
| } |
| friend istream& operator >> (istream& is, Complex& c) |
| { |
| is >> c.real >> c.imag; |
| return is; |
| } |
| }; |
| int main() |
| { |
| Complex c1, c2; |
| cin >> c1 >> c2; |
| cout << c1 + c2 << endl; |
| cout << c1 - c2 << endl; |
| cout << c1 * c2 << endl; |
| cout << c1 / c2 << endl; |
| return 0; |
| } |
| #include <iostream> |
| using namespace std; |
| class vector3D { |
| protected: |
| double x, y, z; |
| public: |
| vector3D (double a = 0.0, double b = 0.0, double c = 0.0): x(a), y(b), z(c) {}; |
| vector3D operator+(const vector3D& v) { |
| return vector3D(x + v.x, y + v.y, z + v.z); |
| } |
| vector3D operator-(const vector3D& v) { |
| return vector3D(x - v.x, y - v.y, z - v.z); |
| } |
| vector3D operator*(const vector3D& v) { |
| return vector3D(x * v.x, y * v.y, z * v.z); |
| } |
| friend ostream& operator<<(ostream& os, const vector3D& v) { |
| os << "(" << v.x << ", " << v.y << ", " << v.z << ")"; |
| return os; |
| } |
| friend istream& operator>>(istream& is, vector3D& v) { |
| is >> v.x >> v.y >> v.z; |
| return is; |
| } |
| }; |
| int main() { |
| vector3D v1, v2; |
| cout << "v1 坐标值为: "; |
| cin >> v1; |
| cout << "v2 坐标值为: "; |
| cin >> v2; |
| cout << v1 + v2 << endl; |
| cout << v1 - v2 << endl; |
| cout << v1 * v2 << endl; |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结