-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.cpp
More file actions
321 lines (276 loc) · 7.76 KB
/
message.cpp
File metadata and controls
321 lines (276 loc) · 7.76 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/////////////////////////////////////////
/////// Marc Week sample C++ code ////
/////// ////
/////// ////
/////////////////////////////////////////
/*
This program manages"simulates" messages from a varitey of different type of communication channels. Phone, email and facebook
It contains a class of persons contact ifnro which is conatined in array, mssages are contained in an AVL tree in.
all structures are managed with a class that contained two AVLs and a contact list.
operator overloading was used in all classes to make the implientaion eaiser
These are the implimentations of the message center classes
*/
#include "message.h"
using namespace std;
//default constructor for the person
person::person() : name(NULL), address(NULL), prefered(NULL)
{}
//constructor or person class with Char argrumenst
person::person(char * newname, char * newaddress, char * newprefered)
{
if (newname)
{
name = new char[strlen(newname)+1];
strcpy(name, newname);
}
if (newaddress)
{
address = new char[strlen(newaddress)+1];
strcpy(address, newaddress);
}
if (newprefered)
{
prefered = new char[strlen(newprefered)+1];
strcpy(prefered, newprefered);
}
}
//person class copy construtor
person::person(person * newperson)
{
if (newperson->name)
{
name = new char[strlen(newperson->name)+1];
strcpy(name, newperson->name);
}
if (newperson->address)
{
address = new char[strlen(newperson->address)+1];
strcpy(address, newperson->address);
}
if (newperson->prefered)
{
prefered = new char[strlen(newperson->prefered)+1];
strcpy(prefered, newperson->prefered);
}
}
//deconstructor for person class
person::~person()
{
if (name) delete [] name;
if (address) delete [] address;
if (prefered) delete [] prefered;
}
//outputs the personclass data members
int person::display()
{
if (name && address && prefered)
{
cout << "Name: ";
cout.width(15);
cout << left << name;
cout << "Prefered address: ";
cout.width(15);
cout << left << prefered;
cout << "Address: ";
cout.width(15);
cout << left << address;
}
}
//copys over the person to a uninitlzed data memeber
int person::copyperson(person * newperson)
{
if (newperson)
{
if (name) delete[] name;
if (newperson->name)
{
name = new char[strlen(newperson->name)+1];
strcpy(name, newperson->name);
}
if (newperson->address)
{
address = new char[strlen(newperson->address)+1];
strcpy(address, newperson->address);
}
if (prefered) delete[] prefered;
if (newperson->prefered)
{
prefered = new char[strlen(newperson->prefered)+1];
strcpy(prefered, newperson->prefered);
}
}
}
//copies in from charictor arguments
int person::copyin(char * newname, char * newaddress, char * newprefered)
{
if (name) delete [] name;
if (newname)
{
name = new char[strlen(newname)+1];
strcpy(name, newname);
}
if (address) delete[] address;
if (newaddress)
{
address = new char[strlen(newaddress)+1];
strcpy(address, newaddress);
}
if (prefered) delete prefered;
if (newprefered)
{
prefered = new char[strlen(newprefered)+1];
strcpy(prefered, newprefered);
}
}
//operator overloaded equalty check based on name.
bool operator== (person & search, char * name)
{
if(!strncmp(search.name, name, 3)) return true;//NOTE I Limited to 3 charitors so the user could type in a first name or mispell and still get all the data from the contact list
return false;
}
int person::getcontact(char *& toname, char *& addressee)//gives the name and address to the arguments
{
strcpy(toname, name);
strcpy(addressee, address);
}
//default constuctor for the contact list
contactlist::contactlist(): index(0)
{}
//contact list copy constutor
contactlist::contactlist(contactlist * newlist)
{
if (newlist)
{
max = newlist->max;
index = newlist->index;
contacts = new person[max];
for (int i = 0; i < index; ++i)
contacts[i].copyperson(&newlist->contacts[i]);
}
}
//deconstructor for contact list
contactlist::~contactlist()
{
if (contacts) delete [] contacts;
}
//adds a person and set the index to the next open space
int contactlist::addcontact(person * contact)
{
if (index != max)
{
contacts[index].copyperson(contact);
++index;
}
}
//reads in the contacts from a file
int contactlist::readincontacts(char * filename)
{
ifstream filein;
if (!filein) return 0;
filein.open(filename);
int rmax = 0;
int i = 1;
char name[20];
char address[20];
char prefered[20];
person * temp;
filein >> rmax ; filein.ignore (100, '\n');//the size of the array is the first line of the file
max = rmax;
contacts = new person[max];
while (!filein.eof() && i < max)
{
filein.get(name, 20, ';'); filein.ignore(100, ';');
filein.get(address, 20, ';'); filein.ignore(100, ';');
filein.get(prefered, 20, '\n'); filein.ignore(100, '\n');
temp = new person(name, address, prefered);
addcontact(temp);
delete temp;
++i;
}
filein.close();
return 1;
}
//outputs the entire contact list
ostream& operator<<(ostream& out, const contactlist & friends)
{
for (int i = 0; i < friends.index; ++i)
{
friends.contacts[i].display();
cout <<endl;
}
}
//finds a conatct entered in by the uses and copys in the full name and address
int contactlist::findcontact(char *& name, char *& address)
{
for (int i = 0; i < index; ++i)
{
if (contacts[i] == name)
{
contacts[i].getcontact(name, address);
return 1;
}
}
return 0;
}
//default consturctor for messagecenter
messagecenter::messagecenter(){}
//constcutor for the messagecnter with the names of the externeal files
messagecenter::messagecenter(char * contactsfile, char * messagefile)//"contact.txt", "messages.txt"
{
friends.readincontacts(contactsfile);
getmessages(messagefile);
}
//gets the messages from the file and sends them into the BST inbox
int messagecenter::getmessages(char * filename)
{
ifstream filein;
if (!filein) return 0;
filein.open(filename);;
char name[20];
char address[20];
char text[50];
message temp;
filein.get(name, 20, ';') ; filein.ignore (100, ';');
while (!filein.eof())
{
filein.get(address, 20, ';'); filein.ignore(100, ';');
filein.get(text, 50, '\n'); filein.ignore(100, '\n');
temp = message(name, address, text);
inbox += temp;
filein.get(name, 20, ';'); filein.ignore(100, ';');
}
filein.close();
return 1;
}
//operator overloaded output function displays the contacts list, inbox and outbox
ostream& operator<<(ostream& out, const messagecenter & mycenter)
{
cout << "\nContacts:\n";
cout << mycenter.friends;
cout << "\nInbox:\n";
cout << mycenter.inbox;
cout << "\nOutbox:\n";
cout << mycenter.outbox;
}
//creats a new message and sends it to the outbox
int messagecenter::sendmessage()
{
char* name = new char [20];
char* address = new char [20];
char text[50];
cout << "\nSelect name from contact list>";
cin.get(name, 4, '\n'); cin.ignore(100, '\n');
name[0] = toupper(name[0]);
if(!friends.findcontact(name, address))//checks the first 3 charictors for a match and then sends in the address
{
cout << "Contact not in friends list\nEnter in address";//lets the user type it in if its not there
cin.get(address, 20, '\n'); cin.ignore(100, '\n');
}
cout << "\nTo: " << name<< '<' << address << ">\n";
cout << "\nEnter message>";
cin.get(text, 50, '\n'); cin.ignore(100, '\n');
message sent(name, address, text);
outbox += sent;
cout << "\nMessage sent\n";
delete [] name; delete[] address;
}