【无标题】

打印 上一主题 下一主题

主题 1806|帖子 1806|积分 5418

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <unistd.h>
  5. #include <sstream>
  6. #include <vector>
  7. #include <memory>
  8. using namespace std;
  9. // 前向声明武器类
  10. class Weapon;
  11. // 英雄基类
  12. class Hero {
  13. private:
  14.     int hp;        // 生命值
  15.     int atk;       // 攻击力
  16.     int def;       // 防御力
  17.     int spd;       // 速度
  18.     string profession; // 职业类型
  19. public:
  20.     // 构造函数(默认职业为战士)
  21.     Hero(int h = 0, int a = 0, int d = 0, int s = 0, string p = "Warrior")
  22.         : hp(h), atk(a), def(d), spd(s), profession(p) {}
  23.     // 属性设置方法
  24.     void setAtk(int a) { atk = a; }
  25.     void setDef(int d) { def = d; }
  26.     void setSpd(int s) { spd = s; }
  27.     void setHp(int h) { hp = h; }
  28.     void setProfession(string p) { profession = p; }
  29.     // 属性获取方法
  30.     int getAtk() { return atk; }
  31.     int getDef() { return def; }
  32.     int getSpd() { return spd; }
  33.     int getHp() { return hp; }
  34.     string getProfession() const { return profession; }
  35.     // 装备武器方法(通过多态调用不同武器的属性加成)
  36.     void equipWeapon(Weapon* w);
  37. };
  38. // 武器基类(抽象类)
  39. class Weapon {
  40. private:
  41.     int atk; // 基础攻击力
  42. public:
  43.     // 构造函数(默认攻击力为1)
  44.     Weapon(int atk = 1) : atk(atk) {}
  45.     // 属性设置方法
  46.     void setAtk(int a) { atk = a; }
  47.     int getAtk() { return atk; }
  48.     // 基础属性加成方法(所有武器都增加攻击力)
  49.     virtual void getAttr(Hero& hero) {
  50.         int new_atk = hero.getAtk() + atk;
  51.         hero.setAtk(new_atk);
  52.     }
  53.     // 纯虚函数,用于获取武器类型
  54.     virtual string getType() = 0;
  55.     virtual ~Weapon() {} // 虚析构函数
  56. };
  57. // 长剑类(继承自武器)
  58. class Sword : public Weapon {
  59. private:
  60.     int hp; // 额外生命加成
  61. public:
  62.     // 构造函数
  63.     Sword(int atk = 1, int hp = 1) : Weapon(atk), hp(hp) {}
  64.     // 设置/获取额外生命值
  65.     void setHp(int h) { hp = h; }
  66.     int getHp() { return hp; }
  67.     // 重写属性加成方法(攻击+生命)
  68.     virtual void getAttr(Hero& hero) {
  69.         Weapon::getAttr(hero); // 调用基类攻击加成
  70.         int new_hp = hero.getHp() + hp;
  71.         hero.setHp(new_hp);
  72.     }
  73.     // 重写获取武器类型
  74.     string getType() override { return "Sword"; }
  75. };
  76. // 匕首类(继承自武器)
  77. class Blade : public Weapon {
  78. private:
  79.     int spd; // 额外速度加成
  80. public:
  81.     // 构造函数
  82.     Blade(int atk = 1, int spd = 1) : Weapon(atk), spd(spd) {}
  83.     // 设置/获取额外速度值
  84.     void setSpd(int s) { spd = s; }
  85.     int getSpd() { return spd; }
  86.     // 重写属性加成方法(攻击+速度)
  87.     virtual void getAttr(Hero& hero) {
  88.         Weapon::getAttr(hero); // 调用基类攻击加成
  89.         int new_spd = hero.getSpd() + spd;
  90.         hero.setSpd(new_spd);
  91.     }
  92.     // 重写获取武器类型
  93.     string getType() override { return "Blade"; }
  94. };
  95. // 英雄装备武器的具体实现
  96. void Hero::equipWeapon(Weapon* w) {
  97.     w->getAttr(*this); // 多态调用对应武器的属性加成
  98. }
  99. // 怪物类
  100. class Monster {
  101. private:
  102.     int hp;        // 怪物生命值
  103.     int atk;       // 怪物攻击力
  104.     int level;     // 怪物等级
  105. public:
  106.     // 构造函数
  107.     Monster(int h = 50, int a = 10, int l = 1) : hp(h), atk(a), level(l) {}
  108.     // 受到伤害方法
  109.     void takeDamage(int damage) {
  110.         hp -= damage;
  111.         if (hp < 0) hp = 0; // 确保生命值不为负
  112.     }
  113.     // 判断是否死亡
  114.     bool isDead() const { return hp <= 0; }
  115.     // 根据英雄职业掉落武器
  116.     Weapon* dropWeapon(const Hero& hero) {
  117.         if (!isDead()) return nullptr; // 未死亡不掉落
  118.         int baseAtk = 5 + level * 2; // 基础攻击力计算
  119.         
  120.         // 根据英雄职业掉落不同武器
  121.         if (hero.getProfession() == "Warrior") {
  122.             return new Sword(baseAtk, 10 + level * 3); // 战士掉落长剑
  123.         } else { // 默认假设为刺客
  124.             return new Blade(baseAtk, 5 + level * 2);  // 刺客掉落匕首
  125.         }
  126.     }
  127. };
  128. int main(int argc, const char​**​ argv) {
  129.     // 创建英雄实例
  130.     Hero warrior(100, 15, 10, 5, "Warrior");   // 战士:高血量高防御
  131.     Hero assassin(80, 20, 5, 15, "Assassin"); // 刺客:高攻速低血量
  132.     // 创建怪物实例
  133.     Monster goblin(50, 8, 1);   // 绿色史莱姆(弱小怪物)
  134.     Monster orc(100, 15, 2);    // 兽人(较强怪物)
  135.     // 战斗过程
  136.     goblin.takeDamage(warrior.getAtk()); // 战士攻击哥布林
  137.     orc.takeDamage(assassin.getAtk());   // 刺客攻击兽人
  138.     // 掉落武器处理
  139.     if (goblin.isDead()) {
  140.         Weapon* weapon = goblin.dropWeapon(warrior);
  141.         cout << "哥布林掉落了 " << weapon->getType() << endl;
  142.         warrior.equipWeapon(weapon); // 战士装备武器
  143.         delete weapon; // 释放内存
  144.     }
  145.     if (orc.isDead()) {
  146.         Weapon* weapon = orc.dropWeapon(assassin);
  147.         cout << "兽人掉落了 " << weapon->getType() << endl;
  148.         assassin.equipWeapon(weapon); // 刺客装备武器
  149.         delete weapon;
  150.     }
  151.     // 显示最终属性
  152.     cout << "
  153. 战士属性 - 攻击力:" << warrior.getAtk()
  154.          << " 生命值:" << warrior.getHp() << endl;
  155.     cout << "刺客属性 - 攻击力:" << assassin.getAtk()
  156.          << " 速度:" << assassin.getSpd() << endl;
  157.     return 0;
  158. }
复制代码

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <unistd.h>
  5. #include <sstream>
  6. #include <vector>
  7. #include <memory>
  8. using namespace std;
  9. // 双向链表模板类定义
  10. template <typename T>
  11. class list {
  12. public:
  13.     // 链表节点结构体
  14.     struct Node {
  15.         T val;        // 节点存储的值
  16.         Node* next;   // 指向下一个节点的指针
  17.         Node* prev;   // 指向前一个节点的指针
  18.         
  19.         // 构造函数
  20.         Node(const T& value) : val(value), next(ptr), prev(ptr) {}
  21.     };
  22.     // 默认构造函数
  23.     list() : head(ptr), tail(ptr) {}
  24.     // 拷贝构造函数(注意:当前实现为浅拷贝,未处理深拷贝)
  25.     list(const list& other) : head(ptr), tail(ptr) {
  26.         Node* current = other.head;
  27.         while (current != ptr) {
  28.             push_back(current->val); // 逐个复制节点值
  29.             current = current->next;
  30.         }
  31.     }
  32.     // 析构函数(释放所有节点内存)
  33.     ~list() {
  34.         Node* current = head;
  35.         while (current != ptr) {
  36.             Node* next = current->next; // 保存下一个节点指针
  37.             delete current;             // 释放当前节点
  38.             current = next;             // 移动到下一个节点
  39.         }
  40.     }
  41.     // 在链表尾部添加元素
  42.     void push_back(const T& value) {
  43.         Node* newNode = new Node(value); // 创建新节点
  44.         if (tail == ptr) {           // 链表为空的情况
  45.             head = tail = newNode;
  46.         } else {                         // 链表非空的情况
  47.             tail->next = newNode;        // 原尾节点指向新节点
  48.             newNode->prev = tail;        // 新节点指向前原尾节点
  49.             tail = newNode;              // 更新尾节点
  50.         }
  51.     }
  52.     // 重载[]运算符(非const版本)
  53.     T& operator[](size_t index) {
  54.         Node* current = head;
  55.         size_t i = 0;
  56.         // 遍历链表直到找到指定索引或末尾
  57.         while (current != ptr && i < index) {
  58.             current = current->next;
  59.             i++;
  60.         }
  61.         if (current == ptr) {
  62.             throw std::out_of_range("Index out of range"); // 越界检查
  63.         }
  64.         return current->val; // 返回找到的节点值(可修改)
  65.     }
  66.     // 重载[]运算符(const版本)
  67.     const T& operator[](size_t index) const {
  68.         Node* current = head;
  69.         size_t i = 0;
  70.         // 遍历链表直到找到指定索引或末尾
  71.         while (current != ptr && i < index) {
  72.             current = current->next;
  73.             i++;
  74.         }
  75.         if (current == ptr) {
  76.             throw std::out_of_range("Index out of range"); // 越界检查
  77.         }
  78.         return current->val; // 返回找到的节点值(不可修改)
  79.     }
  80.     // 重载输出运算符(友元函数)
  81.     friend std::ostream& operator<<(std::ostream& os, const list& lst) {
  82.         Node* current = lst.head;
  83.         os << "[";
  84.         // 遍历链表输出元素
  85.         while (current !=ptr) {
  86.             os << current->val;
  87.             if (current->next != ptr) {
  88.                 os << ", "; // 添加分隔符(非最后一个元素)
  89.             }
  90.             current = current->next;
  91.         }
  92.         os << "]"; // 结束列表符号
  93.         return os;
  94.     }
  95. private:
  96.     Node* head; // 链表头指针
  97.     Node* tail; // 链表尾指针
  98. };
  99. int main(int argc, const char​**​ argv) {
  100.     list<int> myList; // 创建整型链表实例
  101.    
  102.     // 向链表添加元素
  103.     myList.push_back(1);
  104.     myList.push_back(2);
  105.     myList.push_back(3);
  106.    
  107.     // 输出链表内容
  108.     std::cout << "List: " << myList << std::endl;
  109.    
  110.     // 访问指定索引元素
  111.     std::cout << "Element at index 1: " << myList[1] << std::endl;
  112.    
  113.     return 0;
  114. }
复制代码







免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莫张周刘王

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表