C语言如何实现手机电话簿管理系统
更新:HHH   时间:2023-1-7


本篇内容介绍了“C语言如何实现手机电话簿管理系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、任务概述

用c设计出模拟手机通讯录管理系统,实现对手机中的通讯录进行管理

二、设计功能

查看功能:选择此功能时,列出下列三类选择。A办公类,B个人类,C商务类,当选中某类时,显示出此类中的所有数据。
增加功能:能录入新数据,一个结点包括:姓名、电话号码、分类(可选项有:A 办公类,B 个人类,C 商务类)、电子邮件。
拨号功能:能显示出通讯录中所有人的姓名,当选中某个姓名时,屏幕上模拟打字机的效果依次显示出此人的电话号码中的各个数字,并伴随相应的拨号声音。
修改功能:选中某个人的姓名时,可对此人的相应数据进行修改。
删除功能:选中某个人的姓名时,可对此人的相应数据进行删除,并自动调整后续条目的编号。

三、功能展示

四、思维导图

五、程序源码

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define N 15                                   //最大容量为15
typedef struct                                //定义一个结构
{
  char name[10];
  char haoma[15];
     char leibie[10];
     char dizhi[20];
}ren;
//菜单
void menu()                                           //目录
{
  system("cls");                                      //清屏
    printf("\n\n\n ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓");
    printf("                    ┃                                                        ┃");
    printf("                    ┃                                                        ┃");
    printf("                    ┃                                                        ┃");
    printf("                    ┃                                                        ┃");
    printf("                    ┃                                                        ┃");
    printf("                    ┃                                                        ┃");
    printf("                    ┃                                                        ┃");
    printf("                    ┃              ┏━━━━━━━━━━┓                  ┃"); 
    printf("\n ┃              ┃ 欢迎使用手机通讯录 ┃                  ┃ \n");
    printf(" ┃              ┗━━━━━━━━━━┛                  ┃");
    printf(" ┃              ┏━━━━━━━━━━┓                  ┃\n");
    printf(" ┃              ┃                    ┃                  ┃\n");
    printf(" ┃              ┃                    ┃                  ┃\n");
    printf(" ┃              ┃     1.添加联系人   ┃                  ┃\n");
    printf(" ┃              ┃     2.查询联系人   ┃                  ┃\n");
    printf(" ┃              ┃     3.修改联系人   ┃                  ┃\n");
    printf(" ┃              ┃     4.删除联系人   ┃                  ┃\n");
    printf(" ┃              ┃     5.拨号         ┃                  ┃\n");
    printf(" ┃              ┃     0.退出         ┃                  ┃\n");
    printf(" ┃              ┃                    ┃                  ┃\n");
    printf(" ┃              ┃                    ┃                  ┃\n");  
    printf(" ┃              ┗━━━━━━━━━━┛                  ┃\n");
    printf(" ┃                                                        ┃\n");
    printf(" ┃                                                        ┃\n");
    printf(" ┃                                                        ┃\n");
    printf(" ┃                                                        ┃\n");
    printf(" ┃                                                        ┃\n");
    printf(" ┃                                                        ┃\n");
    printf(" ┃                                                        ┃\n");
    printf(" ┃                                                        ┃\n");
    printf(" ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛         ");
    printf("\n");

   printf("请选择相应的功能:");
} 
                                                 //号码添加
void input()
{
     FILE *fp;                                         //定义文件指针
     ren ry,s;                                        // 定义两个ren结构体的量
     if((fp=fopen("ren.dat","ab+"))==NULL)
  {printf("can't open file!\n");exit(0);}
     printf("\n录入格式\n姓名:\n号码:\n类别:\n邮箱:\n当输入姓名为#结束录入\n");
     while(1)
  {  printf("请输入姓名:");
     scanf("%s",ry.name);
      if(strcmp(ry.name,"#")==0) break;               //如果输入#则退出添加
           printf("请输入电话号码:");
         scanf("%s",ry.haoma);
         printf("请输入类别(A办公类B个人类C商务类):");
         scanf("%s",ry.leibie);
         printf("请输入邮箱地址:");
         scanf("%s",ry.dizhi);
         fseek(fp,0,0);                                //指针定位到文件开始位置
       fread(&s,sizeof(ren),1,fp);
       while(!feof(fp))
    {       if(strcmp(s.haoma,ry.haoma)==0)
      {printf("号码已经存在,不能记录.\n");break;}//遇到有相同的号码
            fread(&s,sizeof(ren),1,fp);
  }
       if(feof(fp))                     //读到自后也没有相同的号码
          fwrite(&ry,sizeof(ren),1,fp); //将信息写入文件
  }
     fclose(fp);                         //关闭文件
     getchar();                          //接受一个输入
}
                                         

void search()                             //查询
{
ren ry;
FILE *fp;
char leibie[10];
system("cls");
printf("\n输入要查人员的类别: ");scanf("%s",leibie);//要查询的类别

    if((fp=fopen("ren.dat","rb"))==NULL) {printf("can't open file!\n");exit(0);}
        fread(&ry,sizeof(ren),1,fp);
   if(!feof(fp))
   {   printf("    人员的信息如下:\n");
       printf("说明:(A办公类B个人类C商务类)");
    printf("\n%10s %15s %10s %20s \n","姓名","号码","类别","邮箱");
while(!feof(fp))
{
      if(strcmp(ry.leibie,leibie)==0)                //若为该类别则输出
   { 
      printf("%10s %15s %10s %20s \n",ry.name,ry.haoma,ry.leibie,ry.dizhi);
   }
   fread(&ry,sizeof(ren),1,fp);
}
   }
   else printf("查找失败!!!");
   fclose(fp);
   getch();
}


      //修改:输入人员的姓名,查找该人员,若找到则修改该人员的信息,并显示修改前后的结果;
void modi()
{   ren ry;
FILE *fp;
char name[10];
system("cls");
if((fp=fopen("ren.dat","rb+"))==NULL) {printf("can't open file!\n");exit(0);}
   if(!feof(fp))
   {printf("   修改前全部人员的信息如下:\n");
  printf("\n%10s %15s %10s %20s \n","姓名","号码","类别","邮箱"); 
  fread(&ry,sizeof(ren),1,fp);   
while(!feof(fp))
   { 
            
       printf("%10s %15s %10s %20s \n",ry.name,ry.haoma,ry.leibie,ry.dizhi);
        fread(&ry,sizeof(ren),1,fp);
     }
   }
printf("\n输入要修改人员的姓名: ");scanf("%s",name);
    if((fp=fopen("ren.dat","rb+"))==NULL) {printf("can't open file!\n");exit(0);}
   while(!feof(fp))
   {  fread(&ry,sizeof(ren),1,fp);
      if(strcmp(ry.name,name)==0)            
   {  printf("   修改前人员的信息如下:\n");
      printf("\n%10s %15s %10s %20s \n","姓名","号码","类别","邮箱");
         printf("%10s %15s %10s %20s \n",ry.name,ry.haoma,ry.leibie,ry.dizhi);
         printf("\n请输入该人的号码:");scanf("%s",ry.haoma);
         printf("\n类别:");scanf("%s",ry.leibie);               //新信息输入
         printf("\n邮箱:");scanf("%s",ry.dizhi);
      break;                                               //修改完毕退出
   }
   }
   if(!feof(fp))
   {  
      printf("  \n 修改后的人的信息如下:\n");
   printf("\n%10s %15s %10s %20s \n","姓名","号码","类别","邮箱");
      printf("%10s %15s %10s %20s \n",ry.name,ry.haoma,ry.leibie,ry.dizhi);
      fseek(fp,-sizeof(ren),1);                                //指针向前一位定位
      fwrite(&ry,sizeof(ren),1,fp);
   }
   else
printf("\n此人不存在,修改失败!!");
   fclose(fp);
   getch();
}
     //删除:输入人员姓名,若该人员存在,则删除,并显示删除后余下的人员的信息

void dele()
{   ren  ry[N];
    FILE *fp;
    char name[10];
    int i,len,k=-1;
    system("cls");
    printf("\n输入要删除的人员姓名: ");scanf("%s",name);
    i=0;
    if((fp=fopen("ren.dat","rb+"))==NULL) {printf("can't open file!\n");exit(0);}
    while(!feof(fp))
{     fread(&ry[i],sizeof(ren),1,fp);
          if(feof(fp)) break;
          if(strcmp(ry[i].name,name)==0) k=i;
          i++;
}
    fclose(fp);
                          //以下是删除后重新写文件
   len=i;
   if(k>=0)
   {    if((fp=fopen("ren.dat","wb+"))==NULL) {printf("can't open file!\n");exit(0);}
        for(i=0;i<len;i++)
  {   if(i==k) continue;
            fwrite(&ry[i],sizeof(ren),1,fp);
  }
        fclose(fp);printf("删除成功!!!\n");
   }
   
   else printf("删除失败!!!\n");
   getch();
}

void bohao() //拨号
{    ren ry;
     FILE *fp;
     char name[10];
  system("cls");
  printf("\n输入要拨人员的姓名: ");scanf("%s",name);
     if((fp=fopen("ren.dat","rb"))==NULL) {printf("can't open file!\n");exit(0);}
     while(!feof(fp))
  {     fread(&ry,sizeof(ren),1,fp);
           if(strcmp(ry.name,name)==0)
     {  printf("\n\a\a\a\a\a\a %15s\a\a\a\a\a\n",ry.haoma);   //输出号码和声音
            break;
     }
  }
  if(feof(fp)) printf("拨号失败!!!");
     fclose(fp);
     getch();
}
//菜单选择

void choice()
{ 
char ch;
while(1)
{
  menu();// 显示菜单
  ch=getch();//选择菜单
if(ch=='0') break;
  switch(ch)//判断功能项
  {
  case '1': input();break;
  case '2':search();break;
  case '3':modi();break;
  case '4':dele();break;
  case '5':bohao();break;
  }
}
printf("\n欢迎使用!!!\n");
}

//主函数
main()
{
choice();//调用菜单选择

}

“C语言如何实现手机电话簿管理系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!

返回开发技术教程...