Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////
- // File Name: JohnYun_Lab1_CIS22B
- //
- // Copyright Date: April 13, 2018
- //
- // Description: the program will first ask the user for a location+name from where to read
- // the file and location+name where to save the output file,
- // read the contents into an array, ignoring single character
- // words, sort the contents of the array in alphabetically
- // ascending order and 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.
- // Provide clear prompts as necessary for good user interactivity.
- //
- //
- //////////////////////////////////////
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- //function prototypes
- void inputFunc(string[]);
- void sortWords(string[], const int);
- int searchInput(string[], int, string);
- void outputFunc(int);
- int main(void)
- {
- string words[1024];
- inputFunc(words);
- sortWords(words, 1024);
- system("pause");
- return 0;
- }
- void inputFunc(string list[1024])
- {
- //location?
- //ask user for name
- string in_file;
- cout << "Enter the name of the input file: " << endl;
- cin >> in_file;
- fstream input;
- input.open(in_file, ios::in | ios::binary);
- if (input.fail())
- {
- cout << "file not found. Rerun Program and try again." << endl;
- system("pause");
- exit(100);
- }
- int i = 0;
- while (input.eof() == 0)
- {
- string x;
- input.read(reinterpret_cast<char*>(&x), sizeof(x));
- // take out single character words
- if (x.length() == 1)
- {
- i++;
- }
- //take out all other symbol characters and input words into array
- else
- {
- list[i] = x;
- i++;
- }
- }
- }
- void sortWords(string list[], const int size)
- {
- //start selection sort to sort the words in list[]
- int minimumLocation = 0;
- for (int index = 0; index < size; index++)
- {
- for (int subIndex = index + 1; subIndex < size; subIndex++)
- {
- if (list[minimumLocation] > list[subIndex])
- minimumLocation = subIndex;
- }
- // now we have the minimum location and now switch with the index currently worked on
- string temp = list[index];
- list[index] = list[minimumLocation];
- list[minimumLocation] = temp;
- }
- }
- int searchInput(string list[], int numberElements, string target)
- {
- int location = -1;
- //implement binary search
- int first = 0, last = 1024, mid = 1024 / 2; // (last - first )/2
- while ((location = -1) && (first <= last))
- {
- if (list[mid] == target)
- location = mid;
- else if (list[mid] < target) //move upwards
- {
- first = mid + 1;
- mid = (last - first) / 2;
- }
- else
- {
- last = mid - 1;
- mid = (last - first) / 2;
- }
- }
- return location;
- }
- void outputFunc(int location)
- {
- //output to binary file
- if (location)
- {
- cout << "The word you search for was found at index: " << location;
- }
- else
- {
- cout << "Word was not found. Re-run the program and search a different word";
- }
- }
Add Comment
Please, Sign In to add comment