Guest User

Untitled

a guest
Jun 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <list>
  6. using namespace std;
  7.  
  8. class Shape{
  9. public:
  10. Shape () {};
  11. virtual ~Shape() {}
  12. virtual bool include (pair <float, float> &x)
  13. {
  14. return 0;
  15. }
  16. virtual float square ()
  17. {
  18. return 0;
  19. }
  20. };
  21.  
  22. class Rectangle:public Shape
  23. {
  24. public:
  25. pair <float, float> a;
  26. pair <float, float> b;
  27. Rectangle (pair <float, float> &x, pair <float, float> &y)
  28. {
  29. a.first = x.first;
  30. a.second = x.second;
  31. b.first = y.first;
  32. b.second = y.second;
  33. }
  34. ~Rectangle ()
  35. {
  36. a.first = 0;
  37. a.second = 0;
  38. b.first = 0;
  39. b.second = 0;
  40. }
  41. bool include (pair <float, float> &x)
  42. {
  43. if (x.first > a.first && x.first > b.first)
  44. return 0;
  45. if (x.first < a.first && x.first < b.first)
  46. return 0;
  47. if (x.second > a.second && x.second > b.second)
  48. return 0;
  49. if (x.second < a.second && x.second < b.second)
  50. return 0;
  51. return 1;
  52. }
  53. float square ()
  54. {
  55. return abs((a.first - b.first)*(a.second - b.second));
  56. }
  57. };
  58.  
  59. void main()
  60. {
  61. srand (time(NULL));
  62. Shape *uk;
  63. pair <float, float> p1, p2;
  64. p1.first = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
  65. p1.second = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
  66. p2.first = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
  67. p2.second = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
  68. Rectangle *rec = new Rectangle (p1, p2);
  69. uk = rec;
  70. list <pair <float, float>> lis;
  71. for (int i = 0; i < 1000; ++i)
  72. {
  73. p1.first = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
  74. p1.second = rand() % 10 + rand() % 10 * 0.1 + rand () % 10 * 0.01;
  75. lis.push_back(p1);
  76. }
  77. int count = 0;
  78. list <pair <float, float>>::iterator it = lis.begin();
  79. for (;it != lis.end(); ++it)
  80. if(uk->include(*it))
  81. count++;
  82. cout << "ratio = " << float (count) / float (1000 - count) << endl << "square = " << uk->square();
  83. delete uk;
  84. getchar();
  85. }
Add Comment
Please, Sign In to add comment