well i compiled and ran tests with my program and it seems to count the numbers incorrectly, but it is off by one and i cant seem to find the error, maybe its because im tired, but if you can see an error let me know. thanks!
Code:
/***************************
Made by: Joshua R
Date Created: 4-26-05
Date Finished: 4-26-05
***************************/
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
using namespace std;
struct wordData
{
string word;
int count;
};
struct listType
{
vector< wordData > list;
int size;
int index;
};
void countWords (listType &, string);
bool validate (const char);
void listAdd (listType &, string);
void unCap (string &);
int bsearch (const listType &, string);
void quickSort (vector< wordData > &, int, int);
void printRank (const listType &);
ostream& operator<< (ostream& out, const wordData &);
main()
{
listType myWords;
myWords.size = 0;
myWords.index = 0;
countWords (myWords, "Lesson26b.txt");
quickSort (myWords.list, 0, myWords.size);
printRank (myWords);
cin.get();
return 0;
}
void countWords (listType &words, string fileName)
{
/****************************************/
// Check to see if file exists //
/****************************************/
const char* fn = fileName.c_str();
ifstream infile;
infile.open(fn);
if (infile.fail())
{
cerr << "ERROR: " << fileName << " could not be opened" << endl;
abort();
}
/****************************************/
char letterA, letterB;
string word;
if (infile.get(letterA))
if (validate(letterA))
word += letterA;
while (infile.get(letterB))
{
if (letterB == '-')
{
if (letterA != ' ')
word += letterB;
}
else if (validate(letterB))
word += letterB;
else if (validate(letterA) && !validate(letterB))
{
listAdd (words, word);
word = "";
}
letterA = letterB;
}
infile.close();
}
bool validate (const char letter)
{
if (('a' <= letter && letter <= 'z') ||
('A' <= letter && letter <= 'Z') ||
(letter == '\''))
return true;
else
return false;
}
void listAdd (listType &words, string word)
{
int index;
unCap (word);
index = bsearch (words, word);
if (index == -1)
{
words.size++;
words.list.resize(words.size);
words.list[words.index].word = word;
words.list[words.index].count = 1;
words.index++;
}
else
words.list[index].count++;
}
void unCap (string &word)
{
for (int count = 0; count < word.size(); count++)
if ('A' <= word[count] && word[count] <= 'Z')
word[count] += 32;
}
int bsearch (const listType &words, string wToSearch)
{
for (int count = 0; count < words.size-1; count++)
if (words.list[count].word == wToSearch)
return count;
return -1;
}
void quickSort (vector< wordData > &list, int first, int last)
{
int g = first, h = last;
int midIndex;
int dividingValue;
midIndex = (first + last) / 2;
dividingValue = list[midIndex].count;
do
{
while (list[g].count > dividingValue) g++;
while (list[h].count < dividingValue) h--;
if (g <= h)
swap(list[g++], list[h--]);
}
while (h > g);
if (h > first) quickSort (list,first,h);
if (g < last) quickSort (list,g,last);
}
void printRank (const listType &temp)
{
int count = 0;
cout << "Number of words used = " << temp.size << endl;
cout << "Total # of words = ";
for (int a = 0; a < temp.size-1; a++)
count += temp.list[a].count;
cout << count << endl << endl;
for (int a = 0; a < 30; a++)
{
cout << setw(3) << a+1 << temp.list[a] << endl << endl;
}
}
ostream& operator<< (ostream& out, const wordData &temp)
{
out << setw(6) << temp.count << " " << temp.word;
return out;
}