-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword.cpp
More file actions
202 lines (197 loc) · 4.78 KB
/
word.cpp
File metadata and controls
202 lines (197 loc) · 4.78 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
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
/*******************************************************************************
Title : cp395_hwk1.cpp
Author : Cheng Pan
Created on : September 29, 2014
Description : Get user input, get all combinations, sort string,
delete duplicates, sort vecotrs, and counters
Purpose : object oriented programming.
Usage : ./cp395_hwk1 open file.txt someword
Build with : g++ -o cp395_hwk1 cp395_hwk1.cpp word.cpp word.h
dictionary.cpp dictionary.h
Modifications :
*******************************************************************************/
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include "word.h"
using namespace std;
/******************************************************************************/
/* Function Definitions */
/******************************************************************************/
/**
Word() A default constructor for the Word class
*/
Word::Word()
{
counter = 0;
permCount = 0;
index = 0;
wordLength = 0;
}
/**
~Word() A default destructor for the Word class
*/
Word::~Word()
{
}
/**
returnUserInput() return user input
*/
string Word::returnUserInput()
{
return userInput;
}
/**
getUserInput(argc,argv) get user input;
*/
void Word::getUserInput(int argc, char *argv[])
{
cout << "\r" << "Enter a word to search" << flush; //replace "loading" message
cout << endl;
if (argc < 2) //if running program without command line argument
{
while (true)
{
cin >> userInput;
if (userInput.length() >= 2 && userInput.length() <= 32)
{
break;
}
cerr << "ERROR : PLEASE A STRING OF AT LEAST 2 AND AT MOST 32 CHARACTERS." << endl;
}
}
else //if running program with command line argument
{
userInput = argv[3];
if (userInput.length() >= 2 && userInput.length() <= 32)
{
}
else
{
cerr << "ERROR : PLEASE A STRING OF AT LEAST 2 AND AT MOST 32 CHARACTERS." << endl;
exit(0);
}
}
}
/**
returnWordList() return vector
*/
vector<string> &Word::returnWordList()
{
return wordList;
}
/**
returnIndex() return index
*/
int Word::returnIndex()
{
return index;
}
/**
returnWordLength() return length of word
*/
int Word::returnWordLength()
{
wordLength = userInput.length();
return wordLength;
}
/**
returnCounter() return permutation count
*/
long double &Word::returnCounter()
{
return counter;
}
/**
returnPermCount() return total permutation count
*/
long double Word::returnPermCount()
{
return permCount;
}
/**
generatePerm(word,wordList,index,wordLength,counter,permCount) generate all combinations for word
*/
void Word::generatePerm(string word, vector<string> &wordList, int index, int wordLength, long double &counter, long double permCount)
{
if (counter == permCount) //finish when current permutation counter==total permutation counter
{
return;
}
else
{
if (index == wordLength) //Enter if it chosen all the possible characters
{
for (int i = 0; i<word.length() - 1; i++) //double foor loop to get all the substrings of current permutation
{
for (int l = 1; l<word.length() - i + 1; l++)
{
wordList.push_back(word.substr(i, l)); //store all possible substrings in a vector
}
}
counter++; //permutation counter increases by 1 each time
cout << "\r" << fixed << setprecision(2) << counter / permCount * 100 << "% " << flush; //shows the progress
}
else
{
for (int j = index; j <= wordLength; j++)
{
//switch the element at the 2 location with eachother
char temp[1];
temp[0] = word[index];
word[index] = word[j];
word[j] = temp[0];
generatePerm(word, wordList, index + 1, wordLength, counter, permCount); //get the remaining letters
//undo the previous switch
temp[0] = word[index];
word[index] = word[j];
word[j] = temp[0];
}
}
}
}
/**
sortString(input) sort the string in alphabetical order
*/
void Word::sortString(string input)
{
sort(input.begin(), input.end());
}
/**
deleteDuplicates(List) delete duplicates adjacent to each other
*/
void Word::deleteDuplicates(vector<string> &List)
{
List.erase(unique(List.begin(), List.end()), List.end()); //need to sort first
}
/**
getTotalPerm() get total number of permutation of the string
*/
void Word::getTotalPerm(string input)
{
int counter = 0;
do {
counter++;
} while (next_permutation(input.begin(), input.end()));
permCount = counter;
}
/**
sortList(List) sort the string in alphabetical order
*/
void Word::sortList(vector<string> &List)
{
sort(List.begin(), List.end());
}
/**
resetValues() resets the values to default value if running the program without command line arguments because of loop
*/
void Word::resetValues()
{
counter = 0;
permCount = 0;
index = 0;
wordLength = 0;
wordList.erase(wordList.begin(), wordList.end());
wordList.resize(0);
}