首页 > 其他分享 >实验五

实验五

时间:2022-11-28 22:13:46浏览次数:41  
标签:string success Person 实验 human include cout

#include <iostream>
#include "pets.hpp"

void play(MachinePets &obj){
    std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
}

void test() {
    PetCats cat("miku");
    PetDogs dog("da huang");

    play(cat);
    play(dog);
}

int main() {
    test();
}
#pragma once
#include<iostream>
using namespace std;

class MachinePets {
public:
    MachinePets(const string s):nickname(s){}
    virtual string talk() { return voice; }
    virtual string get_nickname() { return nickname; }
protected:
    string nickname;
    string voice;
};

//宠物猫
class PetCats :public MachinePets {
public:
    PetCats(const string s) :MachinePets(s) {  voice="miao~"; }
    string talk() { return voice; };
    string get_nickname() { return nickname; }
    
};

//宠物狗
class PetDogs :public MachinePets {
public:
    PetDogs(const string s) :MachinePets(s) { voice = "ao wu~"; }
    string talk() { return voice; }
    string get_nickname() { return nickname; }
    
};

 

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class Person {
public:
    Person(){}
    Person(string name0,string telephone0,string email0) :name(name0), telephone(telephone0),email(email0) {}
    Person(const Person& p1);
    void update_telephone();
    void update_email();

    friend std::ostream& operator << (std::ostream& out, const Person& c);
    friend std::istream& operator >> (std::istream& in, Person& c);
    friend bool operator==(const Person& c1, const Person& c2);
private:
    string name;
    string telephone;
    string email;
};
Person::Person(const Person& pl) {
    name = pl.name;
    telephone = pl.telephone;
    email = pl.email;
}
void Person::update_telephone() {
    cout << "Enter the telephone number:";
    cin.clear();
    cin >> telephone;
    cout << "telephone number has been updated..." << endl;
}
void Person::update_email() {
    cout << "Enter the email address:";
    cin.clear();
    cin >> email;
    cout << "email address has been updated..." << endl;
}
bool operator==(const Person& c1,const Person &c2) {
    return(c1.name == c2.name && c1.telephone == c2.telephone && c1.email == c2.email);
}
std::ostream& operator << (std::ostream& out, const Person& c) {
    out << left << setw(15) << c.name << setw(15) << c.telephone << setw(15) << c.email;
    return out;
}
std::istream& operator >> (std::istream& in, Person& c) {
    getline(in, c.name);
    getline(in,c.telephone);
    getline(in, c.email);
    cout << endl;
    return in;
}
#include <iostream>
#include <fstream>
#include <vector>
#include "Person.hpp"

void test() {
    using namespace std;

    vector<Person> phone_book;
    Person p;

    cout << "Enter person's contact until press Ctrl + Z" << endl;
    while (cin >> p)
        phone_book.push_back(p);

    cout << "\nupdate someone's contact: \n";
    phone_book.at(0).update_telephone();
    phone_book.at(0).update_email();

    cout << "\ndisplay all contacts' info\n";
    for (auto& phone : phone_book)
        cout << phone << endl;

    cout << "\ntest whether the same contact\n";
    cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;
}

int main() {
    test();
}

 

//=======================
//        main.cpp
//=======================

// main function for the RPG style game

#include <iostream>
#include <string>
using namespace std;

#include "swordsman.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;        // 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;
}

 

 

 

标签:string,success,Person,实验,human,include,cout
From: https://www.cnblogs.com/hjr123/p/16933468.html

相关文章

  • 实验5
    task4main.cpp#include<iostream>#include"pets.hpp"voidplay(MachinePets&obj){std::cout<<obj.get_nickname()<<"says"<<obj.talk()<<std::en......
  • 实验五
    实验任务四#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings):nickname(s){}......
  • 实验五:全连接神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架,......
  • PTA 21级数据结构与算法实验8—排序
    目录7-1统计工龄7-2寻找大富翁7-3点赞狂魔7-4插入排序还是归并排序7-5插入排序还是堆排序7-6逆序对7-7堆排序7-8石子合并7-9第k小7-10快速排序的过程7-1统计工......
  • 实验五:全连接神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】使用pytorch框架,设......
  • 实验五:全连接神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架,......
  • 实验五 继承和多态
    pets.hpp源码#include<iostream>#include<string.h>usingnamespacestd;classMachinePets{private:stringnickname;public:MachinePets(co......
  • 实验4
    1 2 3 45 6 7  ......
  • 全连接神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架,......
  • Linux实验2:CENTOS7下的用户管理和权限设置
    一、实验目的掌握创建用户和用户组的方法;掌握文件的权限设置方法。二、实验任务1.新增加一个组名为student,密码为123。2.新增加一个用户名为zhouxingchi,其附属组......