策略模式

简介:
#include <string>
using std::string;
/************************************************************************/
/* 请实现一个商场收银软件,包含正常收费,打折收费和返利收费三种具体策略 */
/************************************************************************/

/*
策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成都是相同的工作,只是实现不同,
它可以以相同的方式调用所有算法,减少各种算法类与使用算法类之间的耦合。

策略模式的straterty类觌为Context 定义了一系列的算法和行为,继承有助于析取这些算法中的公共功能。

用CashContext对象管理在堆中分配的算法对象(CashSuper),可以免去调用者手动delete对象
*/

class CashSuper
{
public:
virtual float acceptCash(float money){return 0.0;}
};

class CashNormal : public CashSuper
{
public:
virtual float acceptCash(float money)
{
return money;
}
};

class CashRebate : public CashSuper
{
private:
float moneyRebate;
public:
CashRebate(float r):moneyRebate(r){}
CashRebate():moneyRebate(1){}
float acceptCash(float money)
{
return moneyRebate*money;
}
};

class CashReturn : public CashSuper
{
private:
float moneyCondition;
float moneyReturn;
public:
CashReturn():moneyReturn(0),moneyCondition(0){}
CashReturn(float condition,float ret):moneyCondition(condition),moneyReturn(ret){}
float acceptCash(float money)
{
if(money>=moneyCondition)
money=(int)(money/moneyCondition)*moneyReturn;
return money;
}
};

class CashFactory
{
public:
static CashSuper* createCashAccept(string flag)
{
CashSuper *super=NULL;
if(flag=="正常收费")
super=new CashNormal;
else if (flag=="打8折")
super=new CashRebate(0.8f);
else if(flag=="满300 返100")
super=new CashReturn(300,100);
return super;
}
};

class CashContext
{
protected:
CashSuper *cs;
public:
CashContext():cs(0){}
~CashContext()
{
if(cs)
delete cs;
}
CashContext &operator=(CashSuper*csuper)
{
if(cs) delete cs;
cs=csuper;
return *this;
}
float GetRedult(float money)
{
return cs->acceptCash(money);
}
};


// strategy.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "CashCalculate.h"
#include <iostream>
using namespace std;

//请实现一个商场收银软件,包含正常收费,打折收费和返利收费三种具体策略
int _tmain(int argc, _TCHAR* argv[])
{
//////////////////////////////////////////////////////////////////////////
//策略模式
CashContext cs;
int choice=-1;
cout<<"请输入收费方式(0 1 2 )"<<endl;
cin>>choice;
switch(choice)
{
case 0:
cs=new CashNormal;
break;
case 1:
cs=new CashRebate(0.8f);
break;
case 2:
cs=new CashReturn(100,10);
break;
default:
return 0;
}

float price=0.0;
int number=0;
cout<<"输入单价"<<endl;
cin>>price;
cout<<"请输入数量"<<endl;
cin>>number;
float total=cs.GetRedult(price*number);
cout<<"结果为"<<total<<endl;
system("pause");
return 0;

//////////////////////////////////////////////////////////////////////////
//工厂模式
/*float price=0.0;
cout<<"请输入单价"<<endl;
cin>>price;
int number;
cout<<"请输入数量"<<endl;
cin>>number;
cout<<"单价:"<<price<<" "<<"数量:"<<number<<endl;
cout<<"----------------------------------------------"<<endl;
float currentMoney=price*number;
string choice;
cout<<"请输入收费方式(正常收费 打8折 满300 返100 )"<<endl;
cin>>choice;
CashSuper *cashsuper;
cashsuper=CashFactory::createCashAccept(choice);
cout<<"应交钱为"<<cashsuper->acceptCash(currentMoney);
delete cashsuper;
return 0;
*/



//////////////////////////////////////////////////////////////////////////
//一般模式
/*
float price=0.0;
cout<<"请输入单价"<<endl;
cin>>price;
int number;
cout<<"请输入数量"<<endl;
cin>>number;
cout<<"单价:"<<price<<" "<<"数量:"<<number<<endl;
cout<<"----------------------------------------------"<<endl;

float total=0.0;
float discount=1.0;
float freeLimit=0.0;
float freeMoney=0.0;

int choice=-1;
cout<<"请输入收费方式(0 正常收费 1 打折收费 2 返利收费 )"<<endl;
cin>>choice;
switch(choice)
{
case 0:
total=number*price;
break;
case 1:
cout<<"请输入折扣 如 0.8 0.6"<<endl;
cin>>discount;
total=number*price*discount;
break;
case 2:
cout<<"请输入返利单元和返利金额 比如 满100元 送 10元等"<<endl;
cin>>freeLimit>>freeMoney;
total=number*price;//
total=total-((int)(total/100) *10);
break;
default:
cout<<"输入非法"<<endl;
return 0;
}

cout<<"最终价格为"<<total<<endl;
*/
system("pause");
return 0;
}
复制代码

 

相关文章
|
9天前
|
数据采集 人工智能 安全
|
4天前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:七十、小树成林,聚沙成塔:随机森林与大模型的协同进化
随机森林是一种基于决策树的集成学习算法,通过构建多棵决策树并结合它们的预测结果来提高准确性和稳定性。其核心思想包括两个随机性:Bootstrap采样(每棵树使用不同的训练子集)和特征随机选择(每棵树分裂时只考虑部分特征)。这种方法能有效处理大规模高维数据,避免过拟合,并评估特征重要性。随机森林的超参数如树的数量、最大深度等可通过网格搜索优化。该算法兼具强大预测能力和工程化优势,是机器学习中的常用基础模型。
309 164
|
3天前
|
机器学习/深度学习 自然语言处理 机器人
阿里云百炼大模型赋能|打造企业级电话智能体与智能呼叫中心完整方案
畅信达基于阿里云百炼大模型推出MVB2000V5智能呼叫中心方案,融合LLM与MRCP+WebSocket技术,实现语音识别率超95%、低延迟交互。通过电话智能体与座席助手协同,自动化处理80%咨询,降本增效显著,适配金融、电商、医疗等多行业场景。
318 155
|
4天前
|
编解码 人工智能 自然语言处理
⚽阿里云百炼通义万相 2.6 视频生成玩法手册
通义万相Wan 2.6是全球首个支持角色扮演的AI视频生成模型,可基于参考视频形象与音色生成多角色合拍、多镜头叙事的15秒长视频,实现声画同步、智能分镜,适用于影视创作、营销展示等场景。
346 4
|
12天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
874 6

热门文章

最新文章