#include <iostream>
using namespace std;
class Complex
{
public:
Complex(double r = 0, double i = 0);
friend Complex operator *(Complex c1, Complex c2);//乘法
friend Complex operator /(Complex c1, Complex c2);//除法
friend istream& operator >> (istream& s, Complex& c);
friend ostream& operator << (ostream& s, Complex& c);
private:
double real, imag;
};
Complex::Complex(double r, double i)
{
real = r;
imag = i;
}
Complex operator *(Complex c1, Complex c2)
{
Complex c;
c.real = c1.real*c2.real - c1.imag*c2.imag;
c.imag = c1.real*c2.imag + c1.imag*c2.real;
return c;
}
Complex operator /(Complex c1, Complex c2)
{
Complex c;
c.real = (c1.real*c2.real + c1.imag*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag);
c.imag = (c1.imag*c2.real - c1.real*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag);
return c;
}
istream& operator >> (istream& s, Complex& c)
{
s >> c.real >> c.imag;
return s;
}
ostream& operator << (ostream& s, Complex& c)
{
char* str;
str = (c.imag < 0) ? "" : "+";
s << c.real << str << c.imag << "i" << endl;
return s;
}
int main()
{
Complex c1, c2,c;
cout << "请输入复数c1,c2:" << endl;
cin >> c1 >> c2;
cout << "c1=" << c1;
cout << "c2=" << c2;
c = c1*c2;
cout << "c1*c2=" << c;
c = c1 / c2;
cout << "c1/c2=" << c;
system("pause");
return 0;
}
using namespace std;
class Complex
{
public:
Complex(double r = 0, double i = 0);
friend Complex operator *(Complex c1, Complex c2);//乘法
friend Complex operator /(Complex c1, Complex c2);//除法
friend istream& operator >> (istream& s, Complex& c);
friend ostream& operator << (ostream& s, Complex& c);
private:
double real, imag;
};
Complex::Complex(double r, double i)
{
real = r;
imag = i;
}
Complex operator *(Complex c1, Complex c2)
{
Complex c;
c.real = c1.real*c2.real - c1.imag*c2.imag;
c.imag = c1.real*c2.imag + c1.imag*c2.real;
return c;
}
Complex operator /(Complex c1, Complex c2)
{
Complex c;
c.real = (c1.real*c2.real + c1.imag*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag);
c.imag = (c1.imag*c2.real - c1.real*c2.imag) / (c2.real*c2.real + c2.imag*c2.imag);
return c;
}
istream& operator >> (istream& s, Complex& c)
{
s >> c.real >> c.imag;
return s;
}
ostream& operator << (ostream& s, Complex& c)
{
char* str;
str = (c.imag < 0) ? "" : "+";
s << c.real << str << c.imag << "i" << endl;
return s;
}
int main()
{
Complex c1, c2,c;
cout << "请输入复数c1,c2:" << endl;
cin >> c1 >> c2;
cout << "c1=" << c1;
cout << "c2=" << c2;
c = c1*c2;
cout << "c1*c2=" << c;
c = c1 / c2;
cout << "c1/c2=" << c;
system("pause");
return 0;
}