-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.cpp
467 lines (341 loc) · 10.6 KB
/
code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include <iostream>
#include <cstdlib>
using namespace std;
class building
{
private:
int* area;
int totalFloors, totalApartments, totalRooms;
//////////////////////////// Utility Functions //////////////////////////////
////Gets the room areas from the user
void getAreasOfRooms()
{
int roomPosition = 0;
cout << "\nStoring the areas of the rooms in column major order.";
cout << "\nInput areas for the rooms of the building :\n\n";
//Column Major Order Offset Calculation
for (int k = 0; k < totalRooms; ++k)
{
for (int j = 0; j < totalApartments; ++j)
{
for (int i = 0; i < totalFloors; ++i)
{
//Calculating the position where the room area is to be placed
//in 1D Array (viz. Area)
roomPosition = (k * totalApartments * totalFloors) + (j * totalFloors) + i;
cout << "Enter the area of the room \""<<k+1<<"\" at apartment \""<<j+1<<"\" of floor \""<<i+1<<"\" : ";
cin >> area[roomPosition];
}
}
}
cout << "\nThe areas have been entered successfully.\n";
}
//Copies the room areas from the old array to the newly created array
//which has changed (new) dimensions
void copyAreasToNewArray(int* temp, int nFloors, int nApartments) //Here 'n' means "new" e.g "nFloors means New Floors"
{
int roomPosition, nRoomPosition = 0;
int min_floors = min (totalFloors,nFloors);
int min_apartments = min (totalApartments,nApartments);
//Column Major Order Offset Calculation
for (int k = 0; k < totalRooms; ++k) //totalRooms in an appartment never changes.
{
for (int j = 0; j < min_apartments; ++j)
{
for (int i = 0; i < min_floors; ++i)
{
//Calculating Room Position for Old Array
roomPosition = (k * totalApartments * totalFloors) + (j * totalFloors) + i;
//Calculating Room Position for New Array with Changed Dimensions
nRoomPosition = (k * nApartments * nFloors) + (j * nFloors) + i;
temp[nRoomPosition] = area[roomPosition];
}
}
}
}
public:
//Overloaded Constructor for initializing the building dynamically
//with given dimensions
building (int floors,int appartments,int rooms)
{
totalFloors = floors;
totalApartments = appartments;
totalRooms = rooms;
area = new int [totalFloors * totalApartments * totalRooms];
getAreasOfRooms ();
}
//This functions adds a new floor in the building
void addFloor (int **new_floor)
{
int* temp = new int [ (totalFloors + 1) * totalApartments * totalRooms ];
//Copying the room areas of the old array to the
//new array
copyAreasToNewArray (temp, totalFloors+1, totalApartments);
//Updating the Total Floors
totalFloors = totalFloors + 1;
int roomPosition = 0;
//Copying the room areas of the newly made Floor to the
//new array
for (int k = 0; k < totalRooms; ++k)
{
for (int j = 0; j < totalApartments; ++j)
{
//Calculating the positions for the newly made Rooms
roomPosition = (k * totalApartments * totalFloors) + (j * totalFloors) + (totalFloors-1);
temp [roomPosition] = new_floor[j][k];
}
}
cout <<"\nThe floor has been added successfully.";
delete[] area;
area = temp;
}
//This function removes the top floor from the building
bool removeTopFloor ()
{
if (totalFloors != 0)
{
int* temp = new int [ (totalFloors-1) * totalApartments * totalRooms];
copyAreasToNewArray (temp, totalFloors-1, totalApartments);
//Updating the Total Floors
totalFloors = totalFloors - 1;
cout << "\nThe top floor \""<<totalFloors+1<<"\" has been removed successfully.";
delete[] area;
area = temp;
return true;
}
else
cout <<"\nCurrently the building has no floor.\n";
return false;
}
//This functions adds a new apartment with 'r'
//rooms to each floor
bool expandEachFloor (int **new_apartment)
{
if(totalFloors != 0)
{
int* temp = new int [totalFloors * (totalApartments+1) * totalRooms];
copyAreasToNewArray (temp, totalFloors, totalApartments+1);
//Updating the Total Apartments
totalApartments = totalApartments + 1;
int roomPosition = 0;
for (int k = 0; k < totalRooms; ++k)
{
for (int j = 0; j < totalFloors; ++j)
{
//Calculating the roomPosition for the newly expanded building
roomPosition = (k * totalApartments * totalFloors) + ((totalApartments-1) * totalFloors) + j;
temp[roomPosition] = new_apartment[j][k];
}
}
cout << "\nThe building has been expanded successfully.";
delete[] area;
area = temp;
return true;
}
else
cout << "\nThe building has no floor. So it couldn't be expanded.";
return false;
}
//Returns the specified room's area
int getRoomArea(int currFloor, int currApartment, int currRoom)
{
int room_Area = -1;
if (totalFloors != 0)
{
if ( (currFloor <= totalFloors && currFloor > 0) && (currApartment <= totalApartments && currApartment > 0) && (currRoom <= totalRooms && currRoom > 0))
room_Area = area [ ((currRoom-1) * totalApartments * totalFloors) + ((currApartment-1) * totalFloors) + (currFloor-1)];
else
cout << "\nThe input is not valid.";
}
else
{
cout << "\nThe building has no floor.";
}
return room_Area;
}
//Sets the specified room's area
void setRoomArea(int currFloor, int currApartment, int currRoom, int roomArea)
{
if (totalFloors != 0)
{
if ( (currFloor <= totalFloors && currFloor > 0) && (currApartment <= totalApartments && currApartment > 0) && (currRoom <= totalRooms && currRoom > 0))
{
area [ ((currRoom-1) * totalApartments * totalFloors) + ((currApartment-1) * totalFloors) + (currFloor-1)] = roomArea;
cout << "\nThe area has been successfully set.";
}
else
cout << "\nThe input is not valid.";
}
else
cout << "\nThe building has no floor.";
}
//Outputs the area of rooms of one floor in tabular format
void outputEntireBuilding()
{
if(totalFloors != 0)
{
for (int i = 0; i < totalFloors; ++i)
{
cout <<"\nRoom Areas of Floor \""<<i+1<<"\" are :\n";
for (int j = 0; j < totalApartments; ++j)
{
cout << "Appartment \""<<j+1<<"\" : ";
for (int k = 0; k < totalRooms; ++k)
{
cout << area [(k * totalApartments * totalFloors) + (j * totalFloors) + i] << '\t';
}
cout << endl;
}
}
}
else
cout << "\nThe building has no floor.";
}
void printFunctions()
{
cout << "\n-------------------------------------------------------";
cout << "\nBuilding class has the following functionality.\n";
cout << "\n1) Add Floor";
cout << "\n2) Remove Top Floor";
cout << "\n3) Expand Each Floor";
cout << "\n4) Get Room Area";
cout << "\n5) Set Room Area";
cout << "\n6) Output Entire Building";
cout << "\n-------------------------------------------------------";
cout << "\n\nChoose any option or enter '-1' to exit : ";
}
//Destructor
~building()
{
if (area)
delete[] area;
}
};
//---------------------------------------------------------------------------------------------------------
void Driver()
{
/////Driver/////
int f,a,r;
cout << "Enter the dimensions of the building : ";
cout << "\nFloors : ";
cin >> f;
cout << "Apartments : ";
cin >> a;
cout << "Rooms : ";
cin >> r;
building b1 (f,a,r);
int userInput = 0;
int currRoom = 0,currFloor = 0,currApartment = 0;
int ** new_floor = nullptr;
int ** new_apartment = nullptr;
while (userInput != -1)
{
b1.printFunctions();
cin >> userInput;
//////////////////////////////////Add Floor//////////////////////////////////////
if(userInput == 1)
{
//Creating the new floor
new_floor = new int* [a];
for (int i = 0; i < a; ++i)
new_floor[i] = new int [r];
//Initializng the new floor
cout << "\nInput areas for the newly created floor : \n\n";
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < a; ++j)
{
cout << "Enter the area of the room \""<<i+1<<"\" at apartment \""<<j+1<<"\" of newly created floor : ";
cin >> new_floor[j][i];
}
}
//Adding the new floor
b1.addFloor(new_floor);
f = f + 1;
if (new_floor)
{
//De allocating the new floor array because we don't need that anymore
for (int i = 0; i < a; ++i)
{
delete [] new_floor[i];
}
delete[] new_floor;
new_floor = nullptr;
}
}
//////////////////////////////////Remove Top Floor//////////////////////////////////////
else if (userInput == 2)
{
if (b1.removeTopFloor())
f = f - 1;
}
//////////////////////////////////Expand Each Floor//////////////////////////////////////
else if (userInput == 3)
{
new_apartment = new int* [f];
for (int i = 0; i < f; ++i)
new_apartment[i] = new int [r];
//Initialing the new appartment
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < f; ++j)
{
cout << "Enter the area of the room \""<<i+1<<"\" of new apartment at floor \""<< j+1<<"\" : ";
cin >> new_apartment[j][i];
}
}
if( b1.expandEachFloor(new_apartment) )
a = a + 1;
if (new_apartment)
{
for (int i = 0; i < f; ++i)
delete [] new_apartment[i];
delete[] new_apartment;
new_apartment = nullptr;
}
}
//////////////////////////////////Get Room Area//////////////////////////////////////
else if (userInput == 4)
{
cout << "\nWhich room area do you want to read ? \n";
cout << "Floor Number : ";
cin >> currFloor;
cout << "Apartment Number : ";
cin >> currApartment;
cout << "Room Number : ";
cin >> currRoom;
if (b1.getRoomArea(currFloor,currApartment,currRoom) != -1)
cout << "\n\nThe area of room \""<<currRoom<<"\" of apartment \""<<currApartment<<"\" of floor \""<<currFloor
<< "\" is : \""<<b1.getRoomArea (currFloor,currApartment,currRoom) <<"\"";
}
//////////////////////////////////Set Room Area//////////////////////////////////////
else if (userInput == 5)
{
cout << "\n\nWhich room area do you want to set ? \n";
int currArea = 0;
cout << "Room Area : ";
cin >> currArea;
cout << "Floor Number : ";
cin >> currFloor;
cout << "Apartment Number : ";
cin >> currApartment;
cout << "Room Number : ";
cin >> currRoom;
b1.setRoomArea (currFloor,currApartment,currRoom,currArea);
}
//////////////////////////////////Output Entire Building//////////////////////////////////////
else if (userInput == 6)
b1.outputEntireBuilding();
else if( userInput == -1)
cout << "\nExiting !!!";
else
cout << "---Invalid Entry--- \nPlease choose from the Options below.\n ";
}
}
////////////////////Main/////////////////////////
int main()
{
Driver();
system("PAUSE");
return 0;
}