-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
70 lines (61 loc) · 1.81 KB
/
main.cpp
File metadata and controls
70 lines (61 loc) · 1.81 KB
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
#include <iostream>
#include <sstream>
#include <string>
#include "LRUCache.h"
#include <limits>
int main() {
std::cout << "LRU Cache Interactive Demo\n";
std::cout << "Enter capacity: ";
int cap = 0;
std::cin >> cap;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (cap <= 0) {
std::cout << "Capacity must be > 0\n";
return 1;
}
LRUCache cache(static_cast<size_t>(cap));
std::cout << "\nCommands:\n";
std::cout << " put <key> <value>\n";
std::cout << " get <key>\n";
std::cout << " print\n";
std::cout << " help\n";
std::cout << " quit\n\n";
std::string line;
while (true) {
std::cout << "> ";
if (!std::getline(std::cin, line)) break;
std::istringstream iss(line);
std::string cmd;
iss >> cmd;
if (cmd == "quit" || cmd == "exit") {
break;
} else if (cmd == "help") {
std::cout << "Commands: put <k> <v>, get <k>, print, help, quit\n";
} else if (cmd == "print") {
cache.print();
} else if (cmd == "put") {
int k, v;
if (!(iss >> k >> v)) {
std::cout << "Usage: put <key> <value>\n";
continue;
}
cache.put(k, v);
std::cout << "OK\n";
} else if (cmd == "get") {
int k;
if (!(iss >> k)) {
std::cout << "Usage: get <key>\n";
continue;
}
int val = cache.get(k);
if (val == -1) std::cout << "Not found\n";
else std::cout << val << "\n";
} else if (cmd.empty()) {
continue;
} else {
std::cout << "Unknown command. Type 'help'.\n";
}
}
std::cout << "Bye!\n";
return 0;
}