用C++编写的通讯录

来源:互联网 发布:阿里云 http服务器 编辑:程序博客网 时间:2024/06/07 12:10

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;
class book
{
public:
    book();//默认构造函数
    char inter_face();//首页
    void add_person();//添加联系人
    void del_person();//删除联系人
    void show_all();//显示所有联系人
    void alter();//修改信息
    void select();//查询联系人
    void save_new();//保存新增加的联系人
private:
    string name;//姓名
    string address;//地址
    string number;//电话号码
    string post;//邮编
    string qq;//QQ号
};

struct record
{
    book object;//双向链表中的数据域是是一个记录类
    record *prior;//指向前躯
    record *next;//指向后继
};

book::book()
{
    name = "/0";
    address = "/0";
    number = "/0";
    post = "/0";
    qq = "/0";
}

//首页

char book::inter_face()
{
    system("cls");//???清屏
    cout <<endl;
    cout<<"/t/t★★★★★★★★★★★★★★★★★★★★★★★★★★" <<endl
        <<"/t/t※                                                ※" <<endl
        <<"/t/t※             小 小 通 迅   录                ※" <<endl
        <<"/t/t※                                                ※" <<endl
        <<"/t/t※      1. 添加新联系人.      4. 修改信息.        ※" <<endl
        <<"/t/t※                                                ※" <<endl
        <<"/t/t※      2. 删除联系人.        5. 查询联系人.      ※" <<endl
        <<"/t/t※                                                ※" <<endl
        <<"/t/t※      3. 显示所有联系人.    6. 关闭通迅录.      ※" <<endl
        <<"/t/t※                                                ※" <<endl
        <<"/t/t★★★★★★★★★★★★★★★★★★★★★★★★★★" <<endl <<endl
        <<"/t/t/t选择 :" ;
 

    char choose;
    cin>>choose;
    fflush(stdin);//清除输入流的缓冲区,使它仍然打开,并把输出流的缓冲区的内容写入它所联系的文件中。成功时返回0,出错时返回EOF
   return choose;
}

//添加联系人

void book::add_person()
{
    cout <<endl <<"根据下面提示输入新联系人信息" <<endl <<endl
        <<"姓名 :" ;
    cin >>name;
    fflush(stdin);
    cout <<"电话 : ";
    cin >>number;
    fflush(stdin);
    cout <<"QQ   : ";
    cin >>qq;
    fflush(stdin);
    cout <<"邮编 : ";
    cin >>post;
    fflush(stdin);

    cout <<"地址 : ";
    cin >>address;
    fflush(stdin);
    save_new();
  

    cout <<endl <<endl <<"新联系人信息已经保存好!!!!!" <<endl <<endl;
    system("pause");//直观的讲就是显示:”按任意键继续。。。“

}


//删除联系人

void book::del_person()
{
    ofstream outData("temp.txt", ios::out);//?????ifstream只不过是从文件里读取数据,并输入到内存的某些变量中而已

    ifstream inData("pbook.txt", ios::in); //???????ifstream不就是input file stream(输入文件流),ofstream也不就是output file stream(输出文件流)吗?

    if (!outData || !inData)

    {

        cout <<endl <<"对不起!!!!找不到文件!!!!" <<endl;

        system("pause");

        return;

    }

  

    string sign;

    cout <<endl <<"你要删除输入姓名或电话号 :";

    cin >>sign;

    string str1;

    bool flag = true;

    string str;

  

    while (inData >>name >>number)

    {

        getline(inData, str);

      

        if ((sign==name) || (sign==number))

        {

            cout <<endl <<"你想删除的联系人:" <<endl <<endl;

            cout <<str1 <<endl;

            cout <<setiosflags(ios::left) <<setw(17) <<name

                <<" " <<number <<str <<endl;

            flag = false;

            break;

        }

      

        outData <<setiosflags(ios::left) <<setw(17) <<name

            <<" " <<number <<str <<endl;

    }

  

    if (flag)

    {

        cout <<endl <<"对不起!!!联系人中没你找的人!!!!" <<endl <<endl;

    }

    else

    {

        while (getline(inData, str))

        {

            outData <<str <<endl;

        }

      

        outData.close();//??关闭

        inData.close();//??关闭

      

        ofstream out("pbook.txt", ios::out);//??

        ifstream in("temp.txt", ios::in);//???????

      

        if (!out || !in)

        {

            cout <<endl <<"对不起不能打开文件!!!" <<endl <<endl;

            system("pause");

            return;

        }

      

        while (getline(in, str))

        {

            out <<str <<endl;

        }

      

        out.close();//

         in.close();

        cout <<endl <<"这个人的信息已经从你的通迅录中删除!!!" <<endl <<endl;

    }

  

    system("pause");  

}


//显示所有联系人

void book::show_all()

{

    ifstream inData("pbook.txt",ios::in);

  

  

    if (!inData)

    {

        cout <<endl <<"对不起!!!!没有找到文件!!!!!" <<endl;

        system("pause");

        return;

    }

  

    bool flag = true;

    string record;

  

    while (getline(inData, record))

    {

        if (flag)

        {

            cout <<endl <<"所有联系人信息如下: "<<endl;

        }

        cout <<record <<endl;

        flag = false;

    }

  

    if (flag)

    {

        cout <<endl <<"你的通迅录中没有联系人!!!!!" <<endl <<endl;

    }

    else

    {

        cout <<endl <<"所有联系人已经全部显示出来!!!!!" <<endl <<endl;

    }

  

    system("pause");

}

//修改信息

void book::alter()

{

    ifstream inData("pbook.txt", ios::in);

  

    if (!inData)

    {

        cout <<endl <<"对不起不能打开文件!!!!!" <<endl <<endl;

        system("pause");

        return;

    }

  

    string sign;

    cout <<endl <<"请输入你想要修改的联系人的姓名或电话号码 :";

    cin >>sign;

    fflush(stdin);

  

    string str1;

    getline(inData, str1);

  

    record *H = new record;

    record *p = H;

    record *q = H;

    bool flag = true;

  

    while (inData >>p->object.name >>p->object.number >>p->object.qq

        >>p->object.post >>p->object.address)

    {

        p->next = new record;

        p = p->next;

        p->prior = q;

        q->next = p;

        q = p;

    }

    p->next = NULL;//

inData.close();

  

    p = H;

    while (p)

    {

        if ((p->object.name==sign) || (p->object.number==sign))

        {

            if (flag)

            {

                cout <<endl <<"你想修改你联系人的信息如下: " <<endl <<endl;

                cout <<endl <<str1 <<endl;

                cout <<setiosflags(ios::left) <<setw(17)

                    <<p->object.name <<" " <<setw(16) <<p->object.number

                    <<" " <<setw(18) <<p->object.qq

                    <<" " <<setw(14) <<p->object.post

                    <<" " <<setw(20) <<p->object.address <<endl;

                flag = false;

            }

            cout <<endl <<"请根据下面提示修改信息 : " <<endl;

            cout <<"姓名 :" ;

            cin >>p->object.name;

            fflush(stdin);

            cout <<"电话号:";

            cin >>p->object.number;

            fflush(stdin);

            cout <<"QQ    :";

            cin >>p->object.qq;

            fflush(stdin);

            cout <<"邮编 :";

            cin >>p->object.post;

            fflush(stdin);

            cout <<"地址 :";

            cin >>p->object.address;

            fflush(stdin);

            break;

        }

      

        p = p->next;

    }

  

    if (flag)

    {

        cout <<endl <<"你的通迅录中没有你找的联系人!!!!" <<endl <<endl;

        system("pause");

        return;

    }

    else

    {

        ofstream out("pbook.txt",ios::out);

      

        if (!out)

        {

            cout <<endl <<"对不起!!!不能打开文件!!!!" <<endl;

            system("pause");

            return;

        }

      

        out <<str1 <<endl;

        p = H->next;

        while (p)

        {

            out <<setiosflags(ios::left) <<setw(17) <<p->object.name

                <<" " <<setw(16) <<p->object.number

                <<" " <<setw(18) <<p->object.qq

                <<" " <<setw(14) <<p->object.post

                <<" " <<setw(20) <<p->object.address <<endl;

            p = p->next;

        }

      

        out.close();

        cout <<endl <<"信息已经修改完成!!!!" <<endl <<endl;

        system("pause");

    }

}


//查询联系人

void book::select()

{

    ifstream inData("pbook.txt",ios::in);

  

    if (!inData)

    {

        cout <<endl <<"对不起!!!!文件找不到!!!!" <<endl;

        system("pause");

        return;

    }

  

    string sign;

    cout <<endl <<"输入你想查找的联系人的姓名或电话号码: ";

    cin >>sign;

    fflush(stdin);

  

    string str1;

    bool flag = true;

  

    string str;

    while (inData >>name >>number)

    {

        getline(inData, str);//

if ((name==sign) || (number==sign))

        {

            cout <<endl <<"你要查找的联系人是: " <<endl <<endl;

            cout <<str1 <<endl;

            cout <<setiosflags(ios::left) <<setw(17) <<name

                <<number <<str <<endl;

            flag = false;

            break;

        }

    }

}

void book::save_new()

{

    ofstream outData("pbook.txt", ios::app);


    if (!outData)

    {

        cout <<endl <<"对不起!!!打开文件失败!!!!" <<endl <<endl;

        system("pause");

        return;

    }


    outData << setiosflags(ios::left) << setw(17) << name

        <<" " << setw(16) << number <<" "

        << setw(18) << qq <<" "

        << setw(14) << post <<" "

        << setw(20) << address <<endl;

  

    outData.close();

}

enum power{ADD = '1', DEL = '2', SHOW = '3', Alter = '4', Select = '5', END = '6'};


int main()

{

    char choose;

    book abj;

  

    while (choose = abj.inter_face())

    {

        switch (choose)

        {

        case ADD:

            abj.add_person();////添加联系人

            break;

        case DEL:

            abj.del_person();////删除联系人

            break;

        case SHOW:

            abj.show_all();////显示所有联系人

            break;

        case Alter:

            abj.alter();////修改信息

            break;

        case Select:

            abj.select();////查询联系人

            break;

        case END:

            cout <<endl <<"谢谢使用!!!!!" <<endl <<endl;

            exit(0);

            break;

        default:

            break;

        }

    }
return 0;
}
 

原创粉丝点击