Guest User

Untitled

a guest
Dec 12th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <cstring>
  6. #include <iomanip>
  7. #include "BST.h"
  8. using namespace std;
  9.  
  10. //define a node in a single linked list with data and a link field
  11. struct ListNode
  12. {
  13. char data;
  14. ListNode * next;
  15. };
  16.  
  17. /* The class defines a List ADT with the core functionality, specifically the
  18. ability to: 1) Insert an item into a List, 2) Delete an item from the List,
  19. 3) Print the List, and 4) Edit an item in the List. The only data maintained
  20. by the List is a pointer to the current head (front) of the list. If the
  21. List is empty, then head will be equal to NULL.
  22. */
  23. class List
  24. {
  25. public:
  26. List();
  27. ~List();
  28. bool Enq(char);
  29. ListNode* Deq();
  30. void Print();
  31. bool Edit(char, char);
  32. bool Empty();
  33. string toStr();
  34. void deleteAll();
  35. private:
  36. void deleteAllRecursion(ListNode*);
  37. ListNode * head;
  38. ListNode * rear;
  39. };
  40.  
  41. List::List()
  42. {
  43. head = NULL;
  44. rear = NULL;
  45. }
  46.  
  47. List::~List()
  48. {
  49. //Checking if the list is empty
  50. if (head != NULL)
  51. {
  52. for (ListNode*cur = head; cur != NULL; cur = cur->next)
  53. {
  54. //cycling through the linked list to delete
  55. ListNode*temp = head;
  56. head = head->next;
  57. delete temp;
  58. }
  59. }
  60. head = NULL;
  61. rear = NULL;
  62. }
  63. /*This function creates a new node and if head is null, sets head to it.
  64. If not, rear->next is set to temp. Then rear is set to temp.*/
  65. bool List::Enq(char insertedData)
  66. {
  67. ListNode *temp = new ListNode;
  68. if (!temp){
  69. cout << "Memory allocation failed...\n";
  70. return false;
  71. }
  72.  
  73. temp->data = insertedData;
  74.  
  75. temp->next = NULL;
  76.  
  77. if (head == NULL){
  78. head = temp;
  79. }
  80. else{
  81. rear->next = temp;
  82. }
  83.  
  84. rear = temp;
  85. return true;
  86. }
  87. /*trail, a pointer to a Node, is declared. The list is traversed and each Node's data is checked against the char to be deleted.
  88. The trail keeps track of the previous Node. If the Node to be deleted is head, head is changed to head->next and the Node is
  89. deleted. Otherwise, the list is traversed the rest of the way and Node pointers are reassigned and the Node to be deleted is
  90. removed, deallocating its memory.
  91. */
  92. ListNode* List::Deq()
  93. {
  94. if (head != NULL){
  95. ListNode*returnvalue = head;
  96. head = head->next;
  97. return returnvalue;
  98. }
  99. else{
  100. return NULL;
  101. }
  102. }
  103. //The List is traversed and all of its values are outputted to the console.
  104. void List::Print()
  105. {
  106. for (ListNode*cur = head; cur != NULL; cur = cur->next)
  107. {
  108. cout << cur->data << endl;
  109. }
  110. }
  111. /******************************************************************************
  112. delete all is public, allowing for the deconstruction of the list on the fly.
  113. I don't feel like calling the deconstructor so here you go: an even sillier
  114. way to delete the whole list... through recursion. Because I like recursion.
  115. ******************************************************************************/
  116. void List::deleteAll(){
  117. deleteAllRecursion(head);
  118. head = NULL;
  119. rear = NULL;
  120. }
  121. void List::deleteAllRecursion(ListNode* aye){
  122. if (aye != NULL){
  123. deleteAllRecursion(aye->next);
  124. }
  125. delete aye;
  126. }
  127. //checks if the list is empty
  128. bool List::Empty(){
  129. return(head == NULL && rear == NULL);
  130. }
  131.  
  132. //this loops through the linked list and stores them in a c-string one by one.
  133. //that c-string is stored in a string object and outputted. **WARNING** c-string
  134. //is statically allocated to 50 characters, so no more than 49 characters per word.
  135. //idk if this will be a problem, we'll see.
  136. string List::toStr(){
  137. char cstrOut[50];
  138. int x = 0;
  139. ListNode*out = Deq();
  140. while (out != NULL){
  141. cstrOut[x++] = out->data;
  142. out = Deq();
  143. }
  144. cstrOut[x] = NULL;
  145. string output = cstrOut;
  146. return output;
  147. }
  148.  
  149. /*This function searches the c-string for a word and stores it in a queue
  150. then turns it into a string and returns it.
  151. */
  152. void parsingForWords(string line, BST &wordCount){
  153. int length = line.length();
  154. const char* lineCstr = line.c_str();
  155. List word;
  156. for (int x = 0; x < length; x++)
  157. {
  158. if (isalpha(lineCstr[x])){
  159. word.Enq(toupper(lineCstr[x]));
  160. }
  161. else if (!word.Empty()){
  162. wordCount.Insert(word.toStr());
  163. }
  164. }
  165. }
  166.  
  167.  
  168. void main()
  169. {
  170. //I feel like this could have been more efficient, but this is how I did it.
  171.  
  172. //a new BST object is created
  173. BST WordCount;
  174. //a std::string is created to store the inputted filename
  175. string fileName;
  176. //input the file name
  177. cout << "Enter name of file to be processed (79 characters or less, including extension): ";
  178. cin >> fileName;
  179.  
  180. //the size of the string is saved in a const int
  181. const int fileNameSize = fileName.length();
  182.  
  183. /****************************************************************************************************
  184. Very elaborate code designed to create the input and output files. This prevents the original name
  185. from being altered while simultaneously creating the output file in the BST to create the .frq file.
  186. The function takes a string (the file name) as a parameter, and creates several local variables to
  187. get the output file name to have a .frq instead of .txt (or whatever it's extension is; it's set up
  188. to stop at a '.'). The many local variables create a c-string to store the std::string in order to
  189. process it. A while loop stores the individual characters into a new c-string until a '.' or NULL is
  190. reached, at which point the loop exits and adds '.frq' on, each character at a time. The post-
  191. increment operator is used often. Then finally, NULL is stored in the last slot. **WARNING** The
  192. file name character array is statically allocated to store 80 characters, so the file name cannot
  193. exceed 79 characters.
  194. ****************************************************************************************************/
  195.  
  196. const char* cstr = fileName.c_str();
  197.  
  198. //that elaborate slew of local variables slowing the program down but getting done what I want
  199. char outFileName[80];
  200. int j = 1;
  201. char i = *cstr;
  202. //the loop to store the file name, minus the extension
  203. while (i != '.' && i != NULL)
  204. {
  205. outFileName[j] = i;
  206. i = *(cstr + j);
  207. j++;
  208. }
  209. //adding the new extension
  210. outFileName[j++] = '.';
  211. outFileName[j++] = 'f';
  212. outFileName[j++] = 'r';
  213. outFileName[j++] = 'q';
  214. outFileName[j] = NULL;
  215.  
  216. /*****************************************************************************
  217. the OutputFile member of the WordCount BST's open method is used to open the
  218. file, and the outFileName is passed to it as a name.
  219. *****************************************************************************/
  220. WordCount.OutputFile.open(outFileName);
  221.  
  222. //the input file is opened.
  223. ifstream inFile(fileName);
  224.  
  225. //the parsing function is called while reading in line-by-line.
  226. if (!inFile.good())
  227. cout << "Input file not opened...\n";
  228. else{
  229. while (!inFile.eof()){
  230. string line;
  231. getline(inFile, line);
  232. parsingForWords(line, WordCount);
  233. }
  234. }
  235. //The user is asked if they wish to delete a Node.
  236. //If I had time, I'd learn to use GUIs and implement one, but I dont
  237. //so I won't. Enjoy the command line instead. q:
  238. char input;
  239. string deleteThis;
  240. cout << "Delete a node? (y/n)";
  241. cin >> input;
  242. while (input == 'y' || input == 'Y'){
  243. cin >> deleteThis;
  244. WordCount.Delete(deleteThis);
  245. cout << "Delete a node? (y/n)";
  246. cin >> input;
  247. }
  248.  
  249. //The final frontier... writing it all.
  250. WordCount.write();
  251. WordCount.OutputFile << "Height: " << WordCount.findMax() << endl
  252. << "Number of Nodes: " << WordCount.NodeTotal() << endl;
  253. WordCount.OutputFile.close();
  254. }
Advertisement
Add Comment
Please, Sign In to add comment