C++程序设计实验五 模板类与多态
三、实验内容
2. 实验任务2
Person.hpp:
#ifndef PERSON_TASK_HPP
#define PERSON_TASK_HPP
#include<iostream>
#include<string>
#include<iomanip> using namespace std;
using std::ostream;
using std::istream; class Person
{
public:
Person() {}
Person(string name, string tele) : m_name{ name }, m_tele{ tele } {} void change_tele(string new_tele)
{
m_tele = new_tele;
}
void change_ema(string new_ema)
{
m_ema = new_ema;
} friend ostream& operator<<(ostream& out, const Person& p); friend istream& operator>>(istream& in, Person& p); friend bool operator==(const Person& p1, const Person& p2); private:
string m_name;
string m_tele;
string m_ema;
}; ostream& operator<<(ostream& out, const Person& p)
{
out << left << setw(20) << p.m_name
<< left << setw(20) << p.m_tele
<< left << setw(20) << p.m_ema << endl; return out;
} istream& operator>>(istream& in, Person& p)
{
getline(in, p.m_name);
getline(in, p.m_tele);
getline(in, p.m_ema);
return in;
} bool operator==(const Person& p1, const Person& p2)
{
if (p1.m_name == p2.m_name && p1.m_tele == p2.m_tele)
return true;
else
return false;
} #endif
task2.cpp
#include<iostream>
#include<fstream>
#include<vector>
#include"Person.hpp" using namespace std; int main()
{
vector<Person> phone_book;
Person p("skdj", "44551"); while (cin >> p)
{
phone_book.push_back(p);
cin.ignore();
} for (auto& i : phone_book)
{
cout << i << endl;
} cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; ofstream fout; fout.open("phone_book.txt"); if (!fout.is_open())
{
cerr << "fail to open file phone_book.txt\n";
return 1;
} for (auto& i : phone_book)
fout << i << endl; fout.close(); }
运行结果截图:
实验三
#include <iostream>
#include <string>
using namespace std; #include "swordsman.h" int main()
{
string tempName;
bool success=0;
cout <<"Please input player's name: ";
cin >>tempName;
player *human;
int tempJob;
do
{
cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl;
cin>>tempJob;
system("cls"); // clear the screen
switch(tempJob)
{
case 1:
human=new swordsman(1,tempName); // create the character with user inputted name and job
success=1; // operation succeed
break;
default:
break; // In this case, success=0, character creation failed
}
}while(success!=1); // so the loop will ask user to re-create a character int tempCom; // temp command inputted by user
int nOpp=0; // the Nth opponent
for(int i=1;nOpp<5;i+=2) // i is opponent's level
{
nOpp++;
system("cls");
cout<<"STAGE" <<nOpp<<endl;
cout<<"Your opponent, a Level "<<i<<" Swordsman."<<endl;
system("pause");
swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior"
human->reFill(); // get HP/MP refill before start fight while(!human->death() && !enemy.death()) // no died
{
success=0;
while (success!=1)
{
showinfo(*human,enemy); // show fighter's information
cout<<"Please give command: "<<endl;
cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl;
cin>>tempCom;
switch(tempCom)
{
case 0:
cout<<"Are you sure to exit? Y/N"<<endl;
char temp;
cin>>temp;
if(temp=='Y'||temp=='y')
return 0;
else
break;
case 1:
success=human->attack(enemy);
human->isLevelUp();
enemy.isDead();
break;
case 2:
success=human->specialatt(enemy);
human->isLevelUp();
enemy.isDead();
break;
case 3:
success=human->useHeal();
break;
case 4:
success=human->useMW();
break;
default:
break;
}
}
if(!enemy.death()) // If AI still alive
enemy.AI(*human);
else // AI died
{
cout<<"YOU WIN"<<endl;
human->transfer(enemy); // player got all AI's items
}
if (human->death())
{
system("cls");
cout<<endl<<setw(50)<<"GAME OVER"<<endl;
delete human; // player is dead, program is getting to its end, what should we do here?
system("pause");
return 0;
}
}
}
delete human; // You win, program is getting to its end, what should we do here?
system("cls");
cout<<"Congratulations! You defeated all opponents!!"<<endl;
system("pause");
return 0;
}
//=======================
// container.h
//======================= // The so-called inventory of a player in RPG games
// contains two items, heal and magic water #ifndef _CONTAINER // Conditional compilation
#define _CONTAINER
using namespace std;
#include<iostream>
#include<string> class container // Inventory
{
protected:
int numOfHeal; // number of heal
int numOfMW; // number of magic water
public:
container(); // constuctor
void set(int heal_n, int mw_n); // set the items numbers
int nOfHeal(); // get the number of heal
int nOfMW(); // get the number of magic water
void display(); // display the items;
bool useHeal(); // use heal
bool useMW(); // use magic water
}; #endif
//=======================
// container.cpp
//======================= // default constructor initialise the inventory as empty
#include "container.h"
using namespace std;
#include<iostream>
#include<string> container::container()
{
set(0,0);
} // set the item numbers
void container::set(int heal_n, int mw_n)
{
numOfHeal=heal_n;
numOfMW=mw_n;
} // get the number of heal
int container::nOfHeal()
{
return numOfHeal;
} // get the number of magic water
int container::nOfMW()
{
return numOfMW;
} // display the items;
void container::display()
{
cout<<"Your bag contains: "<<endl;
cout<<"Heal(HP+100): "<<numOfHeal<<endl;
cout<<"Magic Water (MP+80): "<<numOfMW<<endl;
} //use heal
bool container::useHeal()
{
numOfHeal--;
return 1; // use heal successfully
} //use magic water
bool container::useMW()
{
numOfMW--;
return 1; // use magic water successfully
}
//=======================
// player.h
//======================= // The base class of player
// including the general properties and methods related to a character #ifndef _PLAYER
#define _PLAYER #include <iomanip> // use for setting field width
#include <time.h> // use for generating random factor
#include "container.h"
using namespace std;
#include<iostream>
#include<string> enum job {sw, ar, mg}; /* define 3 jobs by enumerate type
sword man, archer, mage */
class player
{
friend void showinfo(player &p1, player &p2);
friend class swordsman; protected:
int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
// General properties of all characters
string name; // character name
job role; /* character's job, one of swordman, archer and mage,
as defined by the enumerate type */
container bag; // character's inventory public:
virtual bool attack(player &p)=0; // normal attack
virtual bool specialatt(player &p)=0; //special attack
virtual void isLevelUp()=0; // level up judgement
/* Attention!
These three methods are called "Pure virtual functions".
They have only declaration, but no definition.
The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects.
The detailed definition of these pure virtual functions will be given in subclasses. */ void reFill(); // character's HP and MP resume
bool death(); // report whether character is dead
void isDead(); // check whether character is dead
bool useHeal(); // consume heal, irrelevant to job
bool useMW(); // consume magic water, irrelevant to job
void transfer(player &p); // possess opponent's items after victory
void showRole(); // display character's job private:
bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited
}; #endif
//=======================
// player.cpp
//=======================
#include "player.h"
using namespace std;
#include<iostream>
#include<string> // character's HP and MP resume
void player::reFill()
{
HP=HPmax; // HP and MP fully recovered
MP=MPmax;
} // report whether character is dead
bool player::death()
{
return playerdeath;
} // check whether character is dead
void player::isDead()
{
if(HP<=0) // HP less than 0, character is dead
{
cout<<name<<" is Dead." <<endl;
system("pause");
playerdeath=1; // give the label of death value 1
}
} // consume heal, irrelevant to job
bool player::useHeal()
{
if(bag.nOfHeal()>0)
{
HP=HP+100;
if(HP>HPmax) // HP cannot be larger than maximum value
HP=HPmax; // so assign it to HPmax, if necessary
cout<<name<<" used Heal, HP increased by 100."<<endl;
bag.useHeal(); // use heal
system("pause");
return 1; // usage of heal succeed
}
else // If no more heal in bag, cannot use
{
cout<<"Sorry, you don't have heal to use."<<endl;
system("pause");
return 0; // usage of heal failed
}
} // consume magic water, irrelevant to job
bool player::useMW()
{
if(bag.nOfMW()>0)
{
MP=MP+100;
if(MP>MPmax)
MP=MPmax;
cout<<name<<" used Magic Water, MP increased by 100."<<endl;
bag.useMW();
system("pause");
return 1; // usage of magic water succeed
}
else
{
cout<<"Sorry, you don't have magic water to use."<<endl;
system("pause");
return 0; // usage of magic water failed
}
} // possess opponent's items after victory
void player::transfer(player &p)
{
cout<<name<<" got"<<p.bag.nOfHeal()<<" Heal, and "<<p.bag.nOfMW()<<" Magic Water."<<endl;
system("pause");
// 3_???????????
bag.set(bag.nOfHeal() + p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW());
// set the character's bag, get opponent's items
} // display character's job
void player::showRole()
{
switch(role)
{
case sw:
cout<<"Swordsman";
break;
case ar:
cout<<"Archer";
break;
case mg:
cout<<"Mage";
break;
default:
break;
}
} // display character's job
//4_??????????????
void showinfo(player &p1, player &p2)
{
system("cls");
cout<<"##############################################################"<<endl;
cout<<"# Player"<<setw(10)<<p1.name<<" LV. "<<setw(3) <<p1.LV
<<" # Opponent"<<setw(10)<<p2.name<<" LV. "<<setw(3) <<p2.LV<<" #"<<endl;
cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<'/'<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999)
<<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<'/'<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999)
<<" # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<'/'<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999)
<<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<'/'<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<" #"<<endl;
cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999)
<<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999)
<<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999)
<<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999)
<<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999)
<<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<" #"<<endl;
cout<<"# EXP"<<setw(7)<<p1.EXP<<" Job: "<<setw(7);
p1.showRole();
cout<<" # EXP"<<setw(7)<<p2.EXP<<" Job: "<<setw(7);
p2.showRole();
cout<<" #"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
p1.bag.display();
cout<<"##############################################################"<<endl;
}
//=======================
// swordsman.h
//======================= // Derived from base class player
// For the job Swordsman
#ifndef _SWORDSMAN
#define _SWORDSMAN
#include<string>
#include "player.h"
class swordsman : public player // subclass swordsman publicly inherited from base player
{
public:
swordsman(int lv_in=1, string name_in="Not Given");
// constructor with default level of 1 and name of "Not given"
void isLevelUp();
bool attack (player &p);
bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent
}; #endif
//=======================
// swordsman.cpp
//=======================
#include "swordsman.h"
using namespace std;
#include<iostream>
#include<string> // constructor. default values don't need to be repeated here
swordsman::swordsman(int lv_in, string name_in)
{
role=sw; // enumerate type of job
LV=lv_in;
name=name_in; // Initialising the character's properties, based on his level
HPmax=150+8*(LV-1); // HP increases 8 point2 per level
HP=HPmax;
MPmax=75+2*(LV-1); // MP increases 2 points per level
MP=MPmax;
AP=25+4*(LV-1); // AP increases 4 points per level
DP=25+4*(LV-1); // DP increases 4 points per level
speed=25+2*(LV-1); // speed increases 2 points per level playerdeath=0;
EXP=LV*LV*75;
bag.set(lv_in, lv_in);
} void swordsman::isLevelUp()
{
if(EXP>=LV*LV*75)
{
LV++;
AP+=4;
DP+=4;
HPmax+=8;
MPmax+=2;
speed+=2;
cout<<name<<" Level UP!"<<endl;
cout<<"HP improved 8 points to "<<HPmax<<endl;
cout<<"MP improved 2 points to "<<MPmax<<endl;
cout<<"Speed improved 2 points to "<<speed<<endl;
cout<<"AP improved 4 points to "<<AP<<endl;
cout<<"DP improved 5 points to "<<DP<<endl;
system("pause");
isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
}
} bool swordsman::attack(player &p)
{
double HPtemp=0; // opponent's HP decrement
double EXPtemp=0; // player obtained exp
double hit=1; // attach factor, probably give critical attack
srand((unsigned)time(NULL)); // generating random seed based on system time // If speed greater than opponent, you have some possibility to do double attack
if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100
{
HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
cout<<name<<"'s quick strike hit "<<p.name<<", "<<p.name<<"'s HP decreased "<<HPtemp<<endl;
p.HP=int(p.HP-HPtemp);
EXPtemp=(int)(HPtemp*1.2);
} // If speed smaller than opponent, the opponent has possibility to evade
if ((speed<p.speed) && (rand()%50<1))
{
cout<<name<<"'s attack has been evaded by "<<p.name<<endl;
system("pause");
return 1;
} // 10% chance give critical attack
if (rand()%100<=10)
{
hit=1.5;
cout<<"Critical attack: ";
} // Normal attack
HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10));
cout<<name<<" uses bash, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
EXPtemp=(int)(EXPtemp+HPtemp*1.2);
p.HP=(int)(p.HP-HPtemp);
cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
EXP=(int)(EXP+EXPtemp);
system("pause");
return 1; // Attack success
} bool swordsman::specialatt(player &p)
{
if(MP<40)
{
cout<<"You don't have enough magic points!"<<endl;
system("pause");
return 0; // Attack failed
}
else
{
MP-=40; // consume 40 MP to do special attack //10% chance opponent evades
if(rand()%100<=10)
{
cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl;
system("pause");
return 1;
} double HPtemp=0;
double EXPtemp=0;
//double hit=1;
//srand(time(NULL));
HPtemp=(int)(AP*1.2+20); // not related to opponent's DP
EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience
cout<<name<<" uses leap attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
p.HP=(int)(p.HP-HPtemp);
EXP=(int)(EXP+EXPtemp);
system("pause");
}
return 1; // special attack succeed
} // Computer opponent
void swordsman::AI(player &p)
{
if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+100<=1.1*HPmax)&&(bag.nOfHeal()>0)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5)))
// AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
{
useHeal();
}
else
{
if(MP>=40 && HP>0.5*HPmax && rand()%100<=30)
// AI has enough MP, it has 30% to make special attack
{
specialatt(p);
p.isDead(); // check whether player is dead
}
else
{
if (MP<40 && HP>0.5*HPmax && bag.nOfMW())
// Not enough MP && HP is safe && still has magic water
{
useMW();
}
else
{
attack(p); // normal attack
p.isDead();
}
}
}
}
C++程序设计实验五 模板类与多态的更多相关文章
- Java程序设计 实验五
实 验 报 告 课程:Java 班级: 1353 姓名:李海空 学号:20135329 成绩: 指导教师:娄嘉鹏 实验日期:2015.6. ...
- 20145222黄亚奇《Java程序设计》实验五实验报告
20145222 <Java程序设计>实验五实验报告 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验步骤 本次实验我的结对编程对象是20 ...
- 20145207《Java程序设计》实验五(网络编程与安全)实验报告
<Java 程序设计>实验五(网络编程与安全)实验报告 目录 改变 网络编程与安全实验要求 实验成果 课后思考 改变 修改了之前仅仅是贴了图片,连代码都没粘的状态.不过这篇博客我只能做到写 ...
- Java程序设计实验 实验五
课程:Java程序设计实验 班级:1353 姓名:符余佳源 学号:20135321 成绩: 指导教师:娄嘉鹏 实验日期:2015. ...
- 20145120 《Java程序设计》实验五实验报告
20145120 <Java程序设计>实验五实验报告 实验名称:Java网络编程 实验内容: 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验内容. ...
- #《JAVA程序设计》 20155214 实验五 网络编程与安全
<JAVA程序设计> 20155214 实验五 网络编程与安全 实验内容 掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验要求 要求一 结对实现中缀表达式转后缀 ...
- 20155218 《Java程序设计》实验五(网络编程与安全)实验报告
20155218 <Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 (一) 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上面 ...
- 20155236 《Java程序设计》实验五(网络编程与安全)实验报告
20155236 <Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上 ...
- 20145328 《Java程序设计》实验五实验报告
20145328 <Java程序设计>实验五实验报告 实验名称 Java网络编程 实验内容 用书上的TCP代码,实现服务器与客户端. 客户端与服务器连接 客户端中输入明文,利用DES算法加 ...
- 20175316 盛茂淞 2018-2019-2 《Java程序设计》实验五 《网络安全与编程》 实验报告
20175316 盛茂淞 2018-2019-2 <Java程序设计>实验五 <网络安全与编程> 实验报告 一.实验报告封面 课程:Java程序设计 班级:1753班 姓名:盛 ...
随机推荐
- conan环境安装
环境 安装conan 使用conan 搜索包 导入包 编译 打包项目 准备源码 编译成conan包 环境 ubuntu:bionic的docker image docker run -it ubunt ...
- 使用腾讯云部署war包
目录 1.前期准备 2.springboot打war包 3.部署war包 4.导入数据库 5.修改Tomcat启动端口 6.启动服务器 7.设置腾讯云服务器防火墙规则 8.从外部访问 9.总结 10. ...
- [R语言] WGCNA入门教程
文章目录 wgcna入门-雌性小鼠肝脏表达数据的网络分析:寻找与体重有关的模块 1 数据输入和清洗 1.1 加载基因表达数据 1.2 数据清洗 1.3 加载临床特征数据 2 建设表达网络与模块检测 2 ...
- python进阶之路12之有参装饰器、多层语法糖、递归函数简介
多层语法糖 def outter1(func1): print('加载了outter1') def wrapper1(*args, **kwargs): print('执行了wrapper1') re ...
- Java基础篇——JUC初步
1.基础知识 java默认的两个线程:Main线程+GC守护线程 java并不能开启线程,需要调用底层用c语言写的本地方法 wait和sleep的区别: wait方法会释放线程锁,并且只能在同步代码块 ...
- 琐碎的想法(五)for 的前世今生
for 起因 记得大学上C语言的课,第一次遇到的问题就是循环结构里面的 for. 选择结构的 if 非常易懂,和日常生活的判断没有区别. 循环结构的 while 同样比较好理解. 本质上是一个判断 如 ...
- Spring MVC的运行流程
Spring MVC的运行流程 摘要:本文档主要结合官方给出的Spring MVC流程图结合一个简单的Spring MVC实例,分析并介绍了Spring MVC的运行流程. 目录 Spring MVC ...
- django框架之drf:2、restful规范,序列、反序列化,drf安装及使用(django原生接口及drf接口编写)
Django之drf 一.restful规范 1.概念 REST全称是Representational State Transfer,中文意思是表述:表征性状态转移,它首次出现在2000年Roy ...
- SQLSERVER 的 truncate 和 delete 有区别吗?
一:背景 1. 讲故事 在面试中我相信有很多朋友会被问到 truncate 和 delete 有什么区别 ,这是一个很有意思的话题,本篇我就试着来回答一下,如果下次大家遇到这类问题,我的答案应该可以帮 ...
- Djanngo-bbs项目
1.项目开发基本流程 1.需求分析 2.架构设计 3.分组开发 4.提交测试 5.交付上线 2.项目流程 仿造博客园项目(核心:文章的增删改查) 1.表分析: 1.1用户表 1.2个人站点表 1.3文 ...