@RamilH/

Madlib

C++11

No description

fork
loading
Files
  • main.cpp
  • adjective.cpp
  • adjective.hpp
  • adverb.cpp
  • adverb.hpp
  • makefile
  • noun.cpp
  • noun.hpp
  • story1.txt
  • story2.txt
  • story3.txt
  • word.hpp

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.42705267212553477","path":"main.cpp","file":{"path":"main.cpp","content":{"asEncoding":{"base64":"CgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxpb21hbmlwPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGFsZ29yaXRobT4gCiNpbmNsdWRlICJ3b3JkLmhwcCIKI2luY2x1ZGUgIm5vdW4uaHBwIgojaW5jbHVkZSAiYWR2ZXJiLmhwcCIKI2luY2x1ZGUgImFkamVjdGl2ZS5ocHAiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdGQ6OnN0cmluZyByZXBsYWNlV29yZChzdHJpbmcmIHMsIGNvbnN0IHN0cmluZyYgdG9SZXBsYWNlLCBjb25zdCBzdHJpbmcmIHJlcGxhY2VXaXRoKTsKYm9vbCB3b3JkRXhpc3RzKHN0cmluZyYgcywgY29uc3Qgc3RyaW5nJiB3b3JkVHlwZSk7CmludCBmaW5kV29yZChzdHJpbmcmIHMsIGNvbnN0IHN0cmluZyYgd29yZFR5cGUpOwoKaW50IG1haW4oKSAKewogIC8vUmVhZCBmcm9tIGxvY2FsIHRleHQgZmlsZXMKICAvL0dpdmUgc2VsZWN0aW9uIHNjcmVlbiB3aXRoIGZpbGVOYW1lIGZvciB0aXRsZQogIC8vRmluZCBhbGwgYWRqZWN0aXZlcywgbm91bnMsIGFkdmVyYnMsIGFuZCBhZHZlcmJzIGluIHRoZSB0ZXh0IGZpbGUKICAvL2FkZCB0byB3b3JkIGxpc3QgYW5kIHJlbW92ZSB0aG9zZSB0YWdz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GFjZS5sZW5ndGgoKSwgcmVwbGFjZVdpdGgpOwp9CgppbnQgZmluZFdvcmQoc3RyaW5nJiBzLCBjb25zdCBzdHJpbmcmIHdvcmRUeXBlKQp7CiAgICBzaXplX3QgcG9zID0gcy5maW5kKHdvcmRUeXBlKTsKCiAgICAvL0lmIERvZXNuJ3QgZXhpc3QgcmV0dXJuIC0xCiAgICBpZiAocG9zID09IHN0cmluZzo6bnBvcykgCiAgICAgIHJldHVybiAtMTsKCiAgICBzLnJlcGxhY2UocG9zLCB3b3JkVHlwZS5sZW5ndGgoKSwgIiIpOwogICAgcmV0dXJuIHBvczsKfQoKYm9vbCB3b3JkRXhpc3RzKHN0cmluZyYgcywgY29uc3Qgc3RyaW5nJiB3b3JkVHlwZSkKewogICAgc2l6ZV90IHBvcyA9IHMuZmluZCh3b3JkVHlwZSk7CiAgICAvL0lmIERvZXNuJ3QgZXhpc3QgcmV0dXJuIC0xCiAgICBpZiAocG9zID09IHN0cmluZzo6bnBvcykgCiAgICAgIHJldHVybiBmYWxzZTsKCiAgICByZXR1cm4gdHJ1ZTsKfQoK"},"asBuffer":null},"loaded":true}}
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

#include <iostream>
#include <vector>
#include <iomanip>
#include <fstream>
#include <string>
#include <algorithm> 
#include "word.hpp"
#include "noun.hpp"
#include "adverb.hpp"
#include "adjective.hpp"
using namespace std;

std::string replaceWord(string& s, const string& toReplace, const string& replaceWith);
bool wordExists(string& s, const string& wordType);
int findWord(string& s, const string& wordType);

int main() 
{
  //Read from local text files
  //Give selection screen with fileName for title
  //Find all adjectives, nouns, adverbs, and adverbs in the text file
  //add to word list and remove those tags
  //Prompt to replace
  //Show result

  int numOfStories;
  int selectedStory;
  int charOffset = 0;
  int letCounter;
  string storyText;
  string response;

  ifstream inFile;


//Scan for stories in directory
while (true)
{
  // cout << "attempting to open story" + to_string(numOfStories+1) <<  ".txt" << endl; 
  inFile.open("story" + to_string(numOfStories+1) +  ".txt");

  if (!inFile)
  {
    inFile.close();

    if (numOfStories == 0)
    {
      cout << "There are no stories in the current directory!";
      exit(1);
    }

    break;
  }

  inFile.close();
  numOfStories++;
}

//Present selection menu for user
  do 
  {
    cout << numOfStories << " Stories found!\n" << endl;
    cout << "Please select a story to choose from by typing its associated number." << endl;
    for (int i = 0; i < numOfStories; i++)  
      cout << i+1 << ".) Story " << i+1 << endl;  
    cout << "Number: ";
    cin >> selectedStory;
    cout << endl;
  }
  while (!(selectedStory > 0 && selectedStory < numOfStories+1));
//******************************


 // Parse Story
  inFile.open("story" + to_string(selectedStory) +  ".txt");

  if (!inFile) 
  {
      cout << "Unable to open file";
  }    

  //Read Selected File and put into 
  while (!inFile.eof())
  {
    getline(inFile, storyText);
    inFile >> storyText;   
  }  
  inFile.close();

  //*******************************


  vector<Word*> words;

  //List of all WordTypes

  //Loop and find all nouns  
  string tempText = storyText;
  letCounter = 0;
  while (wordExists(tempText, "NOUN"))
    words.push_back(new Noun( findWord(tempText, "NOUN") + (letCounter++ * 4) ) );


  //Loop and find all adverbs
  tempText = storyText;
  letCounter = 0;
  while (wordExists(tempText, "ADVERB"))
    words.push_back(new Adverb(findWord(tempText, "ADVERB")  + (letCounter++ * 6)) );

  //Loop and find all adjectives
  tempText = storyText;
  letCounter = 0;
  while (wordExists(tempText, "ADJECTIVE"))
    words.push_back(new Adjective(findWord(tempText, "ADJECTIVE")  + (letCounter++ * 9)) );

  
  //Sort vector with bubble sort since sort failed
   //std::sort (words.rbegin(), words.rend());  
  for (int i = 0; i < words.size(); i++)
    for (int j = 0; j < words.size() - i - 1; j++)
      if (words[j]->position < words[j+1]->position)
      {
        Word* temp = words[j];
        words[j] = words[j+1];
        words[j+1] = temp;
      }   


  //Prompt user to fill words
  for (int i = 0; i < words.size(); i++)
  {
    cout << words[i]->GetPrompt();
    cin >> response;

    //DEBUG INFO
    // cout << "offset " << charOffset << endl;
    // cout << "pos " << words[i]->position  << endl;
    // cout << "Pos+offset: " << words[i]->position + charOffset << endl;
    // cout << "StorySize: " << storyText.length() << endl;
    // cout << endl;

    storyText.replace(words[i]->position + charOffset, words[i]->GetType().length(), response);    
    // charOffset += response.length();
  }

  //Result
  cout << "\nRESULT:";
  cout << endl << storyText << endl;


  cout << "\n\nFinished!" << endl;
}

string replaceWord(string& s, const string& toReplace, const string& replaceWith)
{
    size_t pos = s.find(toReplace);
    if (pos == string::npos) return s;
    return s.replace(pos, toReplace.length(), replaceWith);
}

int findWord(string& s, const string& wordType)
{
    size_t pos = s.find(wordType);

    //If Doesn't exist return -1
    if (pos == string::npos) 
      return -1;

    s.replace(pos, wordType.length(), "");
    return pos;
}

bool wordExists(string& s, const string& wordType)
{
    size_t pos = s.find(wordType);
    //If Doesn't exist return -1
    if (pos == string::npos) 
      return false;

    return true;
}