/////////////////////////////////////////////////////////// // Softwarebauelemente I, Aufgabe M2.1 // // author: Stephan Brumme // last changes: October 18, 2000 // we need const NULL #include // simple class for storing a single point // two constructors to ease creation // contains Equals method which compares the coordinates of two points class CPoint { public: // default constructor, zero the member variables CPoint() { m_nX = 0; m_nY = 0; } // easier creation of a point CPoint(int X, int Y) { m_nX = X; m_nY = Y; } // public member variables int m_nX, m_nY; // compares two points bool Equals(CPoint Point) { return ((m_nX == Point.m_nX) && (m_nY == Point.m_nY)); } }; // class for storing a set of points in an array // each entry is implemented as a pointer to CPoint (see above) class CSetOfPoints { public: // default constructor CSetOfPoints(); // clear the set void Clear(); // insert a point to the set, returns true on success bool Insert(CPoint Point); // remove a point from the set, returns true on success bool Scratch(CPoint Point); // determine whether a point is already in the set bool Contains(CPoint Point); // max. number of stored points enum { MAX_POINTS = 10 }; protected: // set all pointers to NULL void ZeroMem(); // get array position of a specified point, -1 on error int GetPos(CPoint Point); // storage CPoint* arPPoints[MAX_POINTS]; }; // default constructor CSetOfPoints::CSetOfPoints() { // memory is randomly set on startup // initialize all array entry by setting them to NULL ZeroMem(); } // clear the set void CSetOfPoints::Clear() { // delete each entry that is non-NULL for (int nLoop=0; nLoopEquals(Point)) // point found, return current position return nLoop; // point not found return -1; } // main function void main() { // construct a set CSetOfPoints MySet; // do some operations to verify functionality MySet.Insert(CPoint(2,4)); MySet.Insert(CPoint(3,5)); MySet.Insert(CPoint(7,9)); MySet.Scratch(CPoint(3,5)); MySet.Insert(CPoint(3,6)); bool bContains; bContains = MySet.Contains(CPoint(2,5)); bContains = MySet.Contains(CPoint(2,4)); MySet.Clear(); }