Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. void boxSort(Box array[], int size) {
  2. Box temp;
  3. bool swap;
  4.  
  5. do {
  6. swap = false;
  7. for(int count=0; count<(size-1); count++) {
  8. int volume1 = array[count].getVolume(array[count].height, array[count].width, array[count].length);
  9. int volume2 = array[count+1].getVolume(array[count+1].height, array[count+1].width, array[count+1].length);
  10. if(volume1 > volume2) {
  11. temp = array[count];
  12. array[count] = array[count+1];
  13. array[count+1] = temp;
  14. swap = true;
  15. }
  16. }
  17. }
  18. while(swap);
  19. }
  20.  
  21. class Box {
  22. public:
  23. double height, width, length;
  24. double getVolume(double, double, double);
  25. double getSurfaceArea(double, double, double);
  26. void setHeight(double);
  27. void setWidth(double);
  28. void setLength(double);
  29.  
  30. Box() {
  31. height = width = length = 1;
  32. }
  33. Box(double h, double w, double l) {
  34. setHeight(h);
  35. setWidth(w);
  36. setLength(l);
  37. }
  38. };
  39.  
  40. #endif
  41.  
  42. void Box::setHeight(double h) {
  43. height = h;
  44. }
  45.  
  46. void Box::setWidth(double w) {
  47. width = w;
  48. }
  49.  
  50. void Box::setLength(double l) {
  51. length = l;
  52. }
  53. double Box::getVolume(double h, double w, double l) {
  54. double volume = h*w*l;
  55.  
  56. return volume;
  57. }
  58. double Box::getSurfaceArea(double h, double w, double l) {
  59. double surfaceArea = (h*w)*2 + (h*l)*2 + (l*w)*2;
  60.  
  61. return surfaceArea;
  62. }
  63.  
  64. std::sort()
  65.  
  66. class Box
  67. {
  68. public:
  69. bool operator<(Box const& rhs) const
  70. {
  71. // compare this against rhs.
  72. // if this is less return true otherwise false.
  73. }
  74. };
  75.  
  76. std::sort(begin, end, [](Box const& lhs, Box const& rhs) {return result <Some Test>;});
  77.  
  78. void boxSort(Box array[], int size) {
  79.  
  80. template<typename I>
  81. void boxSort(I begin, I end) {
  82.  
  83. temp = array[count];
  84. array[count] = array[count+1];
  85. array[count+1] = temp;
  86.  
  87. std::swap(array[count], array[count+1]);
  88.  
  89. Box() {
  90. height = width = length = 1;
  91. }
  92.  
  93. Box(double h = 1, double w = 1, double l = 1) {
  94. setHeight(h);
  95. setWidth(w);
  96. setLength(l);
  97. }
  98.  
  99. double height, width, length;
  100.  
  101. double height;
  102. double width;
  103. double length;
  104.  
  105. double Box::getVolume(double h, double w, double l) {
  106. double volume = h*w*l;
  107.  
  108. return volume;
  109. }
  110.  
  111. // This is what I would expect to see.
  112. double Box::getVolume() const {
  113. return height * width * length;
  114. }
  115.  
  116. int main()
  117. {
  118. Box data[15];
  119. // Define some boxes here.
  120.  
  121. std::sort(std::begin(data), std::end(data),
  122. [](Box const& lhs, Box const& rhs) {
  123. return lhs.getVolue() > rhs.getVolume();}
  124. );
  125. }
  126.  
  127. void sort_by_volume(Box *array, size_t n)
  128. {
  129. auto volume = [](const Box& a) {
  130. return a.height * a.width * a.length;
  131. };
  132.  
  133. std::sort(array, array+n, [](const Box& a, const Box& b) {
  134. return volume(a) < volume(b);
  135. });
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement