-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoring.cpp
82 lines (74 loc) · 2.15 KB
/
scoring.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
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
#include "scoring.h"
int scoring() {
// get input file containing potential plaintexts
//open input file for reading
ifstream input("plaintexts.txt");
//top 100 most frequent words in English
ifstream topWords("topHundredWordsEnglish.txt");
string words[100];
int i = 0;
//copy top words into words array
while(topWords.good()) {
getline(topWords,words[i]);
i++;
}
//close ifstream
topWords.close();
//capitalize all words in words array
for(i = 0; i < 100; i++) {
for(int j = 0; j < words[i].size(); j++) {
words[i][j] = toupper(words[i][j]);
}
}
//vector to hold user inputs
vector <string> inputs;
//get each potential plaintext, put into vector inputs
while(input.good()) {
string foo = "";
getline(input,foo);
if(input.good()) {
inputs.push_back(foo);
}
}
//close ifstream
input.close();
//capitalize everything that is capitalize-able in the user's inputs, makes string search possible
for(i = 0; i < inputs.size(); i++) {
for(int j = 0; j < inputs[i].size(); j++) {
inputs[i][j] = toupper(inputs[i][j]);
}
}
//index for search
int index[inputs.size()];
//set all indexes to 0, one index per potential plaintext
for(i = 0; i < inputs.size(); i++) {
index[i] = 0;
}
// compare every string in vector inputs with each word in the top 100
for(i = 0; i < inputs.size(); i++) {
for (int j = 0; j < 100 ; j++) {
//if we find a top 100 word in the potential plaintext, the corresponding index is incremente by 1
if(inputs[i].find(words[j]) != std::string::npos) {
index[i]++;
}
}
}
// the most potential plaintext with the highest index is probably our correct plaintext,
int temp = 0;
//find the max index, store in temp
for(i= 0; i < inputs.size(); i++) {
if(index[i] > temp) {
temp = index[i];
}
}
//for each plaintext, if the index of the potential plaintext = maxindex - 1, print the potential plaintext.
//we do this so that we have room for error. It is possible for the wrong plaintext to have a low index,
// so we print highest indexes as a safeguard.
for(i = 0; i < inputs.size(); i++) {
if(index[i] >= temp - 1) {
cout << inputs[i] << "\n";
}
}
cout << "\n";
return 0;
}