Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <time.h>
- #include <stdlib.h>
- #include <list>
- using namespace std;
- class Shape{
- public:
- Shape () {};
- virtual ~Shape() {}
- virtual bool include (pair <float, float> &x)
- {
- return 0;
- }
- virtual float square ()
- {
- return 0;
- }
- };
- class Rectangle:public Shape
- {
- public:
- pair <float, float> a;
- pair <float, float> b;
- Rectangle (pair <float, float> &x, pair <float, float> &y)
- {
- a.first = x.first;
- a.second = x.second;
- b.first = y.first;
- b.second = y.second;
- }
- ~Rectangle ()
- {
- a.first = 0;
- a.second = 0;
- b.first = 0;
- b.second = 0;
- }
- bool include (pair <float, float> &x)
- {
- if (x.first > a.first && x.first > b.first)
- return 0;
- if (x.first < a.first && x.first < b.first)
- return 0;
- if (x.second > a.second && x.second > b.second)
- return 0;
- if (x.second < a.second && x.second < b.second)
- return 0;
- return 1;
- }
- float square ()
- {
- return abs((a.first - b.first)*(a.second - b.second));
- }
- };
- void main()
- {
- srand (time(NULL));
- Shape *uk;
- pair <float, float> p1, p2;
- p1.first = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
- p1.second = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
- p2.first = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
- p2.second = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
- Rectangle *rec = new Rectangle (p1, p2);
- uk = rec;
- list <pair <float, float>> lis;
- for (int i = 0; i < 1000; ++i)
- {
- p1.first = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
- p1.second = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
- lis.push_back(p1);
- }
- int count = 0;
- list <pair <float, float>>::iterator it = lis.begin();
- for (;it != lis.end(); ++it)
- if(uk->include(*it))
- count++;
- cout << "ratio = " << float (count) / float (1000 - count) << endl << "square = " << uk->square();
- delete uk;
- getchar();
- }
Add Comment
Please, Sign In to add comment