Advertisement
Guest User

Lab1_JaymenLuther

a guest
Jan 20th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.75 KB | None | 0 0
  1. // Jaymen Luther
  2. // CIS 22B
  3. // Lab 1
  4. #include <iostream>
  5. #include <string>
  6. #include <fstream>
  7.  
  8.  
  9.  
  10. using namespace std;
  11.  
  12. /*
  13. 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input.  When reading the file contents, you can discard words that are single characters to avoid symbols, special characters etc.
  14.  
  15. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function.
  16.  
  17. 3. Search any item input by user in your sorted list using the Binary Search algorithm implemented in its own function.
  18.  
  19. 4. Use string comparisons as taught in CIS 22A for comparing/ ordering words, i.e. words starting with numbers sort lower than words starting with uppercase letters which are lower than words starting with lowercase letters.
  20. So a word appearing once with one set of case is different than its second appearance with a different set of case, e.g. 'Do' and 'do' are not the same. You also do not need to remove non-alphanumeric characters from the words, e.g. '$12.34' is a valid 6-character word.
  21.  
  22. 5. If a word appears twice using exactly the same case, it can be stored twice side-by-side in the array and either index can be returned in the search.
  23.  
  24. 6. Your program will:
  25. first ask the user for a location+name from where to read the file and location+name where to save the output file,
  26. read the contents into an array, ignoring single character words,
  27. sort the contents of the array in alphabetically ascending order and
  28. then start a loop to allow the user to search for one or more words in the array - your loop should have an appropriate exit condition. If the word is found, the program should output which array location the word was found in, if not found then it should output an appropriate message.
  29. Provide clear prompts as necessary for good user interactivity.
  30.  
  31. 7. Your output should be sent to both screen and an output file concurrently. Screen output should contain the entire user interaction.  The file output should contain all the user interaction that went to the screen as well as the listing of the sorted array.
  32.  
  33. 8. User interactivity should be limited to your main, input and/or output functions only - what that means is your cin/cout should only be in those 3 functions.
  34.  
  35. 9. Make sure your text input, the screen output and file outputs are all inside of your MSVC project folder. Compress all of these into a single ZIP file format to upload.
  36. If you have a hidden folder inside your project folder called '.vs', please remove it before compressing, otherwise your zip file will be too large to upload.
  37.  
  38. 10. I will test your program with my own input file.
  39. */
  40.  
  41. /*
  42. Word count function
  43. While loop to count the amount of words in a file
  44. loop through the inFile
  45. each time it loops through a word add 1 to the wordcount
  46. */
  47.  
  48.  
  49. const int SIZE = 1024; // sizes of our word array
  50.  
  51. void write_toLog(const string str, string& outFileName)
  52. {
  53.     ofstream ofs_log(outFileName);
  54.  
  55.     if (ofs_log.is_open())
  56.     {
  57.         ofs_log << str;
  58.        
  59.     }
  60.     ofs_log.close();
  61.    
  62. }
  63. // Function to get input.txt path
  64. string get_inFile()
  65. {
  66.     string userInput;
  67.     cout << "Where is the input.txt file you would like to read? (use absolute path): " << endl;
  68.     cout << "> ";
  69.     cin >> userInput;
  70.  
  71.     return userInput;
  72.  
  73. }
  74.  
  75. // Function to get output.txt path
  76. string get_outFile()
  77. {
  78.     string userInput;
  79.     cout << "Specify the location you would like to create output.txt (use absolute path): " << endl;
  80.     cout << "> ";
  81.     cin >> userInput;
  82.  
  83.     return userInput;
  84. }
  85.  
  86. // function to store up to 1024 words that are greater than 1 character into wordArray[SIZE]
  87. int word_counter(ifstream& inFileRef, string (&wordArray)[SIZE]) //word counter - use reference to wordArray[SIZE]
  88. {
  89.    
  90.     int i = 0; // counter
  91.     string word; // variable to store each word
  92.    
  93.  
  94.     if (inFileRef.is_open()) //if inFile is open
  95.     {
  96.         while (!inFileRef.eof() && i < SIZE) // while you are not at the end of the file
  97.         {
  98.             inFileRef >> word; // write to word
  99.             if (word.length() > 1) // if that words length is greater than 1, lets store it into our array
  100.             {
  101.                 wordArray[i] = word; // storing... beep...
  102.                 // cout << "Position: " << i << " Length: " << wordArray[i].length() << " " << wordArray[i] << endl; // string statement to make sure everything is all good for now
  103.                 i++; // updating variable i to keep track of position array
  104.                
  105.             }
  106.         }
  107.         return i;
  108.     }
  109.     else
  110.     {
  111.         cout << "There is no input.txt in the path you specified" << endl;
  112.         return -1;
  113.     }
  114.  
  115. }
  116.  
  117. // Selection sort function to sort Array in ascending order
  118. void selection_sort(string (&wordArray)[SIZE], int array_len)
  119. {
  120.     int i, min_indx;
  121.     string strName;
  122.  
  123.     for (i = 0; i < (SIZE - 1); ++i)
  124.     {
  125.         min_indx = i;
  126.         strName = wordArray[i];
  127.         for (int j = i + 1; j < array_len; ++j)
  128.         {
  129.             if (wordArray[j].compare(strName) < 0)
  130.             {
  131.                 strName = wordArray[j];
  132.                 min_indx = j;
  133.             }
  134.         }
  135.         wordArray[min_indx] = wordArray[i];
  136.         wordArray[i] = strName;
  137.     }
  138.    
  139.  
  140. }
  141.    
  142. // prints out sorted array
  143. void array_printer(int array_length, string (&wordArray)[SIZE])
  144. {
  145.  
  146.     for (int i = 0; i < array_length; i++)
  147.     {
  148.         cout << "Position: " << i << " word: " << wordArray[i] << endl;
  149.     }
  150. }
  151.  
  152. // function to search for words
  153. void search(string (&wordArray)[SIZE], int array_len, string outFileName)
  154. {
  155.     string userInput;
  156.     cout << "Please enter the string you are searching for: ";
  157.     write_toLog("Please enter the string you are searching for: ", outFileName);
  158.     cin >> userInput;
  159.     for (int i = 0; i < array_len; i++)
  160.     {
  161.         if (userInput.compare(wordArray[i]) == 0)
  162.         {
  163.             cout << "Match" << endl;
  164.             return;
  165.         }
  166.     }
  167.  
  168.     cout << "No match" << endl;
  169.  
  170.  
  171. }
  172.  
  173.  
  174.  
  175.  
  176. int main()
  177. {
  178.     int selector = 0;
  179.     // Array to store 1024 words in a file
  180.     string wordArray[SIZE];
  181.     // inFile - use get_inFile() for path to input.txt
  182.     ifstream inFile(get_inFile());
  183.     // getting outFile location+name
  184.     string outFileName = get_outFile();
  185.     // Store the words into the array / int variable to get the length of the array
  186.     int array_length = word_counter(inFile, wordArray);
  187.     // sort word8Array in ascending order
  188.     selection_sort(wordArray, array_length);
  189.     // print the sorted array
  190.     array_printer(array_length, wordArray);
  191.  
  192.     //menu
  193.     while (selector != 5)
  194.     {
  195.         search(wordArray, array_length, outFileName);
  196.        
  197.         cout << "1. Search Again" << endl;
  198.         cout << "5. Quit" << endl;
  199.         cout << "Enter Selection: ";
  200.         cin >> selector;
  201.         switch (selector)
  202.         {
  203.         case 1: selector = 0;
  204.             break;
  205.         case 5: selector = 5;
  206.             break;
  207.         default: cout << selector << " is Not a valid option, please select from the menu!" << system("cls") << endl;
  208.         }
  209.        
  210.     }
  211.  
  212.     // you need to create an output.txt file still and then it is done
  213.  
  214.  
  215.    
  216.    
  217.  
  218.     return 0;
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement