编写一个通信录

题目要求:
编写一个关于通讯录的程序
1. 可输入姓名、电话(可多个)、通讯地址、Email等必要的信息;
2. 可根据姓名查询电话等信息(最好还能根据电话查询姓名),并显示在屏幕上;
3. 要求通讯录保存成文件,如张三.txt,用文件流的方式实现;
4. 最好用面向对象方法编写。

开发环境:
QT Creator
但是建立的是纯C++工程

//person.h 人 类
#ifndef PERSON_H
#define PERSON_H
#include <vector>
#include <string>
#include <iostream>
#include <map>

using namespace std;

class Person
{
public:
    void setPerson(string name,vector<string> phone,string address,string email);
    string          getName();
    vector<string>  getPhone();
    string          getAddress();
    string          getEmail();
private:
    string          m_name;
    vector<string>  m_phone;
    string          m_address;
    string          m_email;
};

#endif // PERSON_H
//person.cpp
#include "person.h"

void Person::setPerson(string name,vector<string> phone,string address,string email)
{
    m_name = name;
    m_phone = phone;
    m_address = address;
    m_email = email;
}
string Person::getName()
{
    return m_name;
}
vector<string> Person::getPhone()
{
    return m_phone;
}
string Person::getAddress()
{
    return m_address;
}
string Person::getEmail()
{
    return m_email;
}
//direct.h 通信录类头文件
#ifndef DIRECT_H
#define DIRECT_H

#include "person.h"

class Direct
{
public:
    int menu();//显示通信录功能菜单
    void addperson(Person* person);//添加联系人
    void browse();//浏览通信录
    void findperson();//查找联系人
    void deleteperson();//删除联系人
};

#endif // DIRECT_H
//direct.cpp
#include "direct.h"
#include <stdlib.h>
#include <fstream>
#include <iostream>

using namespace std;

//显示通信录功能菜单
int Direct::menu()
{
    cout<<"***************************************************************"<<endl;
    cout<<"*********************** 功能菜单:*****************************"<<endl;
    cout<<"********************* 1.新建联系人 ****************************"<<endl;
    cout<<"********************* 2.删除联系人 ****************************"<<endl;
    cout<<"********************* 3.查找联系人 ****************************"<<endl;
    cout<<"********************* 4.浏览通信录 ****************************"<<endl;
    cout<<"********************* 5.退出通信录 ****************************"<<endl;
    cout<<"***************************************************************"<<endl;

    cout<<"******************* 请选择您要进行的操作:*********************"<<endl;
    int order = 0;
    cin>>order;
    return order;
}

//添加联系人
void Direct::addperson(Person* person)
{
    person = new Person();
    string name,number,address,email;
    vector<string> phone;
    cout<<"请输入姓名:";
    cin>>name;
    cout<<"您要存几条电话号码: ";
    int count;
    cin>>count;
    for(int i=1;i<=count;i++)
    {
        cout<<"请输入电话号码"<<i<<": ";
        cin>>number;
        phone.push_back(number);
    }
    cout<<"请输入地址:";
    cin>>address;
    cout<<"请输入邮箱:";
    cin>>email;
    person->setPerson(name,phone,address,email);

    ofstream outfile;
    outfile.open("通信录.txt",ios::app);
    if(!outfile)
    {
        cout<<"通信录.txt can't open. "<<endl;
        abort();
    }
    else
    {
        outfile<<"姓名: "<<person->getName()<<" ";
        for(int i=0;i<person->getPhone().size();i++)
        {
            outfile<<"电话号码"<<i+1<<": "<<person->getPhone().at(i)<<" ";
        }
        outfile<<"地址: "<<person->getAddress()<<" ";
        outfile<<"Email: "<<person->getEmail()<<endl;

        outfile.close();
    }
}
//浏览通信录
void Direct::browse()
{
    ifstream infile;
    infile.open("通信录.txt");
    if(!infile)
    {
        cout<<"通信录.txt can't open. "<<endl;
        abort();
    }
    else
    {
        char people[100];
        while (!infile.eof())
        {
            infile.getline(people,100);
            cout<<people<<endl;
        }
    }
    infile.close();
}
//查找联系人
void Direct::findperson()
{
    ifstream infile;
    infile.open("通信录.txt");
    if(!infile)
    {
    cout<<"通信录.txt can't open. "<<endl;
    abort();
    }
    else
    {
        cout<<"请输入要查找人的信息(姓名、电话、住址或者邮箱): "<<endl;
        string str_find;
        cin>>str_find;
        char people[100];
        int i=0;
        while(!infile.eof())
        {
             infile.getline(people,100);
             string str_people = people;
             size_t n = str_people.find(str_find,0);
             int N = (int)n;
             if(N>=0)
             {
                 cout<<str_people<<endl;
                 ++i;
             }
        }
        if(0==i)
        {
            cout<<"查无此人......"<<endl;
        }
    }
}
//删除联系人
void Direct::deleteperson()
{
    ifstream infile;
    infile.open("通信录.txt");
    if(!infile)
    {
    cout<<"通信录.txt can't open. "<<endl;
    abort();
    }
    else
    {
        cout<<"请输入要删除人的信息(姓名、电话、住址或者邮箱): "<<endl;
        string str_find;
        cin>>str_find;
        char people[100];
        int i=0;
        vector<string> str_people2;
        vector<string> str_people3;
        string s = "N";
        while(!infile.eof())
        {
             infile.getline(people,100);
             str_people3.push_back(people);
             string str_people1 = people;
             size_t n = str_people1.find(str_find,0);
             int N = (int)n;
             if(N>=0)
             {
                 cout<<"是否要删除此人信息?Y/N"<<endl;
                 cout<<str_people1<<endl;
                 cin>>s;
                 ++i;
             }
             else if(N==-1&&people!="\n")
             {
                 if(people!="0")
                 {
                 str_people2.push_back(people);
                 }
             }
            }

        ofstream ofile("通信录.txt",ios::trunc);
        string str;
        if(s == "Y")
        {
            ofstream outfile;
            outfile.open("通信录.txt",ios::app);
            for(int i=0;i<str_people2.size()-1;i++)//此处size-1是由于getline()会多读一行空行,使容器内多压入一行空行
            {
                str = str_people2.at(i);
                outfile<<str<<endl;
            }
            str_people2.clear();
            outfile.close();
        }
        else if(s == "N")
        {
            ofstream outfile;
            outfile.open("通信录.txt",ios::app);
            for(int i=0;i<str_people3.size()-1;i++)//此处size-1是由于getline()会多读一行空行,使容器内多压入一行空行
            {
                str = str_people3.at(i);
                outfile<<str<<endl;
            }
            str_people3.clear();
            outfile.close();
        }
        else
        {
            cout<<"输入有误......"<<endl;
        }
        ofile.close();
    }
}
//main.cpp
#include "direct.h"


int main()
{
    Direct* direct = new Direct();
    Person* person;

    int userOrder = 0;

    while(userOrder!=5)
    {
        userOrder = direct->menu();
        switch(userOrder)
        {
            case 1:
                cout<<"********************** 新建联系人 *****************************"<<endl;
                direct->addperson(person);
            break;
            case 2:
                cout<<"********************** 删除联系人 *****************************"<<endl;
                direct->deleteperson();
            break;
            case 3:
                cout<<"********************** 查找联系人 *****************************"<<endl;
                direct->findperson();
            break;
            case 4:
                cout<<"********************** 浏览通信录 *****************************"<<endl;
                direct->browse();
            break;
            case 5:
                cout<<"********************** 退出通信录 *****************************"<<endl;
            break;
            default:
            break;
        }
    }
    return 0;
}

运行结果

猜你喜欢

转载自blog.csdn.net/fan_xingwang/article/details/77047792
今日推荐