Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <ctime>
- #include <climits>
- using namespace std;
- template <typename tName>
- struct Heap {
- vector<tName> vHeap;
- int count;
- Heap(): count(0) {}
- void add(tName value) {
- ++count;
- vHeap.push_back(value);
- }
- void del() {
- if(count > 0) {
- tName minimal = vHeap[0];
- int position = 0;
- for(int i = 0; i < count; ++i) {
- if(minimal > vHeap[i]) {
- minimal = vHeap[i];
- position = i;
- }
- }
- --count;
- vHeap.erase(vHeap.begin() + position);
- } else {
- throw runtime_error("DELETE ERROR: Stack empty!!!");
- }
- }
- void get() {
- if(count > 0) {
- tName minimal = INT_MAX;
- for(int i = 0; i < count; ++i) {
- if(minimal > vHeap[i]) {
- minimal = vHeap[i];
- }
- }
- cout << "~ Minimal Value: " << minimal << endl << endl;
- } else {
- throw runtime_error("GET VALUE ERROR: Stack empty!!!");
- }
- }
- void message() {
- if(count > 0) {
- for(int i = 0; i < count; ++i) {
- cout << vHeap[i] << "\t";
- }
- cout << endl;
- } else {
- throw runtime_error("MESSAGE ERROR: Stack empty!!!");
- }
- }
- };
- int main() {
- try {
- cout << "********* STACK <DOUBLE> ********" << endl;
- Heap <double> Stack;
- Stack.add(5.5);
- Stack.add(8.2);
- Stack.add(-7.3);
- Stack.add(-2.7);
- Stack.add(3.1);
- Stack.message();
- Stack.get();
- Stack.del();
- Stack.del();
- Stack.message();
- Stack.get();
- cout << "********** STACK <INT> **********" << endl;
- Heap <int> Stack2;
- Stack2.add(5);
- Stack2.add(8);
- Stack2.add(-7);
- Stack2.add(-2);
- Stack2.add(3);
- Stack2.message();
- Stack2.get();
- Stack2.del();
- Stack2.del();
- Stack2.message();
- Stack2.get();
- } catch(const exception& e) {
- cout << e.what() << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement