-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (136 loc) · 3.26 KB
/
main.cpp
File metadata and controls
164 lines (136 loc) · 3.26 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Auther: Ctl-F (Spencer Brough)
* Date: 07/15/2020
*/
#include "FormulaHasher.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <chrono>
using std::cout;
using std::cin;
using std::getline;
using std::string;
using std::ifstream;
using std::vector;
using namespace std::chrono;
using ctl_f::FormulaHasher;
inline long currentTime(){
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
double matchStrings(const string& first, const string& second){
int matchedChars = 0;
int maxIndex = (first.length() < second.length()) ? first.length() : second.length();
if(maxIndex == 0){
cout << "Error! Got zero!!" << "\n";
return 0.0;
}
for(int i=0; i<maxIndex; i++){
matchedChars += first[i] == second[i];
}
cout << matchedChars << " / " << maxIndex << " are the same\n";
return ((double)matchedChars / (double)maxIndex) * 100.0;
}
int addtorial(int n){
if(n < 2){
return n;
}
if(n == 2){
return 1;
}
int value = 1;
for(int i=2; i<n; i++){
value += i;
}
return value;
}
void generateReportFromFile(const string& fname){
ifstream f(fname);
vector<string> lines;
string line;
if(f.is_open()){
while(f.good()){
getline(f, line);
lines.push_back(line);
}
f.close();
}
cout << "Read " << lines.size() << " lines from file\n";
vector<string> hashes;
FormulaHasher hasher;
for(string s : lines){
hashes.push_back(hasher.hash(s));
}
cout << "Hashing finished, beginning comparison\n";
string hashA, hashB;
double avg = 0.0, max = -100.0, min = 200.0;
int count = 0;
int targetCount = addtorial(hashes.size());
long timeStamp = currentTime();
for(int i=0; i<hashes.size(); i++){
hashA = hashes[i];
for(int j=0; j<hashes.size(); j++){
++count;
if(i == j) {
continue;
}
hashB = hashes[j];
double match = matchStrings(hashA, hashB);
avg += match;
if(match < min){
min = match;
}
if(match > max){
max = match;
}
if(currentTime() - timeStamp > 1000){
cout << count << "/" << targetCount << "\n";
timeStamp = currentTime();
}
}
}
avg /= (double)hashes.size();
cout << "Average: " << avg << "\nMax: " << max << " - Min: " << min << "\n";
}
int interactiveMode();
int main() {
cout << "Please choose a mode: ";
string userInput;
getline(cin, userInput);
if(userInput == "interactive"){
return interactiveMode();
}
else if(userInput == "file") {
cout << "Filename: ";
getline(cin, userInput);
generateReportFromFile(userInput);
}
return 0;
}
int interactiveMode(){
FormulaHasher f;
string last = "";
string userInput = "";
do {
cout << "Input for Processing: ";
getline(cin, userInput);
if (userInput == "") {
break;
}
string hash = f.hash(userInput);
cout << "> ";
for (char c : hash) {
cout << (int)c << " ";
}
cout << "\n";
int equalityCount = 0;
if (last != "") {
for (int i = 0; i < hash.length(); i++) {
equalityCount += hash[i] == last[i];
}
cout << ((double)(equalityCount) / (double)(hash.length()) * 100.0) << "% match\n";
}
last = hash;
} while (userInput != "end" && userInput != "exit");
return 0;
}