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