您的位置:首页 > 新闻 > 热点要闻 > 宁波网站建设费用报价_在线图片编辑器下载_网站网址查询工具_建设网站公司

宁波网站建设费用报价_在线图片编辑器下载_网站网址查询工具_建设网站公司

2025/4/18 4:09:49 来源:https://blog.csdn.net/qq_68945759/article/details/147027731  浏览:    关键词:宁波网站建设费用报价_在线图片编辑器下载_网站网址查询工具_建设网站公司
宁波网站建设费用报价_在线图片编辑器下载_网站网址查询工具_建设网站公司

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>using namespace std;// 武器基类
class Weapon {
public:virtual ~Weapon() {}virtual string getName() const = 0;  // 获取武器名称virtual int getAtk() const = 0;      // 获取武器攻击力
};// 具体武器类
class Sword : public Weapon {
public:string getName() const override { return "Sword"; }int getAtk() const override { return 10; }
};class Blade : public Weapon {
public:string getName() const override { return "Blade"; }int getAtk() const override { return 7; }
};class Axe : public Weapon {
public:string getName() const override { return "Axe"; }int getAtk() const override { return 15; }
};
// 英雄类
class Hero {
public:enum class Profession { Warrior, Archer, Mage };Hero(Profession profession) : profession(profession) {}// 获取英雄职业Profession getProfession() const { return profession; }private:Profession profession;
};
class Monster {
public:Monster() {// 初始化随机种子srand(static_cast<unsigned int>(time(0)));}// 怪物死亡时掉落武器Weapon* die(const Hero& hero) {cout << "Monster died!" << endl;// 根据英雄职业或随机规则决定掉落武器Weapon* droppedWeapon = dropWeapon(hero);cout << "Dropped weapon: " << droppedWeapon->getName() << endreturn droppedWeapon;}private:// 根据英雄职业或随机掉落武器Weapon* dropWeapon(const Hero& hero) {if (hero.getProfession() == Hero::Profession::Warrior) {return new Axe();  // 战士掉落斧头} else if (hero.getProfession() == Hero::Profession::Archer) return new Sword();  // 弓箭手掉落长剑} else if (hero.getProfession() == Hero::Profession::Mage) {return new Blade();  // 法师掉落短剑}// 如果是其他职业,随机掉落武器int randChoice = rand() % 3;if (randChoice == 0) {return new Sword();} else if (randChoice == 1) {return new Blade();} else {return new Axe();}}
};
int main() {// 创建不同职业的英雄Hero warrior(Hero::Profession::Warrior);Hero archer(Hero::Profession::Archer);Hero mage(Hero::Profession::Mage);// 创建怪物Monster monster;// 模拟怪物死亡并掉落武器Weapon* weapon1 = monster.die(warrior);  // 战士掉落斧头Weapon* weapon2 = monster.die(archer);   // 弓箭手掉落长剑Weapon* weapon3 = monster.die(mage);     // 法师掉落短剑// 删除掉落的武器,防止内存泄漏                                  delete weapon1;delete weapon2;delete weapon3;return 0;
}
#include <iostream>
using namespace std;template <typename T>
class List {
public:struct Node {T val;Node* next;Node* prev;};// 构造函数List() : head(nullptr), tail(nullptr) {}// 析构函数~List() {clear();}// 向链表末尾添加元素void push_back(const T& value) {Node* newNode = new Node{value, nullptr, tail};  // 创建新节点if (tail) {tail->next = newNode;  // 如果链表非空,将新的节点连接到尾节}tail = newNode;  // 更新尾节点if (!head) {head = newNode;  // 如果链表为空,更新头节点}}// 访问链表中指定位置的元素T& operator[](size_t index) {Node* current = head;size_t count = 0;while (current && count < index) {current = current->next;count++;}if (current) {return current->val;}throw out_of_range("Index out of range");}// 打印链表内容friend ostream& operator<<(ostream& os, const List<T>& list) {Node* current = list.head;while (current) {os << current->val << " ";current = current->next;}return os;}// 清除链表void clear() {Node* current = head;while (current) {Node* nextNode = current->next;delete current;  // 删除当前节点current = nextNode;  // 移动到下一个节点}head = tail = nullptr;  // 头尾指针置空}private:Node* head;  // 链表头Node* tail;  // 链表尾
};int main() {List<int> myList;myList.push_back(10);myList.push_back(20);myList.push_back(30);cout << "链表内容: " << myList << endl;cout << "访问索引1的元素: " << myList[1] << endl;return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com