码迷,mamicode.com
首页 > 编程语言 > 详细

【简易通讯录】——C语言实现

时间:2015-11-29 06:41:48      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:c语言   结构体   通讯录   

-------------------------------------------------------------------------------------------------------

       本通讯录实现了一个普通通讯录的所有功能;包括:联系人的添加,删除,修改,排序,以及清空等功能;采用结构体自定义类型实现;排序部分采用的是冒泡排序。界面比较友好,希望各位朋友批评指正。

-------------------------------------------------------------------------------------------------------

 

C语言代码:

-----------------------

contacts.h (头文件)

-----------------------

# include <stdio.h>
# include <string.h>
# include <assert.h>
# include <stdlib.h>

# define MAX 1000          //可存放最大联系人的数量

enum
{
     _exit_sys,              //0——>退出系统
     _add_contacts,           //1——>添加联系人
     _del_contacts,           //2——>删除联系人
     _lookup_contacts,        //3——>查找联系人
     _revise_contacts,        //4——>修改联系人
     _show_contacts,          //5——>显示联系人
     _sort_contacts,          //6——>排序联系人
     _empty_contacts          //7——>清空联系人
};

typedef struct  phone_book    //电话本结构类型
{
     int order_num;
     char name[20];
     char sex[6];
     int age;
     char phone_numb[12];
     char addr[50];
}p_book;

/*函数声明*/
int empty_contacts(p_book *contacts_parr);
//int name_cmp(const char * name1, const char *name2);
int sort_contacts(p_book *contacts_parr);
void show_one_contact(p_book *contacts_parr);
int show_contacts(p_book *contacts_parr);
int revise_contacts(p_book *contacts_parr);
int lookup_contacts(p_book *contacts_parr);
int del_contacts(p_book *contacts_parr);
int add_contacts(p_book *contacts_parr);
int phone_book_menu(int option);
void lookup_style();
void help_menu();

------------------------

功能函数(functions)

------------------------

/*清空联系人*/
int empty_contacts(p_book *contacts_parr)
{
     assert(contacts_parr);
     
     char input_name[20] = "\0";
     char sex[6] = "\0";
     char phone_numb[12] = "\0";
     char addr[50] = "\0";
     char back = ‘y‘;             //判断操作是否要继续
     char y_n_empty;             //是否执行清空操作
     p_book *src = NULL;
     
    /*是否确定清空*/
     printf("\n\t确定是否清空(y/n)? : ");
     fflush(stdin);
     scanf("%c", &y_n_empty);
     
     if (y_n_empty == ‘y‘)
     {
          src = contacts_parr;
         
          while ((src->order_num > 0) && (back == ‘y‘))
          {
               src->order_num = 0;
               strcpy(src->name, "\0");
               strcpy(src->sex, "\0");
               src->age = 0;
               strcpy(src->phone_numb, "\0");
               strcpy(src->addr, "\0");
              
                src++;
            
               if (src->order_num == 0)
               {
                    printf("\n\t联系人已经全部清空!!!\n\n");
                    printf("\t返回主菜单(y)? :");
                    fflush(stdin);
                    scanf("%c", &back);
                   
                    printf("\n\n");
               }
          }
     }
 
    return (1);
}

/*显示指定联系人的信息*/
void show_one_contact(p_book *contacts_parr)
{
     assert(contacts_parr);
    
     p_book *src = contacts_parr;
     printf("\n\n\t序号:%d\t", src->order_num);
     printf("\n\t\t  姓名:%s\t", src->name);
     printf("\t性别:%s", src->sex);
     printf("\t年龄:%d\n", src->age);
     printf("\t\t  电话号码:%s", src->phone_numb);
     printf("\t地址:%s\n\n", src->addr);
}

/*显示全部联系人*/
int show_contacts(p_book *contacts_parr)
{
     assert(contacts_parr);
     
     char back = ‘n‘;             //判断操作是否要继续
     p_book *src = contacts_parr;
     
     /*通讯录为空*/
     if (src->order_num == 0)
     {
          printf("\n\n\t通讯录为空!!!\n");
     }
    
     while (back != ‘y‘)
     {
          while (src->order_num != 0)
          {
               printf("\n\n\t序号:%d\t", src->order_num);
               printf("\n\t\t  姓名:%s\t", src->name);
               printf("\t性别:%s", src->sex);
               printf("\t年龄:%d\n", src->age);
               printf("\t\t  电话号码:%s", src->phone_numb);
               printf("\t地址:%s", src->addr);
            
               src++;
          }
        
          printf("\n\n\n\t返回主菜单(y)? :");
          fflush(stdin);
          scanf("%c", &back);
         
          printf("\n\n");
     }
     
    return (1);
}

/*按联系人姓名排序*/
int sort_contacts(p_book *contacts_parr)
{
     assert(contacts_parr);
    
     int i = 0, j = 0;
     int contacts_num = 0;     //联系人个数
     char show = ‘n‘;          //判断是否需要打印出联系人
     char back = ‘n‘;          //判断操作是否要继续
     int sign = 0;
     p_book *count = NULL;
     p_book *src = NULL;
     p_book *print_src = NULL;
     p_book tmp[] = { 0 };
    
     while (back != ‘y‘)
     {
          contacts_num = 0;
          count = contacts_parr;
          src = contacts_parr;
          print_src = contacts_parr;
        
          while (count->order_num > 0)
          {
               contacts_num++;
               count++;
          }
          
          for (i = 0; i < contacts_num - 1; i++)
          {
               sign = 0;
               for (j = 0; j < contacts_num - i - 1; j++)
               {
                    if (strcmp(src[j].name, src[j + 1].name) > 0)
                    {
                         sign = 1;
                         *tmp = src[j];
                         src[j] = src[j + 1];
                         src[j + 1] = *tmp;
                         src[j].order_num = src[j].order_num - 1;
                         src[j + 1].order_num = src[j + 1].order_num + 1;
                    }
               }
               if (!sign)
               {
                    break;
               }
         }
  
          /*清空tmp*/
          /*src[j].order_num = 0;
          strcpy(src[j].name, "\0");
          strcpy(src[j].sex, "\0");
          src[j].age = 0;
          strcpy(src[j].phone_numb, "\0");
          strcpy(src[j].addr, "\0");*/
        
          printf("\n\n\t排序完成!!!\n");
          
          printf("\n\t是否需要显示所有联系人(y)? :");
          fflush(stdin);
          scanf("%c", &show);
        
          if (show == ‘y‘)
          {
               while (print_src->order_num != 0)
               {
                    printf("\n\n\t序号:%d\t", print_src->order_num);
                    printf("\n\t\t  姓名:%s\t", print_src->name);
                    printf("\t性别:%s", print_src->sex);
                    printf("\t年龄:%d\n", print_src->age);
                    printf("\t\t  电话号码:%s", print_src->phone_numb);
                    printf("\t地址:%s", print_src->addr);
                    
                    print_src++;
               }
          }
          
          printf("\n\n\t返回主菜单(y)? :");
          fflush(stdin);
          scanf("%c", &back);
          printf("\n\n");
     }

     return (1);
}

/*修改联系人*/
int revise_contacts(p_book *contacts_parr)
{
     assert(contacts_parr);
    
     char revise_name[20] = "\0";
     char revise_sex[6] = "\0";
     char revise_phone_numb[12] = "\0";
     char revise_addr[50] = "\0";
     char loop = ‘y‘;             //判断操作是否要继续
     p_book *src = NULL;
    
     while (loop == ‘y‘)
     {
          src = contacts_parr;
          
          printf("\n\n\t请输入被修改人的姓名:");
          scanf("%s", revise_name);
         
          while (strcmp(src->name, revise_name) != 0)
          {
               if (0 == src->order_num)
               {
                    printf("\n\t无此人的信息!\n");
                    break;
               }
               src++;
          }
          
          if (0 != src->order_num)
          {
               show_one_contact(src);         //显示需要修改联系人原来的信息
            
               printf("\n\n\t请输入该联系人的新信息:\n");
            
               printf("\tname:");
               scanf("%s", revise_name);
               strcpy(src->name, revise_name);
               printf("\tsex:");
               scanf("%s", revise_sex);
               strcpy(src->sex, revise_sex);
               printf("\tage:");
               fflush(stdin);
               scanf("%d", &src->age);
            
               printf("\tphone_num:");
               scanf("%s", revise_phone_numb);
               strcpy(src->phone_numb, revise_phone_numb);
            
               printf("\taddr:");
               scanf("%s", revise_addr);
               strcpy(src->addr, revise_addr);
            
               printf("\n\t修改成功!!!\n\n");
          }
         
          printf("\n\n\t继续修改(y)/ 返回主菜单(n)? :");
          fflush(stdin);
          scanf("%c", &loop);
         
          printf("\n\n");
     }
     return (1);
}

/*查找方式*/
void lookup_style()
{
     printf("\n\n\t    *-*-*-*-*-*-*-*-*-*\n");
     printf("\t    *- 1.按姓名查找  -*\n");
     printf("\t    *- 2.按电话查找  -*\n");
     printf("\t    *-*-*-*-*-*-*-*-*-*\n");
}

/*查找联系人*/
int lookup_contacts(p_book *contacts_parr)
{
     assert(contacts_parr);
     int choose = 0;
     char lookup_name[20] = "\0";
     char input_phone_numb[12] = "\0";
     char loop = ‘y‘;             //判断操作是否要继续
     p_book *src = NULL;
     
     while (loop == ‘y‘)
     {
          src = contacts_parr;
          
          lookup_style();
         
          printf("\n\t请选择:");
          fflush(stdin);
          scanf("%d", &choose);
      
          if (1 == choose)           //按姓名查找
          {
               printf("\n\t请输入被查找人的姓名:");
               scanf("%s", lookup_name);
              
               while (strcmp(src->name, lookup_name) != 0)
               {
                    if (0 == src->order_num)
                    {
                         printf("\n\t无此人的信息!\n");
                         break;
                    }
                    src++;
               }
                if (0 != src->order_num)
                    show_one_contact(src);
           }
     
          else if (2 == choose)       //按电话号码查找
          {
               printf("\n\t请输入被查找人的电话号码:");
               scanf("%s", input_phone_numb);
               
               while (strcmp(src->phone_numb, input_phone_numb) != 0)
               {
                    if (0 == src->order_num)
                    {
                         printf("\n\t无此人的信息!\n");
                         break;
                    }
                    src++;
               }
                if (0 != src->order_num)
                    show_one_contact(src);
           }
          else
          {
               printf("\n\t输入的选项不存在!!!\n\n");
          }
     
          printf("\n\n\t继续查找(y)/ 返回主菜单(n) ? :");
          fflush(stdin);
          scanf("%c", &loop);
          printf("\n\n");
     }

 return (1);
}

/*删除联系人*/
int del_contacts(p_book *contacts_parr)
{
     assert(contacts_parr);
     
     char del_name[20] = "\0";
     char loop = ‘y‘;             //判断操作是否要继续
     char y_n_delete;            //判断是否删除
     p_book tmp[] = { 0 };   //交换临时变量
     p_book *src = NULL;
    
     while (loop == ‘y‘)
     {
          src = contacts_parr;
          
          printf("\n\n\t请输入要删除人的姓名:");
          scanf("%s", del_name);
          
          while (strcmp(src->name, del_name) != 0)
          {
               if (0 == src->order_num)
               {
                    printf("\n\t无此人的信息!\n");
                    return (1);
               }
               src++;
          }
          
          /*显示该联系人的信息*/
          show_one_contact(src);
          
          /*是否确定删除*/
          printf("\n\t确定是否删除(y/n)? : ");
          fflush(stdin);
          scanf("%c", &y_n_delete);
         
          if (y_n_delete == ‘y‘)
          {
               /*将该联系人信息清除*/
               src->order_num = 0;
               strcpy(src->name, "\0");
               strcpy(src->sex, "\0");
               src->age = 0;
               strcpy(src->phone_numb, "\0");
               strcpy(src->addr, "\0");
              
               printf("\n\t联系人已删除!!!\n");
          }
          else
          {
               return (1);
          }
          
          /*删除该联系人之后,如果后边还有联系人,则向前移动*/
          while ((++src)->order_num > 0)
          {
               *tmp = *(--src);
               *src = *(src + 1);
               src++;
               *src = *tmp;
               
               (src - 1)->order_num = (src - 1)->order_num - 1;
          }
          
          printf("\n\n\t继续删除(y)/ 返回主菜单(n) ? :");
          fflush(stdin);
          scanf("%c", &loop);
          printf("\n\n");
     }
     
     return (1);
}

/*添加联系人*/
int add_contacts(p_book *contacts_parr)
{
     assert(contacts_parr);
     
     char loop = ‘y‘;             //判断操作是否要继续
     char input_name[20] = "\0";
     char sex[6] = "\0";
     char phone_numb[12] = "\0";
     char addr[50] = "\0";
     p_book *src = NULL;
    
     while (loop == ‘y‘)
     {
          src = contacts_parr;
          
          while (src->order_num > 0)
          {
               src++;
          }
         
          src->order_num = ((src - 1)->order_num) + 1;     //序号加一
         
          printf("\tname:");
          scanf("%s", input_name);
          strcpy(src->name, input_name);
        
          printf("\tsex:");
          scanf("%s", sex);
          strcpy(src->sex, sex);

          printf("\tage:");
          scanf("%d", &src->age);
        
          printf("\tphone_num:");
          scanf("%s", phone_numb);
          strcpy(src->phone_numb, phone_numb);

          printf("\taddr:");
          scanf("%s", addr);
          strcpy(src->addr, addr);

          printf("\n\n\t继续存入(y)/ 返回主菜单(n) ? :");
          fflush(stdin);
          scanf("%c", &loop);
          printf("\n\n");
     }
     
    return (1);
}

/*系统菜单*/
int phone_book_menu(int option)
{
     printf("\n\t              -  phone_book_menu  -             \n\n");
     printf("\t    *-*-*-*-*-*-*-*-*-*");
     printf("\t    *-*-*-*-*-*-*-*-*-*\n");
     printf("\t    *- 1.添加联系人  -*");
     printf("\t    *- 2.删除联系人  -*\n");
     printf("\t    *-*-*-*-*-*-*-*-*-*");
     printf("\t    *-*-*-*-*-*-*-*-*-*\n\n");
     printf("\t    *-*-*-*-*-*-*-*-*-*");
     printf("\t    *-*-*-*-*-*-*-*-*-*\n");
     printf("\t    *- 3.查找联系人  -*");
     printf("\t    *- 4.修改联系人  -*\n");
     printf("\t    *-*-*-*-*-*-*-*-*-*");
     printf("\t    *-*-*-*-*-*-*-*-*-*\n\n");
     printf("\t    *-*-*-*-*-*-*-*-*-*");
     printf("\t    *-*-*-*-*-*-*-*-*-*\n");
     printf("\t    *- 5.显示联系人  -*");
     printf("\t    *- 6.排序联系人  -*\n");
     printf("\t    *-*-*-*-*-*-*-*-*-*");
     printf("\t    *-*-*-*-*-*-*-*-*-*\n\n");
     printf("\t    *-*-*-*-*-*-*-*-*-*");
     printf("\t    *-*-*-*-*-*-*-*-*-*\n");
     printf("\t    *- 7.清空联系人  -*");
     printf("\t    *- 0.退出        -*\n");
     printf("\t    *-*-*-*-*-*-*-*-*-*");
     printf("\t    *-*-*-*-*-*-*-*-*-*\n\n");
    
     printf("\t请选择:");
     scanf("%d", &option);
    
     if (option >= 0 && option <= 7)
     {
          return (option);
     }
     else
     {
          printf("\t对不起,您的输入有误!!!\n");
          return (-1);
     }
}

/*帮助菜单*/
void help_menu()
{
     printf("\n\n\t|*|*|*|*|*|*|*|*|*|—— 欢迎使用XX通讯录 ——|*|*|*|*|*|*|*|*|\n");
     printf("\t|*|                                                        |*|\n");
     printf("\t|*|  tips:                                                 |*|\n");
     printf("\t|*|      1. 姓名最大有效字符长度为19个  (a-z / A-Z)      |*|\n");
     printf("\t|*|      2. 性别最大有效字符长度为5个   (man /woman)     |*|\n");
     printf("\t|*|      3. 手机号最大有效字符长度为11个(0— —9)        |*|\n");
     printf("\t|*|      4. 地址最大有效字符长度为49个  (a-z & A-Z)      |*|\n");
     printf("\t|*|                                                        |*|\n");
     printf("\t||*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|\n\n");
    
     printf("\t\t\t\t\t\t\t   继续... ...");
    
     getch();

     printf("\n\n\n\n");
}

------------------

main.c(主函数)

------------------

int main()
{
     int loop = 1;    //循环判断条件
     int option;                          //选项
     p_book contacts_arr[MAX] = { 0 };
    
     help_menu();
    
     while (loop)
     {
          option = -1;
        
          while (option == -1)
          {
               option = phone_book_menu(option);    //菜单
          }
        
          switch (option)
          {
          case _add_contacts:
               loop = add_contacts(contacts_arr);
               break;
          case _del_contacts:
               loop = del_contacts(contacts_arr);
               break;
          case _lookup_contacts:
               loop = lookup_contacts(contacts_arr);
               break;
          case _revise_contacts:
               loop = revise_contacts(contacts_arr);
               break;
          case _show_contacts:
               loop = show_contacts(contacts_arr);
               break;
          case _sort_contacts:
               loop = sort_contacts(contacts_arr);
               break;
          case _empty_contacts:
               loop = empty_contacts(contacts_arr);
               break;
          case _exit_sys:
               loop = 0;
               printf("\n\n\t    *-*-*-*-*-*--*-  goodbye -*-*--*-*-*-*-*-*-*\n\n\n\n");
               break;
          default:
               loop = 0;
               break;
          }
     }
      
     system("pause");
     return (0);
}

 

 程序运行时的部分截图:

 

 1:帮助界面

技术分享


2:主菜单

技术分享


3:排序前的联系人

技术分享


4:排序后的联系人

技术分享

 


本文出自 “无名小卒” 博客,请务必保留此出处http://814193594.blog.51cto.com/10729329/1717815

【简易通讯录】——C语言实现

标签:c语言   结构体   通讯录   

原文地址:http://814193594.blog.51cto.com/10729329/1717815

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!