-
Notifications
You must be signed in to change notification settings - Fork 1
/
cs235_assign16.cpp
83 lines (78 loc) · 2.19 KB
/
cs235_assign16.cpp
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
/******************************************************************************
* Program:
* Assignment 16, BOM Word Count
* Brother Ercanbrack, CS 235
* Author:
* Tyler Scott
* Summary:
* This program keeps track of the most repeated words in the Book of Mormon
*
* Estimated time: 8.0hrs
* Actual time: 2.0hrs
******************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <map>
using namespace std;
/******************************************************************************
* Main handles everything in this program
*****************************************************************************/
int main(int argc, char* argv[])
{
// open the file from command line
ifstream fin;
fin.open(argv[argc - 1]);
if (fin.fail())
{
cout << "open file error " << argv[argc - 1] << endl;
return 0;
}
int total;
string aWord;
map < string, int > mapped;
//Read through file tracking the number of times words show up.
//ignore the punctuation.
while (fin >> aWord)
{
for (int i = 0; i < aWord.length(); i++)
{
if (ispunct(aWord[i]) && aWord[i] != '-')
{
aWord.erase(aWord.begin() + i);
i--;
}
else if (!ispunct(aWord[i]))
{
aWord[i] = tolower(aWord[i]);
}
}
//add to the counters.
mapped[aWord]++;
total++;
}
cout << endl << "Number of words processed: " << total << endl;
cout << "100 most common words found and their frequencies:\n";
//traverse the map and find the greatest reoccurrence of words
int j = 0;
while (j < 100)
{
//iterator through removing the greatest piece after each find
map < string, int > :: iterator it1 = mapped.begin();
for (map < string, int > :: iterator it2 = mapped.begin();
it2 != mapped.end(); it2++)
{
if (it2->second > it1->second)
{
it1 = it2;
}
}
// display
cout << setw(23) << it1->first << " - " << it1->second << endl;
mapped.erase(it1);
j++;
}
fin.close();
return 0;
}