#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;
}