#include <fstream>
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>
int id();
void code();
void load();
void enter();
void searchname();
void searchnum();
void search();
void delet();
void display();
void save();

typedef struct person
{
    char name[20];
    char tnum[30];
    char hnum[20];
    char gzdw[20];
    char zz[20];
    char guanxi[10];
}dperson;
typedef struct dlnode
{
    dperson data;
    struct dlnode *prior;
    struct dlnode *next;
}dnode;

dnode *head;
FILE *fp;
int n;
int j;

void main()
{
//    char ch;
    int i;
    printf("                                            ");
    printf("           ☆★☆通讯录系统☆★☆\n");
    printf("    ==============================================================\n");
     id();

     code();
    load();
    for(;;)
    {
        printf("                      ╔═════════╗\n");
        printf("                      ║1---输入信息      ║\n");
        printf("                      ║2---查找信息      ║\n");
        printf("                      ║3---删除信息      ║\n");
        printf("                      ║4---显示信息      ║\n");
        printf("                      ║5---存盘◆◇      ║\n");
        printf("                      ║0---退出◇◆      ║\n");
        printf("                      ╚═════════╝\n");
        printf("\n    请输入您的选择:\t");
        scanf("%d",&i);
        switch (i)
        {
        case 1:
            enter();break;
        case 2:
            search();break;
        case 3:
            delet();break;
        case 4:
            display();break;
        case 5:
            save();break;
        case 0:
            printf("-------------------------!谢谢使用!-------------------------\n");
            exit(0);break;
        }
    }
}
void menu()
{
    char ch;
    int i;
    for(;;)
    {
        printf("                      ╔═════════╗\n");
        printf("                      ║1---输入信息      ║\n");
        printf("                      ║2---查找信息      ║\n");
        printf("                      ║3---删除信息      ║\n");
        printf("                      ║4---显示信息      ║\n");
        printf("                      ║5---存盘◆◇      ║\n");
        printf("                      ║0---退出◇◆      ║\n");
        printf("                      ╚═════════╝\n");
        printf("\n    请输入您的选择:\t");
        scanf("%d",&i);
        switch (i)
        {
        case 1:
            enter();break;
        case 2:
            search();break;
        case 3:
            delet();break;
        case 4:
            display();break;
        case 5:
            save();break;
        case 0:
            printf("-------------------------!谢谢使用!-------------------------\n");
            exit(0);break;
        }
    }
}

void enter()
{
    dnode *q,*p;
    char flag;
    int i;
    p=head->next;
    for(i=1;i<n;i++)
        p=p->next;
    q=(dnode *)malloc(sizeof(dnode));
    system("cls");
    printf("请输入相关信息:\n\n");
    printf("            姓名:\t");
    scanf("%s",&q->data.name);
    printf("            电话号码:\t");
    scanf("%s",&q->data.tnum);
    printf("                家庭号码:\t");
    scanf("%s",&q->data.hnum );
    printf("            工作单位:\t");
    scanf("%s",&q->data.gzdw  );
    printf("                家庭住址:\t");
    scanf("%s",&q->data.zz   );
    printf("            与联系人关系:\t");
    scanf("%s",&q->data.guanxi  );
    p->next=q;
    q->prior=p;
    p=q;
    q->next=NULL;
    n++;
    printf("          继续输入信息吗?(Y/N)\t");
    scanf("%s",&flag);
    if(flag=='Y'||flag=='y')
        enter();
    system("cls");
    if(flag=='N'||flag=='n')
        menu();
}
 

 
void searchname()
{
    char name[20];
    char flag;
    int i;
    int j=0;
    dnode *p;
    p=head->next;
    system("cls");
    printf("\n****************************按姓名查找************************\n");
    printf("\n               请输入姓名:\t");
    scanf("%s",name);
    printf("\n查询结果:\n\n");
    
    for(i=0;i<n;i++)
    {
        if(strcmp(p->data .name ,name)>0||strcmp(p->data .name ,name)==0)
        {
             printf("%-10s %-10s %-10s %-10s %-10s %-10s\n","姓名","手机号码","家庭号码","工作单位",
                 "家庭住址","与联系人关系");
             printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",p->data .name ,p->data .tnum ,p->data .hnum ,p->data .gzdw ,
                 p->data .zz ,p->data.guanxi );
             p=p->next;
             ++j;
        }
        else p=p->next;
    }    
    
   if(j==0)
      printf("              !没有相关记录!\n");
    
    printf("--------------------------------------\n");
    printf("            继续查找信息吗?(Y/N)\t");
    scanf("%s",&flag);
    if(flag=='Y'||flag=='y')
        search();
    system("cls");
    if(flag=='N'||flag=='n')
        menu();
}
void searchnum()
{
    char tnum[20];
    char flag;
    int i;
    int j=0;
    dnode *p;
    p=head->next;
    system("cls");
    printf("\n****************************按号码查找************************\n");
    printf("\n               请输入号码:\t");
    scanf("%s",tnum);
    printf("\n查询结果:\n\n");
    
    for(i=0;i<n;i++)
    {
        if(strcmp(p->data .tnum ,tnum)==0)
        {
             printf("%-10s %-10s %-10s %-10s %-10s %-10s\n","姓名","手机号码","家庭号码","工作单位",
                 "家庭住址","与联系人关系");
             printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",p->data .name ,p->data .tnum ,p->data .hnum ,p->data .gzdw ,
                 p->data .zz ,p->data.guanxi );
             p=p->next;
             ++j;
        }
        else p=p->next;
    }    
    
   if(j==0)
      printf("              !没有相关记录!\n");
    
    printf("--------------------------------------\n");
    printf("            继续查找信息吗?(Y/N)\t");
    scanf("%s",&flag);
    if(flag=='Y'||flag=='y')
        search();
    system("cls");
    if(flag=='N'||flag=='n')
        menu();
}
void search()
{
    char xz;
    system("cls"); 
    while(1)
    {
        printf("\n\n\n\n\t                       微型通讯录程序");
        printf("\n\t   *********************************************************");
        printf("\n\t   ************************查找菜单*************************");
        printf("\n\t   **  1.按姓名                              2.按号码     **");
        printf("\n\t   **                                        3.返回主菜单 **");
        printf("\n\t   *********************************************************");
        printf("\n\t   *********************************************************");
        printf("\n\t   选择:");
        xz=getch();
        switch(xz)
        {
        case '1': searchname();break;
        case '2': searchnum();break;
        case '3': return;break;
        }
    system("cls");
    }
}

void delet()
{
    char flag;
    char ch;
    char name[20];
    int i;
    dnode *p;
    p=head->next ;
    system("cls");
    printf("请输入您要删除的姓名:\t");
    scanf("%s",name);
    for(i=0;i<n;i++)
    {
        if(strcmp(p->data .name ,name)==0)
        {
            printf("%-10s %-10s %-10s %-10s %-10s %-10s\n","姓名","手机号码","家庭号码","工作单位",
                 "家庭住址","与联系人关系");
            printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",p->data .name ,p->data .tnum ,
                p->data .hnum ,p->data .gzdw ,p->data .zz ,p->data .guanxi );
            printf("确定删除此信息吗?(Y/N)\t");
            scanf("%s",&ch);
            if(ch=='Y'||ch=='y')
            {   if(p==head->next)
                {
                    head->next=p->next;
                    printf("\n            !成功删除!\n");
                    --n;
                 }  
               else if(p->next==NULL)
               {  p->prior=NULL;
                  printf("\n            !成功删除!\n");
                  --n;
                }
                else
                {
                    p->prior ->next =p->next ;
                    p->next ->prior =p->prior ;
                    free(p);
                    printf("\n            !成功删除!\n");
                    --n;    
                 }
            break;
        }
    }
        
        p=p->next ;

    
    }
    if(strcmp(p->data .name ,name)!=0&&p->next==NULL)
        printf("              !没有相关记录!\n");
 
    printf("\n        继续删除吗?(Y/N)\t");
    scanf("%s",&flag);
    if(flag=='Y'||flag=='y')
        delet();
    system("cls");
    if(flag=='N'||flag=='n')
        menu();;
}
void display1()
{
    char guanxi[10];
    char ch;
    dnode *p;
    int i;
    int j=0;
    p=head->next ;
    system("cls");
    printf("\n               请输入与联系人关系:\t");
    scanf("%s",guanxi);
    printf("\n查询结果:\n\n");
    printf("%-10s %-10s %-10s %-10s %-10s %-10s\n","姓名","手机号码","家庭号码","工作单位",
                 "家庭住址","与联系人关系");
    printf("====================================================================\n");
    for (i=0;i<n;i++)
    {   if(strcmp(p->data .guanxi ,guanxi)==0)
        {
            printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",p->data .name ,p->data .tnum ,
                p->data .hnum ,p->data .gzdw ,p->data .zz ,p->data .guanxi );
            p=p->next ;
            ++j;
         }
       else p=p->next ; 
    }
    if(j==0)
        printf("              !没有相关记录!\n");
    printf("=====================================================================\n");
    printf("\n        请按任意键继续\n");
    scanf("%s",&ch);
    system("cls");
    menu();
    
}
void display2()
{
    char guanxi[10];
    char ch;
    dnode *p;
    int i;
    p=head->next ;
    system("cls");
    printf("搜索到的全部信息:\n\n");
    printf("%-10s %-10s %-10s %-10s %-10s %-10s\n","姓名","手机号码","家庭号码","工作单位",
                 "家庭住址","与联系人关系");
    printf("====================================================================\n");
    for (i=0;i<n;i++)
    {  
        printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",p->data .name ,p->data .tnum ,
                p->data .hnum ,p->data .gzdw ,p->data .zz ,p->data .guanxi );
        p=p->next ;
      
    }
    printf("=====================================================================\n");
    printf("\n        请按任意键继续\n");
    scanf("%s",&ch);
    system("cls");
    menu();
    
}
void display()
{
    char yz;
    system("cls"); 
    while(1)
    {
        printf("\n\n\n\n\t                       微型通讯录程序");
        printf("\n\t   *********************************************************");
        printf("\n\t   ************************浏览菜单*************************");
        printf("\n\t   **  1.按类别                             2.所有      **");
        printf("\n\t   **                                        3.返回主菜单 **");
        printf("\n\t   *********************************************************");
        printf("\n\t   *********************************************************");
        printf("\n\t   选择:");
        yz=getch();
        switch(yz)
        {
        case '1': display1();break;
        case '2': display2();break;
        case '3': return;break;
        }
    system("cls");
    }
}

void save()
{
    int i;
    char ch;
    dnode *p;
    p=head->next ;
    if((fp=fopen("txl.txt","wb"))==NULL)
        printf("        文件打开失败");
    if(p!=NULL)
    {
        fprintf(fp,"%d",n);
        fprintf(fp,"\r\n");
        for(i=0;i<n;i++)
        {
            fprintf(fp,"%-10s %-10s %-10s %-10s %-10s %-10s\n",p->data .name ,p->data .tnum ,
                p->data .hnum ,p->data .gzdw ,p->data .zz ,p->data .guanxi);
            fprintf(fp,"\r\n");
            p=p->next ;
        }
    }
    else
        printf("Please make sure the data is not NULL!");
    fclose(fp);
    printf("          !存盘成功!\n");
    printf("\n        请按任意键继续\n");
    scanf("%s",&ch);
    system("cls");
    menu();
}
void load()
    {
    int i;
    dnode *p,*rear;
    head=(dnode *)malloc(sizeof(dnode));
    rear=head;
    head->next =head;
    head->prior =head;
    if((fp=fopen("txl.txt","rb"))==NULL)
    {
    printf("\n                通讯录文件不存在\n");
    if((fp=fopen("txl.txt","wb"))==NULL)
    {
    printf("               建立文件失败\n");
    exit(0);
    }
    printf("\n              通讯录文件已建立\n");
    }
    fscanf(fp,"%d",&n);
    for(i=1;i<=n;i++)
    {
        p=(dnode *)malloc(sizeof (dnode));
        fscanf(fp,"%-10s %-10s %-10s %-10s %-10s %-10s\n",p->data .name ,p->data .tnum ,
                p->data .hnum ,p->data .gzdw ,p->data .zz ,p->data .guanxi );
        rear->next =p;
        p->prior =rear;
        rear=p;
    
        p->next=NULL;
    }
    fclose(fp);
    printf("\n                【☉☉成功装入☉☉】\n\n");
}


    int id()
    {
        char na[20];
        printf("            用户名:\t");
        scanf("%s",&na);
        if(na!=NULL)
            j=1;
        else
        {
            printf("            用户名不存在,请重新输入\n");
            id();
        }
        return j;
    }
    void code()
    {
        char co[20];
        int i=0;
        char ch;
        printf("            ※密码:");
        ch=getch();
        for(i=0;ch!='\r';i++)
         {
            printf("*");
            co[i] = ch;
            ch = getch();
        }
        co[i]='\0';
        if(strcmp(co,"123")==0&&j==1)
            printf("     \n");
        
        
        else
        {
            printf("        密码错误,请重新输入\n");
            code();
        }
        system("cls");
    }