COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
phonebook.cpp
Go to the documentation of this file.
1// Phonebook Manager (dynamic array version)
2#include <iostream>
3#include <string>
4#include <fstream>
5#include <sstream>
6
7using namespace std;
8
9struct PhoneRec {
10 string name;
11 string phone_no;
12};
13
14char selection_menu();
15// Print selecltion menu to screen and read user selection
16
17int load_phonebook(string filename, PhoneRec * &pb, int &pb_size);
18// Load phonebook data from a file into the array parameter with a maximium size
19// specified by 'pb_size', and return the number of records loaded
20
21void print_phonebook(PhoneRec pb[], int nRec);
22// Print phonebook records stored in the array parameter. 'nRec' specifies the
23// number of records stored in the array parameter
24
25void sort_phonebook(PhoneRec pb[], int nRec);
26// Sort the phonebook records stored in the array parameter by ascending order of
27// the name. 'nRec' specifies the number of records stored in the array parameter
28
29int search_phonebook(string str, PhoneRec pb[], int nRec);
30// Search the recrods of the phonebook by partial match of the name and return
31// the number of records found
32
33int save_phonebook(string filename, PhoneRec pb[], int nRec);
34// Save phonebook data stored in the array parameter into a file. 'nRec' specifies
35// the number of records stored in the array parameter. Return 0 if error in
36// opening file
37
38int add_record(PhoneRec pb[], int nRec);
39// Add a new record to the phonebook and return the new size
40
41string upper_case(string str);
42// Return a string with all letters in upper case
43
44void grow_phonebook(PhoneRec * &pb, int &pb_size, int n);
45// Grow the phonebook by increasing the size of the array by n
46
47int main()
48{
49 int phonebook_size = 3;
50 PhoneRec * phonebook = new PhoneRec[phonebook_size];
51
52 int num_records = 0, count;
53 string str;
54
55 char choice = selection_menu();
56 while (choice != '0')
57 {
58 switch (choice)
59 {
60 case '1':
61 cout << "Please enter the filename: ";
62 cin >> str;
63 num_records = load_phonebook(str, phonebook, phonebook_size);
64 cout << endl << num_records << " record(s) loaded." << endl << endl;
65 break;
66
67 case '2':
68 print_phonebook(phonebook, num_records);
69 break;
70
71 case '3':
72 sort_phonebook(phonebook, num_records);
73 print_phonebook(phonebook, num_records);
74 break;
75
76 case '4':
77 cout << "Please enter a name: ";
78 cin >> str;
79 cout << endl;
80 count = search_phonebook(str, phonebook, num_records);
81 cout << count << " record(s) found." << endl << endl;
82 break;
83
84 case '5':
85 cout << "Please enter the filename: ";
86 cin >> str;
87 count = save_phonebook(str, phonebook, num_records);
88 cout << endl << count << " record(s) saved." << endl << endl;
89 break;
90
91 case '6':
92 if (num_records >= phonebook_size)
93 grow_phonebook(phonebook, phonebook_size, 3);
94
95 if (num_records < phonebook_size)
96 num_records = add_record(phonebook, num_records);
97 cout << "There are now " << num_records <<
98 " record(s) in the phonebook." << endl << endl;
99 break;
100
101 default:
102 cout << "Invalid input!" << endl;
103 }
104 choice = selection_menu();
105 }
106
107 cout << "Goodbye!" << endl << endl;
108
109 delete [] phonebook;
110
111 return 0;
112}
113
115{
116 char choice;
117
118 // print selection menu
119 cout << "********************************" << endl;
120 cout << "* Welcome to Phonebook Manager *" << endl;
121 cout << "********************************" << endl;
122 cout << "1. Load a phonebook." << endl;
123 cout << "2. Print all records." << endl;
124 cout << "3. Sort the records by ascending order of the name." << endl;
125 cout << "4. Search the records by partial match of the name." << endl;
126 cout << "5. Save the phonebook." << endl;
127 cout << "6. Add a new record." << endl;
128 cout << "0. Quit. " << endl;
129 cout << "Please enter your choice: ";
130
131 // read user selection
132 cin >> choice;
133 cout << endl;
134
135 return choice;
136}
137
138int load_phonebook(string filename, PhoneRec * &pb, int &pb_size)
139{
140 ifstream fin;
141 fin.open(filename.c_str());
142 if (fin.fail())
143 {
144 cout << "Error in file opening." << endl;
145 return 0;
146 }
147
148 int i = 0;
149 string line;
150 while (getline(fin, line))
151 {
152 if (i >= pb_size)
153 grow_phonebook(pb, pb_size, 3);
154
155 // extract a name and a phone no. from a line
156 if (i < pb_size) {
157 istringstream iss(line);
158 if (!getline(iss, pb[i].name,','))
159 break;
160 if (!getline(iss, pb[i].phone_no))
161 break;
162 ++i;
163 }
164
165 }
166
167 fin.close();
168 return i;
169}
170
171void print_phonebook(PhoneRec pb[], int nRec)
172{
173 int i;
174
175 for (i = 0; i < nRec; i++)
176 {
177 cout << "Name:\t" << pb[i].name << endl;
178 cout << "Phone#:\t" << pb[i].phone_no << endl << endl;
179 }
180}
181
182void sort_phonebook(PhoneRec pb[], int nRec)
183{
184 int i, j ,idx;
185 string min;
186 // selection sort
187 for (i = 0; i < nRec - 1; i++)
188 {
189 min = pb[i].name;
190 idx = i;
191
192 for (j = i + 1; j < nRec; j++)
193 {
194 if (pb[j].name < min)
195 {
196 min = pb[j].name;
197 idx = j;
198 }
199 }
200
201 if (idx != i)
202 {
203 PhoneRec temp;
204 // swap pb[i] & pb[idx]
205 temp = pb[i];
206 pb[i] = pb[idx];
207 pb[idx] = temp;
208 }
209 }
210}
211
212int search_phonebook(string str, PhoneRec pb[], int nRec)
213{
214 int i, count = 0;
215 for (i = 0; i < nRec; i++)
216 {
217 // extract the name from a record
218 string name = pb[i].name;
219 // search the name for any occurrence of str
220 // both the name and str are converted into upper case to
221 // make the search case insensitive
222 if (upper_case(name).find(upper_case(str)) != string::npos)
223 {
224 // output the record to the screen
225 cout << "Name:\t" << pb[i].name << endl;
226 cout << "Phone#:\t" << pb[i].phone_no << endl << endl;
227 // increase the count
228 count++;
229 }
230 }
231 return count;
232}
233
234int save_phonebook(string filename, PhoneRec pb[], int nRec)
235{
236 ofstream fout;
237 fout.open(filename.c_str());
238 if (fout.fail())
239 {
240 cout << "Error in file opening." << endl;
241 return 0;
242 }
243
244 int i;
245 for (i = 0; i < nRec; i++)
246 {
247 fout << pb[i].name << "," << pb[i].phone_no << endl;
248 }
249
250 fout.close();
251 return i;
252}
253
254int add_record(PhoneRec pb[], int nRec)
255{
256 char ans;
257 string str;
258
259 getline(cin, str); // flush the keyboard buffer
260 cout << "Please enter a name: ";
261 getline(cin, pb[nRec].name);
262 cout << "Please enter a phone number: ";
263 getline(cin, pb[nRec].phone_no);
264 cout << endl;
265 cout << "Name:\t" << pb[nRec].name << endl;
266 cout << "Phone#:\t" << pb[nRec].phone_no << endl << endl;
267 cout << "Add to phonebook (y/n)? ";
268 cin >> ans;
269 if (ans == 'y')
270 {
271 cout << "1 record added." << endl;
272 nRec++;
273 }
274
275 return nRec;
276}
277
278string upper_case(string str)
279{
280 int i, n = str.length();
281 for (i = 0; i < n; i++)
282 {
283 if (str[i] >= 'a' && str[i] <= 'z')
284 str[i] = str[i] - 'a' + 'A';
285 }
286
287 return str;
288}
289
290void grow_phonebook(PhoneRec * &pb, int &pb_size, int n)
291{
292 // create a new dynamic array with a new size = max_size + n
293 PhoneRec * pb_new = new PhoneRec[pb_size + n];
294
295 // copy all the records from the original array to the new dynamic array
296 for (int i = 0; i < pb_size; i++)
297 {
298 pb_new[i] = pb[i];
299 }
300
301 // destroy the old dynamic array to free up the memory allocated to it
302 delete [] pb;
303 // assign the pointer to the new dynamic array to the pointer variable
304 pb = pb_new;
305 // update the size of the array
306 pb_size += n;
307
308 cout << "---> phonebook size enlarged to hold a maximum of " << pb_size << " records." << endl;
309
310 return;
311}
Node * find(Node *head, int num)
Definition ex4ex5.cpp:62
string upper_case(string str)
void grow_phonebook(PhoneRec *&pb, int &pb_size, int n)
void sort_phonebook(PhoneRec pb[], int nRec)
int add_record(PhoneRec pb[], int nRec)
int search_phonebook(string str, PhoneRec pb[], int nRec)
int load_phonebook(string filename, PhoneRec *&pb, int &pb_size)
void print_phonebook(PhoneRec pb[], int nRec)
int save_phonebook(string filename, PhoneRec pb[], int nRec)
int main()
Definition phonebook.cpp:47
char selection_menu()