-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
42 lines (40 loc) · 1.32 KB
/
main.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
#include <iostream>
#include <fstream>
#include <stack>
#include "../head/MinPQ.h"
#include "../head/Transaction.h"
using namespace std;
/**
* The {@code TopM} class provides a client that reads a sequence of
* transactions from standard input and prints the <em>m</em> largest ones
* to standard output. This implementation uses a {@link MinPQ} of size
* at most <em>m</em> + 1 to identify the <em>M</em> largest transactions
* and a {@link Stack} to output them in the proper order.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/24pq">Section 2.4</a>
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
int main() {
int m = 5;
auto f = [](Transaction &a1, Transaction &a2) { return a1.getamount() > a2.getamount(); };
MinPQ<Transaction> pq(f);
fstream file("./data/tinyBatch.txt");
string tmp;
while (getline(file, tmp)) {
// Create an entry from the next line and put on the PQ.
Transaction *trans = new Transaction(tmp);
pq.insert(*trans);
if (pq.size() > m)
pq.delMin();
}
stack<Transaction> st;
for (Transaction trans: pq)
st.push(trans);
while (!st.empty()) {
cout << st.top() << endl;
st.pop();
}
}