4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
– |
#include <iostream> |
7 |
|
#include <string> |
8 |
+ |
#include <sstream> |
9 |
+ |
#include <cassert> |
10 |
|
#include "DoublyLinkedList.hpp" |
11 |
|
|
12 |
|
using namespace std; |
13 |
|
|
14 |
< |
int main(int argc, char* argv[]) |
14 |
> |
template<typename Type> |
15 |
> |
inline bool contains(const Type& value, DoublyLinkedList<Type>& list) |
16 |
|
{ |
17 |
< |
cout.setf(ios_base::boolalpha); |
17 |
> |
for (Iterator<Type> itor(list.iterator()); !itor.end(); itor++) |
18 |
> |
{ |
19 |
> |
if (*itor == value) return true; |
20 |
> |
} |
21 |
> |
|
22 |
> |
return false; |
23 |
> |
} |
24 |
|
|
25 |
+ |
class VowelVisitor : public Visitor<string> |
26 |
+ |
{ |
27 |
+ |
private: |
28 |
+ |
ostream* os; |
29 |
+ |
public: |
30 |
+ |
VowelVisitor(ostream& os) { this->os = &os; } |
31 |
+ |
void visit(const string& value) { if (value.find_first_of("aAeEiIoOuU") == 0) *os << value << '\n'; } |
32 |
+ |
}; |
33 |
+ |
|
34 |
+ |
int main(int argc, char* argv[]) |
35 |
+ |
{ |
36 |
|
DoublyLinkedList<int> list; |
37 |
|
|
38 |
|
list.addFront(1); |
42 |
|
|
43 |
|
// 4 1 2 5 |
44 |
|
|
45 |
< |
cout << "list.contains(1) = " << list.contains(1) << '\n' |
46 |
< |
<< "list.contains(2) = " << list.contains(2) << '\n' |
47 |
< |
<< "list.contains(3) = " << list.contains(3) << '\n' |
48 |
< |
<< "list.contains(4) = " << list.contains(4) << '\n' |
49 |
< |
<< "list.contains(5) = " << list.contains(5) << '\n'; |
45 |
> |
assert(list.contains(1) == true); |
46 |
> |
assert(list.contains(2) == true); |
47 |
> |
assert(list.contains(3) == false); |
48 |
> |
assert(list.contains(4) == true); |
49 |
> |
assert(list.contains(5) == true); |
50 |
|
|
51 |
|
list.removeFirst(2); |
52 |
|
|
53 |
|
// 4 1 5 |
54 |
|
|
55 |
< |
cout << "list.contains(2) = " << list.contains(2) << '\n' |
56 |
< |
<< "list.contains(5) = " << list.contains(5) << '\n'; |
55 |
> |
assert(list.contains(2) == false); |
56 |
> |
assert(list.contains(5) == true); |
57 |
> |
|
58 |
> |
ostringstream sout; |
59 |
|
|
60 |
|
for (Iterator<int> itor(list.iterator()); !itor.end(); itor++) |
61 |
|
{ |
62 |
< |
cout << *itor << '\n'; |
62 |
> |
sout << *itor << '\n'; |
63 |
> |
} |
64 |
> |
|
65 |
> |
assert(sout.str() == "4\n1\n5\n"); |
66 |
> |
sout.str(""); |
67 |
> |
assert(contains(3, list) == false); |
68 |
> |
assert(contains(4, list) == true); |
69 |
> |
|
70 |
> |
DoublyLinkedList<string> words; |
71 |
> |
|
72 |
> |
words.addFront("Douglas"); |
73 |
> |
words.addBack("Allen"); |
74 |
> |
words.addFront("Edward"); |
75 |
> |
words.addBack("attack"); |
76 |
> |
words.addFront("Thrift"); |
77 |
> |
|
78 |
> |
for (Iterator<string> itor(words.iterator()); !itor.end(); itor++) |
79 |
> |
{ |
80 |
> |
if (itor->find_first_of("aAeEiIoOuU") == 0) sout << *itor << '\n'; |
81 |
|
} |
82 |
|
|
83 |
+ |
assert(sout.str() == "Edward\nAllen\nattack\n"); |
84 |
+ |
sout.str(""); |
85 |
+ |
|
86 |
+ |
VowelVisitor vowels(sout); |
87 |
+ |
|
88 |
+ |
words.accept(dynamic_cast<Visitor<string>*>(&vowels)); |
89 |
+ |
|
90 |
+ |
assert(sout.str() == "Edward\nAllen\nattack\n"); |
91 |
+ |
|
92 |
|
return 0; |
93 |
|
} |