Code:
#include<fstream.h>
#include<iomanip.h>
#include<iostream.h>
#include"apvector.h"
#include"apstring.h"
ofstream fout;
apstring getfile(apstring &);
void foutopen(apstring &);
void letterfrequency(apvector <int> &, apstring);
void getwords(apvector <apstring> &, apstring);
void wordfrequency(apvector<int> &, apvector<apstring>);
void output(apvector<int>, apvector<int>, apvector <apstring>, apstring);
void sort(apvector<apstring> &, int, int);
int part(apvector<apstring> &, int, int);
// Daniel Meade, Per 4, 2/2/06, Advanced Programming
// This is the madlib
int main()
{
apstring text, filename;
apvector <int> letfreq(26,0);
apvector <apstring> words;
char ch;
do
{
text=getfile(filename);
foutopen(filename);
letterfrequency(letfreq, text);
getwords(words, text);
sort(words, 0,words.length()-1);
apvector<int>wordfreq(words.length()-1);
wordfrequency(wordfreq, words);
output(letfreq, wordfreq, words, filename);
cin.get(ch);
}while(ch!='n'&&ch!='N');
fout.close();
}
apstring getfile(apstring & filename)
{
ifstream fin;
char let;
apstring text;
cout<< "Input Filename Please:";
getline(cin, filename);
fin.open(filename.c_str());
while (fin.fail())
{
cout << " Invalid File: "<< endl << " Input Filename Please:";
getline(cin, filename);
}
do
{
fin.get(let);
if(let>='a'&&let<='z')
let-=32;
text+=let;
}
while(!fin.eof());
fin.close();
return text;
}
void letterfrequency(apvector<int> & letfreq, apstring text)
{
for(int c=0; c<text.length(); c++)
if(text[c]>='A'&& text[c]<='Z')
letfreq[text[c]-'A']++;
}
void getwords(apvector<apstring> &words, apstring text)
{
apstring word ="";
for(int c=0;c<text.length();c++)
{
if(text[c]>='A' &&text[c]<='Z'||text[c]=='\' '&& word !="")
word+=text[c];
else if (word!="")
{
words.resize(words.length()+1);
words[words.length()-1]=word;
word="";
}
}
}
void wordfrequency(apvector<int> & wordfreq, apvector<apstring>words)
{
int cnt=0;
for (int c=0; c<words.length()-1;c++)
{
if(words[c]==words[c+1])
cnt++;
else
{
wordfreq[c]=cnt+1;
cnt=0;
}
}
}
void output(apvector<int>letfreq, apvector<int> wordfreq, apvector<apstring>words, apstring filename)
{
fout << " The frequency if the letters..."<< endl;
for(int c=0; c<26-3;c+=3)
{
fout << char(c+'A') << '_' << letfreq[c] << "\t\t\t"
<< char(c+'B') << '_' << letfreq[c+1] << "\t\t\t"
<< char(c+'C') << '_' << letfreq[c+2] << endl;
}
fout << endl << "The disctinct words...." << endl;
for(int c=0;c<words.length();c++)
if(wordfreq[c]!=-1)
fout << words[c] << '_' << wordfreq[c] << endl;
fout << endl << "There were " << words.length() << "words";
cout << endl << "\nCheck the text file:" << filename;
cout << "\n\nDo you want to continue?(y/n)";
}
void foutopen(apstring &filename)
{
int c;
for(c=filename.length()-1; c>=0;c--)
if(filename[c]=='.')
break;
filename=filename.substr(0,c)+"h:\\out.txt";
fout.open(filename.c_str());
}
void sort(apvector<apstring> &words, int first, int last)
{
int pivot = 0;
if(first < last)
{
pivot=part(words,first,last);
sort(words,first,(pivot-1));
sort(words,(pivot+1),last);
}
}
int part(apvector<apstring> & words, int first, int last)
{
int firstpos, lastpos;
apstring firstword = words[first],temp;
firstpos = first;
lastpos=last;
do
{
while (words[firstpos] <= firstword && firstpos < last)
firstpos ++ ;
while (words [lastpos] > firstword && lastpos > first)
lastpos--;
if(firstpos < lastpos)
{
temp = words[firstpos];
words[firstpos] = words[lastpos];
words[lastpos] = temp;
}
}
while (lastpos > firstpos);
words[first] = words[lastpos];
words[lastpos] = firstword;
return lastpos;
}
Thanks guys i appreciate it alot