-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaiMiniProject.cpp
55 lines (49 loc) · 1.71 KB
/
SaiMiniProject.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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
int initialBalance, operations, currentSnapshot = 0;
cin >> initialBalance >> operations;
vector<int> transactions;
vector<int> snapshots = {initialBalance};
for (int i = 0; i < operations; i++) {
string command;
cin >> command;
if (command == "read") {
cout << snapshots[currentSnapshot] << endl;
}
else if (command == "credit" || command == "debit") {
int amount;
cin >> amount;
if (command == "credit") {
snapshots[currentSnapshot] += amount;
transactions.push_back(amount);
} else {
snapshots[currentSnapshot] -= amount;
transactions.push_back(-amount);
}
}
else if (command == "abort") {
int transactionIndex;
cin >> transactionIndex;
if (transactionIndex > 0 && transactionIndex <= transactions.size()) {
snapshots[currentSnapshot] -= transactions[transactionIndex - 1];
transactions[transactionIndex - 1] = 0;
}
}
else if (command == "rollback") {
int snapshotIndex;
cin >> snapshotIndex;
if (snapshotIndex > 0 && snapshotIndex <= snapshots.size()) {
currentSnapshot = snapshotIndex - 1;
snapshots.resize(currentSnapshot + 1);
}
}
else if (command == "commit") {
snapshots.push_back(snapshots[currentSnapshot]);
currentSnapshot++;
}
}
return 0;
}