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 <cctype>
#include <string>
#include <iomanip>
using namespace std;
const int SIZE = 200;
struct Word
{
string w;
int count;
};
class List
{
public:
string toLowerCase(const string& s);
string removePunct(const string& s, const string& punct);
void countAndDisplay(const string& s);
void control(const string& s);
private:
int totalWords;
int distinctNeg;
};
int main()
{
string sentences;
List start;
cout << "Please enter some sentences below.\n"
<< "Enter the @ character to terminate input.\n";
getline(cin, sentences, '@');
cout << "\n\n\n" << sentences << "\n\n\n";
start.control(sentences);
system("pause");
return(0);
}
string List::toLowerCase(const string& s)
{
string temp(s);
for (int i = 0; i < s.length(); i++)
temp[i] = tolower(s[i]);
return temp;
}
string List::removePunct(const string& s, const string& punct)
{
string noPunct;
int sLength = s.length();
int punctLength = punct.length();
for (int i = 0; i < sLength; i++)
{
string aChar = s.substr(i,1);
int location = punct.find(aChar, 0);
if (location < 0 || location >= punctLength)
noPunct = noPunct + aChar;
}
return noPunct;
}
void List::countAndDisplay(const string& s)
{
Word words[SIZE];
string str = s;
List counting;
counting.totalWords = 0;
counting.distinctNeg = 0;
//COUNT
for (int k, i = 0; i < SIZE - 1; i++)
{
cin >> str;
getline(cin, words[i].w, ' ');
for (int j = 0; j < i; j++)
{
if (words[j].w == words[i].w)
{
words[j].count += 1;
i--;
counting.distinctNeg++;
}
else
words[i].count +=1;
}
k = str.find(' ');
str = str.remove(0, k - 1);
counting.totalWords++;
}
int totalDistinct = counting.totalWords - counting.distinctNeg;
//Some OUTPUT
cout << "\n\nTotal Number of words: " << counting.totalWords << "\n\n";
cout << "Total Number of distinct words: " << totalDistinct << "\n\n";
//SORT Most to Least
for (int i = 0; i < counting.totalWords; i++)
{
for (int j = 0; j < i; j++)
{
if (words[i].count > words[j].count)
{
Word tempWords = words[i];
words[i] = words[j];
words[j] = tempWords;
}
}
}
//More OUTPUT
for (int i = 0; i < counting.totalWords; i++)
cout << setw(40) << left << words[i].w
<< setw(3) << left << words[i].count << endl;
}
void List::control(const string& s)
{
List words;
string punct(",;:.?!\"");
string lowString, noPunct;
lowString = words.toLowerCase(s);
noPunct = words.removePunct(lowString, punct);
words.countAndDisplay(noPunct);
}
|