<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      實驗五

      任務3:

      源碼:

       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 
      17 int main() {
      18     test();
      19 }
      20 
      21 
      22 
      23 
      24 #include<iostream>
      25 
      26 using namespace std;
      27 
      28 class MachinePets {
      29 private:
      30     string nickname;
      31 public:
      32     MachinePets(const string&);
      33     string get_nickname()const;
      34     virtual string talk() = 0;
      35 };
      36 MachinePets::MachinePets(const string& str) :nickname{ str } {}
      37 string MachinePets::get_nickname()const {
      38     return nickname;
      39 }
      40 
      41 
      42 class PetCats :public MachinePets {
      43 public:
      44     PetCats(const string&);
      45     string talk() override;
      46 };
      47 string PetCats::talk() {
      48     return "miao wu";
      49 }
      50 PetCats::PetCats(const string& str):MachinePets(str){}
      51 
      52 class PetDogs :public MachinePets {
      53 public:
      54     PetDogs(const string&);
      55     string talk() override;
      56 };
      57 string PetDogs::talk() {
      58     return "wang wang";
      59 }
      60 PetDogs::PetDogs(const string& str) :MachinePets(str) {}

       

      結果:

       

      任務4:

      源碼:

       1 #include<iostream>
       2 
       3 using namespace std;
       4 
       5 class Film {
       6 private:
       7     string name;
       8     string director;
       9     string states;
      10     int year;
      11 public:
      12     Film() {};
      13     Film(string name, string director, string states, int year);
      14     friend istream& operator>>(istream& in, Film& film);
      15     friend ostream& operator<<(ostream& out, Film& film);
      16     friend bool compare_by_year(Film& f1, Film& f2);
      17 };
      18 Film::Film(string n, string d, string s, int y):
      19     name{n},directorw0obha2h00,states{s},year{y}{}
      20 
      21 istream& operator>>(istream& in, Film& film) {
      22     cout << "錄入片名:";in >> film.name;
      23     cout << "錄入導演:";in >> film.director; 
      24     cout << "錄入制片國家/地區:";in >> film.states; 
      25     cout << "錄入上映年份:";in >> film.year; 
      26     return in;
      27 }
      28 
      29 ostream& operator<<(ostream& out, Film& film) {
      30     out << film.name + "\t"
      31         << film.director + "\t"
      32         << film.states + "\t"
      33         << film.year;
      34     return out;
      35 }
      36 
      37 inline bool compare_by_year(Film& f1, Film& f2) {
      38     return f1.year < f2.year;
      39 }
      40 
      41 
      42 
      43 #include "film.hpp"
      44 #include <iostream>
      45 #include <string>
      46 #include <vector>
      47 #include <algorithm>
      48 
      49 void test() {
      50     using namespace std;
      51 
      52     int n;
      53     cout << "輸入電影數目: ";
      54     cin >> n;
      55 
      56     cout << "錄入" << n << "部影片信息" << endl;
      57     vector<Film> film_lst;
      58     for (int i = 0; i < n; ++i) {
      59         Film f;
      60         cout << string(20, '-') << "" << i + 1 << "部影片錄入" << string(20, '-') << endl;
      61         cin >> f;
      62         film_lst.push_back(f);
      63     }
      64 
      65     // 按發行年份升序排序
      66     sort(film_lst.begin(), film_lst.end(), compare_by_year);
      67 
      68     cout << string(20, '=') + "電影信息(按發行年份)" + string(20, '=') << endl;
      69     for (auto& f : film_lst)
      70         cout << f << endl;
      71 }
      72 
      73 int main() {
      74     test();
      75 }

       

      結果:

       

       

      任務5:

      源碼:

        1 #include<iostream>
        2 #include<cmath>
        3 
        4 using namespace std;
        5 
        6 template<typename T>
        7 class Complex {
        8 private:
        9     T real;
       10     T imag;
       11 public:
       12     Complex();
       13     Complex(T r, T i);
       14     Complex(Complex<T>& c);
       15     T get_real()const;
       16     T get_imag()const;
       17     void operator+=(Complex<T>& c);
       18 
       19     template<typename A>
       20     friend ostream& operator<<(ostream& out, const Complex<A>& c);
       21 
       22     template<typename A>
       23     friend istream& operator>>(istream& in, Complex<A>& c);
       24 
       25     template<typename A>
       26     friend Complex<A> operator+(const Complex<A>& c1, const Complex<A>& c2);
       27 
       28     template<typename A>
       29     friend bool operator==(const Complex<A>& c1, const Complex<A>& c2);
       30 };
       31 
       32 template<typename T>
       33 Complex<T>::Complex(){}
       34 
       35 template<typename T>
       36 Complex<T>::Complex(T r, T i) :real{ r }, imag{ i } {}
       37 
       38 template<typename T>
       39 Complex<T>::Complex(Complex& c) {
       40     real = c.get_real();
       41     imag = c.get_imag();
       42 }
       43 
       44 template<typename T>
       45 void Complex<T>::operator+=(Complex<T>& c) {
       46     real += c.get_real();
       47     imag += c.get_imag();
       48 }
       49 
       50 template<typename T>
       51 T Complex<T>::get_real() const {
       52     return real;
       53 }
       54 
       55 template<typename T>
       56 T Complex<T>::get_imag() const {
       57     return imag;
       58 }
       59 
       60 template<typename T>
       61 ostream& operator<<(ostream& out, const Complex<T>& c) {
       62     if (c.imag > 0) {
       63         out << c.real << '+' << c.imag << 'i';
       64     }
       65     else if (c.imag < 0) {
       66         out << c.real << c.imag << 'i';
       67     }
       68     else {
       69         out << c.real;
       70     }
       71     return out;
       72 }
       73 
       74 template<typename T>
       75 istream& operator>>(istream& in, Complex<T>& c) {
       76     in >> c.real >> c.imag;
       77     return in;
       78 }
       79 
       80 template<typename T>
       81 Complex<T> operator+(const Complex<T>& c1, const Complex<T>& c2) {
       82     Complex<T> t(c1.real + c2.real, c1.imag + c2.imag);
       83     return t;
       84 }
       85 
       86 template<typename T>
       87 bool operator==(const Complex<T>& c1, const Complex<T>& c2) {
       88     return (c1.real == c2.real && c1.imag == c2.imag);
       89 }
       90 
       91 
       92 
       93 #include "Complex.hpp"
       94 #include <iostream>
       95 
       96 using std::cin;
       97 using std::cout;
       98 using std::endl;
       99 using std::boolalpha;
      100 
      101 void test1() {
      102     Complex<int> c1(2, -5), c2(c1);
      103 
      104     cout << "c1 = " << c1 << endl;
      105     cout << "c2 = " << c2 << endl;
      106     cout << "c1 + c2 = " << c1 + c2 << endl;
      107 
      108     c1 += c2;
      109     cout << "c1 = " << c1 << endl;
      110     cout << boolalpha << (c1 == c2) << endl;
      111 }
      112 
      113 void test2() {
      114     Complex<double> c1, c2;
      115     cout << "Enter c1 and c2: ";
      116     cin >> c1 >> c2;
      117     cout << "c1 = " << c1 << endl;
      118     cout << "c2 = " << c2 << endl;
      119 
      120     cout << "c1.real = " << c1.get_real() << endl;
      121     cout << "c1.imag = " << c1.get_imag() << endl;
      122 }
      123 
      124 int main() {
      125     cout << "自定義類模板Complex測試1: " << endl;
      126     test1();
      127 
      128     cout << endl;
      129 
      130     cout << "自定義類模板Complex測試2: " << endl;
      131     test2();
      132 }

       

      結果:

       

      任務6:

      源碼:

       1 #pragma once
       2 class Date {
       3 private:
       4     int year;
       5     int month;
       6     int day;
       7     int totalDays;
       8 public:
       9     Date(int year, int month, int day);
      10     int getYear()const { return year; }
      11     int getMonth()const { return month; }
      12     int getDay()const { return day; }
      13     int getMaxDay()const;
      14     bool isLeapYear()const {
      15         return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
      16     }
      17     void show() const;
      18     int operator-(const Date& date)const {
      19         return totalDays - date.totalDays;
      20     }
      21 };
       1 #pragma once
       2 #include"date.h"
       3 #include"accumulator.h"
       4 #include<string>
       5 using namespace std;
       6 class Account {
       7 private:
       8     string id;
       9     double balance;
      10     static double total;
      11 protected:
      12     Account(const Date& date, const string& id);
      13     void record(const Date& date, double amount, const string& desc);
      14     void error(const string& msg) const;
      15 public:const string& getId() { return id; }
      16       double getBalance()const { return balance; }
      17       static double getTotal() { return total; }
      18       virtual void deposit(const Date& date, double amount, const string& desc) = 0;
      19       virtual void withdraw(const Date& date, double amount, const string& desc) = 0;
      20       virtual void settle(const Date& date) = 0;
      21       virtual void show()const;
      22 };
      23 class SavingsAccount :public Account {
      24 private:
      25     Accumulator acc;
      26     double rate;
      27 public:
      28     SavingsAccount(const Date& date, const string& id, double rate);
      29     double getRate() const { return  rate; }
      30     void deposit(const Date& date, double amount, const string& desc);
      31     void withdraw(const Date& date, double amount, const string& desc);
      32     void settle(const Date& date);
      33 };
      34 class CreditAccount :public Account {
      35 private:
      36     Accumulator acc;
      37     double credit;
      38     double rate;
      39     double fee;
      40     double getDebt()const {
      41         double balance = getBalance();
      42         return(balance < 0 ? balance : 0);
      43     }
      44 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee);
      45       double getCredit()const { return credit; }
      46       double getRate()const { return rate; }
      47       double getFee() const { return fee; }
      48       double getAvailableCredit()const {
      49           if (getBalance() < 0)return credit + getBalance();
      50           else return credit;
      51       }
      52       void deposit(const Date& date, double amount, const string& desc);
      53       void withdraw(const Date& date, double amount, const string& desc);
      54       void settle(const Date& date);
      55       void show()const;
      56 };
       1 #pragma once
       2 #include "date.h"
       3 class Accumulator {
       4 private:
       5     Date lastDate;
       6     double value;
       7     double sum;
       8 public:
       9     Accumulator(const Date& date, double value) :lastDate(date), value(value), sum{ 0 } {}
      10     double getSum(const Date& date) const {
      11         return sum + value * (date - lastDate);
      12     }
      13     void change(const Date& date, double value) {
      14         sum = getSum(date);
      15         lastDate = date;
      16         this->value = value;
      17     }
      18     void reset(const Date& date, double value) {
      19         lastDate = date;
      20         this->value = value;
      21         sum = 0;
      22     }
      23 };
       1 #include"date.h"
       2 #include<iostream>
       3 #include<cstdlib>
       4 using namespace std;
       5 namespace {
       6     const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 };
       7 }
       8 Date::Date(int year, int month, int day) :year(year), month(month), day(day) {
       9     if (day <= 0 || day > getMaxDay()) {
      10         cout << "Invalid date: ";
      11         show();
      12         cout << endl;
      13         exit(1);
      14     }
      15     int years = year - 1;
      16     totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day;
      17     if (isLeapYear() && month > 2) totalDays++;
      18 }
      19 int Date::getMaxDay()const {
      20     if (isLeapYear() && month == 2)
      21         return 29;
      22     else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1];
      23 }
      24 void Date::show()const {
      25     cout << getYear() << "-" << getMonth() << "-" << getDay();
      26 }
       1 #include "account.h"
       2 #include <cmath>
       3 #include<iostream>
       4 using namespace std;
       5 double Account::total = 0;
       6 Account::Account(const Date& date, const string& id) :id(id), balance(0) {
       7     date.show();
       8     cout << "\t#" << id << "created" << endl;
       9 }
      10 void Account::record(const Date& date, double amount, const string& desc) {
      11     amount = floor(amount * 100 + 0.5) / 100;
      12     balance += amount;
      13     total += amount;
      14     date.show();
      15     cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
      16 }
      17 void Account::show()const { cout << id << "\tBalance:" << balance; }
      18 void Account::error(const string& msg)const {
      19     cout << "Error(#" << id << "):" << msg << endl;
      20 }
      21 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {}
      22 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) {
      23     record(date, amount, desc);
      24     acc.change(date, getBalance());
      25 }
      26 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) {
      27     if (amount > getBalance()) {
      28         error("not enough money");
      29     }
      30     else {
      31         record(date, -amount, desc);
      32         acc.change(date, getBalance());
      33     }
      34 }
      35 void SavingsAccount::settle(const Date& date) {
      36     if (date.getMonth() == 1) {
      37         double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1));
      38         if (interest != 0) record(date, interest, "interest");
      39         acc.reset(date, getBalance());
      40     }
      41 }
      42 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) :Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {}
      43 void CreditAccount::deposit(const Date& date, double amount, const string& desc) {
      44     record(date, amount, desc);
      45     acc.change(date, getDebt());
      46 }
      47 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) {
      48     if (amount - getBalance() > credit) {
      49         error("not enouogh credit");
      50     }
      51     else {
      52         record(date, -amount, desc);
      53         acc.change(date, getDebt());
      54     }
      55 }
      56 void CreditAccount::settle(const Date& date) {
      57     double interest = acc.getSum(date) * rate;
      58     if (interest != 0) record(date, interest, "interest");
      59     if (date.getMonth() == 1)record(date, -fee, "annual fee");
      60     acc.reset(date, getDebt());
      61 }
      62 void CreditAccount::show()const {
      63     Account::show();
      64     cout << "\tAvailable credit:" << getAvailableCredit();
      65 }
       1 #include"account.h"
       2 #include<iostream>
       3 using namespace std;
       4 int main() {
       5     Date date(2008, 11, 1);
       6     SavingsAccount sa1(date, "S3755217", 0.015);
       7     SavingsAccount sa2(date, "02342342", 0.015);
       8     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
       9     Account* accounts[] = { &sa1,&sa2,&ca };
      10     const int n = sizeof(accounts) / sizeof(Account*);
      11     cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
      12     char cmd;
      13     do {
      14         date.show();
      15         cout << "\tTotal:" << Account::getTotal() << "\tcommand>";
      16         int index, day;
      17         double amount;
      18         string desc;
      19         cin >> cmd;
      20         switch (cmd) {
      21         case 'd':
      22             cin >> index >> amount;
      23             getline(cin, desc);
      24             accounts[index]->deposit(date, amount, desc);
      25             break;
      26 
      27         case 'w':
      28             cin >> index >> amount;
      29             getline(cin, desc);
      30             accounts[index]->withdraw(date, amount, desc);
      31             break;
      32         case 's':
      33             for (int i = 0; i < n; i++) {
      34                 cout << "[" << i << "]";
      35                 accounts[i]->show();
      36                 cout << endl;
      37             }
      38             break;
      39 
      40         case 'c':
      41             cin >> day;
      42             if (day < date.getDay()) {
      43                 cout << "You cannot specify a previous day";
      44             }
      45             else if (day > date.getMaxDay())
      46                 cout << "Invalid day";
      47             else date = Date(date.getYear(), date.getMonth(), day);
      48             break;
      49         case 'n':
      50             if (date.getMonth() == 12)
      51                 date = Date(date.getYear() + 1, 1, 1);
      52             else date = Date(date.getYear(), date.getMonth() + 1, 1);
      53             for (int i = 0; i < n; i++) {
      54                 accounts[i]->settle(date);
      55             }
      56             break;
      57         }
      58     } while (cmd != 'e');
      59     return 0;
      60 }

      結果:

       

      任務7:

      player.h

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

      swardsman.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 };

      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
       9 // Conditional compilation
      10 #define _CONTAINER
      11 
      12 class container // Inventory
      13 {
      14 protected:
      15     int numOfHeal; // number of heal
      16     int numOfMW;   // number of magic water
      17 public:
      18     container();                    // constuctor
      19     void set(int heal_n, int mw_n); // set the items numbers
      20     int nOfHeal();                    // get the number of heal
      21     int nOfMW();                    // get the number of magic water
      22     void display();                    // display the items;
      23     bool useHeal();                    // use heal
      24     bool useMW();                    // use magic water
      25 };
      26 
      27 #endif

      player.cpp

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

      swardsman.cpp

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

      container.cpp

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

      main.cpp

       

      //=======================
      //        main.cpp
      //=======================
      
      // main function for the RPG style game
      
      #include <iostream>
      #include <string>
      using namespace std;
      
      #include "swordsman.h"
      #include"player.h"
      
      int main()
      {
          string tempName;
          bool success = 0; // flag for storing whether operation is successful
          cout << "Please input player's name: ";
          cin >> tempName; // get player's name from keyboard input
          player* human = NULL;     // use pointer of base class, convenience for polymorphism
          int tempJob;     // temp choice for job selection
          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;
      }

       

      posted @ 2024-12-08 21:42  阿瓜不瓜  閱讀(21)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 精品素人AV无码不卡在线观看| 亚洲无?码A片在线观看| 午夜射精日本三级| 女人裸体性做爰视频| 国产成人夜色高潮福利app| 国产av中文字幕精品| 久久久天堂国产精品女人| 日韩少妇人妻vs中文字幕| 亚洲AV成人片不卡无码| 无码里番纯肉h在线网站| 精品嫩模福利一区二区蜜臀| 国产精品中文字幕自拍| 秦安县| 人人妻人人妻人人片色av| 亚洲中文字幕无码一区无广告| 兰考县| 天天摸夜夜摸夜夜狠狠添| 俺来也俺去啦最新在线| 午夜爽爽爽男女免费观看影院| 乱色老熟妇一区二区三区| 国产在线精品一区二区三区| 亚洲人成电影在线播放| 中文字幕无码不卡免费视频| 一面膜上边一面膜下边视频| 国产精品中文字幕一区| 新安县| 国产免费无遮挡吸奶头视频| 中文字幕国产精品日韩| 亚洲av永久无码精品天堂久久| 欧美福利电影A在线播放| 青草内射中出高潮| 国产精品入口麻豆| 丝袜美腿亚洲综合在线观看视频| 中文字幕久久人妻熟人妻| 亚洲乱理伦片在线观看中字| 四虎影视一区二区精品| 欧美亚洲精品中文字幕乱码| 92国产精品午夜福利免费| 精品国产成人亚洲午夜福利| 日日橹狠狠爱欧美视频| 国产一区二区高清不卡|