其他分享
首页 > 其他分享> > 实验五

实验五

作者:互联网

#ifndef PERSON_HPP
#define PERSON_HPP
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;

class Person {
private:string name, telephone, email;
public:Person() {}
      Person(string name, string telephone, string email);
      void set_telephone(string telephone);
      void set_email(string email);
      friend ostream& operator<< (ostream& out, Person& p);
      friend istream& operator>> (istream& in, Person& p);
      friend bool operator==(Person& p1, Person& p2);
};

Person::Person(string name, string telephone, string email)
{
    this->name = name;
    this->telephone = telephone;
    this->email = email;
}

void Person::set_telephone(string telephone)
{
    this->telephone = telephone;
}

void Person::set_email(string email)
{
    this->email = email;
}

ostream& operator<<(ostream& out, Person& p)  
{
    out <<setiosflags(ios_base::left )
        <<setw(15) << p.name
        << setiosflags(ios_base::left)
        << setw(15) << p.telephone
        << setiosflags(ios_base::left)
        << setw(15) << p.email;

    return out;
}

istream& operator>>(istream& in, Person& p)
{
    getline(cin, p.name );
    getline(cin, p.telephone );
    getline(cin, p.email );
    return in;
}

bool operator==(Person& p1, Person& p2)
{
    if (p1.name == p2.name)
    {
        if (p1.telephone == p2.telephone)
        {
            if (p1.email == p2.email)
                return true;
            else return false;
        }
        else return false;
    }
    else return false;
}



#endif
#include <iostream>
#include <fstream>
#include <vector>
#include "Person.hpp"

int main()
{
    using namespace std;

    vector<Person> phone_book;
    Person p;

    while (cin >> p)
        phone_book.push_back(p);

    for (auto& i : phone_book)
        cout << i << endl;

    cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;

    ofstream fout;

    fout.open("phone_book.txt");

    if (!fout.is_open())
    {
        cerr << "fail to open file phone_book.txt\n";
        return 1;
    }

    for (auto& i : phone_book)
        fout << i << endl;

    fout.close();
    
}

 

 

task3

 

 

 

 

 

 

 

 

 

 

 

标签:name,telephone,Person,实验,include,email,string
来源: https://www.cnblogs.com/qf20020802/p/15689507.html