欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > 清明假期间

清明假期间

2025/4/14 8:37:55 来源:https://blog.csdn.net/strong_butter/article/details/147028311  浏览:    关键词:清明假期间

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 { // Assassinreturn 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;
}

版权声明:

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

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