Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void boxSort(Box array[], int size) {
- Box temp;
- bool swap;
- do {
- swap = false;
- for(int count=0; count<(size-1); count++) {
- int volume1 = array[count].getVolume(array[count].height, array[count].width, array[count].length);
- int volume2 = array[count+1].getVolume(array[count+1].height, array[count+1].width, array[count+1].length);
- if(volume1 > volume2) {
- temp = array[count];
- array[count] = array[count+1];
- array[count+1] = temp;
- swap = true;
- }
- }
- }
- while(swap);
- }
- class Box {
- public:
- double height, width, length;
- double getVolume(double, double, double);
- double getSurfaceArea(double, double, double);
- void setHeight(double);
- void setWidth(double);
- void setLength(double);
- Box() {
- height = width = length = 1;
- }
- Box(double h, double w, double l) {
- setHeight(h);
- setWidth(w);
- setLength(l);
- }
- };
- #endif
- void Box::setHeight(double h) {
- height = h;
- }
- void Box::setWidth(double w) {
- width = w;
- }
- void Box::setLength(double l) {
- length = l;
- }
- double Box::getVolume(double h, double w, double l) {
- double volume = h*w*l;
- return volume;
- }
- double Box::getSurfaceArea(double h, double w, double l) {
- double surfaceArea = (h*w)*2 + (h*l)*2 + (l*w)*2;
- return surfaceArea;
- }
- std::sort()
- class Box
- {
- public:
- bool operator<(Box const& rhs) const
- {
- // compare this against rhs.
- // if this is less return true otherwise false.
- }
- };
- std::sort(begin, end, [](Box const& lhs, Box const& rhs) {return result <Some Test>;});
- void boxSort(Box array[], int size) {
- template<typename I>
- void boxSort(I begin, I end) {
- temp = array[count];
- array[count] = array[count+1];
- array[count+1] = temp;
- std::swap(array[count], array[count+1]);
- Box() {
- height = width = length = 1;
- }
- Box(double h = 1, double w = 1, double l = 1) {
- setHeight(h);
- setWidth(w);
- setLength(l);
- }
- double height, width, length;
- double height;
- double width;
- double length;
- double Box::getVolume(double h, double w, double l) {
- double volume = h*w*l;
- return volume;
- }
- // This is what I would expect to see.
- double Box::getVolume() const {
- return height * width * length;
- }
- int main()
- {
- Box data[15];
- // Define some boxes here.
- std::sort(std::begin(data), std::end(data),
- [](Box const& lhs, Box const& rhs) {
- return lhs.getVolue() > rhs.getVolume();}
- );
- }
- void sort_by_volume(Box *array, size_t n)
- {
- auto volume = [](const Box& a) {
- return a.height * a.width * a.length;
- };
- std::sort(array, array+n, [](const Box& a, const Box& b) {
- return volume(a) < volume(b);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement