Let us assume all the classrooms are of rectangular size. Each ClassRoom has the
ID: 3546046 • Letter: L
Question
Let us assume all the classrooms are of rectangular size. Each ClassRoom has the following information: Room #, # of seats, length & width. After reading the data from input file to an array of ClassRooms, allow the user to search for best classroom fits your needs. Here is the sample contents of rooms.txt:
ES2.410 110 50 60
ES2.412 110 60 60
ES2.414 110 60 65
ES2.415 110 50 60
EN2.112 50 30 40
EN2.108 55 40 50
EN2.104 52 35 40
EN2.116 53 40 50
EN2.112 50 50 40
After reading input the file, prompt the user for # of seats. Search the array of classrooms to find the best room for the event. Selected room should have # of seats >= user input. Among such candidate rooms, select the room with max per-seat area (you can compute it by length * width / # of seats).
Enter MAX capacity: 50
Your best room is EN2.108
Enter MAX capacity: 100
Your best room is ES2.414
Explanation / Answer
//ClassRoom.h
#ifndef CLASSROOM_H
#define CLASSROOM_H
#include <string>
class ClassRoom
{
public:
ClassRoom(std::string roomNum="", int seats=0, double len=0, double wid=0)
: roomNum(roomNum), numSeats(seats), length(len), width(wid) { }
int getSeats()const { return numSeats; }
double getAreaPerSeat()const { return length*width / numSeats; }
std::string getRoomNumber()const { return roomNum; }
private:
std::string roomNum;
int numSeats;
double length, width;
};
#endif // CLASSROOM_H
//(no ClassRoom.cpp needed)
//ClassRooms.h
#ifndef CLASSROOMS_H
#define CLASSROOMS_H
#include "ClassRoom.h"
#include <string>
class ClassRooms
{
public:
ClassRooms(int nRooms=100)
: numRooms(0), rooms(new ClassRoom[nRooms]) { }
~ClassRooms() { delete [] rooms; }
void addRoom(const ClassRoom& cr) { rooms[numRooms++] = cr; }
std::string findRoom(int seats)const;
private:
int numRooms;
ClassRoom *rooms;
};
#endif //CLASSROOMS_H
//ClassRooms.cpp
#include "ClassRooms.h"
std::string ClassRooms::findRoom(int seats)const
{
int maxIndex = 0;
while (maxIndex < numRooms && rooms[maxIndex].getSeats() < seats)
++maxIndex;
if (maxIndex == numRooms) return "";
for (int i = 0; i < numRooms; ++i)
if (rooms[i].getSeats() >= seats
&& rooms[i].getAreaPerSeat() > rooms[maxIndex].getAreaPerSeat())
maxIndex = i;
return rooms[maxIndex].getRoomNumber();
}
//main.cpp
#include <iostream>
#include <fstream>
#include "ClassRooms.h"
#include <string>
int main()
{
std::ifstream fin("rooms.txt");
if (!fin)
{
std::cout << "Cannot open file ";
return 1;
}
std::string roomNum;
int seats;
double len, wid;
ClassRooms classRooms;
while (fin >> roomNum >> seats >> len >> wid)
classRooms.addRoom(ClassRoom(roomNum, seats, len, wid));
fin.close();
while (seats > 0)
{
std::cout << "Enter MAX capacity: ";
std::cin >> seats;
std::cout << "Your best room is " << classRooms.findRoom(seats) << " ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.