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

时间:2022-10-19 07:25:43

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

这两天用C语言编写了一个简易版通讯录(学生信息管理) ,大致功能有添加信息查看信息(自动按姓名排序,printf输出带颜色字体),查找信息(按姓名查找),删除信息(输入姓名删除相关信息),修改信息(输入修改人的名字,可选择修改其任意信息)和退出

?
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 100
typedef struct student STU;
int person = 0;
 
struct student
{
 char name[10];
 int num;
 int age;
};
 
void welcome()
{
 system("clear");
 printf("\n\n\n\n\t\t\t========================================");
 printf("\n\n\t\t\t\t\tWelcome!\n");
 sleep(3);
}
 
void menu()
{
 system("clear");
 printf("\n\n\t************************************************************************");
 printf("\n\t\t\t\t\t请选择:");
 printf("\n\t\t\t\t\t1.添加信息");
 printf("\n\t\t\t\t\t2.查看信息");
 printf("\n\t\t\t\t\t3.查找信息");
 printf("\n\t\t\t\t\t4.删除信息");
 printf("\n\t\t\t\t\t5.修改信息");
 printf("\n\t\t\t\t\t6.退出");
 printf("\n\t************************************************************************");
 
}
 
/*添加信息*/
void AddInfo(STU *s[])
{
 system("clear");
 printf("姓名 学号 年龄\n");
 printf("------------------------\n");
 printf("请输入信息:(bye结束添加)\n");
 while(1)
 {
  s[person] = (STU*)malloc(sizeof(STU));
  if(NULL == s[person])
  {
   printf("malloc failure!\n");
  }
  scanf("%s", s[person]->name);
  if(!strcmp(s[person]->name, "bye"))
  {
   break;
  }
  scanf("%d%d", &s[person]->num, &s[person]->age);
  getchar();
  person++;
 }
}
 
/*查看信息(按姓名排序)*/
void ShowAll(STU *s[])
{
 system("clear");
 int i, j;
 STU *q[1] = {0};
 q[0] = (STU *)malloc(sizeof(STU));
 
 
 printf("information:\n");
 
 for(i = 0; i < person; i++)
 {
  for(j = 0; j < person - 1 - i; j++)
  {
   if(strcmp(s[j]->name, s[j + 1]->name) > 0)
   {
    q[0] = s[j];
    s[j] = s[j + 1];
    s[j + 1] = q[0];
   }
  }
 }
 
 for(i = 0; i < person; i++)
 {
  printf("\e[1;35mname:%s, num:%d, age:%d\e[0m\n", s[i]->name, s[i]->num, s[i]->age);
 }
 sleep(3);
 getchar();
}
 
/*查找信息*/
 
void Search_name(char *name, STU *s[])
{
 int i, n = 0;
 for(i = 0; i < person; i++)
 {
  if(strcmp(name, s[i]->name) == 0)
  {
   n++;
   printf("name:%s, num:%d, age:%d\n", s[i]->name, s[i]->num, s[i]->age);
  }
 }
 
 if(n == 0)
 {
  printf("不存在!\n");
 }
}
 
void SearchInfo(STU *s[])
{
 system("clear");
 char *name = (char *)malloc(sizeof(char));
 
 printf("请输入要查找的人的名字:\n");
 scanf("%s", name);
 
 Search_name(name, s);
}
 
/*删除信息*/
 
void DeleteInfo(STU *s[])
{
 system("clear");
 int i, n = 0, j;
 char del_name[10];
 
 printf("请输入要删除的人的名字:\n");
 scanf("%s", del_name);
 getchar();
 getchar();
 
 for(i = 0; i < person; i++)
 {
  if(strcmp(del_name, s[i]->name) == 0)
  {
   n++;
   free(s[i]);
   for(j = i; j < person - 1; j++)
   {
    strcpy(s[j]->name, s[j + 1]->name);
    s[j]->num = s[j + 1]->num;
    s[j]->age = s[j + 1]->age;
   }
   person--;
  }
 }
 
 if(n == 0)
 {
  printf("要删除的人不存在!\n");
 }
 else
 {
  printf("删除成功!\n");
 }
}
 
/*修改信息*/
void Change_name(char *name, STU *s[])
{
 int i, n = 0, choice;
 char *new_name = (char *)malloc(sizeof(char));
 int new_num, new_age;
 
 for(i = 0; i < person; i++)
 {
  if(strcmp(name, s[i]->name) == 0)
  {
   n++;
   printf("该学生的信息如下:");
   printf("name:%s, num:%d, age:%d\n", s[i]->name, s[i]->num, s[i]->age);
   printf("----------------------------\n");
   printf("请选择要修改的内容(1.name 2.num 3.age):\n");
   scanf("%d", &choice);
   switch(choice)
   {
    case 1:
     printf("请输入新的名字:\n");
     scanf("%s", new_name);
     strcpy(s[i]->name, new_name);
     break;
    case 2:
     printf("请输入新的学号:\n");
     scanf("%d", &new_num);
     s[i]->num = new_num;
     break;
    case 3:
     printf("请输入新的年龄:\n");
     scanf("%d", &new_age);
     s[i]->age = new_age;
     break;
   }
  }
 }
 
 if(n == 0)
 {
  printf("不存在!\n");
 }
}
 
void ChangeInfo(STU *s[])
{
 system("clear");
 char *name = (char *)malloc(sizeof(char));
 
 printf("请输入要修改的人的名字:\n");
 scanf("%s", name);
 
 Change_name(name, s);
 
}
 
int main()
{
 struct student *s[SIZE] = {0};
 int choice;
 
 welcome();
 
 while(1)
 {
  menu();
  printf("\nPlease input choice:");
  scanf("%d", &choice);
 
  switch(choice)
  {
   case 1:
    AddInfo(s);
    break;
   case 2:
    ShowAll(s);
    break;
   case 3:
    SearchInfo(s);
    break;
   case 4:
    DeleteInfo(s);
    break;
   case 5:
    ChangeInfo(s);
    break;
   case 6:
    exit(0);
    break;
  }
 }
 
 return 0;
}

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

原文链接:https://blog.csdn.net/qq_41998576/article/details/81367187