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 130
|
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
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 largestLetter (int list[]); // takes the list INDEX and returns the most often counted letter
int main ()
{
int lineCount;
int letterCount [26];
char ch;
char LARGEST;
string inputFileName;
ifstream infile;
ofstream outfile;
cout << "please enter the file name";
getline (cin, inputFileName);
infile.open (inputFileName);
if (!infile)
{
cout << "cannot open the file" << endl;
return 1;
}
outfile.open ("encryptedout.txt");
initialize(lineCount, letterCount);
infile.get(ch);
while (infile)
{
copyText (infile, outfile, ch, letterCount);
lineCount ++;
infile.get(ch);
}
cout << endl << endl<< "was the encrypted text"<< endl;
writeTotal(outfile, lineCount, letterCount);
LARGEST = largestLetter(letterCount);
cout <<"the largest count was " << LARGEST;
infile.close();
outfile.close();
return 0;
}
void initialize (int& lc, int list [])
{
int j;
lc = 0;
for (j=0; j<26; j++)
list[j]= 0;
}
void copyText (ifstream& intext, ofstream& outtext, char ch, int list [])
{
while (ch != '\n')
{
outtext<< ch;
characterCount (ch, list);
intext.get(ch);
cout << ch; // echos out the encrypted text
}
outtext<<ch;
}
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]++;
}
void writeTotal (ofstream& outtext, int lc, int list[])
{
int index;
outtext<< endl<< endl;
outtext << "The number of lines = " << lc << endl;
for (index = 0; index < 26; index++)
outtext << static_cast<char>(index +static_cast<int>('A')) << " count " << list[index] << endl;
}
int largestLetter (int list[])
{
int index = 1;
int maxIndex =0;
int largest= 0;
for (index =1; index <26; index++)
{
if (list[index] > list[maxIndex]);
maxIndex = list[index];
largest = list[maxIndex];
}
return largest;
}
|