Question
Create a Stash class specifically for storing Rect objects and call it RectStash. Add a default constructor and a destructor to correctly initialize your RectStash
Create a Stash class specifically for storing Rect objects and call it RectStash. Add a default constructor and a destructor to correctly initialize your RectStash class. Then write a program that will read several lines as input. Each line will contain 4 floats defining a 2D rectangle in the Rect format described above. Read the rectangles adding them to a RectStash object. Stop reading rectangles when your program loads 4 negative float values. After this point you will start reading a series of 2D points, and for each 2D point you will print the classification of each point in respect to all previously read rectangles. The classification should print "in" or "out" according to its result. Stop your program when you read vector (-99,-99). Everything should be contained in one file. You may not assume the existance of any header files in your working directory.
This is what I have so far, but it doesn't produce the correct output.
#include #include
using namespace std;
class Vec {
public: int x, y; static Vec const null; Vec() { this->x = 0; this->y = 0; } Vec(int x, int y) { this->x = x; this->y = y; } void add(Vec othervec) { x += othervec.x; y += othervec.y; } void print() { cout
class Rect { float upper_x, upper_y, width, height;
public: Rect(float x, float y, float width, float height) { this->upper_x = x; this->upper_y = y; this->width = width; this->height = height; } bool contains(Vec othervec) { //check x intersection
if (othervec.x > upper_x + width || othervec.x upper_y) { return false; }
return true; } };
class RectStash { int size; // Size of each space int quantity; // Number of storage spaces int next; // Next empty space
// Dynamically allocated array of bytes: unsigned char* storage; public:
// Functions! void initialize(int sz){ size = sz; quantity = 0; storage = 0; next = 0; }
void cleanup(){ if (storage != 0) { std::cout
int add(const void* element){ if (next >= quantity) // Enough space left? inflate(100);
// Copy element into storage, // starting at next empty space: int startBytes = next * size; unsigned char* e = (unsigned char*)element;
for (int i = 0; i
return(next - 1); // Index number }
void* fetch(int index){ // Check index boundaries: assert(0
if (index >= next) return 0; // To indicate the end
// Produce pointer to desired element: return &(storage[index * size]); }
int count() { return next; // Number of elements in CStash }
void inflate(int increase){ assert(increase > 0);
int newQuantity = quantity + increase; int newBytes = newQuantity * size; int oldBytes = quantity * size; unsigned char* b = new unsigned char[newBytes];
for (int i = 0; i
delete[]storage; // Old storage storage = b; // Point to new memory quantity = newQuantity; } RectStash() { initialize(sizeof(Rect*)); } };
int main(int argc, const char * argv[]) { float a, b, c, d; RectStash storage; Rect * tmp; Vec vector; do { cin >> a; cin >> b; cin >> c; cin >> d; tmp = new Rect(a, b, c, d); if (a >= 0 || b >= 0 || c >= 0 || d >= 0) { storage.add(tmp); } } while (a >= 0 || b >= 0 || c >= 0 || d >= 0);
do { cin >> a; cin >> b; vector = Vec(a, b); if (a != -99 || b != -99) { int i = 0; tmp = static_cast (storage.fetch(i)); while (tmp != 0) { if (tmp->contains(vector)) cout out out in out out out -5 -5 2.5 2.5 58 22 -1-1-1-1 00 |-4-6 69 -99-99
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