#include <iostream>
using namespace std;
typedef struct
{
double real;
double image;
}Complex;
int menu_select()
{
int sn;
cout << "\n 复数四则运算\n";
cout << " =========================================\n";
cout << " 1.构造复数\n";
cout << " 2.加 法\n";
cout << " 3.减 法\n";
cout << " 4.乘 法\n";
cout << " 5.提取实部\n";
cout << " 6.提取虚部\n";
cout << " 0.退 出\n";
cout << " ==========================================\n";
cout << " 请选择0-6: ";
while (true)
{
cin >> sn;
if (sn<0 || sn>6)
cout << "\n\t输入错误,重选0-6\n";
else
break;
}
system("cls");
return sn;
}
void CreateComplex(Complex &Z, double &v1, double &v2)
{
Z.real = v1;
Z.image = v2;
}
void main()
{
double e1, e2, e3, e4, real, image;
Complex Z1, Z2, Sum, Sub, Mul;
void CreateComplex(Complex &Z, double &v1, double &v2);
int menu_select();
while (true){
switch (menu_select())
{
case 1:
cout << " **************************************\n";
cout << " 构造复数 \n";
cout << " ***************************************\n";
cout << " 输入第一个实部:";
cin >> e1;
cout << " 输入第一个虚部:";
cin >> e2;
CreateComplex(Z1, e1, e2);
if (e2 >= 0)
cout << " 第一个复数为:" << e1 << "+" << e2 << "i\n";
if (e2<0)
cout << " 第一个复数为:" << e1 << e2 << "i\n";
cout << " 输入第二个实部:";
cin >> e3;
cout << " 输入第二个虚部:";
cin >> e4;
CreateComplex(Z2, e3, e4);
if (e4 >= 0)
cout << " 第二个复数为:" << e1 << "+" << e2 << "i\n";
if (e4<0)
cout << " 第二个复数为:" << e1 << e2 << "i\n";
system("cls");
break;
case 2:
if (Z1.image == NULL)
break;
else
{
cout << " **************************************\n";
cout << " 复数相加 \n";
cout << " ***************************************\n";
real = Z1.real + Z2.real;
image = Z1.image + Z2.image;
CreateComplex(Sum, real, image);
if (Sum.image >= 0)
cout << " 和为:" << Sum.real << "+" << Sum.image << "i\n";
if (Sum.image<0)
cout << " 和为:" << Sum.real << Sum.image << "i\n";
system("pause");
system("cls");
break;
}
case 3:
cout << " **************************************\n";
cout << " 复数相减 \n";
cout << " ***************************************\n";
real = Z1.real - Z2.real;
image = Z1.image - Z2.image;
CreateComplex(Sub, real, image);
if (Sub.image >= 0)
cout << " 差为:" << Sub.real << "+" << Sub.image << "i\n";
if (Sub.image<0)
cout << " 差为:" << Sub.real << Sub.image << "i\n";
system("pause");
system("cls");
break;
case 4:
cout << " **************************************\n";
cout << " 复数相乘 \n";
cout << " ***************************************\n";
real = Z1.real * Z2.real - Z1.image * Z2.image;
image = Z1.image * Z2.real + Z1.real * Z2.image;
CreateComplex(Mul, real, image);
if (Mul.image >= 0)
cout << " 积为:" << Mul.real << "+" << Mul.image << "i\n";
if (Mul.image<0)
cout << " 积为:" << Mul.real << Mul.image << "i\n";
system("pause");
system("cls");
break;
case 5:
cout << " **************************************\n";
cout << " 提取实部 \n";
cout << " ***************************************\n";
cout << " Z1的实部是:";
cout << Z1.real << endl;
cout << " Z2的实部是:";
cout << Z2.real << endl;
system("pause");
system("cls");
break;
case 6:
cout << " **************************************\n";
cout << " 提取虚部 \n";
cout << " ***************************************\n";
cout << " Z1的虚部是:";
cout << Z1.image << endl;
cout << " Z2的虚部是:";
cout << Z2.image << endl;
system("pause");
system("cls");
break;
case 0:
cout << " 再见!\n";
return;
}
}
}
using namespace std;
typedef struct
{
double real;
double image;
}Complex;
int menu_select()
{
int sn;
cout << "\n 复数四则运算\n";
cout << " =========================================\n";
cout << " 1.构造复数\n";
cout << " 2.加 法\n";
cout << " 3.减 法\n";
cout << " 4.乘 法\n";
cout << " 5.提取实部\n";
cout << " 6.提取虚部\n";
cout << " 0.退 出\n";
cout << " ==========================================\n";
cout << " 请选择0-6: ";
while (true)
{
cin >> sn;
if (sn<0 || sn>6)
cout << "\n\t输入错误,重选0-6\n";
else
break;
}
system("cls");
return sn;
}
void CreateComplex(Complex &Z, double &v1, double &v2)
{
Z.real = v1;
Z.image = v2;
}
void main()
{
double e1, e2, e3, e4, real, image;
Complex Z1, Z2, Sum, Sub, Mul;
void CreateComplex(Complex &Z, double &v1, double &v2);
int menu_select();
while (true){
switch (menu_select())
{
case 1:
cout << " **************************************\n";
cout << " 构造复数 \n";
cout << " ***************************************\n";
cout << " 输入第一个实部:";
cin >> e1;
cout << " 输入第一个虚部:";
cin >> e2;
CreateComplex(Z1, e1, e2);
if (e2 >= 0)
cout << " 第一个复数为:" << e1 << "+" << e2 << "i\n";
if (e2<0)
cout << " 第一个复数为:" << e1 << e2 << "i\n";
cout << " 输入第二个实部:";
cin >> e3;
cout << " 输入第二个虚部:";
cin >> e4;
CreateComplex(Z2, e3, e4);
if (e4 >= 0)
cout << " 第二个复数为:" << e1 << "+" << e2 << "i\n";
if (e4<0)
cout << " 第二个复数为:" << e1 << e2 << "i\n";
system("cls");
break;
case 2:
if (Z1.image == NULL)
break;
else
{
cout << " **************************************\n";
cout << " 复数相加 \n";
cout << " ***************************************\n";
real = Z1.real + Z2.real;
image = Z1.image + Z2.image;
CreateComplex(Sum, real, image);
if (Sum.image >= 0)
cout << " 和为:" << Sum.real << "+" << Sum.image << "i\n";
if (Sum.image<0)
cout << " 和为:" << Sum.real << Sum.image << "i\n";
system("pause");
system("cls");
break;
}
case 3:
cout << " **************************************\n";
cout << " 复数相减 \n";
cout << " ***************************************\n";
real = Z1.real - Z2.real;
image = Z1.image - Z2.image;
CreateComplex(Sub, real, image);
if (Sub.image >= 0)
cout << " 差为:" << Sub.real << "+" << Sub.image << "i\n";
if (Sub.image<0)
cout << " 差为:" << Sub.real << Sub.image << "i\n";
system("pause");
system("cls");
break;
case 4:
cout << " **************************************\n";
cout << " 复数相乘 \n";
cout << " ***************************************\n";
real = Z1.real * Z2.real - Z1.image * Z2.image;
image = Z1.image * Z2.real + Z1.real * Z2.image;
CreateComplex(Mul, real, image);
if (Mul.image >= 0)
cout << " 积为:" << Mul.real << "+" << Mul.image << "i\n";
if (Mul.image<0)
cout << " 积为:" << Mul.real << Mul.image << "i\n";
system("pause");
system("cls");
break;
case 5:
cout << " **************************************\n";
cout << " 提取实部 \n";
cout << " ***************************************\n";
cout << " Z1的实部是:";
cout << Z1.real << endl;
cout << " Z2的实部是:";
cout << Z2.real << endl;
system("pause");
system("cls");
break;
case 6:
cout << " **************************************\n";
cout << " 提取虚部 \n";
cout << " ***************************************\n";
cout << " Z1的虚部是:";
cout << Z1.image << endl;
cout << " Z2的虚部是:";
cout << Z2.image << endl;
system("pause");
system("cls");
break;
case 0:
cout << " 再见!\n";
return;
}
}
}