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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <list>
#include <string.h>
#include <cstring>
#include <algorithm>
using namespace std;
//This struct is to identify the words and do the counting inside the list.
typedef struct
{
string word;
int count;
} Info;//The name of struct type
//This declaration is to find similar word inside the list, if it is equals then return the iterator.
list<Info>::iterator findSimilar(list<Info> &myList, string word)
{
list<Info>::iterator it;
for (it = myList.begin(); it != myList.end(); it++)
{
if ((it->word).compare(word) == 0)
{
return it;
}
}
return it;
}
//This function is to get each different word
int getUniqueWords(list<Info> &myList)
{
return myList.size();
}
//This function is to get all the words inside the list
int getAllWords(list<Info> &myList)
{
int nWords = 0;
list<Info> :: iterator it;
for (it = myList.begin(); it != myList.end(); it++)
{
nWords += it->count;
}
return nWords;
}
//This function is to add word to the list and break it into lines
void addToList(list<Info> &myList, string word)
{
char *cstr, *pch;
list<Info> :: iterator it;
Info temp;
cstr = new char [word.size() + 1];
strcpy (cstr, word.c_str());
pch = strtok (cstr, " %\t,;:”~!#^*()=+[]{}|<>?/\"");
while (pch != NULL)
{
it = findSimilar(myList, pch);
if (it != myList.end())
{
it->count++;
}
else
{
temp.word = pch;
temp.count = 1;
myList.push_back(temp);
}
pch = strtok(NULL, " ");
}
}
//This function is to compare 2 lists
bool compare(Info a, Info b)
{
return a.word.compare(b.word) < 0;
}
//This function is to sort the list using the bool compare
void sortList(list<Info> &myList)
{
myList.sort(compare);
}
//This function is to print list every count, word, and total of unique words and total words in the list
void printList(list<Info> &myList, const string &filename)
{
list<Info> :: iterator it;
cout << "\n" << "COUNT " << "|" << " WORD" << "\n";
cout << "------" << "+" << "-----" << "\n";
for (it = myList.begin(); it != myList.end(); it++)
{
cout << " " << it->count << " | " << it->word << endl;
}
cout << "\n" << "Number of unique words in " << filename << " is "<< getUniqueWords(myList) << endl;
cout << "Total number of words in " << filename << " is "<< getAllWords(myList) << endl;
}
int main(int argc, char *argv[])
{
list <Info> myList;
string str;
ifstream file (argv[1]);
string filename = argv[1];
if (argc != 2)
{
cout << "usage: " << argv[0] << " <filename>" << endl;
}
else
{
while(!file.eof())
{
getline(file, str);
addToList(myList, str);
}
sortList(myList);
printList(myList, filename);
}
return 0;
}
| |