-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseHeap.cpp
131 lines (97 loc) · 3.14 KB
/
useHeap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// This program allows the user to manipulate a binary heap.
// The program only inserts string ids with with associated keys
// into the heap. The heap class, however, is capable of storing
// arbitrary pointers along with each heap item.
//
#include <iostream>
#include <string>
#include <cstdlib>
#include "heap.h"
using namespace std;
// Read an integer from standard input; if a non-integer is in the
// buffer, the state is fixed and the user is re-prompted;
// either way, the remainder of the buffer is cleared
void getInteger(string message, int &ref)
{
bool inputGood = false;
while (!inputGood) {
inputGood = true;
cout << message;
cin >> ref;
if (!cin) {
// Non-integer in input buffer, get out of "fail" state
cin.clear();
inputGood = false;
}
while (cin.get() != '\n'); // clear buffer
}
}
int main()
{
int capacity = 0;
int option;
string stringTmp;
int key, id;
int retVal;
// Have user choose capacity for binary heap
getInteger("Choose a capacity for the binary heap: ", capacity);
// Create the heap
heap myHeap1(capacity);
while (1) {
cout << "\nOptions:" << endl;
cout << "1 - Insert a new item into the binary heap" << endl;
cout << "2 - Set the key of a specified item" << endl;
cout << "3 - Delete a specified item" << endl;
cout << "4 - Perform a deleteMin" << endl;
cout << "5 - Quit" << endl;
// Have the user choose an option
getInteger("Choose an option: ", option);
switch(option) {
case 1:
// Get data to insert into heap from the user and insert it
cout << "Enter an id string (to insert): ";
getline(cin, stringTmp);
getInteger("Enter an associated integer key: ", key);
retVal = myHeap1.insert(stringTmp, key);
cout << "\nCall to 'insert' returned: " << retVal << endl;
break;
case 2:
// Get id string and new key from user and change the key
cout << "Enter an id string (to change its key): ";
getline(cin, stringTmp);
getInteger("Enter an associated integer key: ", key);
retVal = myHeap1.setKey(stringTmp, key);
cout << "\nCall to 'setKey' returned: " << retVal << endl;
break;
case 3:
// Get id string from user and delete it from the heap
cout << "Enter an id string (to delete): ";
getline(cin, stringTmp);
retVal = myHeap1.remove(stringTmp, &key);
cout << "\nCall to 'delete' returned: " << retVal << endl;
if (retVal == 0) {
cout << "\nDeleted item with string id \"" << stringTmp
<< "\" and key " << key << endl;
}
break;
case 4:
// Perform the deleteMin operation on the heap
retVal = myHeap1.deleteMin(&stringTmp, &key);
cout << "\nCall to 'deleteMin' returned: " << retVal << endl;
if (retVal == 0) {
cout << "\nDeleted item with string id \"" << stringTmp
<< "\" and key " << key << endl;
}
break;
case 5:
cout << "\nGoodbye!" << endl;
exit(0);
default:
cerr << "Error, that input is not valid!" << endl;
exit (1);
}
}
cerr << "Error, we should never get here!" << endl;
exit (1);
}