欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > C++——清明

C++——清明

2025/4/8 6:55:04 来源:https://blog.csdn.net/2301_77654321/article/details/147028654  浏览:    关键词:C++——清明

 

#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++

 

版权声明:

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

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

热搜词