C++ | 简单工厂模式 | 复数计算器

简单工厂模式最直观的一个应用便是实现一个计算器的程序。
比如,公司让你给计算器添加一个幂运算的功能,你只需要设计一个幂运算的类,并实现幂运算的逻辑,然后让该类继承自运算类即可。

简单工厂模式
简单工厂模式的设计特点可以防止设计人员私自更改程序中其他函数方法或其他类成员变量。你只能增加新的类方法,无法对已存在的类进行修改。
比如:老板,让你给公司员工管理程序增加一个考勤计数功能,而你在拿到公司程序源码后偷偷给自己的工资加上一笔,或是在其他功能上做改动,都会造成很严重的后果。而简单工厂模式就可以避免此类事件的发生。
同时,简单工厂模式也存在一个缺点,在增加新的方法类的时候不是可以任意增加的,必须是工厂类中已存在的可以调动的方法,否则,写好的类是没有办法调用的。由于它所能创建的类只能是事先考虑到的,如果需要添加新的类,则就需要改变工厂类了。

回归正题,说到计算器,下面就来实现一个复数计算器,同理如果是普通计算器只需要删掉下面的复数类,把运算类的成员变量换成相应的类型(int、double)即可。


#include <iostream>

using namespace std;

class Complex
{
public:
	Complex(int real = 0, int imag = 0) :m_real(real), m_imag(imag)
	{
	}
	//"+"重载 
	Complex operator+(const Complex& c)
	{
		//实部+实部//虚部+虚部 
		return Complex(m_real + c.m_real, m_imag + c.m_imag);
	}
	//"-"重载 
	Complex operator-(const Complex& c)
	{
		//实部-实部//虚部-虚部 
		return Complex(m_real - c.m_real, m_imag - c.m_imag);
	}
	//"*"重载 
	Complex operator*(const Complex& c)
	{
		return Complex(m_real * c.m_real - m_imag * c.m_imag, m_imag * c.m_real + m_real * c.m_imag);

	}
	//"/"重载 
	Complex operator/(const Complex& c)
	{
		/*                  (a+bi)*(c-di)
		* (a+bi)/(c+di) =   -------------
		**                     c^2+d^2       
		*/
		Complex cTmp;
		double d;
		d = c.m_real * c.m_real + c.m_imag * c.m_imag;
		if (-0.00000001 < d && d < 0.00000001)
			throw exception("除数不能为0");
		
		cTmp.m_real = (m_real * c.m_real - m_imag * (-1) * c.m_imag) / d;
		cTmp.m_imag = (m_imag * c.m_real + m_real * (-1) * c.m_imag) / d;
		return cTmp;
	}

	double m_real;
	double m_imag;
};

class Operation						//定义基类:运算类
{
public:
	/*虚函数,实现多态*/
	virtual Complex GetResult() = 0;

	void SetNumberA(Complex a)
	{
		numberA = a;
	}
	Complex GetNumberA()
	{
		return numberA;
	}

	void SetNumberB(Complex b)
	{
		numberB = b;
	}
	Complex GetNumberB()
	{
		return numberB;
	}

protected:
	Complex numberA;
	Complex numberB;
};

class OperationAdd : public Operation		//加法类:继承运算类
{
public:
	virtual Complex GetResult()
	{
		Complex tmp;
		tmp= numberA + numberB;
		return tmp;
	}
};

class OperationSub : public Operation		//减法类:继承运算类
{
public:
	virtual Complex GetResult()
	{
		return numberA - numberB;
	}
};

class OperationMul : public Operation		//乘法类:继承运算类
{
public:
	virtual Complex GetResult()
	{
		return numberA * numberB;
	}
};

class OperationDiv : public Operation		//除法类:继承运算类
{
public:
	virtual Complex GetResult()
	{
		return numberA / numberB;
	}
};

class OperationFactory						//工厂模式:实现对应运算符的实例化
{
public:
	OperationFactory()
	{
		operation = NULL;
	}

	Operation* CreateOperate(char o)
	{
		switch (o)
		{
		case '+':
			operation = new OperationAdd();
			break;
		case '-':
			operation = new OperationSub();
			break;
		case '*':
			operation = new OperationMul();
			break;
		case '/':
			operation = new OperationDiv();
			break;
		default:
			throw exception("操作符错误");
		}

		return operation;
	}

	~OperationFactory()
	{
		delete operation;
	}

private:
	Operation* operation;
};

int main()
{
	Complex numberA;
	Complex numberB;
	char operate;

	cout << "输入复数A的实部: ";
	cin >> numberA.m_real;
	cout << "           虚部: ";
	cin >> numberA.m_imag;

	cout << "运算符(+ - * /): ";
	cin >> operate;

	cout << "输入复数B的实部: ";
	cin >> numberB.m_real;
	cout << "           虚部: ";
	cin >> numberB.m_imag;

	OperationFactory factory;		//实例化运算符后返回基类指针
	Operation* operation;			//接收实例化后的运算符

	try
	{
		//实例化运算符
		operation = factory.CreateOperate(operate);
	}
	catch (exception & e)
	{
		cout << e.what() << endl;
		exit(1);
	}
	operation->SetNumberA(numberA);
	operation->SetNumberB(numberB);

	Complex result;
	try
	{
		result = operation->GetResult();
	}
	catch (exception & e)
	{
		cout << e.what() << endl;
		exit(1);
	}
	cout << "计算结果: (" << result.m_real << ", " << result.m_imag << "i)" << endl;


	return 0;
}


运行截图:
加法:
在这里插入图片描述

减法:
在这里插入图片描述

乘法:
在这里插入图片描述

除法:
在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
本报告将介绍使用C++语言设计复数计算器的过程和实现细节。本报告将分为以下几个部分: 1. 需求分析 2. 设计思路 3. 实现方法 4. 测试和性能优化 ## 1. 需求分析 复数计算器需要支持以下基本操作: - 复数加法 - 复数减法 - 复数乘法 - 复数除法 - 复数的模 - 复数的共轭 为了方便使用,我们还需要支持以下附加功能: - 支持输入输出 - 支持任意精度运算 ## 2. 设计思路 为了实现复数的运算,我们需要设计一个复数类,同时重载运算符以方便使用。在设计复数类时,我们需要考虑的因素包括: - 复数的实部和虚部 - 复数的加减乘除运算 - 复数的模和共轭 - 复数的输入输出 - 复数的精度 为了方便使用,我们还可以设计一个控制台界面来与用户交互,同时可以通过命令行参数来指定精度等设置。 ## 3. 实现方法 ### 复数类 我们首先定义一个复数类,其中包括实部和虚部两个成员变量,以及一些成员函数用于实现复数的基本操作。 ```cpp class Complex { public: Complex(double real = 0, double imag = 0); double getReal() const; double getImag() const; double getMod() const; Complex getConjugate() const; Complex operator+(const Complex &rhs) const; Complex operator-(const Complex &rhs) const; Complex operator*(const Complex &rhs) const; Complex operator/(const Complex &rhs) const; friend std::ostream &operator<<(std::ostream &os, const Complex &c); friend std::istream &operator>>(std::istream &is, Complex &c); private: double real_; double imag_; }; ``` 其中,构造函数用于初始化实部和虚部,getReal()、getImag()、getMod()、getConjugate()分别用于获取实部、虚部、模和共轭。 重载的运算符包括加减乘除四个基本运算符,以及输入输出运算符。 ### 控制台界面 我们可以通过命令行参数来指定精度等设置,同时设计一个控制台界面来与用户交互。 ```cpp int main(int argc, char *argv[]) { int precision = 2; if (argc > 1) { precision = std::atoi(argv[1]); } std::cout << "Welcome to Complex Calculator!" << std::endl; std::cout << "Set precision to " << precision << std::endl; std::string line; while (std::getline(std::cin, line)) { if (line.empty()) { continue; } std::istringstream iss(line); Complex c1, c2; std::string op; iss >> c1 >> op >> c2; if (!iss) { std::cout << "Invalid input!" << std::endl; continue; } Complex result; if (op == "+") { result = c1 + c2; } else if (op == "-") { result = c1 - c2; } else if (op == "*") { result = c1 * c2; } else if (op == "/") { result = c1 / c2; } else { std::cout << "Invalid operator: " << op << std::endl; continue; } std::cout << std::fixed << std::setprecision(precision) << result << std::endl; } return 0; } ``` ## 4. 测试和性能优化 为了测试复数计算器的正确性和性能,我们可以编写一些测试用例,并使用Valgrind等工具来检查内存泄漏等问题。在实际使用中,我们还可以通过多线程等技术来提高计算器的性能。 ## 总结 本报告介绍了使用C++语言设计复数计算器的过程和实现细节,包括复数类的设计、控制台界面的实现、测试和性能优化等方面。复数计算器对于科学计算、工程计算等领域具有重要的应用价值,本报告提供了一种简单易用的实现方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫RT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值