/////////////////////////////////////////////////////////// // Softwarebauelemente I, Aufgabe M6.2. // // author: Stephan Brumme // last changes: December 29, 2000 #include #include "MDate.h" #include "MRoom.h" #include "MHouse.h" using namespace MDate; // initializes the THouse structure void MHouse::Init(THouse &house) { house.Count = 0; house.Cursor = -1; Today(house.DateOfFoundation); } // compares two exemplars // returns "true" if attributes of both are equal; "false" otherwise Boolean MHouse::EqualValue(const THouse& house1, const THouse& house2) { // verify number of rooms and date of foundation if (!(house1.Count == house2.Count && MDate::EqualValue(house1.DateOfFoundation, house2.DateOfFoundation))) return false; // dates and numbers of rooms are equal // now compare each room // !!! they must be in the same order, shuffled houses are NOT recognized !!! // are houses empty ? => they are equal if (house1.Count == 0) return true; // compare each room for (Ordinal nRun = 0; nRun < house1.Count; nRun++) if (!MRoom::EqualValue(house1.PSet[nRun], house2.PSet[nRun])) return false; // all rooms are equal, so the houses are equal return true; } // copies the attributes of house2 // returns "true" if successful, "false" if no memory allocated Boolean MHouse::Copy(THouse& house1, const THouse& house2) { // are both houses equal ? if (EqualValue(house1, house2)) return false; // copy all attributes house1.Count = house2.Count; house1.Cursor = house2.Cursor; MDate::Copy(house1.DateOfFoundation, house2.DateOfFoundation); if (house2.Count > 0) for (Ordinal nRun = 0; nRun < house2.Count; nRun++) MRoom::Copy(house1.PSet[nRun], house2.PSet[nRun]); // successfully done return true; } // retrieve date of foundation TDate MHouse::GetDateOfFoundation(const THouse& house) { TDate date; MDate::Copy(date, house.DateOfFoundation); return date; } // get number of rooms Ordinal MHouse::Card(const THouse& house) { return house.Count; } // add a new room to a house Boolean MHouse::Insert(THouse& house, const MRoom::TRoom& room) { // is any free space available ? if (house.Count >= ROOMS) return false; // copy room, care for errors if (!MRoom::Copy(house.PSet[house.Count], room)) return false; // set cursor house.Cursor = house.Count; // increase size of array house.Count++; return true; } // returns the first room of a house Boolean MHouse::GetFirst(THouse& house, MRoom::TRoom& room) { // is house empty ? if (house.Count == 0) return false; // set cursor house.Cursor = 0; // and return room return GetCurrent(house, room); } // returns the last room of a house Boolean MHouse::GetNext(THouse& house, MRoom::TRoom& room) { // verify that current cursor position is valid // they must be a next room, otherwise function will fail if ((house.Cursor < 0) || (house.Cursor >= house.Count)) return false; // set cursor to next room house.Cursor++; return GetCurrent(house, room); } // looks for a given room and sets cursor, if possible Boolean MHouse::Find(THouse& house, const MRoom::TRoom& room) { // is house empty ? if (house.Count == 0) return false; for (Ordinal nRun=0; nRun= house.Count)) return false; return MRoom::Copy(room, house.PSet[house.Cursor]); } // deletes the room the cursor points to Boolean MHouse::Scratch(THouse& house) { // is house empty and cursor valid ? if ((house.Count == 0) || (house.Cursor < 0) || (house.Cursor >= house.Count)) return false; // move all rooms beyond house.Cursor one position ahead for (Ordinal nRun = house.Cursor; nRun < house.Count; nRun++) // verify copy if (!MRoom::Copy(house.PSet[nRun], house.PSet[nRun+1])) return false; // reset Cursor and Count house.Cursor--; house.Count--; // we're done return true; } // displays the attributes void MHouse::Show(THouse house) { using namespace std; // display general room info cout<<"Das Haus besteht aus "< 0) for (Ordinal nRun=0; nRun