-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
218 lines (203 loc) · 4.13 KB
/
main.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <string>
#include <iostream>
#include <limits>
#include <fstream>
#include <vector>
using namespace std;
int len;
int n;
string reply;
string segment;
string mark = " -- tried ";
vector <string> db_buffer;
bool last_file_exists = false;
string last_file;
bool fileExists(const std::string& filename)
{
ifstream infile(filename);
return infile.good();
}
string init_lst_files()
{
string filename = "combo";
for(int i=0; fileExists(filename+".lst")==true; i++)
{
if(i>=1)
{
filename.pop_back();
}
filename = filename + to_string(i);
}
filename.append(".lst");
ofstream outfile (filename);
return filename;
}
// The main recursive method to print all possible strings of length "length"
void print_str(const char str[], string prefix, const int n, const int len, fstream& obj)
{
if (len == 1)
{
for (int j = 0; j < n; j++)
{
cout << prefix + str[j] <<endl;
obj<<prefix+str[j]<<endl;
}
}//Base case: len = 1, print the string "len" times + the remaining letter
else
{
// One by one add all characters from "str" and recursively call for "len" equals to "len"-1
for (int i = 0; i < n; i++)
// Next character of input added
print_str(str, prefix + str[i], n, len - 1, obj);
// "len" is decreased, because we have added a new character
}
}
void start()
{
cout<<"How many numbers are used? \n";
cin>>len;
cout<<"How many different numbers or symbols are possible? \n";
cin>>n;
char str[n];
cout<<"Enter all numbers or symbols possible. Note: only one symbol supported at a time. \n";
for (int z = 0; z < sizeof(str); z++)
{
cout<<"Enter value #"<<z<<" out of #"<<sizeof(str)<<endl;
cin >> str[z];
}
fstream obj(init_lst_files(), fstream::out | fstream::app | fstream::ate);
print_str(str, "", n, len, obj); //Note: this function works on all cases and not just the case above
cin.get();
}
string rem_extension(string str)
{
size_t pos = str.find_last_of(".");
if (pos!=string::npos)
{
string ext = str.substr(pos);
str.erase( str.size() - ext.length());
return str;
}
else
{
cout<<"No extension in passed string"<<str<<endl;
return str;
}
}
void write_mark(string filename)
{
filename = rem_extension(filename);
filename.append(".mark");
ofstream outfile (filename);
for(int i=0; i<db_buffer.size(); i++)
{
outfile<<db_buffer[i]<<endl;
}
outfile.close();
db_buffer.clear();
}
void mark_comb(string filename, string segment)
{
string line;
size_t pos;
fstream obj(filename, fstream::in);
if(obj.is_open())
{
while(getline(obj,line))
{
pos = line.find(segment);
if(pos!=string::npos)
{
db_buffer.push_back(line+mark);
continue;
}
else
{
db_buffer.push_back(line);
}
}
}
else
{
cout<<"Cant open "<<filename<<endl;
}
write_mark(filename);
}
void list_db(string filename)
{
string line;
fstream obj(filename, fstream::in);
if(obj.is_open())
{
while(getline(obj,line))
{
cout<<line<<endl;
}
}
else
{
cout<<"Cant open "<<filename<<endl;
}
}
int main()
{
cin.ignore(numeric_limits<streamsize>::max(),'\n');
start:
cout<<"Type `s` to start a new combination. Else press any key. \n";
cout<<"Type `c` to mark a combination as `checked` in database. \n";
cout<<"Type `l` to list a database file. \n";
cin>>reply;
if(reply.compare("s")==0)
{
start();
}
else if(reply.compare("c")==0)
{
cin.clear();
cout<<"Type the name of database to load: [Or any key to default = last file opened .] \n";
cin>>reply;
if(!fileExists(reply))
{
if(!last_file_exists)
{
reply = "combo.lst";
last_file_exists = true;
}
else
{
reply = last_file;
}
}
last_file = reply;
cout<<"What segment to search for in the database? \n";
cin>>segment;
mark_comb(reply, segment);
goto start;
}
else if(reply.compare("l")==0)
{
cin.clear();
cout<<"Type the name of database to load: [Or any key to default = last file opened .] \n";
cin>>reply;
if(!fileExists(reply))
{
if(!last_file_exists)
{
reply = "combo.mark";
last_file_exists = true;
}
else
{
reply = last_file;
}
}
last_file = reply;
list_db(reply);
goto start;
}
else
{
goto start;
}
return 0;
}