Advertisement
Guest User

Untitled

a guest
May 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <ctime>
  5. #include <climits>
  6. using namespace std;
  7.  
  8. template <typename tName>
  9. struct Heap {
  10. vector<tName> vHeap;
  11. int count;
  12.  
  13. Heap(): count(0) {}
  14.  
  15. void add(tName value) {
  16. ++count;
  17. vHeap.push_back(value);
  18. }
  19. void del() {
  20. if(count > 0) {
  21. tName minimal = vHeap[0];
  22. int position = 0;
  23. for(int i = 0; i < count; ++i) {
  24. if(minimal > vHeap[i]) {
  25. minimal = vHeap[i];
  26. position = i;
  27. }
  28. }
  29. --count;
  30. vHeap.erase(vHeap.begin() + position);
  31. } else {
  32. throw runtime_error("DELETE ERROR: Stack empty!!!");
  33. }
  34. }
  35. void get() {
  36. if(count > 0) {
  37. tName minimal = INT_MAX;
  38. for(int i = 0; i < count; ++i) {
  39. if(minimal > vHeap[i]) {
  40. minimal = vHeap[i];
  41. }
  42. }
  43. cout << "~ Minimal Value: " << minimal << endl << endl;
  44. } else {
  45. throw runtime_error("GET VALUE ERROR: Stack empty!!!");
  46. }
  47.  
  48. }
  49. void message() {
  50. if(count > 0) {
  51. for(int i = 0; i < count; ++i) {
  52. cout << vHeap[i] << "\t";
  53. }
  54. cout << endl;
  55. } else {
  56. throw runtime_error("MESSAGE ERROR: Stack empty!!!");
  57. }
  58. }
  59. };
  60.  
  61. int main() {
  62. try {
  63. cout << "********* STACK <DOUBLE> ********" << endl;
  64. Heap <double> Stack;
  65.  
  66. Stack.add(5.5);
  67. Stack.add(8.2);
  68. Stack.add(-7.3);
  69. Stack.add(-2.7);
  70. Stack.add(3.1);
  71. Stack.message();
  72. Stack.get();
  73. Stack.del();
  74. Stack.del();
  75. Stack.message();
  76. Stack.get();
  77.  
  78. cout << "********** STACK <INT> **********" << endl;
  79.  
  80. Heap <int> Stack2;
  81. Stack2.add(5);
  82. Stack2.add(8);
  83. Stack2.add(-7);
  84. Stack2.add(-2);
  85. Stack2.add(3);
  86. Stack2.message();
  87. Stack2.get();
  88. Stack2.del();
  89. Stack2.del();
  90. Stack2.message();
  91. Stack2.get();
  92.  
  93. } catch(const exception& e) {
  94. cout << e.what() << endl;
  95. }
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement