COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
phonebook_incomplete.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 fin.close();
167 return i;
168}
169
170void print_phonebook(PhoneRec pb[], int nRec)
171{
172 int i;
173
174 for (i = 0; i < nRec; i++)
175 {
176 cout << "Name:\t" << pb[i].name << endl;
177 cout << "Phone#:\t" << pb[i].phone_no << endl << endl;
178 }
179}
180
181void sort_phonebook(PhoneRec pb[], int nRec)
182{
183 int i, j ,idx;
184 string min;
185 // selection sort
186 for (i = 0; i < nRec - 1; i++)
187 {
188 min = pb[i].name;
189 idx = i;
190
191 for (j = i + 1; j < nRec; j++)
192 {
193 if (pb[j].name < min)
194 {
195 min = pb[j].name;
196 idx = j;
197 }
198 }
199
200 if (idx != i)
201 {
202 PhoneRec temp;
203 // swap pb[i] & pb[idx]
204 temp = pb[i];
205 pb[i] = pb[idx];
206 pb[idx] = temp;
207 }
208 }
209}
210
211int search_phonebook(string str, PhoneRec pb[], int nRec)
212{
213 int i, count = 0;
214 for (i = 0; i < nRec; i++)
215 {
216 // extract the name from a record
217 string name = pb[i].name;
218 // search the name for any occurrence of str
219 // both the name and str are converted into upper case to
220 // make the search case insensitive
221 if (upper_case(name).find(upper_case(str)) != string::npos)
222 {
223 // output the record to the screen
224 cout << "Name:\t" << pb[i].name << endl;
225 cout << "Phone#:\t" << pb[i].phone_no << endl << endl;
226 // increase the count
227 count++;
228 }
229 }
230 return count;
231}
232
233int save_phonebook(string filename, PhoneRec pb[], int nRec)
234{
235 ofstream fout;
236 fout.open(filename.c_str());
237 if (fout.fail())
238 {
239 cout << "Error in file opening." << endl;
240 return 0;
241 }
242
243 int i;
244 for (i = 0; i < nRec; i++)
245 {
246 fout << pb[i].name << "," << pb[i].phone_no << endl;
247 }
248
249 fout.close();
250 return i;
251}
252
253int add_record(PhoneRec pb[], int nRec)
254{
255 char ans;
256 string str;
257
258 getline(cin, str); // flush the keyboard buffer
259 cout << "Please enter a name: ";
260 getline(cin, pb[nRec].name);
261 cout << "Please enter a phone number: ";
262 getline(cin, pb[nRec].phone_no);
263 cout << endl;
264 cout << "Name:\t" << pb[nRec].name << endl;
265 cout << "Phone#:\t" << pb[nRec].phone_no << endl << endl;
266 cout << "Add to phonebook (y/n)? ";
267 cin >> ans;
268 if (ans == 'y')
269 {
270 cout << "1 record added." << endl;
271 nRec++;
272 }
273
274 return nRec;
275}
276
277string upper_case(string str)
278{
279 int i, n = str.length();
280 for (i = 0; i < n; i++)
281 {
282 if (str[i] >= 'a' && str[i] <= 'z')
283 str[i] = str[i] - 'a' + 'A';
284 }
285
286 return str;
287}
288
289void grow_phonebook(PhoneRec * &pb, int &pb_size, int n)
290{
291 // Step 1:
292 // create a new dynamic array with a new size = max_size + n
293
294 // Step 2:
295 // copy all the records from the original array to the new dynamic array
296
297 // Step 3:
298 // destroy the old dynamic array to free up the memory allocated to it
299
300 // assign the pointer to the new dynamic array to the pointer variable
301
302 // Step 4:
303 // update the size of the array
304
305
306 cout << "--->phonebook size enlarged to hold a maximum of " << pb_size << " records." << endl;
307
308 return;
309}
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()
char selection_menu()