C++——清明

发布于:2025-04-08 ⋅ 阅读:(18) ⋅ 点赞:(0)

 

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
#include <ctime>

using namespace std;

class Weapon; // 前置声明

class Hero{
private:
    int hp;
    int atk;
    int def;
    int spd;
    string job;  // 新增职业属性

public:
    Hero(int h=0, int a=0, int d=0, int s=0, string j="Warrior")
        : hp(h), atk(a), def(d), spd(s), job(j)
    {}

    void setAtk(int a) { atk = a; }
    void setDef(int d) { def = d; }
    void setSpd(int s) { spd = s; }
    void setHp(int h) { hp = h; }
    void setJob(string j) { job = j; }

    int getAtk() { return atk; }
    int getDef() { return def; }
    int getSpd() { return spd; }
    int getHp() { return hp; }
    string getJob() { return job; }

    void equipWeapon(Weapon* w);
};

class Weapon{
private:
    int atk;

public:
    Weapon(int atk = 1) : atk(atk) {}

    void setAtk(int a) { atk = a; }
    int getAtk() { return atk; }

    // 根据武器类型的不同,英雄获得不同的属性
    virtual void getAttr(Hero& hero) {
        int new_atk = hero.getAtk() + atk;
        hero.setAtk(new_atk);
    }

    virtual void purl_virtual_func() = 0;
    virtual string getType() = 0;  // 新增获取武器类型
};

class Sword : public Weapon {
private:
    int hp;

public:
    Sword(int atk = 1, int hp = 1)
        : Weapon(atk), hp(hp) {}

    void setHp(int h) { hp = h; }
    int getHp() { return hp; }

    virtual void getAttr(Hero& hero) {
        Weapon::getAttr(hero);
        int new_hp = hero.getHp() + hp;
        hero.setHp(new_hp);
    }

    void purl_virtual_func() {}

    string getType() override {
        return "Sword";
    }
};

class Blade : public Weapon {
private:
    int spd;

public:
    Blade(int atk = 1, int spd = 1)
        : Weapon(atk), spd(spd) {}

    void setSpd(int s) { spd = s; }
    int getSpd() { return spd; }

    virtual void getAttr(Hero& hero) {
        Weapon::getAttr(hero);
        int new_spd = hero.getSpd() + spd;
        hero.setSpd(new_spd);
    }

    void purl_virtual_func() {}

    string getType() override {
        return "Blade";
    }
};

class Monster {
private:
    int hp;
    int atk;

public:
    Monster(int h = 50, int a = 10) : hp(h), atk(a) {}

    int getHp() { return hp; }
    int getAtk() { return atk; }

    // 怪物死亡时掉落武器
    Weapon* die(Hero& hero) {
        srand(time(0));  // 初始化随机种子
        string job = hero.getJob();
        
        // 根据英雄的职业掉落不同的武器
        if (job == "Warrior") {
            return new Sword(10, 20);  // 战士掉落剑,增加攻击力和生命值
        } else if (job == "Rogue") {
            return new Blade(8, 15);  // 盗贼掉落短剑,增加攻击力和速度
        } else {
            // 默认掉落短剑
            return new Blade(5, 10);  // 其他职业掉落短剑
        }
    }
};

void Hero::equipWeapon(Weapon* w) {
    w->getAttr(*this);
}

int main(int argc, const char** argv) {
    Hero hero(100, 15, 10, 5, "Warrior");  // 创建一个战士
    Monster monster(50, 10);  // 创建一个怪物

    cout << "Hero HP: " << hero.getHp() << " Attack: " << hero.getAtk() << endl;

    // 英雄击败怪物,掉落武器
    Weapon* droppedWeapon = monster.die(hero);

    // 英雄装备掉落的武器
    hero.equipWeapon(droppedWeapon);
    
    cout << "After defeating the monster and equipping dropped weapon:" << endl;
    cout << "Hero HP: " << hero.getHp() << " Attack: " << hero.getAtk() << endl;

    delete droppedWeapon;  // 清理掉落的武器
    return 0;
}

#include <iostream>
using namespace std;

template <typename T>
class list {
public:
    struct Node {
        T val;
        Node* next;
        Node* prev;
        Node(const T& value) : val(value), next(nullptr), prev(nullptr) {}
    };
    list() : head(nullptr), tail(nullptr) {}
    ~list() {
        clear();
    }
    void push_back(const T& value) {
        Node* newNode = new Node(value);
        if (!tail) {
            head = tail = newNode;  // 如果链表为空,新节点既是头节点又是尾节点
        } else {
            tail->next = newNode;   // 将当前尾节点的 next 指向新节点
            newNode->prev = tail;   // 新节点的 prev 指向当前尾节点
            tail = newNode;         // 更新尾节点为新节点
        }
    }
    friend ostream& operator<<(ostream& os, const list<T>& lst) {
        Node* current = lst.head;
        while (current) {
            os << current->val << " ";
            current = current->next;
        }
        return os;
    }
    T& operator[](size_t index) {
        Node* current = head;
        size_t count = 0;
        while (current && count < index) {
            current = current->next;
            count++;
        }
        if (!current) {
            throw out_of_range("Index out of range");
        }
        return current->val;
    }
    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 << "List: " << myList << endl;
    try {
        cout << "Element at index 1: " << myList[1] << endl;
        cout << "Element at index 5: " << myList[5] << endl;  // 会抛出异常
    } catch (const out_of_range& e) {
        cout << e.what() << endl;
    }

    return 0;
}

C语言

 C++

 


网站公告

今日签到

点亮在社区的每一天
去签到