/* * File: RandomWriter.cpp * ---------------------- * Name: Ana Gurgenidze * Section: #2, Michael Nemsitsveridze * This file is the final project for the Random Writer problem. * */ #include #include #include "simpio.h" #include "random.h" #include "vector.h" #include "map.h" #include "console.h" using namespace std; /* Function prototypes */ void promptUserForFile(ifstream & in, string prompt = ""); int main() { string prompt = ("Input file: "); ifstream inputFile; promptUserForFile(inputFile, prompt); Map > data; //ifstream inputFile; //inputFile.open("TomSawyer.txt"); int k = getInteger("Enter the Markov order: "); string str; char c; for (int i = 0; i < k; i++) { if (inputFile.fail()) break; inputFile.get(c); str += c; } while (true) { if (inputFile.fail()) break; inputFile.get(c); data[str].add(c); data.put(str, data[str]); str = str.substr(1, str.length() - 1) + c; } string max; int maxSize = 0; foreach (string str in data) { if (data.get(str).size() > maxSize) { maxSize = data.get(str).size(); max = str; } } string result = max; string tmp = max; while (result.size() < 2000) { int randomIndex = randomInteger(0, data.get(tmp).size() - 1); // ?????? char ch = data.get(tmp).get(randomIndex); result += ch; tmp = tmp.substr(1, k - 1) + ch; } cout << result << endl; inputFile.close(); return 0; } /* * function lets user enter valid name for file. */ void promptUserForFile(ifstream & in, string prompt) { while (true) { cout << prompt << endl; string filename; getline(cin, filename); in.open(filename.c_str()); if (!in.fail()) break; in.clear(); prompt = "Unable to open that file. Try again: "; } }