#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;
}
标签:real,return,vector3D,imag,编程,C++,Complex,operator,打卡
From: https://www.cnblogs.com/sugar-refinery/p/17372833.html