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
|
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
typedef struct
{
string word;
int count;
size_t size;
} wordData;
// FUNCTION PROTOTYPES
string directoryRequest();
void outputHeader(string, int, int, int);
void fileParse(vector<wordData> &, ifstream &, int &, int &, int &);
void outputData(vector<wordData> &, int, int, int);
int main()
{
int wordsTotal, wordsDistinct, lines;
wordsTotal = 0;
wordsDistinct = 0;
lines = 0;
string directory;
vector<wordData> wordParse;
wordData vectorDefault = {"", 0, 0};
ifstream inputFile;
do
{
cout << "Enter an input file name: ";
getline(cin, directory);
inputFile.open(directory.c_str());
if(!inputFile.is_open())
{
cout << "Could not open requested file, please try again.\n";
}
}while(!inputFile.is_open());
fileParse(wordParse, inputFile, wordsTotal, wordsDistinct, lines);
inputFile.close();
outputHeader(directory, wordsTotal, wordsDistinct, lines);
return 0;
}
void fileParse(vector<wordData>& wordParse, ifstream& inputFile,
int& wordsTotal, int& wordsDistinct, int& lines)
{
string fileLine, currentWord;
stringstream currentStream;
unsigned int idx;
int vectorSize;
char test;
wordData vectorDefault = {"", 0, 0};
while(!inputFile.eof())
{
getline(inputFile, fileLine);
currentStream << fileLine;
lines++;
while(!currentStream.eof())
{
wordsTotal++;
getline(currentStream, currentWord, ' ');
if(wordParse.size() == 0)
{
wordsDistinct++;
wordParse.push_back(vectorDefault);
wordParse[wordParse.size() - 1].word = currentWord;
wordParse[wordParse.size() - 1].size = currentWord.size();
}
vectorSize = wordParse.size();
for(idx = 0;idx < vectorSize;idx++)
{
if(currentWord == wordParse[idx].word)
{
wordParse[idx].count++;
idx = vectorSize;
}
else if(idx == (wordParse.size() - 1))
{
wordsDistinct++;
wordParse.push_back(vectorDefault);
wordParse[wordParse.size() - 1].count++;
wordParse[wordParse.size() - 1].word = currentWord;
wordParse[wordParse.size() - 1].size = currentWord.size();
}
}
}
currentStream << "";
currentStream.clear();
}
return;
}
//string directoryRequest()
//{
// string directory;
// cout << "Enter an input file name: ";
// getline(cin, directory);
// return directory;
}
void outputHeader(string directory, int wordsTotal, int wordsDistinct, int lines)
{
cout << "C S 1 6 1 F I L E I N D E X G E N E R A T O R" << endl
<< "=================================================" << endl
<< "File Name: \"" << directory << "\"" << endl
<< "Total Lines: " << lines << endl
<< "Total Words: " << wordsTotal << endl
<< "Distinct Words: " << wordsDistinct << endl
<< "=================================================" << endl;
return;
}
| |