-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.cpp
More file actions
149 lines (136 loc) · 3.79 KB
/
counter.cpp
File metadata and controls
149 lines (136 loc) · 3.79 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <stdexcept>
#include <chrono>
#include <ctime>
#include <stack>
#include <map>
using namespace std;
struct Stats
{
unsigned short deaths;
unsigned short wins;
} stats;
string diffTime(time_t start, time_t end) {
unsigned short sec = (long)difftime(end, start);
unsigned short min = sec / 60;
unsigned __int8 hours = min / 60;
char buffer [8];
sprintf(buffer, "%02d:%02d:%02d",short(hours),short(min%60),short(sec%60));
return buffer;
}
void printDict(map<string,Stats>& Dict){
for(auto it = Dict.cbegin(); it != Dict.cend(); ++it)
cout << it->first << ": " << it->second.deaths << ", " << it->second.wins << "\n";
}
void writeStats(map<string,Stats>& Dict){
fstream file;
file.open("count.csv", ios_base::out | std::ios_base::trunc);
file.close();
fstream file2;
file2.open("count.csv", std::ios_base::app);
file2 << "Name,Deaths,Victories\n";
if (file2.is_open()){
for(auto it = Dict.cbegin(); it != Dict.cend(); ++it)
{
string line = it->first + "," + to_string(it->second.deaths) + "," + to_string(it->second.wins) + "\n";
file2 << line;
}
}
file2.close();
}
unsigned int countNames (stack<string>& nameList, map<string,Stats>& Dict)
{
unsigned int deaths = 0;
while (nameList.size() > 1)
{
string key_to_find = nameList.top();
bool isWin = false;
if (key_to_find[0] == '*')
{
key_to_find.erase(0,1);
isWin = true;
}
auto it = Dict.find(key_to_find);
if (it != Dict.end()){
if (isWin)
it->second.wins++;
else {
it->second.deaths++;
deaths++;
}
}
else {
struct Stats temp;
if (isWin)
temp = {0,1};
else {
temp = {1,0};
deaths++;
}
Dict.insert(make_pair(key_to_find, temp));
}
nameList.pop();
}
return deaths;
}
int main()
{
cout << "Welcome to Death Counter. \n[enemy] = death, *enemy = victory, p for stats, q to quit." << endl;
time_t t2;
time (&t2); stack<string> nameList; map<string, Stats> Dict;
ifstream myFile ("log.csv");
string line, name;
if (myFile.is_open()){
while (myFile.good()){
getline(myFile, line);
//myFile >> line;
name = "";
for (unsigned short i = 0; i < line.size(); i++)
{
if (line[i] == ',') break;
name += line[i];
}
if (name.size() != 0)
nameList.push(name);
}
}
unsigned int total = countNames(nameList, Dict);
getline (cin, line);
unsigned short deathCount = 0; unsigned short winCount = 0;
fstream file;
file.open("log.csv", std::ios_base::app);
time_t tt;struct tm * ti; //initialize
if (file.is_open()){
while(line != "q")
{
if (line == "p") {
printDict(Dict); cout << endl;
}
else if (line.size() != 0) {
time (&tt); ti = localtime(&tt);
char output[20]; strftime(output, 20, "%m/%d/%Y,%H:%M:%S", ti);
file << endl << line << "," << output << "," << diffTime(t2,tt);
nameList.push(line);
countNames(nameList, Dict);
if (line[0] == '*')
line.erase(0,1);
else
deathCount++;
auto it = Dict.find(line);
cout << "Deaths to " << line << ": " << to_string(it->second.deaths) << endl;
cout << "Victories to " << line << ": " << to_string(it->second.wins) << endl;
cout << "Deaths this session: " << deathCount << "\t" << "Total: " << total + deathCount << endl << endl;
}
getline (cin, line);
}
}
file << "\n";
file.close();
writeStats(Dict);
//print shortDict(Dict);
//cout << "Press ENTER to close...";
//std::cin.get();
}