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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
void initialize(int& lc, int list[]);//declaring prototypes
void characterCount (char ch, int list[]);
void readText(ifstream& intext, char& ch, int list[]);
void totalCount( int lc, int list[]);
int main()
{
int lineCount;
int letterCount[26]; //Because there are 26 letters in the english language, we create an array of 26 components.
char ch;
ifstream infile;
infile.open("C:\\mp6frequency.txt");
if (!infile)
{
cout << "Cannot open the file." << endl;
}
initialize(lineCount, letterCount);
infile.get(ch);
while (infile)
{
readText(infile, ch, letterCount);
lineCount++;
infile.get(ch);
}
totalCount(lineCount, letterCount);
infile.close();
system("pause");
return 0;
}
//this function initializies the variable lineCount and the array letterCount to 0.
void initialize(int& lc, int list[])
{
int j;
lc = 0;
for (j = 0; j < 26; j++)
list[j] = 0;
}
//this function increments the letter count. It also makes sure that it is only counting letters.
void characterCount (char ch, int list[])
{
int index;
ch = toupper(ch);
index = static_cast<int>(ch) - static_cast<int>('A');
if ( 0 <= index && index < 26)
list[index]++;
}
//this function reads the text and counts the characters
void readText(ifstream& intext, char& ch, int list[])
{
while ( ch != '\n')
{
characterCount (ch, list);
}
}
//this function displays data
void totalCount( int lc, int list[])
{
int index;
for ( index = 0; index < 26; index++)
cout << static_cast<char>(index + static_cast<int>('A')) << ' ' << list[index] << endl;
cout << "The number of lines = " << lc << endl;
}
| |