zoukankan      html  css  js  c++  java
  • 数据结构期末课程设计-----通讯录(文件存取数据)

      1 #include<bits/stdc++.h>
      2 #include<string>
      3 #include<algorithm>
      4 #include<fstream>
      5 #include<iomanip>
      6 #include<stdlib.h>
      7 #include<cstdlib>
      8 #include<string>
      9 using namespace std;
     10 typedef struct DataType{
     11     string num;  //编号
     12     string name; //姓名
     13     string sex;  //性别
     14     string phone; //电话号
     15     string QQ;//QQ号 
     16     string Wechat;//微信号 
     17     string addr; //住址
     18     string email;//邮箱
     19     string birth;//生日
     20 }DataType;
     21 typedef struct InfoNode{
     22     DataType data;
     23     struct InfoNode* next;
     24 }InfoNode;
     25 //菜单选择程序
     26 void menu()
     27 {
     28     printf("     通讯录管理系统
    ");
     29     printf("=========================
    ");
     30     printf("   1.通讯录链表的建立
    ");
     31     printf("   2.通讯录信息的插入
    ");
     32     printf("   3.通讯录信息的查询
    ");
     33     printf("   4.通讯录信息的修改
    ");
     34     printf("   5.通讯录信息的删除
    ");
     35     printf("   6.通讯录链表的保存
    ");
     36     printf("   7.通讯录链表的输出
    ");
     37     printf("   0.退出管理系统
    ");
     38     printf("=========================
    ");
     39     printf("请选择 0-7: ");
     40 }
     41 //通讯录链表的建立(带头的单向链表)
     42 void createPersonInfoList(InfoNode* pHead)
     43 {
     44     pHead->next=NULL;
     45     string str;
     46     ifstream fin;
     47     fin.open("Person.txt", ios::in);//读取形式打开文件
     48     if (!fin.is_open())//打开失败
     49         cout << "--------文件不能打开,建立一个新的通讯录--------" << endl;
     50     else
     51     {
     52         while (fin.peek()!=EOF)
     53         {
     54             InfoNode* newMember=new InfoNode();
     55             getline(fin,newMember->data.num);
     56             if(newMember->data.num=="########")
     57                 continue;
     58             getline(fin,newMember->data.name);
     59             getline(fin,newMember->data.sex);
     60             getline(fin,newMember->data.phone);
     61             getline(fin,newMember->data.QQ);
     62             getline(fin,newMember->data.Wechat);
     63             getline(fin,newMember->data.addr);
     64             getline(fin,newMember->data.email);
     65             getline(fin,newMember->data.birth);
     66             if (pHead->next==NULL)
     67             {
     68                 pHead->next=newMember;
     69                 newMember->next=NULL;
     70             }
     71             else
     72             {
     73                 InfoNode* p=pHead->next;
     74                 while(p->next!=NULL)
     75                 {
     76                     p=p->next;
     77                 }
     78                 p->next=newMember;
     79                 newMember->next=NULL;
     80             }
     81         }
     82     }
     83     fin.close();//关闭文件
     84     cout << "--------通讯录链表创建完成!--------" << endl;
     85 }
     86 //通讯录信息的插入(尾插法)
     87 void insertPersonInfoToListT(InfoNode* pHead)
     88 {
     89     char flag = 'y';
     90     regex reg_email("([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)");
     91     regex reg_birth("([12]\d{3}\/(0?[1-9]|1[0-2])\/(0?[1-9]|[12]\d|3[01]))");
     92     while (flag=='y')
     93     {
     94         //先申请新的节点
     95         InfoNode* newNode=new InfoNode();
     96         cout<<"编号: ";
     97         cin >> newNode->data.num;
     98         InfoNode* p=pHead->next;
     99         while (p!=NULL)
    100         {
    101             if (p->data.num!=newNode->data.num){
    102                 p=p->next;
    103             }else{
    104                 cout<<"您输入的编号已经在通讯录中!请确认后再输入信息!"<<endl;    
    105                 return;
    106             }
    107         }
    108         cout<<"姓名: ";
    109         cin>>newNode->data.name;
    110         cout<<"性别(男或女): ";
    111         cin>>newNode->data.sex;
    112         //性别格式 (男或女)
    113         while(!(newNode->data.sex==""||newNode->data.sex=="")){
    114             cout<<"输入的性别格式错误,重新输入:";
    115             cin>> newNode->data.sex;
    116         }     
    117         cout<<"电话: ";
    118         cin>>newNode->data.phone;
    119         cout<<"QQ号: ";
    120         cin>>newNode->data.QQ;
    121         cout<<"微信号: ";
    122         cin>>newNode->data.Wechat;
    123         cout<<"住址: ";
    124         cin>>newNode->data.addr;
    125         cout<<"邮箱: ";
    126         cin>>newNode->data.email;
    127         //邮箱格式 (用户名@主机名)
    128         while(!regex_match(newNode->data.email,reg_email)){
    129             cout<<"输入的邮箱格式错误,重新输入:";
    130             cin>> newNode->data.email;
    131         } 
    132         cout<<"生日: ";
    133         cin>>newNode->data.birth;
    134         //生日格式 (2019/03/04)
    135         while(!regex_match(newNode->data.birth,reg_birth)){
    136             cout<<"输入的生日格式错误,重新输入:";
    137             cin>>newNode->data.birth;
    138         }
    139         //输入完成后,将该节点尾插到通讯录链表上
    140         //若只有一个信息节点,则直接尾插
    141         if (pHead->next==NULL)
    142         {
    143             pHead->next=newNode;
    144             newNode->next=NULL;
    145         }
    146         //若已经有了若干个节点
    147         else
    148         {
    149             //遍历找到最后节点
    150             InfoNode* pCur = pHead->next;
    151             while (pCur->next != NULL)
    152             {
    153                 pCur = pCur->next;
    154             }
    155             pCur->next = newNode;
    156             newNode->next = NULL;
    157         }
    158         cout<<"是否继续录入(y/n):";
    159         cin>>flag;
    160     }
    161 }
    162 //通讯录信息的插入(中间插入)
    163 void insertPersonInfoToListN(InfoNode* pHead)
    164 {    
    165     char flag = 'y';
    166     regex reg_email("([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)");
    167     regex reg_birth("([12]\d{3}\/(0?[1-9]|1[0-2])\/(0?[1-9]|[12]\d|3[01]))");
    168     while (flag=='y')
    169     {
    170         //先申请新的节点
    171         InfoNode* newNode=new InfoNode();
    172         InfoNode* p=pHead->next;
    173         int n=0,cnt=0;
    174         cout<<"输入你要插入的位置:";
    175         cin>>n;
    176         cout<<"编号: ";
    177         cin>>newNode->data.num;
    178         while(p!=NULL)
    179         {
    180             if(p->data.num!=newNode->data.num){
    181                 p=p->next;
    182             }else{
    183                 cout<<"您输入的编号已经在通讯录中!请确认后再输入信息!"<<endl;    
    184                 return;
    185             }
    186         }
    187         p=pHead->next;
    188         cout<<"姓名: ";
    189         cin>>newNode->data.name;
    190         cout<<"性别(男或女): ";
    191         cin>>newNode->data.sex;
    192         while(!(newNode->data.sex==""||newNode->data.sex=="")){
    193             cout<<"输入的性别格式错误,重新输入:";
    194             cin>>newNode->data.sex;
    195         }
    196         cout<<"电话: ";
    197         cin>>newNode->data.phone;
    198         cout<<"QQ号: ";
    199         cin>>newNode->data.QQ;
    200         cout<<"微信号: ";
    201         cin>>newNode->data.Wechat;
    202         cout<<"住址: ";
    203         cin>>newNode->data.addr;
    204         cout<<"邮箱: ";
    205         cin>>newNode->data.email;
    206         //邮箱格式 (用户名@主机名)
    207         while(!regex_match(newNode->data.email,reg_email)){
    208             cout<<"输入的邮箱格式错误,重新输入:";
    209             cin>>newNode->data.email;
    210         } 
    211         cout<<"生日: ";
    212         cin>>newNode->data.birth;
    213         //生日格式 (2019/03/04)
    214         while(!regex_match(newNode->data.birth,reg_birth)){
    215             cout<<"输入的生日格式错误,重新输入:";
    216             cin>>newNode->data.birth;
    217         }
    218         //若只有一个信息节点,则直接尾插
    219         if (pHead->next==NULL)
    220         {
    221             pHead->next=newNode;
    222             newNode->next=NULL;
    223         }
    224         //若已经有了若干个节点
    225         else
    226         {
    227             InfoNode* q=new InfoNode();
    228             while(p!=NULL)
    229             {
    230                 if(cnt!=n)
    231                 {
    232                     q=p;
    233                     p=p->next;
    234                     cnt++;
    235                 }
    236                 else
    237                 {
    238                     newNode->next=q->next;
    239                     q->next=newNode;
    240                     break;
    241                 }
    242             }
    243         }
    244         cout<<"是否继续录入(y/n):";
    245         cin>>flag;
    246     }
    247 }
    248 //通讯录信息的查询
    249 void searchPersonInfoList(InfoNode* pHead)
    250 {
    251     regex reg_email("([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)");
    252     regex reg_birth("([12]\d{3}\/(0?[1-9]|1[012])\/(0?[1-9]|[12]\d|3[01]))");
    253     if (pHead->next == NULL)
    254         cout << "--------通讯录为空,查询失败!--------" << endl;
    255     else
    256     {
    257         int select = 0;
    258         printf("===============
    ");
    259         printf(" 1. 按姓名查询
    ");
    260         printf(" 2. 按性别查询
    ");
    261         printf(" 3. 按生日查询
    ");
    262         printf("===============
    ");
    263         cout<<"请选择查询方式: ";
    264         cin>>select;
    265         if(select==1)
    266         {
    267             int count=0;
    268             string name;
    269             cout<<"请输入姓名: ";
    270             cin>>name;
    271             //遍历链表,查询与输入的姓名相同的信息
    272             InfoNode* pCur=pHead->next;
    273             while(pCur!=NULL)
    274             {
    275                 if(pCur->data.name!=name)
    276                     pCur=pCur->next;
    277                 else
    278                 {
    279                     ++count;
    280                     printf("-----------------------------------------------------
    ");
    281                     cout<<"编号: "<<pCur->data.num<<endl;
    282                     cout<<"姓名: "<<pCur->data.name<<endl;
    283                     cout<<"性别: "<<pCur->data.sex<<endl;
    284                     cout<<"电话号: "<<pCur->data.phone<<endl;
    285                     cout<<"QQ: "<<pCur->data.QQ<<endl;
    286                     cout<<"微信: "<<pCur->data.Wechat<<endl;
    287                     cout<<"住址: "<<pCur->data.addr<<endl;
    288                     cout<<"邮箱: "<<pCur->data.email<<endl;
    289                     cout<<"生日: "<<pCur->data.birth<<endl;
    290                     printf("-----------------------------------------------------
    ");
    291                     pCur=pCur->next;
    292                 }
    293             }
    294             if(count)
    295                 printf("------共有%d条记录------
    ", count);
    296             else
    297                 printf("------没有符合要求的记录------
    ");
    298         }
    299         if (select==2)
    300         {
    301             int count=0;
    302             string sex;
    303             cout<<"请输入性别: ";
    304             cin>>sex;
    305             while(!(sex==""||sex=="")){
    306                 cout<<"输入的性别格式错误,重新输入:";
    307                 cin>>sex;
    308             } 
    309             //遍历链表,查询与输入的性别相同的信息
    310             InfoNode* pCur = pHead->next;
    311             while(pCur != NULL)
    312             {
    313                 if(pCur->data.sex!=sex)
    314                     pCur=pCur->next;
    315                 else
    316                 {
    317                     ++count;
    318                     printf("-----------------------------------------------------
    ");
    319                     cout<<"编号: "<<pCur->data.num<<endl;
    320                     cout<<"姓名: "<<pCur->data.name<<endl;
    321                     cout<<"性别: "<<pCur->data.sex<<endl;
    322                     cout<<"电话号: "<<pCur->data.phone<<endl;
    323                     cout<<"QQ: "<<pCur->data.QQ<<endl;
    324                     cout<<"微信: "<<pCur->data.Wechat<<endl;
    325                     cout<<"住址: "<<pCur->data.addr<<endl;
    326                     cout<<"邮箱: "<<pCur->data.email<<endl;
    327                     cout<<"生日: "<<pCur->data.birth<<endl;
    328                     printf("-----------------------------------------------------
    ");
    329                     pCur=pCur->next;
    330                 }
    331             }
    332             if(count)
    333                 printf("------共有%d条记录------
    ", count);
    334             else
    335                 printf("------没有符合要求的记录------
    ");
    336         }
    337         if(select==3)
    338         {
    339             int count=0;
    340             string birth;
    341             cout<<"请输入生日: ";
    342             cin>>birth;
    343             while(!regex_match(birth,reg_birth)){
    344                 cout<<"输入的生日格式错误,重新输入:";
    345                 cin>> birth;
    346             }
    347             //遍历链表,查询与输入的生日相同的信息
    348             InfoNode* pCur=pHead->next;
    349             while (pCur!=NULL)
    350             {
    351                 if (pCur->data.birth!=birth)
    352                     pCur=pCur->next;
    353                 else
    354                 {
    355                     ++count;
    356                     printf("-----------------------------------------------------
    ");
    357                     cout<<"编号: "<<pCur->data.num<<endl;
    358                     cout<<"姓名: "<<pCur->data.name<<endl;
    359                     cout<<"性别: "<<pCur->data.sex<<endl;
    360                     cout<<"电话号: "<<pCur->data.phone<<endl;
    361                     cout<<"QQ: "<<pCur->data.QQ<<endl;
    362                     cout<<"微信: "<<pCur->data.Wechat<<endl;
    363                     cout<<"住址: "<<pCur->data.addr<<endl;
    364                     cout<<"邮箱: "<<pCur->data.email<<endl;
    365                     cout<<"生日: "<<pCur->data.birth<<endl;
    366                     printf("-----------------------------------------------------
    ");
    367                     pCur=pCur->next;
    368                 }
    369             }
    370             if(count)
    371                 printf("------共有%d条记录------
    ", count);
    372             else
    373                 printf("------没有符合要求的记录------
    ");
    374         }
    375     }
    376 }
    377 //通讯录信息的修改
    378 void modifyPersonInfoList(InfoNode* pHead)
    379 {
    380     if(pHead->next==NULL)
    381     {
    382         cout<<"--------通讯录为空,修改失败!--------"<<endl;
    383         return;
    384     }
    385     string num;
    386     bool flag=false;
    387     regex reg_email("([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)");
    388     cout<<"请输入将要修改的联系人编号: ";
    389     cin>>num;
    390     //遍历链表,查询此节点
    391     InfoNode* pCur=pHead->next;
    392     while(pCur!=NULL)
    393     {
    394         if(pCur->data.num!=num)
    395             pCur=pCur->next;
    396         else
    397         {
    398             int select=0;
    399             printf("=================
    ");
    400             printf(" 1. 修改电话号
    ");
    401             printf(" 2. 修改QQ号
    ");
    402             printf(" 3. 修改微信号
    ");
    403             printf(" 4. 修改住址
    ");
    404             printf(" 5. 修改邮箱
    ");
    405             printf("=================
    ");
    406             cout<<"请输入: ";
    407             cin>>select;
    408             if(select==1)
    409             {
    410                 string newPhone;
    411                 cout<<"请输入新的电话号:";
    412                 cin>>newPhone;
    413                 pCur->data.phone=newPhone;
    414             }
    415             if(select==2)
    416             {
    417                 string newQQ;
    418                 cout<<"请输入新的QQ号:";
    419                 cin>>newQQ;
    420                 pCur->data.QQ=newQQ;
    421             }
    422             if(select==3)
    423             {
    424                 string newWechat;
    425                 cout<<"请输入新的微信号:";
    426                 cin>>newWechat;
    427                 pCur->data.Wechat=newWechat;
    428             }
    429             if(select==4)
    430             {
    431                 string newaddr;
    432                 cout<<"请输入新的住址:";
    433                 cin>>newaddr;
    434                 pCur->data.addr=newaddr;
    435             }
    436             if(select==5)
    437             {
    438                 string newemail;
    439                 cout<<"请输入新的邮箱:";
    440                 cin>>newemail;
    441                 while(!regex_match(newemail,reg_email)){
    442                     cout<<"输入的邮箱格式错误,重新输入:";
    443                     cin>>newemail;
    444                 } 
    445                 pCur->data.email=newemail;
    446             }
    447             cout<<"--------修改成功--------
    ";
    448             flag=true;
    449             break;
    450             pCur=pCur->next;
    451         }
    452     }
    453     if(!flag){
    454         cout<<"--------不存在您要修改的联系人编号!请确认后再使用此功能!--------"<<endl;
    455     }
    456 }
    457 //通讯录联系人的删除
    458 void delPersonInfo(InfoNode* pHead)
    459 {
    460     if (pHead->next==NULL)
    461     {
    462         cout<<"通讯录为空,删除失败!"<<endl;
    463         return;
    464     }
    465     string delnum;
    466     cout<<"请输入将要删除的联系人编号: ";
    467     cin>>delnum;
    468     //遍历链表,查找这个节点
    469     InfoNode* pPrev=pHead;
    470     InfoNode* pCur=pHead->next;
    471     while (pCur!=NULL)
    472     {
    473         if (pCur->data.num!=delnum)
    474         {
    475             pPrev=pCur;
    476             pCur=pCur->next;
    477         }
    478         else
    479         {
    480             //找到了这个联系人
    481             InfoNode* delNode=pCur;
    482             pPrev->next=pCur->next;
    483             pCur=pCur->next;
    484             delete delNode;
    485             delNode=NULL;
    486             cout<<"删除成功!"<<endl;
    487             return;
    488         }
    489     }
    490     cout<<"-------无本地搜索结果-------
    ";
    491 }
    492 //通讯录链表的输出
    493 void printPersonInfoList(InfoNode* pHead)
    494 {
    495     if (pHead->next==NULL)
    496     {
    497         cout<<"通讯录为空,输出失败!"<<endl;
    498     }
    499     else
    500     {
    501         cout<<"---------------------联系人信息----------------------
    ";
    502         InfoNode* pCur=pHead->next;
    503         while (pCur!=NULL)
    504         {
    505             printf("-----------------------------------------------------
    ");
    506             cout<<"编号: "<<pCur->data.num<<endl;
    507             cout<<"姓名: "<<pCur->data.name<<endl;
    508             cout<<"性别: "<<pCur->data.sex<<endl;
    509             cout<<"电话号: "<<pCur->data.phone<<endl;
    510             cout<<"QQ: "<<pCur->data.QQ<<endl;
    511             cout<<"微信: "<<pCur->data.Wechat<<endl;
    512             cout<<"住址: "<<pCur->data.addr<<endl;
    513             cout<<"邮箱: "<<pCur->data.email<<endl;
    514             cout<<"生日: "<<pCur->data.birth<<endl;
    515             printf("-----------------------------------------------------
    ");
    516             pCur=pCur->next;
    517         }
    518     }
    519 }
    520 //文件存储 
    521 void savePersonInfoList(InfoNode* pHead)
    522 {
    523     ofstream fos;
    524     fos.open("Person.txt", ios::out);//写形式打开文件
    525     if (fos.is_open())   
    526     { 
    527         if (pHead->next!=NULL){
    528             InfoNode* pCur=pHead->next;
    529             while (pCur!=NULL)
    530             {
    531                 fos<<pCur->data.num<<"
    ";
    532                 fos<<pCur->data.name<<"
    ";
    533                 fos<<pCur->data.sex<<"
    ";
    534                 fos<<pCur->data.phone<<"
    ";
    535                 fos<<pCur->data.QQ<<"
    ";
    536                 fos<<pCur->data.Wechat<<"
    ";
    537                 fos<<pCur->data.addr<<"
    ";
    538                 fos<<pCur->data.email<<"
    ";
    539                 fos<<pCur->data.birth<<"
    ";
    540                 fos<<"########
    ";
    541                 pCur=pCur->next;
    542             }
    543         } 
    544     }
    545     fos.close();
    546     cout<<"--------通讯录保存成功--------
    "; 
    547 }

    主函数部分

     1 #include "head.h"
     2 int main()
     3 {
     4     InfoNode personInfoList;
     5     int select = 0;
     6     while (1)
     7     {
     8         menu();
     9         while(1)
    10         {
    11             cin>>select;
    12             system("cls");
    13             if(select<0||select>7)
    14             {
    15                 cout<<"输入有误,请重新选择: ";
    16             }
    17             else
    18                 break;
    19         }
    20         switch(select)
    21         {
    22             case 0: 
    23                 cout<<"------感谢使用本程序------"<<endl;
    24                 exit(0);
    25             case 1:
    26                 createPersonInfoList(&personInfoList);
    27                 break;
    28             case 2:
    29                 int n;
    30                 cout<<"--------1.中间插入         2.尾插法--------
    ";
    31                 cin>>n;
    32                 while(n<1&&n>2)
    33                 {
    34                     cout<<"输入有误,请重新输入"<<endl;
    35                     cin>>n;
    36                 }
    37                 if(n==1)
    38                     insertPersonInfoToListN(&personInfoList);
    39                 if(n==2)
    40                     insertPersonInfoToListT(&personInfoList);
    41                 break;
    42             case 3:
    43                 searchPersonInfoList(&personInfoList);
    44                 break;
    45             case 4:
    46                 modifyPersonInfoList(&personInfoList);
    47                 break;
    48             case 5:
    49                 delPersonInfo(&personInfoList);
    50                 break;
    51             case 6:
    52                 savePersonInfoList(&personInfoList);
    53                 break;
    54             case 7:
    55                 printPersonInfoList(&personInfoList);
    56                 break;
    57         }
    58     }
    59     system("pause");
    60     return 0;
    61 }

    基本上没有什么错误,只是文件读写不太会,所以我写的有点麻烦,需要用的话改改就行,功能都能够完美运行。提示:需要创建Person.txt文件。包含正则表达式,所以测试的时候需要正确输入。

  • 相关阅读:
    POJ
    luogu- P1373 小a和uim之大逃离 DP 四维,其中一维记录差值
    牛客国庆集训派对Day3 B Tree(树形dp + 组合计数)
    【CF 1059C】 Sequence Transformation 数学
    POJ
    牛客国庆集训派对Day6 A Birthday 费用流
    Treap + 无旋转Treap 学习笔记
    牛客2018国庆集训派对Day3 I Metropolis 多源最短路径
    Gym
    CodeForces
  • 原文地址:https://www.cnblogs.com/xiaowangdatie/p/13273021.html
Copyright © 2011-2022 走看看