Anophoo

random writer 3

Apr 13th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. /*
  2. * File: RandomWriter.cpp
  3. * ----------------------
  4. * Name: Ana Gurgenidze
  5. * Section: #2, Michael Nemsitsveridze
  6. * This file is the final project for the Random Writer problem.
  7. *
  8. */
  9.  
  10. #include <iostream>
  11. #include <fstream>
  12. #include "simpio.h"
  13. #include "random.h"
  14. #include "vector.h"
  15. #include "map.h"
  16. #include "console.h"
  17. using namespace std;
  18.  
  19.  
  20. /* Function prototypes */
  21. void promptUserForFile(ifstream & in, string prompt = "");
  22.  
  23. int main() {
  24. string prompt = ("Input file: ");
  25. ifstream inputFile;
  26. promptUserForFile(inputFile, prompt);
  27.  
  28. Map <string, Vector<char> > data;
  29. //ifstream inputFile;
  30. //inputFile.open("TomSawyer.txt");
  31. int k = getInteger("Enter the Markov order: ");
  32. string str;
  33. char c;
  34. for (int i = 0; i < k; i++) {
  35. if (inputFile.fail())
  36. break;
  37. inputFile.get(c);
  38. str += c;
  39. }
  40. while (true) {
  41. if (inputFile.fail())
  42. break;
  43. inputFile.get(c);
  44. data[str].add(c);
  45. data.put(str, data[str]);
  46. str = str.substr(1, str.length() - 1) + c;
  47. }
  48. string max;
  49. int maxSize = 0;
  50. foreach (string str in data) {
  51. if (data.get(str).size() > maxSize) {
  52. maxSize = data.get(str).size();
  53. max = str;
  54. }
  55. }
  56. string result = max;
  57. string tmp = max;
  58. while (result.size() < 2000) {
  59. int randomIndex = randomInteger(0, data.get(tmp).size() - 1); // ??????
  60. char ch = data.get(tmp).get(randomIndex);
  61. result += ch;
  62. tmp = tmp.substr(1, k - 1) + ch;
  63. }
  64. cout << result << endl;
  65.  
  66. inputFile.close();
  67. return 0;
  68. }
  69.  
  70. /*
  71. * function lets user enter valid name for file.
  72. */
  73. void promptUserForFile(ifstream & in, string prompt) {
  74. while (true) {
  75. cout << prompt << endl;
  76. string filename;
  77. getline(cin, filename);
  78. in.open(filename.c_str());
  79. if (!in.fail()) break;
  80. in.clear();
  81. prompt = "Unable to open that file. Try again: ";
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment