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
|
#include <sstream>
#include <vector>
#include <cctype>
using namespace std;
const int SIZE = 8000;
struct WordCount
{
string name;
int count;
};
void bubbleSort(WordCount arr[], int size)
{
int i, j;
WordCount temp;
for(i = 0; i < size; i++)
for(j = 0; j < size - 1; j++)
{
if(arr[j].name > arr[j + 1].name)
{
temp = arr[j], arr[j] = arr[j + 1], arr[j + 1] = temp;
}
}
}
void removeSpecificWord(WordCount arr[], int idx, int &size)
{
int i;
for(i = idx; i < size - 1; i++) arr[i] = arr[i + 1]; size--;
}
void removeDuplicateWord(WordCount arr[], int &size)
{
int i, j;
string target;
for(i = 0; i < size; i++)
{
arr[i].count++;
target = arr[i].name;
for(j = i + 1; j < size; j++)
{
if(target == arr[j].name) removeSpecificWord(arr, j, size), arr[i].count++, j--;
}
}
}
int main()
{
int i;
int arraySize = 0;
WordCount wordArray[SIZE];
string fileName("gdp.txt");
ifstream inFile(fileName.c_str());
while(!inFile.is_open())
{
cout << "The file \"" << fileName << "\" not found. Enter another file : "; getline(cin, fileName);
inFile.clear(); inFile.open(fileName.c_str()); cout << endl;
}
cout << "The file \"" << fileName << "\" has been opened successfully.\n\n";
string word;
string fileContent;
while(inFile >> word) fileContent += word + ' ';
inFile.close();
for(i = 0; i < fileContent.size(); i++) if(ispunct(fileContent[i])) fileContent[i] = ' ';
stringstream ss;
ss << fileContent;
while(ss >> fileContent) wordArray[arraySize].name = fileContent, wordArray[arraySize].count = 0, arraySize++;
removeDuplicateWord(wordArray, arraySize);
bubbleSort(wordArray, arraySize);
cout << "Array content..." << endl << endl;
for (i = 0; i < arraySize; i++)
{
cout << setw(13) << left << wordArray[i].name << setw(10) << "Count = " << wordArray[i].count << endl;
}
return 0;
}
|