清明假期间

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

1.思维导图

2.90题

 

 

 

3.

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

using namespace std;

class Weapon;

class Hero{
private:
	int hp;
	int atk;
	int def;
	int spd;
	string profession;
public:
	Hero(int h=0,int a=0,int d=0,int s=0,string p = "Warrior")
		:hp(h),atk(a),def(d),spd(s),profession(p)
		{}
	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 setProfession(string p) {profession = p;}

	int getAtk(){return atk;}
	int getDef(){return def;}
	int getSpd(){return spd;}
	int getHp(){return hp;}
	string getProfession() const {return profession;}
	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 string getType() = 0; // 获取武器类型
    virtual ~Weapon() {} 

};


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);
	}
 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);
	}
 string getType() override { return "Blade"; }
};


void Hero::equipWeapon(Weapon* w){
	// 英雄装备武器的目的,是为了获得不同的属性
	// 根据武器的类型不同,获得的属性是不同的
	// 我只要让不同武器,里面都写上一个叫做 getAttr的函数,但是不同的武器,getAttr函数会为英雄获得不同的属性
	w->getAttr(*this);
}
class Monster {
private:
    int hp;
    int atk;
    int level; // 怪物等级影响武器属性
public:
    Monster(int h=50, int a=10, int l=1) : hp(h), atk(a), level(l) {}

    void takeDamage(int damage) {
        hp -= damage;
        if (hp < 0) hp = 0;
    }

    bool isDead() const {
        return hp <= 0;
    }
	 // 根据英雄职业掉落武器
    Weapon* dropWeapon(const Hero& hero) {
        if (!isDead()) return nullptr;

        string prof = hero.getProfession();
        int baseAtk = 5 + level * 2;

        if (prof == "Warrior") {
            return new Sword(baseAtk, 10 + level * 3); // 战士掉落长剑
        } else { // Assassin
            return new Blade(baseAtk, 5 + level * 2);  // 刺客掉落短剑
        }
    }
};
int main(int argc,const char** argv){
 Hero warrior(100, 15, 10, 5, "Warrior");
    Hero assassin(80, 20, 5, 15, "Assassin");
    
    // 创建怪物
    Monster goblin(50, 8, 1);
    Monster orc(100, 15, 2);
    
    // 战斗
    goblin.takeDamage(warrior.getAtk());
    orc.takeDamage(assassin.getAtk());
    
    // 掉落武器
    if (goblin.isDead()) {
        Weapon* weapon = goblin.dropWeapon(warrior);
        cout << "Goblin dropped a " << weapon->getType() << endl;
        warrior.equipWeapon(weapon);
        delete weapon;
    }
    
    if (orc.isDead()) {
        Weapon* weapon = orc.dropWeapon(assassin);
        cout << "Orc dropped a " << weapon->getType() << endl;
        assassin.equipWeapon(weapon);
        delete weapon;
    }
    
    // 显示英雄属性
    cout << "\nWarrior stats - ATK:" << warrior.getAtk() 
         << " HP:" << warrior.getHp() << endl;
    cout << "Assassin stats - ATK:" << assassin.getAtk() 
         << " SPD:" << assassin.getSpd() << endl;
	return 0;
}

4.

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

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(const list& other) : head(nullptr), tail(nullptr) {
        Node* current = other.head;
        while (current != nullptr) {
            push_back(current->val);
            current = current->next;
        }
    }
 // 析构函数
    ~list() {
        Node* current = head;
        while (current != nullptr) {
            Node* next = current->next;
            delete current;
            current = next;
        }
    }
 // 在链表尾部添加元素
    void push_back(const T& value) {
        Node* newNode = new Node(value);
        if (tail == nullptr) {
            // 链表为空
            head = tail = newNode;
        } else {
            tail->next = newNode;
            newNode->prev = tail;
            tail = newNode;
        }
    }
	  T& operator[](size_t index) {
        Node* current = head;
        size_t i = 0;
        while (current != nullptr && i < index) {
            current = current->next;
            i++;
        }
        if (current == nullptr) {
            throw std::out_of_range("Index out of range");
        }
        return current->val;
    }

	   const T& operator[](size_t index) const {
        Node* current = head;
        size_t i = 0;
        while (current != nullptr && i < index) {
            current = current->next;
            i++;
        }
        if (current == nullptr) {
            throw std::out_of_range("Index out of range");
        }
        return current->val;
    }
	    friend std::ostream& operator<<(std::ostream& os, const list& lst) {
        Node* current = lst.head;
        os << "[";
        while (current != nullptr) {
            os << current->val;
            if (current->next != nullptr) {
                os << ", ";
            }
            current = current->next;
        }
        os << "]";
        return os;
    }

private:
    Node* head; // 记录链表头节点
    Node* tail; // 记录链表尾节点
};
int main(int argc,const char** argv){
  list<int> myList;
    myList.push_back(1);
    myList.push_back(2);
    myList.push_back(3);
    
    std::cout << "List: " << myList << std::endl;
    std::cout << "Element at index 1: " << myList[1] << std::endl;
    
    return 0;
}


网站公告

今日签到

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