-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyHW.cpp
115 lines (96 loc) · 2.52 KB
/
myHW.cpp
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
#include <iostream>
#include <vector>
using namespace std;
class CompositeIterator;
class Composite;
class Component
{
public:
virtual void traverse(){};
virtual ComponentIterator* CreateIterator(){};
};
class Leaf : public Component
{
private:
string value;
public:
Leaf(string val)
{
value = val;
}
void traverse()
{
cout << value << ' ';
}
};
class Composite: public Component
{
vector < Component * > leaves;
public:
void add(Component *node)
{
leaves.push_back(node);
}
int getCount () const {return leaves.size(); }
Leaf * get(int index) const { return leaves[index];};
void traverse()
{
for (int i = 0; i < leaves.size(); i++)
leaves[i]->traverse();
}
CompositeIterator* CreateIterator() {
return new CompositeIterator(this);
};
class ComponentIterator {
public:
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone () const = 0;
virtual Leaf* CurrentItem() const = 0 ;
protected:
ComponentIterator(){};
};
class ComponentIterator;
class CompositeIterator : public ComponentIterator {
public:
CompositeIterator(const Composite *composite) : _composite(composite), _current(0) {}
void First(){_current = 0;};
void Next(){_current++;};
Leaf* CurrentItem() const{return (IsDone()?NULL:_composite->get(_current));};
bool IsDone()const ;
bool IsDone() const {
return _current >= _composite->getCount();
}
private:
const Composite *_composite;
int _current;
};
void printAggregate(ComponentIterator& i) {
cout << "Iterating over collection:" << endl;
for(i.First(); !i.IsDone(); i.Next()) {
cout << i.CurrentItem()->traverse() << endl;
}
cout << endl;
}
int main()
{
Component *component;
component = new Composite();
Leaf *root = new Leaf("expression");
Leaf *expr_left = new Leaf("expression");
Leaf *term_left = new Leaf("term");
Leaf *const_left = new Leaf("5");
Leaf *term = new Leaf("-");
Leaf *right_right_term = new Leaf("term");
Leaf *right_left_const = new Leaf("4");
Leaf *right_term= new Leaf("/");erat
Leaf *right_right_const = new Leaf("2");
ComponentIterator *iterator = component->CreateIterator();
component.add(const_left);
component.add(term);
component.add(right_left_const);
component.add(right_term);
component.add(right_right_const);
printAggregate(*iterator);
delete iterator;
}