1.实验4
pets.hpp
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class MachinePets{ 5 private: 6 string nickname; 7 public: 8 MachinePets(const string s) 9 { 10 nickname=s; 11 } 12 virtual string talk()=0; 13 string get_nickname() const{ 14 return nickname; 15 } 16 }; 17 class PetCats: public MachinePets{ 18 public: 19 PetCats(const string s):MachinePets(s) 20 {} 21 string talk() 22 { 23 string talk="miao wu ~"; 24 return talk; 25 } 26 }; 27 class PetDogs: public MachinePets{ 28 public: 29 PetDogs(const string s):MachinePets(s) 30 { 31 32 } 33 string talk() 34 { 35 string talk="wang wang ~"; 36 return talk; 37 } 38 };
task4.cpp
1 #include <iostream> 2 #include "pets.hpp" 3 4 void play(MachinePets &obj) { 5 std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl; 6 } 7 8 void test() { 9 PetCats cat("miku"); 10 PetDogs dog("da huang"); 11 12 play( cat ); 13 play( dog ); 14 } 15 16 int main() { 17 test(); 18 }
运行结果
2.实验5
person.hpp
1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 #include<vector> 5 #include <sstream> 6 using namespace std; 7 class Person{ 8 private: 9 string name; 10 string telephone; 11 string email; 12 public: 13 Person(string n=" ",string t=" ",string e=" ") 14 { 15 name=n;telephone=t;email=e; 16 } 17 Person(const Person &p) 18 { 19 name=p.name;telephone=p.telephone;email=p.email; 20 } 21 void update_telephone() 22 { 23 cout<<"Enter the telephone number: "; 24 string ut; 25 cin>>ut; 26 telephone=ut; 27 cout<<"telephone number has been updated...."<<endl; 28 } 29 void update_email() 30 { 31 cout<<"Enter the email address: "; 32 string um; 33 cin>>um; 34 email=um; 35 cout<<"email address has been updated...."<<endl; 36 } 37 friend ostream& operator<<(ostream &out, const Person &p); 38 friend istream& operator>>(istream &in, Person &p); 39 friend bool operator==(const Person &p1, const Person &p2); 40 }; 41 bool operator==(const Person &p1, const Person &p2) 42 { 43 if(p1.name!=p2.name||p1.telephone!=p2.telephone) 44 return false; 45 else return true; 46 } 47 ostream& operator <<(ostream &out, const Person &p) 48 { 49 out<<left<<setfill(' ')<<setw(15)<<p.name 50 <<setw(15)<<p.telephone 51 <<setw(20)<<p.email; 52 return out; 53 } 54 istream& operator >>(istream &in, Person &p) 55 { 56 57 getline(cin,p.name); 58 getline(cin,p.telephone); 59 getline(cin,p.email); 60 cout<<endl; 61 return in; 62 }
task5.cpp
1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 #include<vector> 5 #include <sstream> 6 void test() { 7 vector<Person> phone_book; 8 Person p; 9 cout << "Enter person's contact until press Ctrl + Z" << endl; 10 while(cin>>p) 11 { 12 phone_book.push_back(p); 13 } 14 cin.clear(); 15 cout << "\nupdate someone's contact: \n"; 16 phone_book.at(0).update_telephone(); 17 phone_book.at(0).update_email(); 18 19 cout << "\ndisplay all contacts' info\n"; 20 for(auto &phone: phone_book) 21 cout << phone << endl; 22 23 cout << "\ntest whether the same contact\n"; 24 cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; 25 } 26 27 int main() { 28 test(); 29 } 30
运行结果
3.实验6
swordsman.h
1 #include "player.h" 2 class swordsman : public player // subclass swordsman publicly inherited from base player 3 { 4 public: 5 swordsman(int lv_in=1, string name_in="Not Given"); 6 // constructor with default level of 1 and name of "Not given" 7 void isLevelUp(); 8 bool attack (player &p); 9 bool specialatt(player &p); 10 /* These three are derived from the pure virtual functions of base class 11 The definition of them will be given in this subclass. */ 12 void AI(player &p); // Computer opponent 13 };
container.cpp
1 #include<iostream> 2 #include"container.h" 3 using namespace std; 4 container::container() 5 { 6 set(0,0); 7 } 8 9 // set the item numbers 10 void container::set(int heal_n, int mw_n) 11 { 12 numOfHeal=heal_n; 13 numOfMW=mw_n; 14 } 15 16 // get the number of heal 17 int container::nOfHeal() 18 { 19 return numOfHeal; 20 } 21 22 // get the number of magic water 23 int container::nOfMW() 24 { 25 return numOfMW; 26 } 27 28 // display the items; 29 void container::display() 30 { 31 cout<<"Your bag contains: "<<endl; 32 cout<<"Heal(HP+100): "<<numOfHeal<<endl; 33 cout<<"Magic Water (MP+80): "<<numOfMW<<endl; 34 } 35 36 //use heal 37 bool container::useHeal() 38 { 39 numOfHeal--; 40 return 1; // use heal successfully 41 } 42 43 //use magic water 44 bool container::useMW() 45 { 46 numOfMW--; 47 return 1; // use magic water successfully 48 }
container.h
1 #ifndef _CONTAINER // Conditional compilation 2 #define _CONTAINER 3 4 class container // Inventory 5 { 6 protected: 7 int numOfHeal; // number of heal 8 int numOfMW; // number of magic water 9 public: 10 container(); // constuctor 11 void set(int heal_n, int mw_n); // set the items numbers 12 int nOfHeal(); // get the number of heal 13 int nOfMW(); // get the number of magic water 14 void display(); // display the items; 15 bool useHeal(); // use heal 16 bool useMW(); // use magic water 17 }; 18 19 #endif
main.cpp
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 #include "swordsman.h" 6 7 8 int main() 9 { 10 string tempName; 11 bool success=0; //flag for storing whether operation is successful 12 cout <<"Please input player's name: "; 13 cin >>tempName; // get player's name from keyboard input 14 player *human; // use pointer of base class, convenience for polymorphism 15 int tempJob; // temp choice for job selection 16 do 17 { 18 cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl; 19 cin>>tempJob; 20 system("cls"); // clear the screen 21 switch(tempJob) 22 { 23 case 1: 24 human=new swordsman(1,tempName); // create the character with user inputted name and job 25 success=1; // operation succeed 26 break; 27 default: 28 break; // In this case, success=0, character creation failed 29 } 30 }while(success!=1); // so the loop will ask user to re-create a character 31 32 int tempCom; // temp command inputted by user 33 int nOpp=0; // the Nth opponent 34 for(int i=1;nOpp<5;i+=2) // i is opponent's level 35 { 36 nOpp++; 37 system("cls"); 38 cout<<"STAGE" <<nOpp<<endl; 39 cout<<"Your opponent, a Level "<<i<<" Swordsman."<<endl; 40 system("pause"); 41 swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior" 42 human->reFill(); // get HP/MP refill before start fight 43 44 while(!human->death() && !enemy.death()) // no died 45 { 46 success=0; 47 while (success!=1) 48 { 49 showinfo(*human,enemy); // show fighter's information 50 cout<<"Please give command: "<<endl; 51 cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl; 52 cin>>tempCom; 53 switch(tempCom) 54 { 55 case 0: 56 cout<<"Are you sure to exit? Y/N"<<endl; 57 char temp; 58 cin>>temp; 59 if(temp=='Y'||temp=='y') 60 return 0; 61 else 62 break; 63 case 1: 64 success=human->attack(enemy); 65 human->isLevelUp(); 66 enemy.isDead(); 67 break; 68 case 2: 69 success=human->specialatt(enemy); 70 human->isLevelUp(); 71 enemy.isDead(); 72 break; 73 case 3: 74 success=human->useHeal(); 75 break; 76 case 4: 77 success=human->useMW(); 78 break; 79 default: 80 break; 81 } 82 } 83 if(!enemy.death()) // If AI still alive 84 enemy.AI(*human); 85 else // AI died 86 { 87 cout<<"YOU WIN"<<endl; 88 human->transfer(enemy); // player got all AI's items 89 } 90 if (human->death()) 91 { 92 system("cls"); 93 cout<<endl<<setw(50)<<"GAME OVER"<<endl; 94 delete human; // player is dead, program is getting to its end, what should we do here? 95 system("pause"); 96 return 0; 97 } 98 } 99 } 100 delete human; // You win, program is getting to its end, what should we do here? 101 system("cls"); 102 cout<<"Congratulations! You defeated all opponents!!"<<endl; 103 system("pause"); 104 return 0; 105 } 106
player.h
1 #ifndef _PLAYER 2 #define _PLAYER 3 #include<iostream> 4 #include <iomanip> // use for setting field width 5 #include <time.h> // use for generating random factor 6 #include "container.h" 7 #include<string> 8 using namespace std; 9 enum job {sw, ar, mg}; /* define 3 jobs by enumerate type 10 sword man, archer, mage */ 11 class player 12 { 13 friend void showinfo(player &p1, player &p2); 14 friend class swordsman; 15 16 protected: 17 int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV; 18 // General properties of all characters 19 string name; // character name 20 job role; /* character's job, one of swordman, archer and mage, 21 as defined by the enumerate type */ 22 container bag; // character's inventory 23 24 public: 25 virtual bool attack(player &p)=0; // normal attack 26 virtual bool specialatt(player &p)=0; //special attack 27 virtual void isLevelUp()=0; // level up judgement 28 /* Attention! 29 These three methods are called "Pure virtual functions". 30 They have only declaration, but no definition. 31 The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects. 32 The detailed definition of these pure virtual functions will be given in subclasses. */ 33 34 void reFill(); // character's HP and MP resume 35 bool death(); // report whether character is dead 36 void isDead(); // check whether character is dead 37 bool useHeal(); // consume heal, irrelevant to job 38 bool useMW(); // consume magic water, irrelevant to job 39 void transfer(player &p); // possess opponent's items after victory 40 void showRole(); // display character's job 41 42 private: 43 bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited 44 }; 45 46 #endif
swordsman.cpp
1 #include<iostream> 2 #include"swordsman.h" 3 #include<cstdlib> 4 using namespace std; 5 swordsman::swordsman(int lv_in, string name_in) 6 { 7 role=sw; // enumerate type of job 8 LV=lv_in; 9 name=name_in; 10 11 // Initialising the character's properties, based on his level 12 HPmax=150+8*(LV-1); // HP increases 8 point2 per level 13 HP=HPmax; 14 MPmax=75+2*(LV-1); // MP increases 2 points per level 15 MP=MPmax; 16 AP=25+4*(LV-1); // AP increases 4 points per level 17 DP=25+4*(LV-1); // DP increases 4 points per level 18 speed=25+2*(LV-1); // speed increases 2 points per level 19 20 playerdeath=0; 21 EXP=LV*LV*75; 22 bag.set(lv_in, lv_in); 23 } 24 25 void swordsman::isLevelUp() 26 { 27 if(EXP>=LV*LV*75) 28 { 29 LV++; 30 AP+=4; 31 DP+=4; 32 HPmax+=8; 33 MPmax+=2; 34 speed+=2; 35 cout<<name<<" Level UP!"<<endl; 36 cout<<"HP improved 8 points to "<<HPmax<<endl; 37 cout<<"MP improved 2 points to "<<MPmax<<endl; 38 cout<<"Speed improved 2 points to "<<speed<<endl; 39 cout<<"AP improved 4 points to "<<AP<<endl; 40 cout<<"DP improved 5 points to "<<DP<<endl; 41 system("pause"); 42 isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp 43 } 44 } 45 46 bool swordsman::attack(player &p) 47 { 48 double HPtemp=0; // opponent's HP decrement 49 double EXPtemp=0; // player obtained exp 50 double hit=1; // attach factor, probably give critical attack 51 srand((unsigned)time(NULL)); // generating random seed based on system time 52 53 // If speed greater than opponent, you have some possibility to do double attack 54 if ((speed>p.speed) && (rand()%100<(speed-p.speed))) // rand()%100 means generates a number no greater than 100 55 { 56 HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance 57 cout<<name<<"'s quick strike hit "<<p.name<<", "<<p.name<<"'s HP decreased "<<HPtemp<<endl; 58 p.HP=int(p.HP-HPtemp); 59 EXPtemp=(int)(HPtemp*1.2); 60 } 61 62 // If speed smaller than opponent, the opponent has possibility to evade 63 if ((speed<p.speed) && (rand()%50<1)) 64 { 65 cout<<name<<"'s attack has been evaded by "<<p.name<<endl; 66 system("pause"); 67 return 1; 68 } 69 70 // 10% chance give critical attack 71 if (rand()%100<=10) 72 { 73 hit=1.5; 74 cout<<"Critical attack: "; 75 } 76 77 // Normal attack 78 HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10)); 79 cout<<name<<" uses bash, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; 80 EXPtemp=(int)(EXPtemp+HPtemp*1.2); 81 p.HP=(int)(p.HP-HPtemp); 82 cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; 83 EXP=(int)(EXP+EXPtemp); 84 system("pause"); 85 return 1; // Attack success 86 } 87 88 bool swordsman::specialatt(player &p) 89 { 90 if(MP<40) 91 { 92 cout<<"You don't have enough magic points!"<<endl; 93 system("pause"); 94 return 0; // Attack failed 95 } 96 else 97 { 98 MP-=40; // consume 40 MP to do special attack 99 100 //10% chance opponent evades 101 if(rand()%100<=10) 102 { 103 cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl; 104 system("pause"); 105 return 1; 106 } 107 108 double HPtemp=0; 109 double EXPtemp=0; 110 //double hit=1; 111 //srand(time(NULL)); 112 HPtemp=(int)(AP*1.2+20); // not related to opponent's DP 113 EXPtemp=(int)(HPtemp*1.5); // special attack provides more experience 114 cout<<name<<" uses leap attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl; 115 cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl; 116 p.HP=(int)(p.HP-HPtemp); 117 EXP=(int)(EXP+EXPtemp); 118 system("pause"); 119 } 120 return 1; // special attack succeed 121 } 122 123 // Computer opponent 124 void swordsman::AI(player &p) 125 { 126 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))) 127 // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round 128 { 129 useHeal(); 130 } 131 else 132 { 133 if(MP>=40 && HP>0.5*HPmax && rand()%100<=30) 134 // AI has enough MP, it has 30% to make special attack 135 { 136 specialatt(p); 137 p.isDead(); // check whether player is dead 138 } 139 else 140 { 141 if (MP<40 && HP>0.5*HPmax && bag.nOfMW()) 142 // Not enough MP && HP is safe && still has magic water 143 { 144 useMW(); 145 } 146 else 147 { 148 attack(p); // normal attack 149 p.isDead(); 150 } 151 } 152 } 153 }
player.cpp
1 #include<iostream> 2 #include<iomanip> 3 #include"player.h" 4 using namespace std; 5 6 void player::reFill() 7 { 8 HP=HPmax; // HP and MP fully recovered 9 MP=MPmax; 10 } 11 12 // report whether character is dead 13 bool player::death() 14 { 15 return playerdeath; 16 } 17 18 // check whether character is dead 19 void player::isDead() 20 { 21 if(HP<=0) // HP less than 0, character is dead 22 { 23 cout<<name<<" is Dead." <<endl; 24 system("pause"); 25 playerdeath=1; // give the label of death value 1 26 } 27 } 28 29 // consume heal, irrelevant to job 30 bool player::useHeal() 31 { 32 if(bag.nOfHeal()>0) 33 { 34 HP=HP+100; 35 if(HP>HPmax) // HP cannot be larger than maximum value 36 HP=HPmax; // so assign it to HPmax, if necessary 37 cout<<name<<" used Heal, HP increased by 100."<<endl; 38 bag.useHeal(); // use heal 39 system("pause"); 40 return 1; // usage of heal succeed 41 } 42 else // If no more heal in bag, cannot use 43 { 44 cout<<"Sorry, you don't have heal to use."<<endl; 45 system("pause"); 46 return 0; // usage of heal failed 47 } 48 } 49 50 // consume magic water, irrelevant to job 51 bool player::useMW() 52 { 53 if(bag.nOfMW()>0) 54 { 55 MP=MP+100; 56 if(MP>MPmax) 57 MP=MPmax; 58 cout<<name<<" used Magic Water, MP increased by 100."<<endl; 59 bag.useMW(); 60 system("pause"); 61 return 1; // usage of magic water succeed 62 } 63 else 64 { 65 cout<<"Sorry, you don't have magic water to use."<<endl; 66 system("pause"); 67 return 0; // usage of magic water failed 68 } 69 } 70 71 // possess opponent's items after victory 72 void player::transfer(player &p) 73 { 74 cout<<name<<" got"<<p.bag.nOfHeal()<<" Heal, and "<<p.bag.nOfMW()<<" Magic Water."<<endl; 75 system("pause"); 76 HP+=p.bag.nOfHeal(); 77 MP+=p.bag.nOfMW(); 78 // set the character's bag, get opponent's items 79 } 80 81 // display character's job 82 void player::showRole() 83 { 84 switch(role) 85 { 86 case sw: 87 cout<<"Swordsman"; 88 break; 89 case ar: 90 cout<<"Archer"; 91 break; 92 case mg: 93 cout<<"Mage"; 94 break; 95 default: 96 break; 97 } 98 } 99 100 101 // display character's job 102 void showinfo(player &p1, player &p2) 103 { 104 system("cls"); 105 cout<<"##############################################################"<<endl; 106 cout<<"# Player"<<setw(10)<<p1.name<<" LV. "<<setw(3) <<p1.LV 107 <<" # Opponent"<<setw(10)<<p2.name<<" LV. "<<setw(3) <<p2.LV<<" #"<<endl; 108 cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<'/'<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999) 109 <<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<'/'<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999) 110 <<" # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<'/'<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999) 111 <<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<'/'<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<" #"<<endl; 112 cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999) 113 <<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999) 114 <<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999) 115 <<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999) 116 <<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999) 117 <<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<" #"<<endl; 118 cout<<"# EXP"<<setw(7)<<p1.EXP<<" Job: "<<setw(7); 119 p1.showRole(); 120 cout<<" # EXP"<<setw(7)<<p2.EXP<<" Job: "<<setw(7); 121 p2.showRole(); 122 cout<<" #"<<endl; 123 cout<<"--------------------------------------------------------------"<<endl; 124 p1.bag.display(); 125 cout<<"##############################################################"<<endl; 126 }
运行结果
标签:string,继承,void,多态,player,int,实验,include,name From: https://www.cnblogs.com/lc114514/p/16927725.html