forked from sgreenlee/C-Primer-Plus-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise08.c
164 lines (140 loc) · 3.32 KB
/
exercise08.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
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
// C Primer Plus
// Chapter 17 Exercise 8:
// Modify the Pet Club program so that all pets with the same name are stored
// in a list in the same node. When the user chooses to find a pet, the
// program should request the pet name and then list all pets (along with their
// kinds) having that name.
// compile with pettree.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "pettree.h"
char menu(void);
void addpet(Tree * pt);
void printpet(Pet pet);
void droppet(Tree * pt);
void showpets(const Tree * pt);
void findpet(const Tree * pt);
void uppercase(char * str);
char * get(char * st, int n);
int main(void) {
Tree pets;
char choice;
InitializeTree(&pets);
while ((choice = menu()) != 'q') {
puts("");
if (choice == 'a') {
addpet(&pets);
} else if (choice == 'l') {
showpets(&pets);
} else if (choice == 'f') {
findpet(&pets);
} else if (choice == 'n') {
printf("%d pets in club\n", TreeItemCount(pets));
} else if (choice == 'd') {
droppet(&pets);
} else {
printf("Not a valid option.\n");
}
puts("");
}
DeleteAll(&pets);
puts("Bye.");
return 0;
}
char menu(void) {
int ch;
puts("Nerfville Pet Club Membership Program");
puts("Enter the letter corresponding to your choice:");
puts("a) add a pet l) show list of pets");
puts("n) number of pets f) find pets");
puts("d) delete a pet q) quit");
while ((ch = getchar()) != EOF) {
while (getchar() != '\n')
continue;
ch = tolower(ch);
if (strchr("alfndq", ch) == NULL)
puts("Please enter an a, l, f, n, d or q:");
else
break;
}
if (ch == EOF)
ch = 'q';
return ch;
}
void addpet(Tree * pt) {
Pet * ppet = (Pet *) malloc(sizeof(Pet));
if (ppet == NULL) {
fprintf(stderr, "Could not allocate memory.\n");
return;
}
if (TreeIsFull(pt)) {
puts("No room in the club!");
}
else {
puts("Please enter name of pet:");
get(ppet->name, STRLEN);
puts("Please enter pet kind:");
get(ppet->type, STRLEN);
uppercase(ppet->name);
uppercase(ppet->type);
ppet->next = NULL;
AddPet(ppet, pt);
}
}
void droppet(Tree * pt) {
Pet temp;
if (TreeIsEmpty(pt)) {
printf("No entries!\n");
return;
}
puts("Please enter name of pet you wish to delete:");
get(temp.name, STRLEN);
puts("Please enter type of pet:");
get(temp.type, STRLEN);
uppercase(temp.name);
uppercase(temp.type);
printf("%s the %s ", temp.name, temp.type);
if (DeletePet(&temp, pt))
printf("is dropped from the club.\n");
else
printf("is not a member.\n");
}
void showpets(const Tree * pt) {
if (TreeIsEmpty(pt))
puts("No entries!");
else
TraverseTree(pt, printpet);
}
void printpet(Pet pet) {
printf("Pet: %-19s Kind: %-19s\n", pet.name, pet.type);
}
void findpet(const Tree * pt) {
Pet temp;
if (TreeIsEmpty(pt)) {
puts("No entries.");
return;
}
puts("Please enter name of pet you wish to find:");
get(temp.name, STRLEN);
uppercase(temp.name);
printf("Here are all pets named %s:\n", temp.name);
ApplyToNode(&temp, pt, printpet);
}
void uppercase(char * str) {
for (; *str != '\0'; str++)
*str = toupper(*str);
}
char * get(char * string, int n) {
// wrapper for fgets - read from stdin and replace
// first newline with null character
char * retval = fgets(string, n, stdin);
for (; *string != '\0'; string++) {
if (*string == '\n') {
*string = '\0';
break;
}
}
return retval;
}