C++實(shí)例:用C++編寫通訊錄

字號:

#include
     #include
    #include
    #include
    #include
    struct Address
    {
    char name[20];
    char tel[20];
    char email[50];
    char relation[10];
    Address*next;
    };
    //判斷email
    int Doe(char * s)
    {
    int a=1;
    if(*s!=’_’&&*s!=’/’&&*s!=’\\’&&*s!=’.’)
    {
    while(*s)
    {
    if(*s==’@’)
    {
    a=0;
    break;
    }
    s++;
    }
    if(*s==’\0’)
    {
    a=1;
    cout<<"沒有@!"<    }
    }
    else cout<<"首字母不能是:_、/、\\、.\n";
    return a;
    }
    //添加記錄
    void CreatList(Address*&head) //引用參數(shù)是表頭指針
    {
    Address*s,*p;
    cout<<"(添加記錄一直到輸入人名為0時結(jié)束。)"<    s=new Address;
    cout<<"name :";
    cin>>s->name;
    cout<<"tel :";
    cin>>s->tel;
    do
    {
    cout<<"email :"<    cin>>s->email;
    }
    while(Doe(s->email));
    cout<<"relation :";
    cin>>s->relation;
    while(strcmp(s->name,"0\0"))
    {
    if(head==NULL) //判斷列表是否已經(jīng)存在。若不存在,則首先自動新建一個鏈表。
    {
    cout<<"列表不存在,新建一個鏈表。"<    head=s;
    }else
    {
    p->next=s;
    }
    p=s;
    s=new Address;
    cout<<"name :";
    cin>>s->name;
    cout<<"tel :";
    cin>>s->tel;
    do
    {
    cout<<"email :"<    cin>>s->email;
    }
    while(Doe(s->email));
    cout<<"relation :";
    cin>>s->relation;
    }
    p->next=NULL;
    delete s; //釋放名字為0的結(jié)點(diǎn)。
    return;
    }
    //顯示記錄
    void ShowList(Address*head)
    {
    cout<<"顯示當(dāng)前所有記錄:\n";
    while(head)
    {
    cout<name<<’\t’<tel<<’\t’<email<<’\t’<relation<<’\n’;
    head=head->next;
    }
    cout<    }
    //查詢記錄
    void FindPerson(Address*head)
    {
    char in_name[20];
    cout<<"輸入要查詢的名字:";
    cin>>in_name;  while(head)
    {
    if(strcmp(head->name,in_name)==0) //比較名字是否相等。
    {
    cout<name<<’\t’<tel<<’\t’<email<<’\t’<relation<<’\n’;
    break;
    }
    head=head->next;
    }
    if(head==NULL)
    cout<<"沒有你要找的名字!";
    cout<    }
    //通訊錄排序
    void Bubble(Address*head)
    {
    Address*m=head;
    Address*start=head,*present;
    present=start->next;
    Address*r=new Address;
    while(head->next!=NULL)
    {
    while(present!=NULL)
    {
    if(strcmp(start->name,present->name)>0)
    {
    strcpy(r->name,start->name);
    strcpy(r->tel,start->tel);
    strcpy(r->email,start->email);
    strcpy(r->relation,start->relation);
    strcpy(start->name,present->name);
    strcpy(start->tel,present->tel);
    strcpy(start->email,present->email);
    strcpy(start->relation,present->relation);
    strcpy(present->name,r->name);
    strcpy(present->tel,r->tel);
    strcpy(present->email,r->email);
    strcpy(present->relation,r->relation);
    }
    start=present;
    present=present->next;
    }
    start=m;
    present=m->next;
    head=head->next;
    }
    cout<<"顯示當(dāng)前所有記錄:\n";
    while(m)
    {
    cout<name<<’\t’<tel<<’\t’<email<<’\t’<relation<<’\n’;
    m=m->next;
    }
    cout<    }
    //刪除記錄
    void MoveAway(Address*head)
    {
    Address*start,*present=head;
    char in_name[20];
    cout<<"輸入要刪除人的名字:";
    cin>>in_name;
    while(head)
    {
    if(strcmp(head->name,in_name)==0) //比較名字是否相等。
    {
    cout<<"要刪除的內(nèi)容是: "<    cout<name<<’\t’<tel<<’\t’<email<<’\t’<relation<<’\n’;
    break;
    }
    if(head->next!=NULL)
    head=head->next;
    else
    cout<<"沒有你要找的名字!"; break;
    }
    cout<    start=present;
    present=head;
    head=start;
    if(!head)
    { cout<<"列表是空的。\n"; return;}
    if(present==head) //被刪除的節(jié)點(diǎn)是頭節(jié)點(diǎn)。
    {
    start=head;
    head=head->next;
    delete start;
    start=NULL;
    cout<<"已刪除。";
    return;
    }
    for(start=head;start!=present;start=start->next) //被刪除節(jié)點(diǎn)不是頭節(jié)點(diǎn)。找到被刪除節(jié)點(diǎn)的前一節(jié)點(diǎn)。
    {
    if(start->next==present)
    {
    start->next=present->next;
    delete present;
    present=NULL;
    cout<<"已刪除。";
    return;
    }
    }
    return;
    }
    //更新記錄
    void ReSet(Address*head)
    {
    Address*s=head;
    char in_name[20];
    cout<<"輸入要更新的名字:";
    cin>>in_name;
    int i;
    while(head) //首先判斷該記錄是否存在。
    {
    if(strcmp(head->name,in_name)==0) //比較名字是否相等。
    {
    cout<<"要更新的內(nèi)容存在。"<    cout<name<<’\t’<tel<<’\t’<email<<’\t’<relation<    break;
    }
    head=head->next;
    }
    if(head==NULL)
    cout<<"沒有你要找的名字!";
    cout<    head=s;
    while(s)
    {
    if(strcmp(s->name,in_name)==0) //比較名字是否相等。
    {
    cout<<"要更新什么內(nèi)容?輸入1選擇更新人名;輸入2選擇更新電話;輸入3選擇更新Email;輸入4選擇更新與本人的關(guān)系。"<    cin>>i;
    cout<<"輸入要更改的內(nèi)容。"<    {
    case 1:cin>>s->name;break;
    case 2:cin>>s->tel;break;
    case 3:cin>>s->email;break;
    case 4:cin>>s->relation;
    }
    cout<<"更新成功!"<    cout<name<<’\t’<tel<<’\t’<email<<’\t’<relation<    break;
    }
    s=s->next;
    }
    }
    //按關(guān)系顯示記錄
    void ListInRela(Address*head)
    {
    char in_rela[10];
    cout<<"請輸入要顯示的記錄的關(guān)系:"<    cin>>in_rela;
    while(head)
    {
    if(strcmp(head->relation,in_rela)==0) //比較是否相等。
    {
    cout<name<<’\t’<tel<<’\t’<email<<’\t’<relation<<’\n’;
    }
    head=head->next;
    }
    cout<    }
    //保存記錄
    void KeepList(Address*head)
    {
    char fileName[80];
    ofstream outstuf; //建立輸出文件流對象
    cout<<"請輸入要保存記錄的目的文件名:\n";
    cin>>fileName;
    outstuf.open(fileName,ios::out); //鏈接文件,指定打開方式
    if(!outstuf) //調(diào)用重載算符函數(shù)測試流
    {
    cerr<<"文件無法打開。"<    abort();
    }
    cout<<"開始保存記錄……"<    Address*s=head;
    while(s) //向流插入數(shù)據(jù)
    {
    outstuf<name<<’ ’<tel<<’ ’<email<<’ ’<relation<<’\n’;
    s=s->next;
    }
    outstuf.close(); //關(guān)閉文件
    cout<<"記錄已保存。"<    }
    //讀入記錄
    void ReadFile()
    {
    Address*sa=new Address;
    ifstream instuf("d:\\file\\Phonebook.txt",ios::in); //打開文件
    if(!instuf)
    {
    cerr<<"文件無法打開。"<    abort();
    }
    while(instuf>>sa->name>>sa->tel>>sa->email>>sa->relation) //提取數(shù)據(jù)
    {
    cout<<"讀入成功,數(shù)據(jù)為:"<    cout<name<<" "<tel<<" "<email<<" "<relation<    }
    instuf.close(); //關(guān)閉文件
    }
    void main()
    {
    int choice=1;
    Address*head=NULL;
    cout<<"添加記錄…………………………………………1"<    cout<<"顯示記錄…………………………………………2"<    cout<<"查詢記錄…………………………………………3"<    cout<<"通訊錄排序………………………………………4"<    cout<<"刪除記錄…………………………………………5"<    cout<<"更新記錄…………………………………………6"<    cout<<"按關(guān)系顯示記錄…………………………………7"<    cout<<"保存記錄…………………………………………8"<    cout<<"讀入記錄…………………………………………9"<    cout<<"退出通訊錄………………………………………0"<    cout<<"請輸入相應(yīng)的序號選擇要執(zhí)行的任務(wù)"<    while(cin>>choice)
    {
    switch(choice)
    {
    case 1:CreatList(head);continue;
    case 2:ShowList(head);continue;
    case 3:FindPerson(head);continue;
    case 4:Bubble(head);continue;
    case 5:MoveAway(head);continue;
    case 6:ReSet(head);continue;
    case 7:ListInRela(head);continue;
    case 8:KeepList(head);continue;
    case 9:ReadFile();continue;
    }
    if(choice==0)
    {
    cout<<"退出操作"<    break;
    }
    }
    }