• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
nuist0415
博客园    首页    新随笔    联系   管理    订阅  订阅
实验5 继承和多态

task3

pets.hpp

 1 #pragma once
 2 
 3 using std::string;
 4 using std::cout;
 5 using std::endl;
 6 
 7 class MachinePets {
 8 public:
 9     MachinePets(const string& nickname0);
10 
11     string get_nickname() const;
12 
13     virtual string talk() const = 0;
14 
15 private:
16     string nickname;
17 };
18 
19 MachinePets::MachinePets(const string& nickname0) : nickname{ nickname0 } {
20 }
21 
22 string MachinePets::get_nickname() const {
23     return nickname;
24 }
25 
26 
27 class PetCats : public MachinePets {
28 public:
29     PetCats(const string& nickname0);
30 
31     string talk() const override;
32 };
33 
34 PetCats::PetCats(const string& nickname0) : MachinePets{ nickname0 } {
35 }
36 
37 string PetCats::talk() const {
38     return "Nyaa~";
39 }
40 
41 
42 class PetDogs : public MachinePets {
43 public:
44     PetDogs(const string& nickname0);
45 
46     string talk() const override;
47 };
48 
49 PetDogs::PetDogs(const string& nickname0) : MachinePets{ nickname0 } {
50 }
51 
52 string PetDogs::talk() const {
53     return "Wan~";
54 }

task3.cpp

 1 #include <iostream>
 2 #include <vector>
 3 #include "pets.hpp"
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     vector<MachinePets*> pets;
 9 
10     pets.push_back(new PetCats("miku"));
11     pets.push_back(new PetDogs("da huang"));
12 
13     for (auto& ptr : pets)
14         cout << ptr->get_nickname() << " says " << ptr->talk() << endl;
15 }
16 int main() {
17     test();
18 }

result3

task4

film.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <iomanip>
 5 
 6 using std::string;
 7 using std::istream;
 8 using std::ostream;
 9 using std::left;
10 using std::setw;
11 using std::endl;
12 using std::cout;
13 
14 class Film {
15 public:
16     Film(const string& name0 = "null", const string& director0 = "null",
17         const string& region0 = "null", const string& year0 = "null");
18 
19     friend istream& operator>>(istream& in, Film& film);
20     friend ostream& operator<<(ostream& out, const Film& film);
21 
22     string get_year() const;
23 
24 private:
25     string name, director, region, year;
26 };
27 
28 Film::Film(const string& name0, const string& director0, const string& region0, const string& year0)
29     :name{ name0 }, director{ director0 }, region{ region0 }, year{ year0 } {
30 }
31 
32 istream& operator>>(istream& in, Film& film) {
33     cout << "录入片名: "; in >> film.name;
34     cout << "录入导演: "; in >> film.director;
35     cout << "录入制片国家/地区: "; in >> film.region;
36     cout << "录入上映年份: "; in >> film.year;
37 
38     return in;
39 }
40 
41 ostream& operator<<(ostream& out, const Film& film) {
42     out << left;
43     out << setw(20) << film.name
44         << setw(20) << film.director
45         << setw(20) << film.region
46         << setw(20) << film.year << endl;
47 
48     return out;
49 }
50 
51 string Film::get_year() const {
52     return year;
53 }
54 
55 bool compare_by_year(const Film& f1, const Film& f2) {
56     return f1.get_year() < f2.get_year();
57 }

task4.cpp

 1 #include "film.hpp"
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 void test() {
 8     using namespace std;
 9 
10     int n;
11     cout << "输入电影数目: ";
12     cin >> n;
13 
14     cout << "录入" << n << "部影片信息" << endl;
15     vector<Film> film_lst;
16     for (int i = 0; i < n; ++i) {
17         Film f;
18         cout << string(20, '-') << "第" << i + 1 << "部影片录入" << string(20, '-') << endl;
19         cin >> f;
20         film_lst.push_back(f);
21     }
22 
23     // 按发行年份升序排序
24     sort(film_lst.begin(), film_lst.end(), compare_by_year);
25 
26     cout << string(20, '=') + "电影信息(按发行年份)" + string(20, '=') << endl;
27     for (auto& f : film_lst)
28         cout << f << endl;
29 }
30 
31 int main() {
32     test();
33 }

result4

 Task5

Complex.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 
 5 using std::istream;
 6 using std::ostream;
 7 using std::abs;
 8 
 9 template<typename T>
10 class Complex {
11 public:
12     Complex(const T & = T{}, const T& i = T{});
13     Complex(const Complex& c);
14 
15     T get_real() const;
16     T get_imag() const;
17 
18     Complex<T> operator+=(const Complex<T>& c);
19     bool operator==(const Complex<T>& other) const;
20 
21     template<typename U>
22     friend istream& operator>>(istream& in, Complex<U>& c);
23 
24     template<typename U>
25     friend ostream& operator<<(ostream& out, const Complex<U>& c);
26     
27     template<typename U>
28     friend Complex<U> operator+(const Complex<U>& c1, const Complex<U>& c2);
29 
30 private:
31     T real, imag;
32 };
33 
34 template<typename T>
35 Complex<T>::Complex(const T& r, const T& i) : real{ r }, imag{ i } {
36 }
37 
38 template<typename T>
39 Complex<T>::Complex(const Complex& c) : real{ c.real }, imag{ c.imag } {
40 }
41 
42 template<typename T>
43 T Complex<T>::get_real() const {
44     return real;
45 }
46 
47 template<typename T>
48 T Complex<T>::get_imag() const {
49     return imag;
50 }
51 
52 template<typename T>
53 Complex<T> Complex<T>::operator+=(const Complex<T>& c) {
54     real += c.real;
55     imag += c.imag;
56 
57     return *this;
58 }
59 
60 template<typename T>
61 bool Complex<T>::operator==(const Complex<T>& other) const {
62     return real == other.real && imag == other.imag;
63 }
64 
65 template<typename T>
66 istream& operator>>(istream& in, Complex<T>& c) {
67     in >> c.real >> c.imag;
68 
69     return in;
70 }
71 
72 template<typename T>
73 ostream& operator<<(ostream& out, const Complex<T>& c) {
74     if (c.imag >= 0)
75         out << c.real << " + " << c.imag << "i";
76     else
77         out << c.real << " - " << -c.imag << "i";
78 
79     return out;
80 }
81 
82 template<typename T>
83 Complex<T> operator+(const Complex<T>& c1, const Complex<T>& c2) {
84     return Complex<T>(c1.real + c2.real, c1.imag + c2.imag);
85 }

task5.cpp

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 7 using std::boolalpha;
 8 
 9 void test1() {
10     Complex<int> c1(2, -5), c2(c1);
11 
12     cout << "c1 = " << c1 << endl;
13     cout << "c2 = " << c2 << endl;
14     cout << "c1 + c2 = " << c1 + c2 << endl;
15     
16     c1 += c2;
17     cout << "c1 = " << c1 << endl;
18     cout << boolalpha << (c1 == c2) << endl;
19 }
20 
21 void test2() {
22     Complex<double> c1, c2;
23     cout << "Enter c1 and c2: ";
24     cin >> c1 >> c2;
25     cout << "c1 = " << c1 << endl;
26     cout << "c2 = " << c2 << endl;
27 
28     cout << "c1.real = " << c1.get_real() << endl;
29     cout << "c1.imag = " << c1.get_imag() << endl;
30 }
31 
32 int main() {
33     cout << "自定义类模板Complex测试1: " << endl;
34     test1();
35 
36     cout << endl;
37     
38     cout << "自定义类模板Complex测试2: " << endl;
39     test2();
40 }
View Code

result5

 task6

accumulator.h

 1 #pragma once
 2 #include "date.h"
 3 
 4 class Accumulator {
 5 private:
 6     Date lastDate;
 7     double value;
 8     double sum;
 9 
10 public:
11     Accumulator(const Date& date, double value) : lastDate(date), value(value), sum{ 0 } {}
12 
13     double getSum(const Date& date) const {
14         return sum + value * (date - lastDate);
15     }
16 
17     void change(const Date& date, double value) {
18         sum = getSum(date);
19         lastDate = date;
20         this->value = value;
21     }
22 
23     void reset(const Date& date, double value) {
24         lastDate = date;
25         this->value = value;
26         sum = 0;
27     }
28 };
View Code

account.h

 1 #pragma once
 2 
 3 #include "date.h"
 4 #include "accumulator.h"
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 class Account {
10 private:
11     string id;
12     double balance;
13     static double total;
14 
15 protected:
16     Account(const Date& date, const string& id);
17 
18     void record(const Date& date, double amount, const string& desc);
19     void error(const string& msg) const;
20 
21 public:
22     const string& getId() { return id; }
23     double getBalance() const { return balance; }
24     static double getTotal() { return total; }
25 
26     virtual void deposit(const Date& date, double amount, const string& desc) = 0;
27     virtual void withdraw(const Date& date, double amount, const string& desc) = 0;
28     virtual void settle(const Date& date) = 0;
29     
30     virtual void show() const;
31 };
32 
33 
34 class SavingsAccount : public Account {
35 private:
36     Accumulator acc;
37     double rate;
38 
39 public:
40     SavingsAccount(const Date& date, const string& id, double rate);
41 
42     double getRate() const { return rate; }
43 
44     void deposit(const Date& date, double amount, const string& desc);
45     void withdraw(const Date& date, double amount, const string& desc);
46     void settle(const Date& date);
47 };
48 
49 
50 class CreditAccount : public Account {
51 private:
52     Accumulator acc;
53     double credit;
54     double rate;
55     double fee;
56 
57     double getDebt() const {
58         double balance = getBalance();
59         return (balance < 0 ? balance : 0);
60     }
61 
62 public:
63     CreditAccount(const Date& date, const string& id, double credit, double rate, double fee);
64 
65     double getCredit() const { return credit; }
66     double getRate() const { return rate; }
67     double getFee() const { return fee; }
68     double getAvailableCredit() const {
69         if (getBalance() < 0) return credit + getBalance();
70         else return credit;
71     }
72 
73     void deposit(const Date& date, double amount, const string& desc);
74     void withdraw(const Date& date, double amount, const string& desc);
75     void settle(const Date& date);
76     void show() const;
77 };
View Code

account.cpp

 1 #include "account.h"
 2 #include <cmath>
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 double Account::total = 0;
 8 
 9 Account::Account(const Date& date, const string& id) : id(id), balance(0) {
10     date.show();
11     cout << "\t#" << id << " created" << endl;
12 }
13 
14 void Account::record(const Date& date, double amount, const string& desc) {
15     amount = floor(amount * 100 + 0.5) / 100;
16     balance += amount;
17     total += amount;
18     date.show();
19     cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
20 }
21 
22 void Account::show() const {
23     cout << id << "\tBalance: " << balance;
24 }
25 
26 void Account::error(const string& msg) const {
27     cout << "Error (#" << id << "): " << msg << endl;
28 }
29 
30 
31 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate)
32     : Account(date, id), rate(rate), acc(date, 0) {}
33 
34 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) {
35     record(date, amount, desc);
36     acc.change(date, getBalance());
37 }
38 
39 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) {
40     if (amount > getBalance()) {
41         error("not enough money");
42     }
43     else {
44         record(date, -amount, desc);
45         acc.change(date, getBalance());
46     }
47 }
48 
49 void SavingsAccount::settle(const Date& date) {
50     double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1));
51     if (interest != 0) record(date, interest, "interest");
52     acc.reset(date, getBalance());
53 }
54 
55 
56 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee)
57     : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {}
58 
59 void CreditAccount::deposit(const Date& date, double amount, const string& desc) {
60     record(date, amount, desc);
61     acc.change(date, getDebt());
62 }
63 
64 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) {
65     if (amount - getBalance() > credit) {
66         error("not enough credit");
67     }
68     
69     else {
70         record(date, -amount, desc);
71         acc.change(date, getDebt());
72     }
73 }
74 
75 void CreditAccount::settle(const Date& date) {
76     double interest = acc.getSum(date) * rate;
77     if (interest != 0) record(date, interest, "interest");
78     if (date.getMonth() == 1) record(date, -fee, "annual fee");
79     acc.reset(date, getDebt());
80 }
81 
82 void CreditAccount::show() const {
83     Account::show();
84     cout << "\tAvailable credit: " << getAvailableCredit();
85 }
View Code

date.h

 1 #pragma once
 2 
 3 class Date {
 4 private:
 5     int year;
 6     int month;
 7     int day;
 8     int totalDays;
 9 
10 public:
11     Date(int year, int month, int day);
12 
13     int getYear() const { return year; }
14     int getMonth() const { return month; }
15     int getDay() const { return day; }
16     int getMaxDay() const;
17     
18     bool isLeapYear() const {
19         return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
20     }
21     void show() const;
22     
23     int operator-(const Date& date) const {
24         return totalDays - date.totalDays;
25     }
26 };
View Code

date.cpp

 1 #include "date.h"
 2 #include <iostream>
 3 #include <cstdlib>
 4 
 5 using namespace std;
 6 
 7 namespace {
 8     const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
 9 }
10 
11 Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
12     if (day <= 0 || day > getMaxDay()) {
13         cout << "Invalid date: ";
14         show();
15         cout << endl;
16         exit(1);
17     }
18     int years = year - 1;
19     totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
20     if (isLeapYear() && month > 2) totalDays++;
21 }
22 
23 int Date::getMaxDay() const {
24     if (isLeapYear() && month == 2)
25         return 29;
26         
27     else return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
28 }
29 
30 void Date::show() const {
31     cout << getYear() << "-" << getMonth() << "-" << getDay();
32 }
View Code

task6.cpp

 1 #include "account.h"
 2 #include<iostream>
 3 using namespace std;
 4 
 5 int main() {
 6     Date date(2008, 11, 1);
 7 
 8     SavingsAccount sa1(date, "S3755217", 0.015);
 9     SavingsAccount sa2(date, "02342342", 0.015);
10     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
11 
12     Account* accounts[] = { &sa1, &sa2, &ca };
13     const int n = sizeof(accounts) / sizeof(Account*);
14 
15     cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
16     char cmd;
17     do {
18         date.show();
19         cout << "\tTotal:" << Account::getTotal() << "\tcommand>";
20 
21         int index, day;
22         double amount;
23         string desc;
24         cin >> cmd;
25 
26         switch (cmd) {
27         case 'd':
28             cin >> index >> amount;
29             getline(cin, desc);
30             accounts[index]->deposit(date, amount, desc);
31             break;
32 
33         case 'w':
34             cin >> index >> amount;
35             getline(cin, desc);
36             accounts[index]->withdraw(date, amount, desc);
37             break;
38             
39         case 's':
40             for (int i = 0; i < n; i++) {
41                 cout << "[" << i << "]";
42                 accounts[i]->show();
43                 cout << endl;
44             }
45             break;
46 
47         case 'c':
48             cin >> day;
49             if (day < date.getDay()) {
50                 cout << "You cannot specify a previous day";
51             }
52             else if (day > date.getMaxDay())
53                 cout << "Invalid day";
54             else date = Date(date.getYear(), date.getMonth(), day);
55             break;
56 
57         case 'n':
58             if (date.getMonth() == 12)
59                 date = Date(date.getYear() + 1, 1, 1);
60             else date = Date(date.getYear(), date.getMonth() + 1, 1);
61             for (int i = 0; i < n; i++) {
62                 accounts[i]->settle(date);
63             }
64             break;
65         }
66 
67     } while (cmd != 'e');
68     
69     return 0;
70 }
View Code

result6

 改进:

使用了Account数组,运用了多态的特性,使代码复用性更高。且运用了抽象类,使得共同接口得以封装。

task7

play.h

 1 //=======================
 2 //        player.h
 3 //=======================
 4 
 5 // The base class of player
 6 // including the general properties and methods related to a character
 7 
 8 #ifndef _PLAYER
 9 #define _PLAYER
10 
11 #include <iomanip> // use for setting field width
12 #include <time.h>  // use for generating random factor
13 #include "container.h"
14 
15 using std::string;
16 
17 enum job
18 {
19     sw,
20     ar,
21     mg
22 }; /* define 3 jobs by enumerate type
23           sword man, archer, mage */
24 class player
25 {
26     friend void showinfo(player &p1, player &p2);
27     friend class swordsman;
28 
29 protected:
30     int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
31     // General properties of all characters
32     string name;   // character name
33     job role;       /* character's job, one of swordman, archer(射手) and mage(魔法使い),
34                       as defined by the enumerate type */
35     container bag; // character's inventory
36 
37 public:
38     virtual bool attack(player &p) = 0;        // normal attack
39     virtual bool specialatt(player &p) = 0; // special attack
40     virtual void isLevelUp() = 0;            // level up judgement
41     /* Attention!
42     These three methods are called "Pure virtual functions".
43     They have only declaration, but no definition.
44     The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects.
45     The detailed definition of these pure virtual functions will be given in subclasses. */
46 
47     void reFill();              // character's HP and MP resume
48     bool death();              // report whether character is dead
49     void isDead();              // check whether character is dead
50     bool useHeal();              // consume heal, irrelevant to job
51     bool useMW();              // consume magic water, irrelevant to job
52     void transfer(player &p); // possess opponent's items after victory
53     void showRole();          // display character's job
54 
55 private:
56     bool playerdeath; // whether character is dead, doesn't need to be accessed or inherited
57 };
58 
59 #endif

play.cpp

  1 //=======================
  2 //        player.cpp
  3 //=======================
  4 
  5 #include "player.h"
  6 #include <iostream>
  7 
  8 using std::cout;
  9 using std::endl;
 10 using std::setw;
 11 
 12 // character's HP and MP resume
 13 void player::reFill()
 14 {
 15     HP = HPmax; // HP and MP fully recovered
 16     MP = MPmax;
 17 }
 18 
 19 // report whether character is dead
 20 bool player::death()
 21 {
 22     return playerdeath;
 23 }
 24 
 25 // check whether character is dead
 26 void player::isDead()
 27 {
 28     if (HP <= 0) // HP less than 0, character is dead
 29     {
 30         cout << name << " is Dead." << endl;
 31         system("pause");
 32         playerdeath = 1; // give the label of death value 1
 33     }
 34 }
 35 
 36 // consume heal, irrelevant to job
 37 bool player::useHeal()
 38 {
 39     if (bag.nOfHeal() > 0)
 40     {
 41         HP = HP + 100;
 42         if (HP > HPmax) // HP cannot be larger than maximum value
 43             HP = HPmax; // so assign it to HPmax, if necessary
 44         cout << name << " used Heal, HP increased by 100." << endl;
 45         bag.useHeal(); // use heal
 46         system("pause");
 47         return 1; // usage of heal succeed
 48     }
 49     else // If no more heal in bag, cannot use
 50     {
 51         cout << "Sorry, you don't have heal to use." << endl;
 52         system("pause");
 53         return 0; // usage of heal failed
 54     }
 55 }
 56 
 57 // consume magic water, irrelevant to job
 58 bool player::useMW()
 59 {
 60     if (bag.nOfMW() > 0)
 61     {
 62         MP = MP + 100;
 63         if (MP > MPmax)
 64             MP = MPmax;
 65         cout << name << " used Magic Water, MP increased by 100." << endl;
 66         bag.useMW();
 67         system("pause");
 68         return 1; // usage of magic water succeed
 69     }
 70     else
 71     {
 72         cout << "Sorry, you don't have magic water to use." << endl;
 73         system("pause");
 74         return 0; // usage of magic water failed
 75     }
 76 }
 77 
 78 // possess opponent's items after victory
 79 void player::transfer(player &p)
 80 {
 81     cout << name << " got" << p.bag.nOfHeal() << " Heal, and " << p.bag.nOfMW() << " Magic Water." << endl;
 82     system("pause");
 83     // set the character's bag, get opponent's items
 84     HP += p.bag.nOfHeal();
 85     MP += p.bag.nOfMW();
 86 
 87     
 88 }
 89 
 90 // display character's job
 91 void player::showRole()
 92 {
 93     switch (role)
 94     {
 95     case sw:
 96         cout << "Swordsman";
 97         break;
 98     case ar:
 99         cout << "Archer";
100         break;
101     case mg:
102         cout << "Mage";
103         break;
104     default:
105         break;
106     }
107 }
108 
109 // display character's job
110 void showinfo(player &p1, player &p2)
111 {
112     system("cls");
113     cout << "##############################################################" << endl;
114     cout << "# Player" << setw(10) << p1.name << "   LV. " << setw(3) << p1.LV
115          << "  # Opponent" << setw(10) << p2.name << "   LV. " << setw(3) << p2.LV << " #" << endl;
116     cout << "# HP " << setw(3) << (p1.HP <= 999 ? p1.HP : 999) << '/' << setw(3) << (p1.HPmax <= 999 ? p1.HPmax : 999)
117          << " | MP " << setw(3) << (p1.MP <= 999 ? p1.MP : 999) << '/' << setw(3) << (p1.MPmax <= 999 ? p1.MPmax : 999)
118          << "     # HP " << setw(3) << (p2.HP <= 999 ? p2.HP : 999) << '/' << setw(3) << (p2.HPmax <= 999 ? p2.HPmax : 999)
119          << " | MP " << setw(3) << (p2.MP <= 999 ? p2.MP : 999) << '/' << setw(3) << (p2.MPmax <= 999 ? p2.MPmax : 999) << "      #" << endl;
120     cout << "# AP " << setw(3) << (p1.AP <= 999 ? p1.AP : 999)
121          << " | DP " << setw(3) << (p1.DP <= 999 ? p1.DP : 999)
122          << " | speed " << setw(3) << (p1.speed <= 999 ? p1.speed : 999)
123          << " # AP " << setw(3) << (p2.AP <= 999 ? p2.AP : 999)
124          << " | DP " << setw(3) << (p2.DP <= 999 ? p2.DP : 999)
125          << " | speed " << setw(3) << (p2.speed <= 999 ? p2.speed : 999) << "  #" << endl;
126     cout << "# EXP" << setw(7) << p1.EXP << " Job: " << setw(7);
127     p1.showRole();
128     cout << "   # EXP" << setw(7) << p2.EXP << " Job: " << setw(7);
129     p2.showRole();
130     cout << "    #" << endl;
131     cout << "--------------------------------------------------------------" << endl;
132     p1.bag.display();
133     cout << "##############################################################" << endl;
134 }

container.h

 1 //=======================
 2 //        container.h
 3 //=======================
 4 
 5 // The so-called inventory(在庫) of a player in RPG games
 6 // contains two items, heal and magic water
 7 
 8 #ifndef _CONTAINER// Conditional compilation(編集)
 9 #define _CONTAINER
10 
11 class container // Inventory
12 {
13 protected:
14     int numOfHeal; // number of heal
15     int numOfMW;   // number of magic water
16 public:
17     container();                    // constuctor
18     void set(int heal_n, int mw_n); // set the items numbers
19     int nOfHeal();                    // get the number of heal
20     int nOfMW();                    // get the number of magic water
21     void display();                    // display the items;
22     bool useHeal();                    // use heal
23     bool useMW();                    // use magic water
24 };
25 
26 #endif

container.cpp

 1 //=======================
 2 //        container.cpp
 3 //=======================
 4 
 5 // default constructor initialise the inventory as empty
 6 
 7 #include "container.h"
 8 #include <iostream>
 9 
10 using std::cout;
11 using std::endl;
12 
13 container::container()
14 {
15     set(0, 0);
16 }
17 
18 // set the item numbers
19 void container::set(int heal_n, int mw_n)
20 {
21     numOfHeal = heal_n;
22     numOfMW = mw_n;
23 }
24 
25 // get the number of heal
26 int container::nOfHeal()
27 {
28     return numOfHeal;
29 }
30 
31 // get the number of magic water
32 int container::nOfMW()
33 {
34     return numOfMW;
35 }
36 
37 // display the items;
38 void container::display()
39 {
40     cout << "Your bag contains: " << endl;
41     cout << "Heal(HP+100): " << numOfHeal << endl;
42     cout << "Magic Water (MP+80): " << numOfMW << endl;
43 }
44 
45 // use heal
46 bool container::useHeal()
47 {
48     numOfHeal--;
49     return 1; // use heal successfully
50 }
51 
52 // use magic water
53 bool container::useMW()
54 {
55     numOfMW--;
56     return 1; // use magic water successfully
57 }

swordsman.h

 1 //=======================
 2 //        swordsman.h
 3 //=======================
 4 
 5 // Derived from base class player
 6 // For the job Swordsman
 7 
 8 #include "player.h"
 9 class swordsman :public player // subclass swordsman publicly inherited from base player
10 {
11 public:
12     swordsman(int lv_in = 1, string name_in = "Not Given");
13     // constructor with default level of 1 and name of "Not given"
14     void isLevelUp();
15     bool attack(player& p);
16     bool specialatt(player& p);
17     /* These three are derived from the pure virtual functions of base class
18        The definition of them will be given in this subclass. */
19     void AI(player& p); // Computer opponent
20 };

swordsman.cpp

  1 //=======================
  2 //        swordsman.cpp
  3 //=======================
  4 
  5 #include "swordsman.h"
  6 #include <iostream>
  7 
  8 using std::cout;
  9 using std::endl;
 10 
 11 // constructor. default values don't need to be repeated here
 12 swordsman::swordsman(int lv_in, string name_in)
 13 {
 14     role = sw; // enumerate type of job
 15     LV = lv_in;
 16     name = name_in;
 17 
 18     // Initialising the character's properties, based on his level
 19     HPmax = 150 + 8 * (LV - 1); // HP increases 8 point2 per level
 20     HP = HPmax;
 21     MPmax = 75 + 2 * (LV - 1); // MP increases 2 points per level
 22     MP = MPmax;
 23     AP = 25 + 4 * (LV - 1);       // AP increases 4 points per level
 24     DP = 25 + 4 * (LV - 1);       // DP increases 4 points per level
 25     speed = 25 + 2 * (LV - 1); // speed increases 2 points per level
 26 
 27     playerdeath = 0;
 28     EXP = LV * LV * 75;
 29     bag.set(lv_in, lv_in);
 30 }
 31 
 32 void swordsman::isLevelUp()
 33 {
 34     if (EXP >= LV * LV * 75)
 35     {
 36         LV++;
 37         AP += 4;
 38         DP += 4;
 39         HPmax += 8;
 40         MPmax += 2;
 41         speed += 2;
 42         cout << name << " Level UP!" << endl;
 43         cout << "HP improved 8 points to " << HPmax << endl;
 44         cout << "MP improved 2 points to " << MPmax << endl;
 45         cout << "Speed improved 2 points to " << speed << endl;
 46         cout << "AP improved 4 points to " << AP << endl;
 47         cout << "DP improved 5 points to " << DP << endl;
 48         system("pause");
 49         isLevelUp(); // recursively call this function, so the character can level up multiple times if got enough exp
 50     }
 51 }
 52 
 53 bool swordsman::attack(player &p)
 54 {
 55     double HPtemp = 0;             // opponent's HP decrement
 56     double EXPtemp = 0;             // player obtained exp
 57     double hit = 1;                 // attach factor, probably give critical attack
 58     srand((unsigned)time(NULL)); // generating random seed based on system time
 59 
 60     // If speed greater than opponent, you have some possibility to do double attack
 61     if ((speed > p.speed) && (rand() % 100 < (speed - p.speed))) // rand()%100 means generates a number no greater than 100
 62     {
 63         HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10)); // opponent's HP decrement calculated based their AP/DP, and uncertain chance
 64         cout << name << "'s quick strike hit " << p.name << ", " << p.name << "'s HP decreased " << HPtemp << endl;
 65         p.HP = int(p.HP - HPtemp);
 66         EXPtemp = (int)(HPtemp * 1.2);
 67     }
 68 
 69     // If speed smaller than opponent, the opponent has possibility to evade
 70     if ((speed < p.speed) && (rand() % 50 < 1))
 71     {
 72         cout << name << "'s attack has been evaded by " << p.name << endl;
 73         system("pause");
 74         return 1;
 75     }
 76 
 77     // 10% chance give critical attack
 78     if (rand() % 100 <= 10)
 79     {
 80         hit = 1.5;
 81         cout << "Critical attack: ";
 82     }
 83 
 84     // Normal attack
 85     HPtemp = (int)((1.0 * AP / p.DP) * AP * 5 / (rand() % 4 + 10));
 86     cout << name << " uses bash, " << p.name << "'s HP decreases " << HPtemp << endl;
 87     EXPtemp = (int)(EXPtemp + HPtemp * 1.2);
 88     p.HP = (int)(p.HP - HPtemp);
 89     cout << name << " obtained " << EXPtemp << " experience." << endl;
 90     EXP = (int)(EXP + EXPtemp);
 91     system("pause");
 92     return 1; // Attack success
 93 }
 94 
 95 bool swordsman::specialatt(player &p)
 96 {
 97     if (MP < 40)
 98     {
 99         cout << "You don't have enough magic points!" << endl;
100         system("pause");
101         return 0; // Attack failed
102     }
103     else
104     {
105         MP -= 40; // consume 40 MP to do special attack
106 
107         // 10% chance opponent evades
108         if (rand() % 100 <= 10)
109         {
110             cout << name << "'s leap attack has been evaded by " << p.name << endl;
111             system("pause");
112             return 1;
113         }
114 
115         double HPtemp = 0;
116         double EXPtemp = 0;
117         // double hit=1;
118         // srand(time(NULL));
119         HPtemp = (int)(AP * 1.2 + 20); // not related to opponent's DP
120         EXPtemp = (int)(HPtemp * 1.5); // special attack provides more experience
121         cout << name << " uses leap attack, " << p.name << "'s HP decreases " << HPtemp << endl;
122         cout << name << " obtained " << EXPtemp << " experience." << endl;
123         p.HP = (int)(p.HP - HPtemp);
124         EXP = (int)(EXP + EXPtemp);
125         system("pause");
126     }
127     return 1; // special attack succeed
128 }
129 
130 // Computer opponent
131 void swordsman::AI(player &p)
132 {
133     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)))
134     // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
135     {
136         useHeal();
137     }
138     else
139     {
140         if (MP >= 40 && HP > 0.5 * HPmax && rand() % 100 <= 30)
141         // AI has enough MP, it has 30% to make special attack
142         {
143             specialatt(p);
144             p.isDead(); // check whether player is dead
145         }
146         else
147         {
148             if (MP < 40 && HP > 0.5 * HPmax && bag.nOfMW())
149             // Not enough MP && HP is safe && still has magic water
150             {
151                 useMW();
152             }
153             else
154             {
155                 attack(p); // normal attack
156                 p.isDead();
157             }
158         }
159     }
160 }

main.cpp

  1 //=======================
  2 //        main.cpp
  3 //=======================
  4 
  5 // main function for the RPG style game
  6 
  7 #include <iostream>
  8 #include <string>
  9 using namespace std;
 10 
 11 #include "swordsman.h"
 12 
 13 int main()
 14 {
 15     string tempName;
 16     bool success = 0; // flag for storing whether operation is successful
 17 
 18     cout << "Please input player's name: ";
 19     cin >> tempName; // get player's name from keyboard input
 20 
 21     player* human;     // use pointer of base class, convenience for polymorphism
 22     int tempJob;     // temp choice for job selection
 23 
 24     do
 25     {
 26         cout << "Please choose a job: 1 Swordsman, 2 Archer, 3 Mage" << endl;
 27         cin >> tempJob;
 28         system("cls"); // clear the screen
 29 
 30         switch (tempJob)
 31         {
 32         case 1:
 33             human = new swordsman(1, tempName); // create the character with user inputted name and job
 34             success = 1;                        // operation succeed
 35             break;
 36 
 37         default:
 38             break; // In this case, success=0, character creation failed
 39         }
 40     } while (success != 1); // so the loop will ask user to re-create a character
 41 
 42     int tempCom;                      // temp command inputted by user
 43     int nOpp = 0;                      // the Nth opponent
 44     for (int i = 1; nOpp < 5; i += 2) // i is opponent's level
 45     {
 46         nOpp++;
 47         system("cls");
 48 
 49         cout << "STAGE" << nOpp << endl;
 50         cout << "Your opponent, a Level " << i << " Swordsman." << endl;
 51         system("pause");
 52 
 53         swordsman enemy(i, "Warrior"); // Initialise an opponent, level i, name "Junior"
 54         human->reFill();               // get HP/MP refill before start fight
 55 
 56         while (!human->death() && !enemy.death()) // no died
 57         {
 58             success = 0;
 59             while (success != 1)
 60             {
 61                 showinfo(*human, enemy); // show fighter's information
 62                 cout << "Please give command: " << endl;
 63                 cout << "1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game" << endl;
 64                 cin >> tempCom;
 65 
 66                 switch (tempCom)
 67                 {
 68                 case 0:
 69                     cout << "Are you sure to exit? Y/N" << endl;
 70                     char temp;
 71                     cin >> temp;
 72 
 73                     if (temp == 'Y' || temp == 'y')
 74                         return 0;
 75                     else
 76                         break;
 77 
 78                 case 1:
 79                     success = human->attack(enemy);
 80                     human->isLevelUp();
 81                     enemy.isDead();
 82                     break;
 83 
 84                 case 2:
 85                     success = human->specialatt(enemy);
 86                     human->isLevelUp();
 87                     enemy.isDead();
 88                     break;
 89 
 90                 case 3:
 91                     success = human->useHeal();
 92                     break;
 93 
 94                 case 4:
 95                     success = human->useMW();
 96                     break;
 97 
 98                 default:
 99                     break;
100                 }
101             }
102 
103             if (!enemy.death()) // If AI still alive
104                 enemy.AI(*human);
105             else // AI died
106             {
107                 cout << "YOU WIN" << endl;
108                 human->transfer(enemy); // player got all AI's items
109             }
110 
111             if (human->death())
112             {
113                 system("cls");
114                 cout << endl
115                     << setw(50) << "GAME OVER" << endl;
116                 delete human;// player is dead, program is getting to its end, what should we do here?
117                 system("pause");
118                 return 0;
119             }
120         }
121     }
122     delete human;// You win, program is getting to its end, what should we do here?
123     system("cls");
124     cout << "Congratulations! You defeated all opponents!!" << endl;
125     system("pause");
126     return 0;
127 }

resul7

 

posted on 2024-12-04 17:02  Haruto  阅读(30)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3