-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentence.cpp
More file actions
363 lines (293 loc) · 8.02 KB
/
Sentence.cpp
File metadata and controls
363 lines (293 loc) · 8.02 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <iostream>
#include "Sentence.h"
using namespace std;
string arr;
int yo = 0;
Sentence::Sentence() {
//cout << "Entering Sentence::Sentence() Constructor" << endl;
next = NULL;
//cout << " Exiting Sentence::Sentence() Constructor" << endl;
}
Sentence::~Sentence() {
//cout << "Entering Sentence::~Sentence() Deconstructor" << endl;
/*
delete head;
head = NULL;
*/
//cout << " Exiting Sentence::~Sentence() Deconstructor" << endl;
}
Sentence::Sentence(const string& aString){
//cout << "Entering Sentence::Sentence(string arg) Value Constructor" << endl;
next = NULL;
head = NULL;
Word* temp = head;//create a linked list of words
//cout << "Sen:" << aString << endl;
Word* word;
string tempWord;
for(int i=0; i < aString.length(); i++){
//compile word
if(aString[i] != ' ' && aString[i] != '.' && aString[i] != '!' && aString[i] != '?'){
tempWord += aString[i];
}
//sent out completed word
if(aString[i] == ' '){
word = new Word(tempWord);
if(head == NULL){
head = word;
temp = head;
}else{
temp->next = word;
temp = word;
}
tempWord = "";
}
if(aString[i] == '.'){
word = new Word(tempWord);
arr += '.';
if(head == NULL){
head = word;
temp = head;
}else{
temp->next = word;
temp = word;
}
tempWord = "";
}
if(aString[i] == '!'){
word = new Word(tempWord);
arr += '!';
if(head == NULL){
head = word;
temp = head;
}else{
temp->next = word;
temp = word;
}
tempWord = "";
}
if(aString[i] == '?'){
word = new Word(tempWord);
arr += '?';
if(head == NULL){
head = word;
temp = head;
}else{
temp->next = word;
temp = word;
}
tempWord = "";
}
}//end of for loop
temp = NULL; //set temp to null
//cout << " Exiting Sentence::Sentence(string arg) Value Constructor" << endl;
}
Sentence::Sentence(const Sentence& aSen) {
//cout << "Entering Sentence::Sentence(Sentence arg) Copy Constructor" << endl;
if (aSen.head == NULL){
cout << "aSen.head is null" << endl;
return;
}
head = new Word(*aSen.head);
if (head == NULL) {
cout << "head is null in sent copy struct" << endl;
return;
}
Word* temp = aSen.head->next;
Word* newTemp = head;
while (temp != NULL) {
newTemp->next = new Word(*temp);
newTemp = newTemp->next;
temp = temp->next;
}
newTemp->next = NULL;
//cout << " Exiting Sentence::Sentence(Sentence arg) Copy Constructor" << endl;
}
//Sentence + Word
//new Sentence with the Word added to the end
Sentence Sentence::operator+(const Word& b) {
//cout << "Entering Sentence::operator+(Word arg)" << endl;
Sentence newSen(*this);
if(this->head == NULL) {
cout << "b.head is NULL in Setence + Word" << endl;
newSen.head = new Word(b);
return newSen;
}
Word* newWord = new Word(b);
Sentence* sen = new Sentence(*this);
Word* temp = sen->head;
while(temp->next != NULL){
temp = temp->next;
}
newWord->next = NULL;
temp->next = newWord;
newSen.next = NULL;
newSen.head = sen->head;
//cout << " Exiting Sentence::operator+(Word arg)" << endl;
return newSen;
}
//Word + Sentence
//new Sentence with the Word added to the beginning
Sentence operator+(const Word& a, const Sentence& b) {
//cout << "Entering Sentence operator+(Word arg, Sentence arg)" << endl;
Sentence newSen;
if(b.head == NULL){
cout << "b.head is NULL in Word + Sentence" << endl;
return newSen;
}
Word* newWord = new Word(a);
Sentence* nSen = new Sentence(b);
Word *temp = newWord;
temp->next = nSen->head;
newSen.head = newWord;
newSen.next = NULL;
//cout << " Exiting Sentence operator+(Word arg, Sentence arg)" << endl;
return newSen;
}
//Word + Word
//new Sentence containing the two words
//friend function declared in word.h
Sentence operator+(const Word& a, const Word& b) {
//cout << "Entering Sentence operator+(Word arg, Word arg)" << endl;
//cout << "Passed Values for operator+ word word:-->"<< a << " " << b;
Sentence newSen;
Word* nwleft = new Word(a);
Word* nwright = new Word(b);
nwleft->next = nwright;
nwright->next = NULL;
newSen.head = nwleft;
newSen.next = NULL;
//cout << " Exiting Sentence operator+(Word arg, Word arg)" << endl;
return newSen;
}
//If a is a Sentence
//makes the whole sentence all caps
//postfix
Sentence Sentence::operator++(int) {
//cout << "Entering Sentence::operator++, postfix" << endl;
Sentence s(*this);
if(this->head == NULL) cout << "this->head is NULL" << endl;
Word* temp = s.head;
while(temp){
*temp = (*temp)++;
temp = temp->next;
}
//cout << " Exiting Sentence::operator++, postfix" << endl;
return s;
}
//If a is a Sentence
//makes the whole sentence all lowercase.
Sentence Sentence::operator--(int) {
//cout << "Entering Sentence::operator--, postfix" << endl;
Sentence s(*this);
if(this->head == NULL) cout << "this->head is NULL" << endl;
Word* temp = s.head;
while(temp){
*temp = (*temp)--;
temp = temp->next;
}
//cout << " Exiting Sentence::operator--, postfix" << endl;
return s;
}
//If a is a Sentence
//capitalizes the first letter of the sentence.
Sentence& Sentence::operator+(const int& i) {
//cout << "Entering Sentence::operator-" << endl;
if (i != 1) return *this;
else{
Word* temp = head;
(*temp)+1;
}
//cout << " Exiting Sentence::operator-" << endl;
return *this;
}
//If a is a Sentence
//converts the whole sentence to Pig Latin.
Sentence& Sentence::operator++() {
//cout << "Entering Sentence::operator++, prefix" << endl;
Word* temp = this->head;
while(temp != NULL){
v.push_back(temp->dummyString);
*temp = ++(*temp);
temp = temp->next;
}
//cout << " Exiting Sentence::operator++, prefix" << endl;
return *this;
}
//If a is a Sentence
//converts all contained Words that are in Pig Latin back to English.
Sentence& Sentence::operator--() {
//cout << "Entering Sentence::operator--, prefix" << endl;
Word* temp = this->head;
int i = 0;
while(temp != NULL){
temp->dummyString = v.at(i);
i++;
temp = temp->next;
}
//cout << " Exiting Sentence::operator--, prefix" << endl;
return *this;
}
void Sentence::show() {
//cout << "Entering Sentence::show" << endl;
Word* temp = head;
while(temp !=NULL){
cout << temp->dummyString << endl;
temp = temp->next;
}
//cout << " Exiting Sentence::show" << endl;
}
//The Sentence class should output each word. All but the last word should be followed by a single space.
//The last word should be followed by the punctuation mark associated with the Sentence (., !, or ?), and then a single space.
ostream& operator<<(ostream& out, const Sentence& b) {
//cout << "Entering Sentence operator<<" << endl;
Word* temp = b.head;
while(temp != NULL){
out << temp->dummyString;
if(temp->next != NULL) out << ' ';
temp = temp->next;
}
out << arr[1] << ' ';
//cout << " Exiting Sentence operator<<" << endl;
return out;
}
//mySent.first() returns a Word containing a copy of the first word of mySent.
Word Sentence::first() {
//cout << "Entering Sentence::first()" << endl;
Sentence copy(*this);
Word newW;
Word* temp = copy.head;
newW = *temp;
//cout << " Exiting Sentence::first()" << endl;
return newW;
}
//mySent.rest() returns a Sentence containing a copy of all but the first word of mySent.
Sentence Sentence::rest(){
//cout << "Entering Sentence::rest()" << endl;
Sentence copy(*this);
Sentence newS;
if(head == NULL) cout <<"Head is NULL with sent.rest()" << endl;
Word* temp = copy.head;
newS.head = temp->next;
//cout << " Exiting Sentence::rest()" << endl;
return newS;
}
Sentence& Sentence::operator=(const Sentence& aSen) {
//cout << "Entering Sentence::operator=(Sentence arg)" << endl;
//perform destructor
if (aSen.head == NULL){
cout << "aSen.head is null" << endl;
this->head = NULL;
return *this;
}
head = new Word(*aSen.head);
Word* temp = aSen.head->next;
Word* newTemp = head;
while (temp != NULL) {
newTemp->next = new Word(*temp);
newTemp = newTemp->next;
temp = temp->next;
}
newTemp->next = NULL;
return *this;
//cout << " Exiting Sentence::operator=(Sentence arg)" << endl;
}