注释全 新手可懂 可复用式复数计算器C++实现

#include<iostream>
#include<math.h>
#include<string>
#include<stdlib.h>
using namespace std;
/********* Begin *********/
class Complex
{
	//复数类的声明
public:
	Complex() {}
	Complex(const Complex& c) {         //拷贝构造函数实现
		real = c.real;
		image = c.image;
		name = c.name;
	}
	Complex(float r, float i,string name)//有参构造函数实现
	{
		real = r;
		image = i;
		this->name = name;
	}
	void Print()						//输出函数实现,方便打印结果
	{
		if (this->image >= 0)           //为了输出美观加了点空格,虚部大于0和小于0的情况
		{
			cout << real << " + " << image << "i\n";
		}	
		else
		{
			cout << real << " - " << abs(image) << "i\n";
		}	
	}
	Complex operator+(Complex& r)      //加法运算符重载
	{
		Complex c;
		c.real = this->real + r.real;
		c.image = this->image + r.image;

		return c;
	}
	Complex operator-(Complex& r)	   //减法运算符重载
	{
		Complex c;
		c.real = this->real - r.real;
		c.image = this->image - r.image;

		return c;
	}
	Complex operator*(Complex& r)	   //乘法运算符重载
	{
		Complex c;
		c.real = (this->real * r.real) - (this->image * r.image);
		c.image = (this->image * r.real) + (this->real * r.image);
		return c;
	}
	Complex operator/(Complex& r)	   //除法运算符重载
	{
		Complex c;
		c.real = (real * r.real + image * r.image) / (r.image * r.real + r.image * r.image);
		c.image = (image * r.real + real * r.image) / (r.real * r.real + r.image * r.image);
		return c;
	}
	
private:
	float real; //实部
	float image;//虚部
	string name;//名字,c1 c2什么的


};
int flag = 1;
void is_continue();
//复数类的定义
int main()
{
	float real = 0, image = 0;
	cout << "\t欢迎使用复数计算器\n\n";
	cout << "\t请输入第一个复数的实部和虚部(用空格分割)\n";
	cin >> real >> image;
	Complex complex_1(real, image, "c1");//第一个复数对象实例化
	cout << "c1 = ";
	complex_1.Print();
	cout << "\n";
	cout << "\t请输入第二个复数的实部和虚部(用空格分割)\n";
	cin >> real >> image;
	Complex complex_2(real, image, "c2");//第二个复数对象实例化
	cout << "c2 = ";
	complex_2.Print();
	cout << "\n";
	cout << "****************************************************\n\n";
	cout << "\t\t菜单\n";

	cout << "+\t2个复数相加\n\n";

	cout << "-\t2个复数相减\n\n";

	cout << "*\t2个复数相乘\n\n";

	cout << "/\t2个复数相除\n\n";

	cout << "****************************************************\n\n";
	char oper = 0;	//操作符
	char c;			//用来清空输入缓存区,怕一次性输入多个操作符影响下次输入
	while (flag)    //可复用计算器的实现
	{
		cout << "\n请选择你要进行的操作\n";
		cin >> oper;
		while ((c = getchar()) != '\n');//用来清空输入缓存区,怕一次性输入多个操作符影响下次输入
		Complex ans;				    //作为答案的复数对象
		switch (oper)
		{
		case '+':
			ans = complex_1 + complex_2;
			cout << "c1 + c2 =";
			ans.Print();
			cout << "\n";//输出答案
			is_continue();//判断是否继续,flag是全局变量
			break;
		case '-':
			ans = complex_1 - complex_2;
			cout << "c1 -c2 =";
			ans.Print();//输出答案
			cout << "\n";
			is_continue();//判断是否继续,flag是全局变量
			break;
		case '*':
			ans = complex_1 * complex_2;
			cout << "c1 * c2 =";
			ans.Print();//输出答案
			cout << "\n";
			is_continue();//判断是否继续,flag是全局变量
			break;
		case '/':
			ans = complex_1 / complex_2;
			cout << "c1 / c2 =";
			ans.Print();//输出答案
			cout << "\n";
			is_continue();//判断是否继续,flag是全局变量
			break;
		default:
			cout << "请输入有效操作符:\n";
			break;
		}
		if (flag == 0)
		{
			system("cls");//清屏函数
			cout << "****************************************************\n\n";
			cout << "\t\t欢迎下次使用\n\n";
			cout << "****************************************************\n\n";
			break;
		}
	}
}
void is_continue()
{
	string choose = "";
	cout << "是否要进行别的操作?yes/no :\n";
	while (1)
	{
		cin >> choose;
		if (choose == "yes")
		{
			flag = 1;
			break;
		}
		else if (choose == "no")
		{
			flag = 0;
			break;
		}
		else {
			cout << "\t请输入有效选择\n";
		}
	}
	
}

有帮助的话点个攒哇👍

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值