Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
int const wordLength = 21;
int const Num = 100;
int const fileSize = 255;
struct words
{
char word[wordLength];
int count;
};
void storeFile(char[], char [], words []);
void displayFile(char[], char[], words[]);
int wordSearch(char word[], int array_size, words []);
void sortSetup(char[], words [], int);
int sort (words [], int, int);
void averageLength(char[], words[], int);
void Occurrence(char[], words[], int);
void commonWord(char[], double[], int, words []);
void averageOccurrence(char[], words[], int);
int main ()
{
words array[Num];
char fileName[fileSize];
char out_file_name[fileSize];
cout << "Please enter the name of the file you wish to open: "<< endl;
cin.getline(fileName,fileSize);
if (!cin.good() ) {
cout << "Error reading cin..." << endl ;
return -1 ;
}
cout<<"Please enter the name of the file you wish to send the data too" << endl;
cin.getline(out_file_name,fileSize);
displayFile(out_file_name, fileName, array);
storeFile(out_file_name, fileName, array);
cin.ignore();
}
void storeFile (char out_file_name[], char fileName[], words array[] )
{
ofstream outFile;
outFile.open(out_file_name);
int i = 0;
ifstream inFile;
char line [Num];
int array_size = 0 ;
inFile.open(fileName);
while (inFile.getline(line,Num))
{
array_size = wordSearch(line, array_size, array);
i++;
}
inFile.close();
outFile<< "The number of unique words are: "<< array_size << endl;
outFile<< "total number of words are: " << i << endl;
outFile<< endl;
}
int wordSearch( char line[], int array_size, words array[])
{
int i = array_size ;
while (i && array_size > 0)
{
if (strcmp(line, array[i-1].word) == 0)
{
array[i-1].count++;
return array_size ;
}
i-- ;
}
strcpy(array[array_size].word, line) ;
array[array_size].count = 1 ;
return array_size+1;
}
void displayFile (char out_file_name[], char fileName[], words array[] )
{
int i = 0;
char line [Num];
ifstream inFile;
inFile.open(fileName);
while (inFile.getline(line,Num))
{
strncpy(array[i].word, line, wordLength);
i++;
}
sortSetup(out_file_name, array, i);
averageLength(out_file_name, array, i);
Occurrence(out_file_name, array, i);
averageOccurrence(out_file_name, array, i);
inFile.close();
}
void sortSetup (char out_file_name[], words array [], int array_length)
{
ofstream outFile;
outFile.open(out_file_name);
char temp[wordLength];
int position;
int i = array_length;
for (int loop = 0; loop < array_length - 1; loop++)
{
position = sort (array, loop, array_length - 1);
if (position != loop)
{
strcpy(temp, array[position].word);
strcpy(array[position].word, array[loop].word);
strcpy(array[loop].word, temp);
}
}
outFile << "The words in alphabetical order are:"<< endl;
for (int j = 0; j< array_length; j++)
{
if(strcmp(array[j].word,array[j-1].word)!=0)
outFile << array[j].word << endl;
}
outFile << endl;
}
int sort (words array[], int start, int stop)
{
int n;
int loc = start;
for (int pos = start + 1; pos <= stop; pos++)
{
n = (strcmp(array[pos].word, array[loc].word));
if (n < 0)
loc = pos;
}
return loc;
}
void averageLength(char out_file_name[], words array[], int i)
{
ofstream outFile;
outFile.open(out_file_name);
double average = 0;
for(int j = 0; j<i; j++)
average = average + strlen(array[j].word);
average = average/i;
outFile << "The average length of the words are: " << average <<endl;
outFile << endl;
}
void Occurrence(char out_file_name[], words array[], int array_length)
{
ofstream outFile;
outFile.open(out_file_name);
int n;
char cmp_array[wordLength];
double count[Num];
for( int i= 0; i< array_length; i++)
{
strcpy(cmp_array, array[i].word);
count[i] = 0;
for (int j=0; j<array_length; j++)
{
n = (strcmp(array[j].word, cmp_array));
if(n == 0)
count[i]++;
}
}
outFile<<"The unique words and the number of times they appear in the text file appears asthe following:"<< endl;
outFile<<"word/times it appears:" << endl;
outFile<< endl;
for (int k = 0; k< array_length; k++)
{
if(strcmp(array[k].word,array[k-1].word)!=0)
outFile <<array[k].word << " / " << count[k] << endl;
}
outFile<<endl;
commonWord(out_file_name, count, array_length, array);
}
void commonWord(char out_file_name[], double count[], int array_length, words array[])
{
ofstream outFile;
outFile.open(out_file_name);
int count_max;
count_max = count[0];
int j = 0;
int i = 0;
for(i = 1; i<array_length; i++)
{
if(count[i] > count_max)
count_max = count[i];
}
outFile<< "The word(s) that occur the most are: "<< endl;
for( j = 0; j<array_length; j++)
{
if(strcmp(array[j].word,array[j-1].word)!=0)
{
if(count[j] == count_max)
outFile << array[j].word<< endl;
}
}
outFile<< endl;
}
void averageOccurrence(char out_file_name[], words array[], int array_length)
{
ofstream outFile;
outFile.open(out_file_name);
int n;
char cmp_array[wordLength];
double count[Num];
for( int i= 0; i< array_length; i++)
{
strcpy(cmp_array, array[i].word);
count[i] = 0;
for (int j=0; j<array_length; j++)
{
n = (strcmp(array[j].word, cmp_array));
if(n == 0)
count[i]++;
}
}
outFile<<"The average occurence of a word appears as the following:" << endl; outFile <<"word/average appearence:" << endl;
outFile<< endl;
for (int k = 0; k< array_length; k++)
{
if(strcmp(array[k].word,array[k-1].word)!=0)
outFile <<array[k].word << " / " << count[k]/array_length << endl;
}
outFile<<endl;
}#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
int const wordLength = 21;
int const Num = 100;
int const fileSize = 255;
struct words
{
char word[wordLength];
int count;
};
void storeFile(char[], char [], words []);
void displayFile(char[], char[], words[]);
int wordSearch(char word[], int array_size, words []);
void sortSetup(char[], words [], int);
int sort (words [], int, int);
void averageLength(char[], words[], int);
void Occurrence(char[], words[], int);
void commonWord(char[], double[], int, words []);
void averageOccurrence(char[], words[], int);
int main ()
{
words array[Num];
char fileName[fileSize];
char out_file_name[fileSize];
cout << "Please enter the name of the file you wish to open: "<< endl;
cin.getline(fileName,fileSize);
if (!cin.good() ) {
cout << "Error reading cin..." << endl ;
return -1 ;
}
cout<<"Please enter the name of the file you wish to send the data too" << endl;
cin.getline(out_file_name,fileSize);
displayFile(out_file_name, fileName, array);
storeFile(out_file_name, fileName, array);
cin.ignore();
}
void storeFile (char out_file_name[], char fileName[], words array[] )
{
ofstream outFile;
outFile.open(out_file_name);
int i = 0;
ifstream inFile;
char line [Num];
int array_size = 0 ;
inFile.open(fileName);
while (inFile.getline(line,Num))
{
array_size = wordSearch(line, array_size, array);
i++;
}
inFile.close();
outFile<< "The number of unique words are: "<< array_size << endl;
outFile<< "total number of words are: " << i << endl;
outFile<< endl;
}
int wordSearch( char line[], int array_size, words array[])
{
int i = array_size ;
while (i && array_size > 0)
{
if (strcmp(line, array[i-1].word) == 0)
{
array[i-1].count++;
return array_size ;
}
i-- ;
}
strcpy(array[array_size].word, line) ;
array[array_size].count = 1 ;
return array_size+1;
}
void displayFile (char out_file_name[], char fileName[], words array[] )
{
int i = 0;
char line [Num];
ifstream inFile;
inFile.open(fileName);
while (inFile.getline(line,Num))
{
strncpy(array[i].word, line, wordLength);
i++;
}
sortSetup(out_file_name, array, i);
averageLength(out_file_name, array, i);
Occurrence(out_file_name, array, i);
averageOccurrence(out_file_name, array, i);
inFile.close();
}
void sortSetup (char out_file_name[], words array [], int array_length)
{
ofstream outFile;
outFile.open(out_file_name);
char temp[wordLength];
int position;
int i = array_length;
for (int loop = 0; loop < array_length - 1; loop++)
{
position = sort (array, loop, array_length - 1);
if (position != loop)
{
strcpy(temp, array[position].word);
strcpy(array[position].word, array[loop].word);
strcpy(array[loop].word, temp);
}
}
outFile << "The words in alphabetical order are:"<< endl;
for (int j = 0; j< array_length; j++)
{
if(strcmp(array[j].word,array[j-1].word)!=0)
outFile << array[j].word << endl;
}
outFile << endl;
}
int sort (words array[], int start, int stop)
{
int n;
int loc = start;
for (int pos = start + 1; pos <= stop; pos++)
{
n = (strcmp(array[pos].word, array[loc].word));
if (n < 0)
loc = pos;
}
return loc;
}
void averageLength(char out_file_name[], words array[], int i)
{
ofstream outFile;
outFile.open(out_file_name);
double average = 0;
for(int j = 0; j<i; j++)
average = average + strlen(array[j].word);
average = average/i;
outFile << "The average length of the words are: " << average <<endl;
outFile << endl;
}
void Occurrence(char out_file_name[], words array[], int array_length)
{
ofstream outFile;
outFile.open(out_file_name);
int n;
char cmp_array[wordLength];
double count[Num];
for( int i= 0; i< array_length; i++)
{
strcpy(cmp_array, array[i].word);
count[i] = 0;
for (int j=0; j<array_length; j++)
{
n = (strcmp(array[j].word, cmp_array));
if(n == 0)
count[i]++;
}
}
outFile<<"The unique words and the number of times they appear in the text file appears asthe following:"<< endl;
outFile<<"word/times it appears:" << endl;
outFile<< endl;
for (int k = 0; k< array_length; k++)
{
if(strcmp(array[k].word,array[k-1].word)!=0)
outFile <<array[k].word << " / " << count[k] << endl;
}
outFile<<endl;
commonWord(out_file_name, count, array_length, array);
}
void commonWord(char out_file_name[], double count[], int array_length, words array[])
{
ofstream outFile;
outFile.open(out_file_name);
int count_max;
count_max = count[0];
int j = 0;
int i = 0;
for(i = 1; i<array_length; i++)
{
if(count[i] > count_max)
count_max = count[i];
}
outFile<< "The word(s) that occur the most are: "<< endl;
for( j = 0; j<array_length; j++)
{
if(strcmp(array[j].word,array[j-1].word)!=0)
{
if(count[j] == count_max)
outFile << array[j].word<< endl;
}
}
outFile<< endl;
}
void averageOccurrence(char out_file_name[], words array[], int array_length)
{
ofstream outFile;
outFile.open(out_file_name);
int n;
char cmp_array[wordLength];
double count[Num];
for( int i= 0; i< array_length; i++)
{
strcpy(cmp_array, array[i].word);
count[i] = 0;
for (int j=0; j<array_length; j++)
{
n = (strcmp(array[j].word, cmp_array));
if(n == 0)
count[i]++;
}
}
outFile<<"The average occurence of a word appears as the following:" << endl; outFile <<"word/average appearence:" << endl;
outFile<< endl;
for (int k = 0; k< array_length; k++)
{
if(strcmp(array[k].word,array[k-1].word)!=0)
outFile <<array[k].word << " / " << count[k]/array_length << endl;
}
outFile<<endl;
}
The "storeFile" function is the only one that prints to the file.