服务器之家

服务器之家 > 正文

C语言设计简易电话簿

时间:2021-08-09 12:28     来源/作者:芥末味鸭肠

本文实例为大家分享了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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <getch.h>
 
typedef struct Contact{ //定义联系人结构体
 char name[20]; //姓名
 char sex; //性别
 char tel[12]; //电话
}Contact;
 
Contact contacts[100];
 
void show_contact(Contact* conp){ //显示联系人信息
 printf("姓名:%s\t性别:%s\t电话:%s\n",conp->name,'w'==conp->sex?"女":"男",conp->tel);
}
 
void scan_contact(Contact* conp){ //输入联系人信息
 printf("请输入姓名,性别(w:女m:男),电话:");
 scanf("%s%s%s",conp->name,&conp->sex,conp->tel);
}
 
void add_contacts(void){ //添加联系人
 for(int i=0;i<100;i++){
 if(0 == contacts[i].sex){
  scan_contact(contacts+i);
  printf("添加成功!\n");
  return;
 }
 }
 printf("添加人已满\n");
}
 
void del_contacts(void){ //删除联系人
 char str[20] = {};
 printf("请输入删除人姓名:\n");
 scanf("%s",str);
 for(int i=0;i<100;i++){
 if(0 == strcmp(str,contacts[i].name)){
  contacts[i].sex = 0;
  printf("删除联系人成功\n");
  return;
 }
 }
 printf("联系人不存在\n");
}
void find_contacts(void){ //查找联系人
 char str[20] = {};
 printf("请输入要查询的手机号\n");
 scanf("%s",str);
 getchar();
 for(int i=0;i<100;i++){
 if(strstr(contacts[i].tel,str)){
  show_contact(contacts+i);
 }
 }
 printf("请输入任意键继续...\n");
 getch();
}
 
void list_contacts(void){ //显示联系人信息
 for(int i=0;i<100;i++){
 if(contacts[i].sex){
  show_contact(contacts+i);
 }
 }
 printf("请输入任意键继续...\n");
 getch();
 
}
 
void change_contacts(void){ //修改联系人信息
 char str[20] = {};
 printf("请输入要修改的联系人姓名:\n");
 scanf("%s",str);
 for(int i=0;i<100;i++){
 if(0 == strcmp(str,contacts[i].name)){
  show_contact(contacts+i);
  scan_contact(contacts+i);
  return;
 }
 }
 printf("没有找到要修改的联系人");
}
char menu(void){
 system("clear");
 printf("欢迎使用电话蒲\n");
 printf("--------------\n");
 printf("1、添加联系人 \n");
 printf("2、删除联系人\n");
 printf("3、修改联系人信息\n");
 printf("4、查找联系人\n");
 printf("5、显示所有联系人\n");
 printf("--------------\n");
 printf("请输入指令:");
 char cmd = getch();
 printf("%c\n",cmd);
 return cmd;
}
 
int main(){
 while(true){
 switch(menu()){
  case '1':add_contacts(); break;
  case '2':del_contacts(); break;
  case '3':change_contacts(); break;
  case '4':find_contacts(); break;
  case '5':list_contacts(); break;
  //case '6':exit(); break;
  default: printf("cmd error!\n");
 }
 }
}
//------------------------------------总结------------------------------------------
//添加与删除联系人的突破口:可以选择性别的返回值来实现添加与删除。
//查找联系人 strstr()函数的作用:
//strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
//因此查找联系人时便可只打出电话的一部分就能查找到联系人。
 
//该程序的弊端:不能每次打开就有之前保存的联系人。
//优化: 可以将联系人保存到文件中,并且在程序打开的时候打开文件。

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

原文链接:https://blog.csdn.net/weixin_43505112/article/details/89601037

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部