模拟通讯录

c语言模拟

  1 #include<stdio.h>
  2 #include<string.h>
  3 #define NAME_MAX 15
  4 #define SEX_MAX 3
  5 #define AGE_MAX 100
  6 #define TEL_MAX 15
  7 #define ADDR_MAX 20
  8 #define PEOPLE_MAX 1000
  9 
 10 typedef struct LINK
 11 {
 12     char name[15];
 13     char sex[SEX_MAX];
 14     int age;
 15     char tel[TEL_MAX];
 16     char addr[ADDR_MAX];
 17 }linkman;
 18 
 19 typedef struct PEOPLE
 20 {
 21     int num;
 22     linkman count[1000];
 23 }*people;
 24 
 25 
 26 //在通讯录里模糊搜索遍历到指定联系人
 27 static int _search(people cou, const char *name)
 28 {
 29     int number = 0;
 30 
 31     for (int i = 0; i < cou->num; i++)
 32     {
 33         if ((strstr((cou->count[i]).name, name) != NULL) || (strstr((cou->count[i]).tel, name) != NULL))
 34         {
 35             printf("%s\t%s\t%d\t%s\t%s\t",
 36                 (cou->count[i]).name,
 37                 (cou->count[i]).sex,
 38                 (cou->count[i]).age,
 39                 (cou->count[i]).tel,
 40                 (cou->count[i]).addr);
 41             printf("\n");
 42             number++;
 43         }
 44     }
 45 
 46     if (number == 0)
 47     {
 48         printf("此联系人不存在,查找失败!\n");
 49     }
 50     return 0;
 51 }
 52 
 53 //在通讯录里遍历到指定联系人
 54 static int search(people cou, const char *name)
 55 {
 56     for (int i = 0; i < cou->num; i++)
 57     {
 58         if (strcmp(name, (cou->count[i]).name) == 0)
 59         {
 60             return i;
 61         }
 62     }
 63 
 64     return -1;
 65 }
 66 
 67 
 68 //添加联系人
 69 void add_linkman(people cou)
 70 {
 71     if (cou->num == 1000)
 72     {
 73         printf("通讯录已满,无法录入信息!\n");
 74     }
 75 
 76     printf("please input name:\n");
 77     scanf("%s", (cou->count[cou->num]).name);
 78     printf("please input sex:\n");
 79     scanf("%s", (cou->count[cou->num]).sex);
 80     printf("please input age:\n");
 81     scanf("%d", &(cou->count[cou->num]).age);
 82     printf("please input telephone:\n");
 83     scanf("%s", (cou->count[cou->num]).tel);
 84     printf("please input address:\n");
 85     scanf("%s", (cou->count[cou->num]).addr);
 86 
 87     cou->num++;
 88 }
 89 
 90 //删除联系人
 91 void delete_linkman(people cou)
 92 {
 93     char name[NAME_MAX];
 94     int i = 0;
 95 
 96     if (cou->num == 0)
 97     {
 98         printf("通讯录已空,无法删除信息!\n");
 99         return;
100     }
101 
102     printf("请输入要删除人的名字:");
103     scanf("%s", &name);
104     printf("\n");
105 
106     int ret = search(cou, name);//找到要删除的联系人
107 
108     if (ret == -1)
109     {
110         printf("此人不存在,删除失败!\n");
111     }
112     else
113     {
114         for (i = ret; i<cou->num; i++)
115         {
116             cou->count[i] = cou->count[i + 1];
117         }
118         cou->num--;
119         printf("删除成功!\n");
120     }
121 }
122 
123 //修改指定联系人信息
124 void change_linkman(people cou)
125 {
126     char name[NAME_MAX];
127     int i = 0;
128     printf("请输入要修改人的名字:");
129     scanf("%s", name);
130 
131     if (cou->num == 0)
132     {
133         printf("当前通讯录为空,修改失败!\n");
134         return;
135     }
136 
137     int ret = search(cou, name);//找到要修改信息的联系人
138 
139     if (ret != -1)
140     {
141         printf("please input name again:\n");
142         scanf("%s", (cou->count[ret]).name);
143         printf("please input the sex:\n");
144         scanf("%s", (cou->count[ret]).sex);
145         printf("please input the age:\n");
146         scanf("%d", &(cou->count[ret]).age);
147         printf("please input the telephone:\n");
148         scanf("%s", (cou->count[ret]).tel);
149         printf("please input the address:\n");
150         scanf("%s", (cou->count[ret]).addr);
151     }
152     printf("修改信息成功!\n");
153 }
154 
155 //查找联系人
156 void find_linkman(people cou)
157 {
158     char name[NAME_MAX];
159     int i = 0;
160 
161     printf("请输入要查找人的名字或电话号码:");
162     scanf("%s", name);
163 
164     if (cou->num == 0)
165     {
166         printf("当前通讯录为空,查找失败!\n");
167         return;
168     }
169 
170     _search(cou, name);//找到要查找的联系人
171 
172 }
173 
174 //显示所有联系人信息
175 void display_linkman(people cou)
176 {
177     int i = 0;
178     if (cou->num == 0)
179     {
180         printf("当前通讯录为空,显示失败!\n");
181         return;
182     }
183 
184     for (i = 0; i<cou->num; i++)
185     {
186         printf("%s\t%s\t%d\t%s\t%s",
187             (cou->count[i]).name,
188             (cou->count[i]).sex,
189             (cou->count[i]).age,
190             (cou->count[i]).tel,
191             (cou->count[i]).addr);
192         printf("\n");
193     }
194 }
195 
196 void empty_linkman(people cou)//清空所有联系人
197 {
198     cou->num = 0;
199 }
200 
201 struct stu
202 {
203     char name[15];
204 };
205 
206 void name_sort(people cou)//按名字顺序排序所有人
207 {
208     int i = 0;
209     int j = 0;
210     if (cou->num == 0)
211     {
212         printf("当前通讯录为空,排序失败!\n");
213         return;
214     }
215 
216     for (i = 0; i<cou->num - 1; i++)//冒泡实现联系人的排序
217     {
218         for (j = 0; j<cou->num - i - 1; j++)
219         {
220             if (strcmp(cou->count[j].name, (cou->count[j + 1]).name)>0)
221             {
222                 linkman tmp = cou->count[j];
223                 cou->count[j] = cou->count[j + 1];
224                 cou->count[j + 1] = tmp;
225             }
226         }
227     }
228 
229     printf("排序成功!\n");
230 }
231 
232 //菜单界面
233 void menu()
234 {
235     printf("************************************\n");
236     printf("*********1.添加联系人信息***********\n");
237     printf("*********2.删除指定联系人信息*******\n");
238     printf("*********3.查找指定联系人信息*******\n");
239     printf("*********4.修改指定联系人信息*******\n");
240     printf("*********5.显示所有联系人信息*******\n");
241     printf("*********6.清空所有联系人***********\n");
242     printf("*********7.以名字排序所有联系人*****\n");
243     printf("*********0.退出*********************\n");
244     printf("************************************\n");
245 }
246 
247 int main()
248 {
249     int opp = 1;
250     struct PEOPLE cou;
251     cou.num = 0;
252 
253     while (opp)
254     {
255         menu();
256         printf("请选择>:");
257         scanf("%d", &opp);
258         switch (opp)
259         {
260         case 0:
261             break;
262         case 1:
263             add_linkman(&cou);
264             break;
265         case 2:
266             delete_linkman(&cou);
267             break;
268         case 3:
269             find_linkman(&cou);
270             break;
271         case 4:
272             change_linkman(&cou);
273             break;
274         case 5:
275             display_linkman(&cou);
276             break;
277         case 6:
278             empty_linkman(&cou);
279             break;
280         case 7:
281             name_sort(&cou);
282             break;
283         default:
284         {
285                    printf("输入有误,请重新输入>:\n");
286                    break;
287         }
288         }
289     }
290 
291     getchar();
292     return 0;
293 }
View Code

参考

https://blog.csdn.net/yanxiaolx/article/details/53335599

posted @ 2019-08-26 01:55  wxwreal  阅读(402)  评论(0编辑  收藏  举报