sources:
M02_1.cpp (3.9k)


website:
more info here
studies/bauelemente/Softwarebauelemente-CodeM2/M02_1.cpp
download file

  1 ///////////////////////////////////////////////////////////
  2 // Softwarebauelemente I, Aufgabe M2.1
  3 //
  4 // author: Stephan Brumme
  5 // last changes: October 18, 2000
  6
  7
  8 // we need const NULL
  9 #include <stdio.h>
 10
 11
 12 // simple class for storing a single point
 13 // two constructors to ease creation
 14 // contains Equals method which compares the coordinates of two points
 15 class CPoint {
 16 public:
 17     // default constructor, zero the member variables
 18     CPoint()
 19     {    m_nX = 0; m_nY = 0;    }
 20
 21     // easier creation of a point
 22     CPoint(int X, int Y)
 23     {    m_nX = X; m_nY = Y;    }
 24
 25     // public member variables
 26     int m_nX, m_nY;
 27
 28     // compares two points
 29     bool Equals(CPoint Point)
 30     {    return ((m_nX == Point.m_nX) && (m_nY == Point.m_nY));    }
 31 };
 32
 33
 34 // class for storing a set of points in an array
 35 // each entry is implemented as a pointer to CPoint (see above)
 36 class CSetOfPoints {
 37 public:
 38     // default constructor
 39     CSetOfPoints();
 40
 41     // clear the set
 42     void Clear();
 43     // insert a point to the set, returns true on success
 44     bool Insert(CPoint Point);
 45     // remove a point from the set, returns true on success
 46     bool Scratch(CPoint Point);
 47     // determine whether a point is already in the set
 48     bool Contains(CPoint Point);
 49
 50     // max. number of stored points
 51     enum { MAX_POINTS = 10 };
 52
 53 protected:
 54     // set all pointers to NULL
 55     void ZeroMem();
 56     // get array position of a specified point, -1 on error
 57     int  GetPos(CPoint Point);
 58
 59     // storage
 60     CPoint* arPPoints[MAX_POINTS];
 61 };
 62
 63
 64 // default constructor
 65 CSetOfPoints::CSetOfPoints()
 66 {
 67     // memory is randomly set on startup
 68     // initialize all array entry by setting them to NULL
 69     ZeroMem();
 70 }
 71
 72
 73 // clear the set
 74 void CSetOfPoints::Clear()
 75 {
 76     // delete each entry that is non-NULL
 77     for (int nLoop=0; nLoop<MAX_POINTS; nLoop++)
 78         if (arPPoints[nLoop] != NULL)
 79             delete arPPoints[nLoop];
 80
 81     // set all array entries to NULL
 82     ZeroMem();
 83 }
 84
 85
 86 // insert a point to the set, returns true on success
 87 bool CSetOfPoints::Insert(CPoint Point)
 88 {
 89     // if point is already part of the set we don't need to insert it
 90     if (Contains(Point))
 91         return true;
 92
 93     // find a free entry
 94     for (int nLoop=0; nLoop<MAX_POINTS; nLoop++)
 95         // free entry found ?
 96         if (arPPoints[nLoop] == NULL)
 97         {
 98             // create a new CPoint on heap
 99             arPPoints[nLoop] = new CPoint(Point.m_nX, Point.m_nY);
100             // we are done
101             return true;
102         }
103
104     // no free entry found !
105     return false;
106 }
107
108
109 // remove a point from the set, returns true on success
110 bool CSetOfPoints::Scratch(CPoint Point)
111 {
112     // get position of this point
113     int nPos = GetPos(Point);
114     // if GetPos returned -1 the point is not part of the set
115     if (nPos == -1)
116         return false;
117
118     // free the memory
119     delete arPPoints[nPos];
120     // set pointer to NULL
121     arPPoints[nPos] = NULL;
122
123     // we are successfully done
124     return true;
125 }
126
127
128 // determine whether a point is already in the set
129 bool CSetOfPoints::Contains(CPoint Point)
130 {
131     // position only equals -1 if the point is not part of the set
132     return (GetPos(Point) != -1);
133 }
134
135
136 // set all pointers to NULL
137 void CSetOfPoints::ZeroMem()
138 {
139     for (int nLoop=0; nLoop<MAX_POINTS; nLoop++)
140         arPPoints[nLoop] = NULL;
141 }
142
143
144 // get array position of a specified point, -1 on error
145 int CSetOfPoints::GetPos(CPoint Point)
146 {
147     // look at each entry
148     for (int nLoop=0; nLoop<MAX_POINTS; nLoop++)
149         // is this entry valid ?
150         if (arPPoints[nLoop] != NULL)
151             // compare the values
152             if (arPPoints[nLoop]->Equals(Point))
153                 // point found, return current position
154                 return nLoop;
155
156     // point not found
157     return -1;
158 }
159
160
161 // main function
162 void main()
163 {
164     // construct a set
165     CSetOfPoints MySet;
166
167     // do some operations to verify functionality
168     MySet.Insert(CPoint(2,4));
169     MySet.Insert(CPoint(3,5));
170     MySet.Insert(CPoint(7,9));
171
172     MySet.Scratch(CPoint(3,5));
173     MySet.Insert(CPoint(3,6));
174
175     bool bContains;
176     bContains = MySet.Contains(CPoint(2,5));
177     bContains = MySet.Contains(CPoint(2,4));
178
179     MySet.Clear();
180 }
181