C语言实现通讯录功能

时间:2022-09-13 08:23:16

本文实例为大家分享了C语言实现通讯录功能的具体代码,供大家参考,具体内容如下

先定义头文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef __CONTACT_H__
#define __CONTACT_H__
 
#define NAME_LEN 10
#define SEX_LEN 6
#define TELE_LEN 12
#define ADDR_LEN 20
#define MAX_PEO 1000
 
typedef struct PEO
{
  char name[NAME_LEN];
  int age;
  char sex[SEX_LEN];
  char tele[TELE_LEN];
  char addr[ADDR_LEN];
}Peo, *PPEO;
 
typedef struct Contact
{
  Peo nb[MAX_PEO];
  int count;//记录有效个数
}PCON,*Pcon;
 
void add_peo(Pcon pcon);
void del_peo(Pcon pcon);
void search(Pcon pcon);
void modify(Pcon pcon);
void show(Pcon pcon);
void clear(Pcon pcon);
 
 
#endif

然后创建contact.c,将函数写入

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<stdio.h>
#include <string.h>
#include"contact.h"
 
void add_peo(Pcon pcon)
{
  if (pcon->count <= MAX_PEO)
  {
    printf("请输入姓名:");
    scanf("%s", pcon->nb[pcon->count].name);
    printf("请输入年龄:");
    scanf("%d", &pcon->nb[pcon->count].age);
    printf("请输入性别:");
    scanf("%s", pcon->nb[pcon->count].sex);
    printf("请输入电话:");
    scanf("%s", pcon->nb[pcon->count].tele);
    printf("请输入地址:");
    scanf("%s", pcon->nb[pcon->count].addr);
    pcon->count ++;
    printf("添加成功!\n");
  }
  else
    printf("电话本已满!");
}
 
void show(Pcon pcon)
{
  int i=0;
  printf("姓名 年龄  性别      电话       地址\n");
  for(i=0;i<pcon->count;i++)
  {
    printf("%s%8d%8s%15s%18s\n",pcon->nb[i].name,pcon->nb[i].age,pcon->nb[i].sex,pcon->nb[i].tele,pcon->nb[i].addr);
  }
}
 
int find(char *c,Pcon pcon)
{
  int i=0;
  for(;i<pcon->count;i++)
  {
    if(strcmp(c,pcon->nb[i].name)==0)
      return i;
       
  }
  return -1;
}
void del_peo(Pcon pcon)
{
  char nam[NAME_LEN];
  int index=0;
  printf("请输入你想删除的人:");
  scanf("%s",nam);
  index = find(nam,pcon->nb[index].name);
  for(;index<=pcon->count-1;index++)
  {
    pcon->nb[index]=pcon->nb[index+1];
  }
  pcon->count--;
  printf("删除成功!\n");
}
 
void search(Pcon pcon)
{
  char nam[NAME_LEN];
  int i=0;
  printf("请输入你想查找的人:");
  scanf("%s",nam);
  i = find(nam,pcon->nb[i].name);
  printf("姓名 年龄  性别      电话        地址\n");
  printf("%s%8d%8s%15s%20s\n",pcon->nb[i].name,
      pcon->nb[i].age,pcon->nb[i].sex,
      pcon->nb[i].tele,pcon->nb[i].addr);
}
 
void modify(Pcon pcon)
{
  char nam[NAME_LEN];
  int i=0;
  printf("请输入你想修改的人:");
  scanf("%s",nam);
  i = find(nam,pcon->nb[i].name);
  pcon->nb[i] = pcon->nb[-1];
  printf("请输入姓名:");
  scanf("%s", pcon->nb[i].name);
  printf("请输入年龄:");
  scanf("%d", &pcon->nb[i].age);
  printf("请输入性别:");
  scanf("%s", pcon->nb[i].sex);
  printf("请输入电话:");
  scanf("%s", pcon->nb[i].tele);
  printf("请输入地址:");
  scanf("%s", pcon->nb[i].addr);
  printf("添加成功!\n");
}
 
void clear(Pcon pcon)
{
  pcon->count = 0;
}

最后写入函数主体

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include "contact.h"
 
#define _CRT_SECURE_NO_WARINGS_
void print_menu()
{
  printf("****************************\n");
  printf("*** 1>add    2>del  ***\n");
  printf("*** 3>show   4>search ***\n");
  printf("*** 5>modify  6>clear ***\n");
  printf("****************************\n");
}
 
int main()
{
  PCON con;
  int input = 1;
  con.count = 0;
  while (input)
  {
    print_menu();
    printf("请选择:");
    scanf("%d", &input);
  // system("cls");
    switch (input)
    {
    case 1:
      add_peo(&con);
      break;
    case 2:
      del_peo(&con);
      break;
    case 3:
      show(&con);
      break;
    case 4:
      search(&con);
      break;
    case 5:
      modify(&con);
      break;
    case 6:
      clear(&con);
      break;
    default:
      break;
    }
  }
 
  return 0;
}

下图为通讯录运行示例截图

C语言实现通讯录功能C语言实现通讯录功能C语言实现通讯录功能C语言实现通讯录功能C语言实现通讯录功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/Lolita0917/article/details/45875139