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
|
/**
Name: William Lee
Compiler: GNU GCC Compiler
Assignment #2
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
void assign_array(const int, string[], int, ifstream&);
void selectionSort(const int, string[], ifstream&);
int binarySearch(const int, const int, string[], string[], ifstream&, ifstream&, int);
//void output(ofstream, string[], int);
const int SIZE1 = 84;
const int SIZE2 = 16000;
int main(){
ifstream infile1, infile2;
ofstream outfile;
infile1.open("keywords.txt");
infile2.open("unsorted_dictionary.txt");
outfile.open("output.txt");
string dummy;
string keywords[SIZE1], unsorted_dictionary[SIZE2] ;
int sizee = sizeof(keywords) / sizeof(int);
int i = 0, j = 0;
if (infile1 && infile2){
assign_array(SIZE1,keywords,i, infile1);
assign_array(SIZE2,unsorted_dictionary,j, infile2);
selectionSort(SIZE1,keywords, infile1);
selectionSort(SIZE2,unsorted_dictionary, infile2);
binarySearch(SIZE1, SIZE2, keywords, unsorted_dictionary, infile1, infile2, sizee);
for (i = 0; i < SIZE1 ; i++){
if (keywords[i] == 0){
cout << "Keyword found: " << endl;
}
else{
cout << "Keyword not found: " << endl;
}
}
outfile.close();
return 0;
}
else{
cout << "Error Running File";
exit(1);
}
}
void assign_array(const int size_of_array, string areee[], int counter, ifstream& file){
while(counter < size_of_array && file >> areee[counter]){
counter++;
}
}
void selectionSort(const int size_of_array, string areee[], ifstream& file)
{
int minIndex;
for (int i = 0; i < size_of_array - 1; i++)
{
minIndex = i;
for (int j = i+1; j < size_of_array; j++)
{
if (areee[j] < areee[minIndex])
{
minIndex = j;
}
}
if(minIndex != i) {
swap(areee[i],areee[minIndex]);
}
}
}
int binarySearch(const int size_of_array1, const int size_of_array2, string areee1[], string areee2[], ifstream &file1, ifstream &file2, int sizer)
{
//int searchValue = areee1[];
int low, high, middle;
low = 0;
high = size_of_array2-1;
int i = 0;
for (i = 0; i < size_of_array1; i++){
while (low <= high){
middle = (low + high) / 2;
if (areee2[middle] <= areee1[middle]){
high = middle - 1;
}
else if (areee2[middle] >= areee1[middle]){
low = middle + 1;
}
else{
return 0;
}
}
}
return -1;
}
|