#include<iostream>
using namespace std;
class Complex //复数类
{
public:
double real, image;
public:
Complex(double r = 0, double i = 0) { real = r; image = i; };
Complex operator+(const Complex& c) //复数加复数
{
Complex temp;
temp.real = this->real + c.real;
temp.image = this->image + c.image;
return temp;
}
Complex operator-(const Complex& c) //复数减复数
{
Complex temp;
temp.real = this->real - c.real;
temp.image = this->image - c.image;
return temp;
}
Complex operator*(const Complex& c) //复数乘复数
{
Complex temp;
temp.real = this->real * c.real - this->image * c.image;
temp.image = this->image * c.real + this->real * c.image;
return temp;
}
Complex operator/(const Complex& c) //复数除复数
{
Complex temp;
temp.real = (this->real * c.real + this->image * c.image) / (c.real * c.real + c.image * c.image);
temp.image=(this->image * c.real - this->real * c.image)/ (c.real * c.real + c.image * c.image);
return temp;
}
Complex operator+(const double& n) //复数加实数
{
Complex temp;
temp.real = this->real + n;
temp.image = this->image;
return temp;
}
Complex operator-(const double& n) //复数减实数
{
Complex temp;
temp.real = this->real - n;
temp.image = this->image;
return temp;
}
Complex operator*(const double& n) //复数乘实数
{
Complex temp;
temp.real = this->real * n;
temp.image = this->image * n;
return temp;
}
Complex operator/(const double& n) //复数除实数
{
Complex temp;
temp.real = this->real / n;
temp.image = this->image / n;
return temp;
}
};
ostream& operator<<(ostream& cout, const Complex& c)
{
if (c.image != 0)
cout << c.real << " + " << c.image << "i";
else
{
cout << c.real;
}
return cout;
}
Complex operator+(const double& n, const Complex& c) //实数加复数
{
Complex temp;
temp.real = n + c.real;
temp.image = c.image;
return temp;
}
Complex operator-(const double& n, const Complex &c) //实数减复数
{
Complex temp;
temp.real = n - c.real;
temp.image = -c.image;
return temp;
}
Complex operator*(const double& n, const Complex& c) //实数乘复数
{
Complex temp;
temp.real = n * c.real;
temp.image = n * c.image;
return temp;
}
Complex operator/(const double& n, const Complex &c) //实数除复数
{
Complex temp;
temp.real = n * c.real / c.real * c.real + c.image * c.image;
temp.image = -n * c.image / c.real * c.real + c.image * c.image;
return temp;
}
void fJiaf() //复数加复数
{
double r1, i1, r2, i2;
cout << "请输入第一个复数的实部和虚部" << endl;
cin >> r1 >> i1;
cout << "请输入第二个复数的实部和虚部" << endl;
cin >> r2 >> i2;
Complex c1(r1, i1), c2(r2, i2), c;
c = c1 + c2;
cout << "c=" << c1 + c2 << endl;
system("pause");
system("cls");
}
void fJianf() //复数减复数
{
double r1, i1, r2, i2;
cout << "请输入第一个复数的实部和虚部" << endl;
cin >> r1 >> i1;
cout << "请输入第二个复数的实部和虚部" << endl;
cin >> r2 >> i2;
Complex c1(r1, i1), c2(r2, i2), c;
c = c1 - c2;
cout << "c=" << c1 - c2 << endl;
system("pause");
system("cls");
}
void fChengf() //复数乘复数
{
double r1, i1, r2, i2;
cout << "请输入第一个复数的实部和虚部" << endl;
cin >> r1 >> i1;
cout << "请输入第二个复数的实部和虚部" << endl;
cin >> r2 >> i2;
Complex c1(r1, i1), c2(r2, i2), c;
c = c1 * c2;
cout << "c=" << c1 * c2 << endl;
system("pause");
system("cls");
}
void fChuf() //复数除复数
{
double r1, i1, r2, i2;
cout << "请输入第一个复数的实部和虚部" << endl;
cin >> r1 >> i1;
cout << "请输入第二个复数的实部和虚部" << endl;
cin >> r2 >> i2;
Complex c1(r1, i1), c2(r2, i2), c;
cout << "c=" << c1 / c2 << endl;
system("pause");
system("cls");
}
void fJias() //复数加实数
{
double r1, i1, n;
cout << "请输入复数的实部和虚部" << endl;
cin >> r1 >> i1;
cout << "请输入一个实数" << endl;
cin >> n;
Complex c(r1, i1);
cout << "c=" << c + n << endl;
system("pause");
system("cls");
}
void fJians() //复数减实数
{
double r1, i1, n;
cout << "请输入复数的实部和虚部" << endl;
cin >> r1 >> i1;
cout << "请输入一个实数" << endl;
cin >> n;
Complex c(r1, i1);
cout << "c=" << c - n << endl;
system("pause");
system("cls");
}
void fChengs() //复数乘实数
{
double r1, i1, n;
cout << "请输入复数的实部和虚部" << endl;
cin >> r1 >> i1;
cout << "请输入一个实数" << endl;
cin >> n;
Complex c(r1, i1);
cout << "c=" << c * n << endl;
system("pause");
system("cls");
}
void fChus() //复数除实数
{
double r1, i1, n;
cout << "请输入复数的实部和虚部" << endl;
cin >> r1 >> i1;
cout << "请输入一个实数" << endl;
cin >> n;
if (n != 0)
{
Complex c(r1, i1);
cout << "c=" << c / n << endl;
}
else
{
cout << "除数不能为0!" << endl;
}
system("pause");
system("cls");
}
void sJiaf() //实数加复数
{
double r1, i1, n;
cout << "请输入一个实数" << endl;
cin >> n;
cout << "请输入复数的实部和虚部" << endl;
cin >> r1 >> i1;
Complex c(r1, i1);
cout << "c=" << n + c << endl;
system("pause");
system("cls");
}
void sJianf() //实数减复数
{
double r1, i1, n;
cout << "请输入一个实数" << endl;
cin >> n;
cout << "请输入复数的实部和虚部" << endl;
cin >> r1 >> i1;
Complex c(r1, i1);
cout << "c=" << n - c << endl;
system("pause");
system("cls");
}
void sChengf() //实数乘复数
{
double r1, i1, n;
cout << "请输入一个实数" << endl;
cin >> n;
cout << "请输入复数的实部和虚部" << endl;
cin >> r1 >> i1;
Complex c(r1, i1);
cout << "c=" << n * c << endl;
system("pause");
system("cls");
}
void sChuf() //实数除复数
{
double r1, i1, n;
cout << "请输入一个实数" << endl;
cin >> n;
cout << "请输入复数的实部和虚部" << endl;
cin >> r1 >> i1;
if (r1 == 0 && i1 == 0)
{
cout << "除数不能为0!" << endl;
}
else
{
Complex c(r1, i1);
cout << "c=" << n / c << endl;
}
system("pause");
system("cls");
}
void Menushow() //菜单
{
cout << "1.复数加复数" << " " << "2.复数减复数" << endl;
cout << "3.复数乘复数" << " " << "4.复数除复数" << endl;
cout << "5.复数加实数" << " " << "6.复数减实数" << endl;
cout << "7.复数乘实数" << " " << "8.复数除实数" << endl;
cout << "9.实数加复数" << " " << "10.实数减复数" << endl;
cout << "11.实数乘复数" << " " << "12.实数除复数" << endl;
}
int main()
{
int select;
while (1)
{
Menushow();
cin >> select;
switch (select)
{
case 1:
fJiaf();
break;
case 2:
fJianf();
break;
case 3:
fChengf();
break;
case 4:
fChuf();
break;
case 5:
fJias();
break;
case 6:
fJians();
break;
case 7:
fChengs();
break;
case 8:
fChus();
break;
case 9:
sJiaf();
break;
case 10:
sJianf();
break;
case 11:
sChengf();
break;
case 12:
sChuf();
break;
}
}
return 0;
}
12-06
2740