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
|
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int countWord(string);
int countChar(string);
void initialize(int&, int[]);
void copyText(ifstream&, ofstream&, char&, int[]);
void count(char, int[]);
void print(ofstream&,string, int, int[]);
int main()
{
ifstream inFile;
ofstream outFile;
string fileName;
string word;
int line, letter[26], count = 1;
char ch;
cout << "please enter a file. " << endl;
cin >> fileName;
inFile.open(fileName);
outFile.open("C:\\Users\\nick\\CS-121\\output.txt");
initialize(line, letter);
while (inFile)
{
copyText(inFile, outFile, ch, letter);
line++;
inFile.get(ch);
}
print(outFile,fileName, line, letter);
return 0;
}
int countWord(string file)
{
ifstream inFile;
inFile.open(file);
string word;
int count = 1;
while (!inFile.eof())
{
inFile.ignore(100,' ') >> word;
count++;
}
return count;
}
void initialize(int& loc, int list[])
{
loc = 0;
for (int i = 0; i < 26; i++)
list[i] = 0;
}
void copyText(ifstream& in, ofstream& out, char& ch, int list[])
{
while (ch != '\n')
{
out << ch;
count(ch, list);
in.get(ch);
}
out << ch;
}
void count(char ch, int list[])
{
ch = toupper(ch);
int index = static_cast<int>(ch)-static_cast<int>('A');
if (0 <= index && index < 26)
list[index]++;
}
void print(ofstream& out,string file, int loc, int list[])
{
out << "The number of words in this file is "<< countWord(file);
out << endl << endl;
out << "The number of lines = " << loc << endl;
for (int i = 0; i < 26; i++)
out << static_cast<char>(i + static_cast<int>('A')) << "= " << list[i] << endl;
}
|