Advertisement
SteveStage

HW 7

Feb 19th, 2023
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. class spell;
  6.  
  7. template<typename T>
  8. void heapSort(std::vector<T>& v);
  9. template<typename T>
  10. void heapify(std::vector<T>& v, const int& end);
  11. template<typename T>
  12. void siftDown(std::vector<T>& v, const int& start, const int& end);
  13. template<typename T>
  14. void heapSortComparator(std::vector<T>& v);
  15. template<typename T>
  16. void heapifyComparator(std::vector<T>& v, const int& end);
  17. template<typename T>
  18. void siftDownComparator(std::vector<T>& v, const int& start, const int& end);
  19. bool lessSpell(const spell& a, const spell& b);
  20.  
  21. class spell
  22. {
  23.     std::string name;
  24.     int power, cost;
  25. public:
  26.     spell(const std::string& n, const int& p, const int& c) : name(n), power(p), cost(c) {}
  27.     bool operator<(const spell& s)
  28.     {
  29.         return (this->cost < s.cost);
  30.     }
  31.     bool operator<=(const spell& s)
  32.     {
  33.         return (this->cost <= s.cost);
  34.     }
  35.     bool operator>(const spell& s)
  36.     {
  37.         return (this->cost > s.cost);
  38.     }
  39.     bool operator>=(const spell& s)
  40.     {
  41.         return (this->cost >= s.cost);
  42.     }
  43.     const std::string& getName() const
  44.     {
  45.         return this->name;
  46.     }
  47.     const int& getPower() const
  48.     {
  49.         return this->power;
  50.     }
  51.     const int& getCost() const
  52.     {
  53.         return this->cost;
  54.     }
  55. };
  56.  
  57. int main()
  58. {
  59.     std::vector<spell> v1, v2;
  60.     for (int i = 0; i < 10; i++)
  61.     {
  62.         std::string name;
  63.         int power, cost;
  64.         std::cout << i+1 << ". Enter spell name: ";
  65.         std::getline(std::cin >> std::ws, name);
  66.         std::cout << "Enter spell power: ";
  67.         std::cin >> power;
  68.         std::cout << "Enter spell mana cost: ";
  69.         std::cin >> cost;
  70.         spell o(name, power, cost);
  71.         v1.push_back(o);
  72.     }
  73.     v2 = v1;
  74.     heapSort(v1);
  75.     heapSortComparator(v2);
  76.     std::cout << "Default heapsort:" << std::endl;
  77.     for (auto i : v1)
  78.     {
  79.         std::cout << "Spell <" << i.getName() << "> with power " << i.getPower() << " and mana cost " << i.getCost() << std::endl;
  80.     }
  81.     std::cout << "Comparator heapsort:" << std::endl;
  82.     for (auto i : v2)
  83.     {
  84.         std::cout << "Spell <" << i.getName() << "> with power " << i.getPower() << " and mana cost " << i.getCost() << std::endl;
  85.     }
  86.     return 0;
  87. }
  88.  
  89. template<typename T>
  90. void heapSort(std::vector<T>& v)
  91. {
  92.     int end = v.size()-1;
  93.     heapify(v, end);
  94.     while (end >= 0)
  95.     {
  96.         std::swap(v[0], v[end]);
  97.         end--;
  98.         siftDown(v, 0, end);
  99.     }
  100. }
  101.  
  102. template<typename T>
  103. void heapify(std::vector<T>& v, const int& end)
  104. {
  105.     for (int i = end; i >= 0; i--)
  106.     {
  107.         siftDown(v, i, end);
  108.     }
  109. }
  110.  
  111. template<typename T>
  112. void siftDown(std::vector<T>& v, const int& start, const int& end)
  113. {
  114.     int parent = start, child = parent * 2 + 1, swap;
  115.     while(child <= end)
  116.     {
  117.         swap = parent;
  118.         if (v[child] > v[parent])
  119.         {
  120.             swap = child;
  121.         }
  122.         if ((child + 1) <= end && v[child + 1] > v[swap])
  123.         {
  124.             swap = child + 1;
  125.         }
  126.         if (swap == parent)
  127.             break;
  128.         std::swap(v[parent], v[swap]);
  129.         parent = swap;
  130.         child = parent * 2 + 1;
  131.     }
  132. }
  133.  
  134. template<typename T>
  135. void heapSortComparator(std::vector<T>& v)
  136. {
  137.     int end = v.size() - 1;
  138.     heapifyComparator(v, end);
  139.     while (end >= 0)
  140.     {
  141.         std::swap(v[0], v[end]);
  142.         end--;
  143.         siftDownComparator(v, 0, end);
  144.     }
  145. }
  146.  
  147. template<typename T>
  148. void heapifyComparator(std::vector<T>& v, const int& end)
  149. {
  150.     for (int i = end; i >= 0; i--)
  151.     {
  152.         siftDownComparator(v, i, end);
  153.     }
  154. }
  155.  
  156. template<typename T>
  157. void siftDownComparator(std::vector<T>& v, const int& start, const int& end)
  158. {
  159.     int parent = start, child = parent * 2 + 1, swap;
  160.     while (child <= end)
  161.     {
  162.         swap = parent;
  163.         if (lessSpell(v[parent], v[child]))
  164.         {
  165.             swap = child;
  166.         }
  167.         if ((child + 1) <= end && lessSpell(v[swap], v[child + 1]))
  168.         {
  169.             swap = child + 1;
  170.         }
  171.         if (swap == parent)
  172.             break;
  173.         std::swap(v[parent], v[swap]);
  174.         parent = swap;
  175.         child = parent * 2 + 1;
  176.     }
  177. }
  178.  
  179. bool lessSpell(const spell& a, const spell& b)
  180. {
  181.     if (a.getPower() < b.getPower())
  182.         return true;
  183.     else if (a.getPower() == b.getPower())
  184.     {
  185.         return (a.getCost() > b.getCost());
  186.     }
  187.     else
  188.         return false;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement