Guest User

Untitled

a guest
Apr 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. //////////////////////////////////////
  2. // File Name: JohnYun_Lab1_CIS22B
  3. //
  4. // Copyright Date: April 13, 2018
  5. //
  6. // Description: the program will first ask the user for a location+name from where to read
  7. // the file and location+name where to save the output file,
  8. // read the contents into an array, ignoring single character
  9. // words, sort the contents of the array in alphabetically
  10. // ascending order and then start a loop to allow the user to
  11. // search for one or more words in the array - your loop should
  12. // have an appropriate exit condition. If the word is found,
  13. // the program should output which array location the word was
  14. // found in, if not found then it should output an appropriate
  15. // message.
  16. // Provide clear prompts as necessary for good user interactivity.
  17. //
  18. //
  19. //////////////////////////////////////
  20.  
  21. #include <iostream>
  22. #include <fstream>
  23. #include <string>
  24.  
  25. using namespace std;
  26.  
  27. //function prototypes
  28. void inputFunc(string[]);
  29. void sortWords(string[], const int);
  30. int searchInput(string[], int, string);
  31. void outputFunc(int);
  32.  
  33. int main(void)
  34. {
  35. string words[1024];
  36. inputFunc(words);
  37. sortWords(words, 1024);
  38. system("pause");
  39. return 0;
  40. }
  41.  
  42. void inputFunc(string list[1024])
  43. {
  44. //location?
  45. //ask user for name
  46. string in_file;
  47. cout << "Enter the name of the input file: " << endl;
  48. cin >> in_file;
  49.  
  50. fstream input;
  51. input.open(in_file, ios::in | ios::binary);
  52.  
  53. if (input.fail())
  54. {
  55. cout << "file not found. Rerun Program and try again." << endl;
  56. system("pause");
  57. exit(100);
  58. }
  59.  
  60. int i = 0;
  61. while (input.eof() == 0)
  62. {
  63. string x;
  64. input.read(reinterpret_cast<char*>(&x), sizeof(x));
  65. // take out single character words
  66. if (x.length() == 1)
  67. {
  68. i++;
  69. }
  70. //take out all other symbol characters and input words into array
  71. else
  72. {
  73. list[i] = x;
  74. i++;
  75. }
  76.  
  77. }
  78. }
  79.  
  80. void sortWords(string list[], const int size)
  81. {
  82. //start selection sort to sort the words in list[]
  83. int minimumLocation = 0;
  84.  
  85. for (int index = 0; index < size; index++)
  86. {
  87. for (int subIndex = index + 1; subIndex < size; subIndex++)
  88. {
  89. if (list[minimumLocation] > list[subIndex])
  90. minimumLocation = subIndex;
  91. }
  92. // now we have the minimum location and now switch with the index currently worked on
  93. string temp = list[index];
  94. list[index] = list[minimumLocation];
  95. list[minimumLocation] = temp;
  96. }
  97. }
  98.  
  99. int searchInput(string list[], int numberElements, string target)
  100. {
  101. int location = -1;
  102. //implement binary search
  103. int first = 0, last = 1024, mid = 1024 / 2; // (last - first )/2
  104. while ((location = -1) && (first <= last))
  105. {
  106. if (list[mid] == target)
  107. location = mid;
  108. else if (list[mid] < target) //move upwards
  109. {
  110. first = mid + 1;
  111. mid = (last - first) / 2;
  112. }
  113. else
  114. {
  115. last = mid - 1;
  116. mid = (last - first) / 2;
  117. }
  118. }
  119.  
  120. return location;
  121. }
  122.  
  123. void outputFunc(int location)
  124. {
  125. //output to binary file
  126. if (location)
  127. {
  128. cout << "The word you search for was found at index: " << location;
  129. }
  130. else
  131. {
  132. cout << "Word was not found. Re-run the program and search a different word";
  133. }
  134. }
Add Comment
Please, Sign In to add comment