当前位置: 首页 > news >正文

7月30日作业

1:将鸟笼放飞所有鸟类的题,改成观察者模式

代码:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>using namespace std;//基类:鸟类
class Bird{
public:virtual void run(){cout << "鸟飞走了" << endl;}
};//派生类1:企鹅类
class Penguin:public Bird{
public:void run(){cout << "企鹅跑掉了" << endl;}
};//派生类2:鸵鸟类
class Ostrich:public Bird{
public:void run(){cout << "鸵鸟跑掉了" << endl;}
};//派生类3:老鹰类
class Eagle:public Bird{
public:void run(){cout << "老鹰飞走了" << endl;}
};//鸟笼类
class Bird_cage{
private:Bird* list[20];int count=0;  //鸟数
public://将鸟关进笼子void insert(Bird* b){list[count]=b;count++;}//可以重载>>算术符便于将鸟加入笼子Bird_cage& operator>>(Bird& b){this->insert(&b);return *this;}//释放所有鸟void open_cage(){for(int i=0;i<count;i++){list[i]->run();}}
};
/*
void open_cage(Bird** cage){for(int i=0;i<4;i++){cage[i]->run();}
}*/int main(int argc,const char** argv){Bird b;Penguin p;Ostrich o;Eagle e;Bird_cage cage;cage>>b>>p>>o>>e;cage.open_cage();return 0;
}

2:使用 策略模式 + 简单工厂模式实现以下功能

有一个英雄类,拥有私有成员: hp ,atk,dep 英雄可以打怪掉落武器:怪物可以掉落3种武器: 长剑,匕首,斧头 英雄装备长剑,获得2点hp 英雄装备匕首,获得2点atk 英雄装备斧头,获得2点def 英雄装备不同的武器,使用策略模式去实现 注意:测试的时候,英雄在更换武器的时候,记得使用策略模式,将英雄之间装备的武器属性扣除后,再增加新属性 打败怪物掉落什么武器,自己设计,但是要求怪物掉落武器是一个简单工厂模式

代码

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>using namespace std;class Hero{
private:string name; // 创建英雄的名字int hp; //血量int atk; //攻击力int dep; //武器攻击力加成
public:Hero(string name="新英雄",int hp=100,int atk=5,int dep=0):name(name),hp(hp),atk(atk),dep(dep){}void showStatus(){cout << name <<":" <<endl <<"血量:"<<hp<<"   攻击力:"<<atk<<"   武器攻击力加成:"<<dep<<endl;}//设置血量void setHp(int hp=100){this->hp=hp;}//获取血量int getHp(){return this->hp;}//设置攻击力void setAtk(int atk=5){this->atk=atk;}//获取当前攻击力int getAtk(){return this->atk;}//设置武器攻击力加成void setDep(int dep=0){this->dep=dep;}//获取当前攻击力加成int getDep(){return this->dep;}
};//武器装备基类
class Weapon{
public:virtual void equip(Hero& hero)=0; //纯虚函数virtual ~Weapon(){};
};//装备武器长剑,继承于Weapon武器基类
class Longsword:public Weapon{
public:Longsword(){}void equip(Hero& hero){int new_hp=hero.getHp()+6;hero.setHp(new_hp);  //长剑血量加成 6cout<<"血量增加 6"<<endl;}
};//装备武器匕首,继承于Weapon武器基类
class Dagger:public Weapon{
public:Dagger(){}void equip(Hero& hero){int new_atk=hero.getAtk()+5;hero.setDep(new_atk);  //匕首攻击力加成 5cout<<"攻击力增加 5"<<endl;}
};//装备武器斧头,继承于Weapon武器基类
class Axe:public Weapon{
public:Axe(){}void equip(Hero& hero){hero.setDep(3);  //斧头攻击力武器加成 3cout<<"获得武器攻击力加成 3"<<endl;}
};class Monster{
private:int hp;
public:Monster(int hp=100):hp(hp){}void atkHp(int hp){if(hp<0) hp=0; this->hp-=hp;cout<<"怪物受到 "<<hp<<" 点攻击,剩余血量: "<<this->hp<<endl;}//判断怪物是否死亡bool isDead(){return this->hp<=0;}int getHp(){return this->hp;}virtual ~Monster(){}virtual Weapon* Montowea()=0;
};class Monster_1:public Monster{
public:Monster_1():Monster(100){}Weapon* Montowea(){cout<<"恭喜获得武器 - 长剑:";return new Longsword;}
};class Monster_2:public Monster{
public:Monster_2():Monster(105){}Weapon* Montowea(){cout<<"恭喜获得武器 - 匕首:";return new Dagger;}
};class Monster_3:public Monster{
public:Monster_3():Monster(110){}Weapon* Montowea(){cout<<"恭喜获得武器 - 斧头:";return new Axe;}
};int main(int argc,const char** argv){Hero hero1("xiaohua",800,100);Monster_1 m;while(1){int choose;cout<<"请选择要打的怪物:"<<endl<<"[1]怪物1  [2]怪物2  [3]怪物3"<<endl;cout<<"请输入选择";cin>>choose;Monster* m;switch (choose){case 1:m=new Monster_1;break;case 2:m=new Monster_2;break;case 3:m=new Monster_3;break;default:cout<<"输入有误"<<endl;continue;}while(!m->isDead()){int damage=hero1.getAtk()+hero1.getDep(); //攻击力加武器加成m->atkHp(damage);hero1.showStatus();if(m->isDead()){cout<<"恭喜你!怪物已被击败"<<endl;Weapon* weapon=m->Montowea(); //怪物掉落武器weapon->equip(hero1);delete weapon;}sleep(1);}hero1.showStatus();}return 0;
}

http://www.dtcms.com/a/307011.html

相关文章:

  • Spring Boot 数据源配置中为什么可以不用写 driver-class-name
  • 第六章第一节 TIM 定时中断
  • vue2中的过滤器filter
  • 1+1>2!特征融合如何让目标检测更懂 “场景”?
  • 深入理解数据库事务:从概念到实践
  • 防止飞书重复回调通知分布式锁
  • 白话容器基础(一):进程
  • Mybatis分页查询当前页数据条数大于实际返回的数据条数
  • 拥抱智慧物流时代:数字孪生技术的应用与前景
  • Matplotlib(四)- 图表样式美化
  • Linux日志管理和时钟同步配置指南
  • OneCode3.0 框架深入研究与应用扩展
  • html页面跳转或者a标签锚点跳转,解决页面滚动问题3个(1.从底部开始滚动,2.滚动不到指定锚点位置,3.页面展示不在最上面)
  • MySQL图解索引篇(2)
  • 斯皮尔曼spearman相关系数
  • 25年新算法!基于猛禽的优化算法(BPBO):一种元启发式优化算法,附完整免费MATLAB代码
  • Java反射-动态代理
  • cmake_parse_arguments()构建清晰灵活的 CMake 函数接口
  • 智汇AI,应用领航 | 华宇万象问数入选2025全景赋能典型案例
  • 36、spark-measure 源码修改用于数据质量监控
  • Linux零基础Shell教学全集(可用于日常查询语句,目录清晰,内容详细)(自学尚硅谷B站shell课程后的万字学习笔记,附课程链接)
  • 「Spring Boot + MyBatis-Plus + MySQL 一主两从」读写分离实战教程
  • Linux 中,命令查看系统版本和内核信息
  • Linux 系统原理深度剖析与技术实践:从内核架构到前沿应用
  • 【选型】HK32L088 与 STM32F0/L0 系列 MCU 参数对比与选型建议(ST 原厂 vs 国产芯片)(单片机选型主要考虑的参数与因素)
  • 【python】列表“*”方式与推导式方式初始化区别
  • 数据结构——单链表1
  • 【WRF-Chem】EDGAR 排放数据处理:分部门合并转化为二进制(Python全代码)
  • RAG实战指南 Day 27:端到端评估框架实现
  • CSS-in-JS 动态主题切换与首屏渲染优化