Sie sind auf Seite 1von 2

I'm trying to write a program that reads in a text file and then

calculates the frequency of each of the alphabet characters that


appears in the file. The characters are read into an array that maps them to the
ir upper-case equivalent in ACSII like this:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9950, 1830,
2606, 5025, 14960, 2698, 2420, 8731, 8511, 110, 1272, 5847, 4253, 8297, 11218, 2
016, 220, 7777, 8379, 11863, 4343, 1222, 3132, 179, 3204, 72
This is the output it produces so far:
The file "hamlet.txt" has following letter frequencies:
a: 9950 b: 1830 c: 2606 d: 5025 e: 14960 f: 2698
g: 2420 h: 8731 i: 8511 j: 110 k: 1272 l: 5847
m: 4253 n: 8297 o: 11218 p: 2016 q: 220 r: 7777
s: 8379 t: 11863 u: 4343 v: 1222 w: 3132 x: 179
y: 3204 z: 72
What I want to do now is simply print a list of the alphabet
characters from most frequent to least frequent, e.g.
e, t, o, a, h, i, s, n, r, l, d, u, m, y, w, f, c, g, p, b, k, v, q,
x, j, z
It's easy to print out the single most frequent (or least frequent)
character but I'm really struggling to think of a way to print them
all out, from most frequent to least frequent.
Any help with this problem would be most appreciated.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string filename;
cout << "enter name of input file: ";
cin >> filename;
ifstream input;
input.open(filename.c_str());
if (input.fail() )
{ cout << "could not open file " << filename << endl;
exit(1);
}
string word;
const int SIZE = 91;
int lines = 0, words = 0, freq[SIZE] = {0}, len;
char c;
while (input >> word)
{ ++words;
input.get(c);
if (c == '\n') ++lines;
len = word.length();
for (int i = 0; i < len; i++)
{ c = word[i];
if (c >= 'a' && c <= 'z') c+= 'A' - 'a'; // capitalise c
if (c >= 'A' && c <= 'Z') ++freq[c]; // count c
}
}
cout << "The input had " << lines << " lines, " << words
<< " words, \nand the following letter frequencies:\n";
for (int i = 65; i<SIZE; i++)
{ cout << '\t' << char(i) << ": " << freq[i];
if (i > 0 && i%8 == 0) cout << endl;
}
return 0;
}

an interesting way to be absolutely clear about what you are doing is to use str
ucts to handle this problem.
//declare struct
struct _letter
{
char letter;
int frequency;
}
//declare an array of struct
_letter letters[26];
//initialize array elements to capital letters for letters[i].letter and
//all zeros for frequency
int i;
char ch = 'A';
for(i = 0, i < 26; i++)
{
letters[i].letter = ch++;
letters[i].frequency = 0;
}
now read in your file and increment letters[i].frequiency where letters[i].lette
r = charReadIn.
When file completely read, then sort file by letters[i].frequency, and print the
list of letters in order by printing letters[i].letter.
http://www.scribd.com/doc/6851196/solmanual

Das könnte Ihnen auch gefallen