sources:
M07_2.cpp (4.8k)
MDate.cpp (1.8k)
MDate.h (956 bytes)
MHouse.cpp (4.9k)
MHouse.h (1.9k)
MRoom.cpp (1.5k)
MRoom.h (1.1k)
PrimitiveTypes.h (421 bytes)


website:
more info here
studies/bauelemente/Softwarebauelemente-CodeM7-2/MHouse.cpp
download file

  1 ///////////////////////////////////////////////////////////
  2 // Softwarebauelemente I, Aufgabe M6.2.
  3 //
  4 // author: Stephan Brumme
  5 // last changes: December 29, 2000
  6
  7
  8 #include <iostream>
  9 #include "MDate.h"
 10 #include "MRoom.h"
 11 #include "MHouse.h"
 12
 13
 14 using namespace MDate;
 15
 16
 17 // initializes the THouse structure
 18 void MHouse::Init(THouse &house)
 19 {
 20     house.Count  = 0;
 21     house.Cursor = -1;
 22     Today(house.DateOfFoundation);
 23 }
 24
 25
 26 // compares two exemplars
 27 // returns "true" if attributes of both are equal; "false" otherwise
 28 Boolean MHouse::EqualValue(const THouse& house1, const THouse& house2)
 29 {
 30     // verify number of rooms and date of foundation
 31     if (!(house1.Count == house2.Count &&
 32         MDate::EqualValue(house1.DateOfFoundation, house2.DateOfFoundation))
)

 33         return false;
 34
 35     // dates and numbers of rooms are equal
 36     // now compare each room
 37     // !!! they must be in the same order, shuffled houses are NOT recognized !!!
 38
 39     // are houses empty ? => they are equal
 40     if (house1.Count == 0)
 41         return true;
 42
 43     // compare each room
 44     for (Ordinal nRun = 0; nRun < house1.Count; nRun++)
 45         if (!MRoom::EqualValue(house1.PSet[nRun], house2.PSet[nRun]))
 46             return false;
 47
 48     // all rooms are equal, so the houses are equal
 49     return true;
 50 }
 51
 52
 53 // copies the attributes of house2
 54 // returns "true" if successful, "false" if no memory allocated
 55 Boolean MHouse::Copy(THouse& house1, const THouse& house2)
 56 {
 57     // are both houses equal ?
 58     if (EqualValue(house1, house2))
 59         return false;
 60
 61     // copy all attributes
 62     house1.Count  = house2.Count;
 63     house1.Cursor = house2.Cursor;
 64     MDate::Copy(house1.DateOfFoundation, house2.DateOfFoundation);
 65
 66     if (house2.Count > 0)
 67         for (Ordinal nRun = 0; nRun < house2.Count; nRun++)
 68             MRoom::Copy(house1.PSet[nRun], house2.PSet[nRun]);
 69
 70     // successfully done
 71     return true;
 72 }
 73
 74
 75 // retrieve date of foundation
 76 TDate MHouse::GetDateOfFoundation(const THouse& house)
 77 {
 78     TDate date;
 79     MDate::Copy(date, house.DateOfFoundation);
 80
 81     return date;
 82 }
 83
 84
 85 // get number of rooms
 86 Ordinal MHouse::Card(const THouse& house)
 87 {
 88     return house.Count;
 89 }
 90
 91
 92 // add a new room to a house
 93 Boolean MHouse::Insert(THouse& house, const MRoom::TRoom& room)
 94 {
 95     // is any free space available ?
 96     if (house.Count >= ROOMS)
 97         return false;
 98
 99     // copy room, care for errors
100     if (!MRoom::Copy(house.PSet[house.Count], room))
101         return false;
102
103     // set cursor
104     house.Cursor = house.Count;
105     // increase size of array
106     house.Count++;
107
108     return true;
109 }
110
111
112 // returns the first room of a house
113 Boolean MHouse::GetFirst(THouse& house, MRoom::TRoom& room)
114 {
115     // is house empty ?
116     if (house.Count == 0)
117         return false;
118
119     // set cursor
120     house.Cursor = 0;
121
122     // and return room
123     return GetCurrent(house, room);
124 }
125
126
127 // returns the last room of a house
128 Boolean MHouse::GetNext(THouse& house, MRoom::TRoom& room)
129 {
130     // verify that current cursor position is valid
131     // they must be a next room, otherwise function will fail
132     if ((house.Cursor < 0) ||
133         (house.Cursor >= house.Count))

134         return false;
135
136     // set cursor to next room
137     house.Cursor++;
138        
139     return GetCurrent(house, room);
140 }
141
142
143 // looks for a given room and sets cursor, if possible
144 Boolean MHouse::Find(THouse& house, const MRoom::TRoom& room)
145 {
146     // is house empty ?
147     if (house.Count == 0)
148         return false;
149
150     for (Ordinal nRun=0; nRun<house.Count; nRun++)
151         if (MRoom::EqualValue(house.PSet[nRun], room))
152         {
153             house.Cursor = nRun;
154             return true;
155         }
156
157     // no room found, cursor remains unchanged
158     return false;
159 }
160
161
162 // returns the room the cursors points to
163 Boolean MHouse::GetCurrent(const THouse& house, MRoom::TRoom& room)
164 {
165     // verify that room exists
166     if ((house.Cursor < 0) ||
167         (house.Cursor >= house.Count))

168         return false;
169
170     return MRoom::Copy(room, house.PSet[house.Cursor]);
171 }
172
173
174 // deletes the room the cursor points to
175 Boolean MHouse::Scratch(THouse& house)
176 {
177     // is house empty and cursor valid ?
178     if ((house.Count == 0) ||
179         (house.Cursor < 0) ||
180         (house.Cursor >= house.Count))

181         return false;
182
183     // move all rooms beyond house.Cursor one position ahead
184     for (Ordinal nRun = house.Cursor; nRun < house.Count; nRun++)
185         // verify copy
186         if (!MRoom::Copy(house.PSet[nRun], house.PSet[nRun+1]))
187             return false;
188    
189     // reset Cursor and Count
190     house.Cursor--;
191     house.Count--;
192
193     // we're done
194     return true;
195 }
196
197
198 // displays the attributes
199 void MHouse::Show(THouse house)
200 {
201     using namespace std;
202
203     // display general room info
204     cout<<"Das Haus besteht aus "<<house.Count<<" Zimmern."<<endl;
205     cout<<"Das aktuelle Zimmer ist "<<house.Cursor<<endl;
206
207     // foundation date
208     cout<<"Das Haus wurde gebaut am ";
209     MDate::Show(house.DateOfFoundation);
210     cout<<endl;
211    
212     // display each room
213     if (house.Count > 0)
214         for (Ordinal nRun=0; nRun<house.Count; nRun++)
215         {
216             cout<<"Raum "<<nRun<<": ";
217             MRoom::Show(house.PSet[nRun]);
218         }
219     else         cout<<"Das Haus ist leer."<<endl;
220 }
221
222