Skip to content

Commit 2f2b400

Browse files
committed
Add AddressBook in c
1 parent 1f6a09c commit 2f2b400

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

c/AddressBook/main.c

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// fishc
2+
// Code Check is required bcs DataEraser did't do that
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
struct Person
8+
{
9+
char name[20];
10+
char phone[40];
11+
struct Person *next;
12+
};
13+
void getInput(struct Person *person);
14+
void printPerson(struct Person *person);
15+
void addPerson(struct Person **contacts);
16+
void changePerson(struct Person *contacts);
17+
void delPerson(struct Person **contacts);
18+
struct Person *findPerson(struct Person *contacts);
19+
void displayContacts(struct Person *contacts);
20+
void releaseContacts(struct Person **contacts);
21+
int main()
22+
{
23+
short code;
24+
struct Person *contacts = NULL;
25+
printf("|Welcome to ContactsBook Manager Program|\n");
26+
printf("|1:insert new Contact-------------------|\n");
27+
printf("|2:find for existing Contact------------|\n");
28+
printf("|3.change existing Contact information--|\n");
29+
printf("|4.delete existing Contact--------------|\n");
30+
printf("|5.display all Contact------------------|\n");
31+
printf("|6.exit---------------------------------|\n");
32+
while (1)
33+
{
34+
printf("Please input the command code:\n");
35+
scanf("%d", &code);
36+
switch (code)
37+
{
38+
case 1: {
39+
addPerson(&contacts);
40+
break;
41+
}
42+
case 2: {
43+
struct Person *person = findPerson(contacts);
44+
if (person != NULL)
45+
{
46+
printPerson(person);
47+
}
48+
else
49+
{
50+
printf("The person is not found\n");
51+
}
52+
break;
53+
}
54+
case 3: {
55+
changePerson(contacts);
56+
break;
57+
}
58+
case 4: {
59+
delPerson(&contacts);
60+
break;
61+
}
62+
case 5: {
63+
displayContacts(contacts);
64+
break;
65+
}
66+
case 6: {
67+
goto END;
68+
break;
69+
}
70+
default: {
71+
break;
72+
}
73+
}
74+
}
75+
END:
76+
releaseContacts(&contacts);
77+
}
78+
void getInput(struct Person *person)
79+
{
80+
printf("Please input name:\n");
81+
scanf("%s", person->name);
82+
// Bounds Check Elimination is required
83+
printf("Please input phone number:\n");
84+
scanf("%s", person->phone);
85+
// Bounds Check Elimination is required
86+
}
87+
void printPerson(struct Person *person)
88+
{
89+
printf("Name:\n%s\n", person->name);
90+
printf("Phone number:\n%s\n", person->phone);
91+
}
92+
void addPerson(struct Person **contacts)
93+
{
94+
struct Person *person = (struct Person *)malloc(sizeof(struct Person));
95+
if (person == NULL)
96+
{
97+
printf("ERROR: malloc failed");
98+
exit(1);
99+
}
100+
if (contacts != NULL)
101+
{
102+
getInput(person);
103+
person->next = *contacts;
104+
*contacts = person;
105+
}
106+
else
107+
{
108+
*contacts = person;
109+
person->next = NULL;
110+
}
111+
}
112+
struct Person *findPerson(struct Person *contacts)
113+
{
114+
printf("Please input the name:\n");
115+
char temp[20];
116+
scanf("%s", temp);
117+
// Bounds Check Elimination is required
118+
struct Person *current = contacts;
119+
while (current != NULL && strcmp(current->name, temp))
120+
{
121+
current = current->next;
122+
}
123+
if (current != NULL)
124+
{
125+
printf("Name:\n%s\n", current->name);
126+
printf("Phone number:\n%s\n", current->phone);
127+
}
128+
else
129+
{
130+
printf("Not Found\n");
131+
}
132+
133+
return current;
134+
}
135+
void changePerson(struct Person *contacts)
136+
{
137+
struct Person *current = findPerson(contacts);
138+
if (current != NULL)
139+
{
140+
printf("Please input new Phone number:\n");
141+
scanf("%s", current->phone);
142+
// Bounds Check Elimination is required
143+
printf("Done");
144+
}
145+
else
146+
{
147+
printf("Not Found\n");
148+
}
149+
}
150+
void delPerson(struct Person **contacts)
151+
{
152+
struct Person *person = findPerson(*contacts);
153+
if (person == NULL)
154+
{
155+
printf("Not Found");
156+
}
157+
else
158+
{
159+
struct Person *current = *contacts;
160+
if (current == person)
161+
{
162+
*contacts = current->next;
163+
// the first struct is target
164+
}
165+
else
166+
{
167+
while (current->next != person)
168+
{
169+
current = current->next;
170+
}
171+
current->next = current->next->next;
172+
}
173+
}
174+
}
175+
void displayContacts(struct Person *contacts)
176+
{
177+
while (contacts != NULL)
178+
{
179+
printPerson(contacts);
180+
contacts = contacts->next;
181+
}
182+
}
183+
void releaseContacts(struct Person **contacts)
184+
{
185+
struct Person *temp = *contacts;
186+
while (*contacts != NULL)
187+
{
188+
temp = *contacts;
189+
*contacts = (*contacts)->next;
190+
free(temp);
191+
}
192+
}

0 commit comments

Comments
 (0)