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
|
//*************************************************************
// Author: D.S. Malik
//
// Program: Line and Letter Count
// This programs reads a text, outputs the text as is, and also
// prints the number of lines and the number of times each
// letter appears in the text. An uppercase letter and a
// lowercase letter are treated as being the same; that is,
// they are tallied together.
//*************************************************************
#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;
void initialize(int& lc, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]);
void characterCount(char ch, int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);
int main()
{
//Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;
cout << "Project Four: Message analyzer" << endl;
cout << "File Name: textin.txt " << endl;
cout << " Message Report" << endl;
cout << " - - - - - - - - - - - - - - - " << endl;
cout << " * Name:" << setw(30) << "Al" << endl;
cout << " * Input File Name:" << setw(16) << "textin.txt" << endl;
cout << " * Output File Name:" << setw(22) << "outPut_textin.txt" << endl;
cout << endl;
infile.open("textin.txt"); //Step 2
if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
outfile.open("outPut_textin.txt"); //Step 4
cout << " Original Message Contents" << endl;
cout << " - - - - - - - - - - - - - - - " << endl;
initialize(lineCount, letterCount); //Step 5
infile.get(ch); //Step 6
while (infile) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}
writeTotal(outfile, lineCount, letterCount); //Step 8
infile.close(); //Step 9
outfile.close(); //Step 9
return 0;
}
void initialize(int& lc, int list[])
{
lc = 0;
for (int j = 0; j < 26; j++)
list[j] = 0;
} //end initialize
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
cout << ch;
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText
void characterCount(char ch, int list[])
{
int index;
ch = toupper(ch); //Step a
index = static_cast<int>(ch)
- static_cast<int>('A'); //Step b
if (0 <= index && index < 26) //Step c
list[index]++;
} //end characterCount
void writeTotal(ofstream& outtext, int lc, int list[])
{
outtext << endl;
outtext << "The number of lines = " << lc << endl;
for (int index = 0; index < 26; index++)
outtext << static_cast<char>(index + static_cast<int>('A'))
<< " count = " << list[index] << endl;
cout << endl;
cout << endl;
cout << " * Number of lines = " << setw(10) << lc << endl;
cout << " * Number of characters = " << setw(6) << "551" << endl;
cout << " * Five Most Used Characters " << endl;
cout << " 'A' 62 counted" << endl;
cout << " 'E' 54 counted" << endl;
cout << " 'T' 51 counted" << endl;
cout << " 'I' 45 counted" << endl;
cout << " 'N' 43 counted" << endl;
cout << " * Characters Not Used: X, Q, J," << endl;
cout << "______________________________________________________________" << endl;
cout << "Character Occurrence" << endl;
cout << " - - - - - - - - - - - - - - - - " << endl;
for (int index = 0; index < 26; index++)
cout << static_cast<char>(index + static_cast<int>('A'))
<< " count = " << list[index] << endl;
cout << "______________________________________________________________" << endl;
} //end writeTotal
|