Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. //#include <string
  5.  
  6. using namespace std;
  7.  
  8. const double PI = atan(1.0) * 4;
  9.  
  10. class Dot {
  11. double x;
  12. double y;
  13. public:
  14. Dot() { x = 0; y = 0; }
  15. Dot(double coord1, double coord2) { x = coord1; y = coord2; }
  16. Dot(const Dot& obj) { x = obj.x; y = obj.y; }
  17. void set_x(double coord) { x = coord; }
  18. void set_y(double coord) { y = coord; }
  19. double get_x() { return x; }
  20. double get_y() { return y; }
  21. };
  22.  
  23. class Circle :virtual public Dot {
  24. double radius;
  25. public:
  26. Circle() { radius = 1; }
  27. Circle(double coord1, double coord2, double r) : Dot(coord1, coord2) { radius = r; }
  28. Circle(const Circle& obj) { radius = obj.radius; }
  29. void set_radius(double r) { radius = r; }
  30. double get_radius() { return radius; }
  31. };
  32.  
  33. class Rhombus :virtual public Dot {
  34. double side;
  35. public:
  36. Rhombus() { side = 1; }
  37. Rhombus(double coord1, double coord2, double s) : Dot(coord1, coord2) { side = s; }
  38. Rhombus(const Rhombus& obj) { side = obj.side; }
  39. void set_side(double s) { side = s; }
  40. double get_side() { return side; }
  41. };
  42.  
  43. class CircleInRhombus :public Circle, public Rhombus {
  44. double square;
  45. string str;
  46. char* arr;
  47. char* generate_string();
  48. void set_square() { square = 2 * get_side() * get_radius() - PI * pow(get_radius(), 2); }
  49. public:
  50. CircleInRhombus() { square = 1; arr = generate_string(); str = arr; }
  51. CircleInRhombus(double coord1, double coord2, double r, double s) : Circle(coord1, coord2, r), Rhombus(coord1, coord2, s) { set_square(); arr = generate_string(); str = arr; cout << "Constructing\n"; }
  52. CircleInRhombus(const CircleInRhombus& obj) {
  53. if (arr) {
  54. delete[] arr;
  55. }
  56. arr = new char[strlen(obj.arr) + 1];
  57. strcpy_s(arr, strlen(obj.arr) + 1, obj.arr);
  58. str = obj.str;
  59. square = obj.square;
  60. }
  61. ~CircleInRhombus() { cout << "Destructing\n"; delete[] arr; }
  62. double get_square() { return square; }
  63. string get_str() { return str; }
  64. char* get_arr() { return arr; }
  65. };
  66.  
  67. class ShapesContainer {
  68. CircleInRhombus* array;
  69. unsigned __int32 size;
  70. public:
  71. ShapesContainer() { size = 1; array = new CircleInRhombus[size]; }
  72. ~ShapesContainer() { delete [] array; }
  73. void add(CircleInRhombus& obj);
  74. unsigned __int32 get_size() { return size; }
  75. CircleInRhombus& operator[](unsigned __int32 index);
  76. CircleInRhombus* get_array() { return array; }
  77. };
  78.  
  79. char* CircleInRhombus::generate_string() {;
  80. int size = rand() % 1000;
  81. arr = new char[size + 1];
  82. for (unsigned int i = 0; i < size; i++) {
  83. arr[i] = 'a' + rand() % ('z' - 'a');
  84. }
  85. arr[size] = '\0';
  86. return arr;
  87. }
  88.  
  89. void ShapesContainer::add(CircleInRhombus& obj) {
  90. CircleInRhombus* temp = new CircleInRhombus[size + 1];
  91. if (!temp) {
  92. cout << "Allocation error.\n";
  93. exit(1);
  94. }
  95. for (unsigned __int32 i = 1; i < size - 1; i++) {
  96. temp[i] = array[i];
  97. }
  98. temp[size - 1] = obj;
  99. delete[] array;
  100. array = temp;
  101. size++;
  102. }
  103.  
  104. CircleInRhombus& ShapesContainer::operator[](unsigned __int32 index){
  105. if (index < 0 || index >= size) {
  106. cout << "Bound Error.\n";
  107. exit(1);
  108. }
  109. return array[index];
  110. }
  111.  
  112. double average(ShapesContainer& obj) {
  113. double num = 0;
  114. for (unsigned __int32 i = 0; i < obj.get_size(); i++) {
  115. num += 1 / obj[i].get_square();
  116. }
  117. return obj.get_size() / num;
  118. }
  119.  
  120. CircleInRhombus generate() {
  121. double radius, side;
  122. do {
  123. radius = rand() % 100;
  124. side = rand() % 100;
  125. } while (side < radius * PI / 2);
  126. CircleInRhombus temp(rand() % 100, rand() % 100, radius, side);
  127. return temp;
  128. }
  129.  
  130. int compare_char(CircleInRhombus* obj1, CircleInRhombus* obj2) {
  131. return strcmp(obj1->get_arr(), obj2->get_arr());
  132. }
  133.  
  134. int compare_string(CircleInRhombus* obj1, CircleInRhombus* obj2) {
  135. return obj1->get_str().compare(obj2->get_str());
  136. }
  137.  
  138. int main() {
  139. srand(time(NULL));
  140. ShapesContainer obj;
  141. for (unsigned __int32 i = 0; i < 5; i++) {
  142. CircleInRhombus temp(generate());
  143. obj.add(temp);
  144. }
  145. cout << average(obj) << endl;
  146. //qsort(obj.get_array(), obj.get_size(), sizeof(CircleInRhombus), (int(*) (const void*, const void*))compare_char);
  147. for (int i = 0; i < 5; i++) {
  148. cout << obj[i].get_arr() << endl;
  149. }
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement