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
|
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <ctype.h>
#include <iterator>
#include <map>
#include <list>
#include <cctype>
#include <algorithm>
#include <set>
#include <sstream>
#include <time.h>
using namespace std;
void readFileToSet(string filename, set<string> &mySet)
{
ifstream loadFile;
loadFile.open(filename, ifstream::in);
while (loadFile.fail()){
cout << "The file \"" << filename << "\" failed to open.\n\n";
exit(EXIT_FAILURE);
}
string line;
while (getline(loadFile, line)){
for (int i = 0, len = line.size(); i < len; i++)
{
if (ispunct(line[i]))
{
line.erase(i--, 1);
len = line.size();
}
}
istringstream iss(line);
string word;
while (iss >> word){
mySet.insert(word);
}
}
loadFile.close();
}
void write_set(set<string> &output) {
cout << "\nEnter file name to save: ";
string file_name_save;
std::cin >> file_name_save;
ofstream outfile(file_name_save);
if (outfile.is_open()) {
for (set<string>::iterator iter = output.begin(); iter != output.end(); ++iter){
outfile << *iter << ' ';
outfile << '\n';
}
outfile.close();
}
else{
cout << "Unable to open file\n";
}
}
void readFileToVector(string filename, vector<string> &myVec)
{
ifstream loadFile;
loadFile.open(filename, ifstream::in);
while (loadFile.fail()){
cout << "The file \"" << filename << "\" failed to open.\n\n";
exit(EXIT_FAILURE);
}
string line;
while (getline(loadFile, line)){
for (int i = 0, len = line.size(); i < len; i++)
{
if (ispunct(line[i]))
{
line.erase(i--, 1);
len = line.size();
}
}
istringstream iss(line);
string word;
while (iss >> word){
myVec.push_back(word);
}
}
loadFile.close();
}
void write_file(vector<string> &output) {
cout << "\nEnter file name to save: ";
string file_name_save;
cin >> file_name_save;
ofstream outfile(file_name_save);
if (outfile.is_open()) {
for (int i = 0; i < output.size(); i++) {
outfile << output[i] << "\n";
}
outfile.close();
}
else {
cout << "Unable to open file\n";
}
}
void write_file_comma(vector<string> &output) {
cout << "\nEnter file name to save: ";
string file_name_save;
cin >> file_name_save;
ofstream outfile(file_name_save);
if (outfile.is_open()) {
for (int i = 0; i < output.size(); i++) {
outfile << output[i];
outfile << ", ";
}
outfile.close();
}
else {
cout << "Unable to open file\n";
}
}
int main(/*int argc, char **argv???*/){
vector<string> myVec;
set<string> mySet;
string charactersFilename/*(argv[1])???*/;
cin >> charactersFilename;
readFileToSet(charactersFilename, mySet);
write_set(mySet);
readFileToVector(charactersFilename, myVec);
write_file(myVec);
map<string, string> wordmaps;
string last = "";
for (vector<string>::iterator it = myVec.begin(); it != myVec.end(); it++){
wordmaps[last] = *it;
last = *it;
}
write_file_comma(myVec);
cout << "\n\nPart 4\n\n" << endl;
string state = "";
for (int i = 0; i < 100; i++) {
cout << wordmaps[state] << " ";
state = wordmaps[state];
}
cout << endl;
cout << "\n\nPart 5\n\n" << endl;
map< string, vector<string> > wordmap;
for (vector<string>::iterator it = myVec.begin(); it != myVec.end(); it++){
wordmap[state].push_back(*it);
state = *it;
}
srand(time(NULL));
for (int i = 0; i < 100; i++){
int ind = rand() % wordmap[state].size();
cout << wordmap[state][ind] << " ";
state = wordmap[state][ind];
}
cout << endl;
cout << "\n\nPart 6\n\n" << endl;
map<list<string>, vector<string>> wordmapies;
list<string> states;
for (int i = 0; i < 2; i++){
states.push_back("");
}
for (vector<string>::iterator it = myVec.begin(); it != myVec.end(); it++){
wordmapies[states].push_back(*it);
states.push_back(*it);
states.pop_front();
}
list<string> statos;
for (int i = 0; i < 2; i++){
statos.push_back("");
}
for (int i = 0; i < 100; i++){
int ind = rand() % wordmapies[statos].size();
cout << wordmapies[statos][ind] << " ";
statos.push_back(wordmapies[statos][ind]);
statos.pop_front();
}
cout << endl;
system("PAUSE");
return 0;
}
|