Answered step by step
Verified Expert Solution
Question
1 Approved Answer
c++ I have the following code from the problem beneath it. I believe I have done everything correctly besides implementing the A or Area query.
c++
I have the following code from the problem beneath it. I believe I have done everything correctly besides implementing the A or Area query. It is explained in the problem below but I am unsure how to do so. I also dont know how to release the memory with a destructor as mentioned at the end of the problem.
Thank you for your help.
#include
#include
#include
#include
#include
using namespace std;
// Rectangle structure
struct Rect
{
int width;
int height;
};
// Rectangle manager class starts
class RectMgr
{
private:
Rect rectangles[10];
int numRects;
public:
// Method to load data in the rectangle array
void loadData(string filename){
ifstream in(filename.c_str());
if(in.fail())
{
cout << "ERROR: could not open input file " << filename << endl;
exit(1);
}
int w, h;
numRects = 0;
while(in >> w >> h)
{
rectangles[numRects].width = w;
rectangles[numRects].height = h;
numRects++;
}
in.close();
}
// Default constructor
RectMgr()
{
loadData("input.txt"); // default input file
}
// Parameterized constructor
RectMgr(string filename)
{
loadData(filename);
}
// Method to display valid triangles
void displayRect(char type)
{
for(int i = 0; i < numRects; i++)
{
if(((type == 'v' || type =='V') && rectangles[i].height > rectangles[i].width)
|| ((type == 'h' || type =='H') && rectangles[i].height < rectangles[i].width)
|| ((type == 's' || type =='S') && rectangles[i].height == rectangles[i].width))
cout << "R" << setw(3) << setfill('0') << (i+1) << " "
<< rectangles[i].width << " " << rectangles[i].height << endl;
}
}
};
int main()
{
string filename;
cout << "Enter input filename: ";
cin >> filename;
RectMgr mgr(filename);
string choice;
while(choice != "q" && choice != "Q")
{
// Get user input
cout << "(V)ertical \t (H)orizontal \t (S)quare \t (Q)uit" << endl;
cout << "Your choice: ";
cin >> choice;
// display rectangles as per the selection
if(choice != "q" && choice != "Q")
mgr.displayRect(choice[0]);
cout << endl;
}
cout << "Good Bye!" << endl;
}
Requirement:
You are assigned to design the foundation for a system which stores various shapes and the user may query what are available. The system will store many different shapes, such as rectangles, circles, triangles, lines. But in the first phase, you only need to consider three types of rectangles. The following is a listing of requirements.
Three types of rectangle class, i.e. Vertical, Horizontal and Square, are stored in the system.
As shown below the input file named rectangles.txt has 10 rows, each row has two columns. The first column is the width and second height.
3 4
2 2
4 9
7 3
5 5
2 10
4 2
3 6
3 9
10 10
The system accepts queries from the user. The valid input are V, H, S, A, and Q.
Input
Meaning
Action
V
Vertical
When the user enters a V, the system returns with all the Vertical rectangles, and the output should like the following.
R001 3 4
R003 4 9
R005 2 10
R003 3 6
R003 3 9
The first column is the label of the rectangle, which is automatically generated by the system. (The label should an attribute of a rectangle.) The second and third columns are the width and height, respectively.
H
Horizontal
When the user enters an H, the system returns with all the Horizontal rectangles.
S
Square
When the user enters a S, the system returns with all the Horizontal squares.
A
Area
If the user enters an A, the system should then ask the user to enter the label of the rectangle. With the label, the system responds with the label, width, height and area. For example, if the user enters R001, then the system should respond with
R001 3 4 12
Q
Quit
After the user enters Q, the system is terminated.
The class named Rectangle has all the attributes of rectangles. Its data member named type is for storing the type of the rectangle.
The class named Manager is for managing the shapes. At this moment an array of size 10 is needed for storing the 10 rectangles. The array should store the pointers to the objects which are constructed in rum time. When the system is terminated, all memory has to be released.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started