c++实现复数计算器

复数计算器

1)由输入的实部和虚部生成一个复数;2)求两个复数的和;3)求两个复数的差;4)求两个复数的乘积;5)求复数的实部;6)求复数的虚部 


代码实现:

#define  _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
struct Compiex
{
	double real;
	double imag;
};
void mune()
{
	printf("*************************************\n");
	printf("********1.使用加法    2.使用减法*****\n");
	printf("********3.使用乘法    4.仅求实部*****\n");
	printf("********5.仅求虚部    0.退出计算器***\n");
	printf("*************************************\n");
}
Compiex my_add(Compiex a1, Compiex a2)//实现复数加法
{
	Compiex b = { 0 };
	b.real = a1.real + a2.real;
	b.imag = a1.imag + a2.imag;
	return b;

}

Compiex my_sub(Compiex a1, Compiex a2)//实现复数减法
{
	Compiex b = { 0 };
	b.real = a1.real - a2.real;
	b.imag = a1.imag - a2.imag;
	return b;

}

Compiex my_mul(Compiex a1, Compiex a2)//实现复数乘法
{
	Compiex b = { 0 };
	b.real = a1.real * a2.real-a1.imag*a2.imag;
	b.imag = a1.imag*a2.imag + a2.real*a1.imag;
	return b;
}

int main()
{
	Compiex a1 = { 0 };
	Compiex a2 = { 0 };
	int input = 0;
	do
	{
		mune();
		printf("请输入\n");
		scanf("%d", &input);
		system("cls");
		switch (input)
		{
		case 1:
		{
			printf("开始实现加法\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "通过加法所得的复数为" << my_add(a1, a2).real << '+' <<  '(' << my_add(a1, a2).imag << 'i' <<')' << endl;
			break;
		}
		case 2:
		{
			printf("开始实现减法\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "通过减法所得的复数为" << my_sub(a1, a2).real << '+' <<'(' << my_sub(a1, a2).imag << 'i' <<')' <<  endl;
			break;
		}
		case 3:
		{
			printf("开始实现乘法\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "通过乘法所得的复数为" << my_mul(a1, a2).real << '+' << '(' << my_mul(a1, a2).imag << 'i' << ')' << endl;
			break;
		}
		case 4:
		{
			printf("开始实现求复数的实部\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "复数的实部分别为" << a1.real << "和" << a2.real << endl;
			break;
		}
		case 5:
		{
			printf("开始实现求复数的虚部\n");
			cout << "请分别输入两个数的实部和虚部";
			cin >> a1.real >> a1.imag;
			cin >> a2.real >> a2.imag;
			cout << "复数的虚部分别为" << a1.imag << 'i' << "和" << a2.imag << 'i' << endl;
			break;
		}
		case 0:
		{
			printf("退出成功\n");
			break;
		}
		default:
			printf("输入错误请重新输入\n");
			break;
		}

	} while (input);

	return 0;
}

运行结果: 

c++实现复数计算器_第1张图片

c++实现复数计算器_第2张图片

你可能感兴趣的:(c++,开发语言)