All pastes #260272 Raw Edit

Something

public text v1 · immutable
#260272 ·published 2006-11-28 05:05 UTC
rendered paste body
// APS105 Labi8 12/26/06
// Geoffrey Lang
// 995155772
// langgeof@ecf.utoronto.ca

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const int MAX_WORDS = 10000;
const int MAX_WORD_LENGTH = 25;

struct WordListNode
{
        char *word;
        int count;
        WordListNode *next;
};

void readWords(WordListNode *head, ifstream &inFile, int& n);
void removePunctuation(char word[]);
bool goodLetter(char i);
void printWords(WordListNode *head);
void printInfo(int n, int u);
void sortWords(WordListNode *head);
void deleteDuplicateWords(WordListNode *head, int &n, int &u);
WordListNode *searchWords(WordListNode *head);

int main(int argc, char *argv[])
{
        int n = 0, u = 0;                       // n - total words
        WordListNode head;                      // u - unique words

        if(argc == 2)
        {
                ifstream inFile;
                inFile.open(argv[1]);

                if(!inFile.fail())
                {
                        readWords(&head, inFile, n);
                        sortWords(&head);
                        deleteDuplicateWords(&head, n, u);
                        printWords(&head);
                        printInfo(n, u);
                        searchWords(&head);
                }
        }
        else
        {
                cout << "Correct usage: " << argv[0] << " filename" << endl;
        }

        return 0;
}

void readWords(WordListNode *head, ifstream &inFile,int &n)
{
        WordListNode *i = head;
        int j = 0;

        while(!inFile.eof())
        {
                char current[MAX_WORD_LENGTH + 1];

                inFile >> current;

                removePunctuation(current);

                if(strlen(current) > 0 && !inFile.eof())
                {
                        if(j != 0)
                        {
                                i -> next = new WordListNode;
                                i = i -> next;
                        }
                        i -> word = new char[strlen(current) + 1];
                        strcpy(i -> word, current);
                        i -> count = 1;
                        n++;
                        j++;
                }
        }
        i -> next = NULL;
}


void removePunctuation(char word[])
{
        char temp[MAX_WORD_LENGTH];
        int i = 0, j = 0, k;

        strncpy(temp, word, MAX_WORD_LENGTH);

        while(temp[i] != '\0' && !goodLetter(temp[i]))
        {
                i++;
        }

        k = i;
        word[0] = '\0';

        while(temp[i] != '\0')
        {
                word[j] = tolower(temp[i]);
                if(goodLetter(temp[i]))
                {
                        k = j;
                }
                i++;
                j++;
        }
        word[k+1] = '\0';
}

bool goodLetter(char i)
{
        if((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') || (i >= '0' && i <= '9'))
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

void printWords(WordListNode *head)
{
        for(WordListNode *i = head; i -> next != NULL; i = i -> next)
        {
                cout << i -> word << " (" << i -> count << ")" << endl;
        }
}

void printInfo(int n,int u)
{
        cout << "Total words:\t" << n << "." << endl;
        cout << "Unique words:\t" << u << "." << endl;
}

void sortWords(WordListNode *head)
{
        int shorter;
        WordListNode *smallest, temp;

        for(WordListNode *i = head; i -> next != NULL; i = i -> next)
        {
                smallest = i;
                for(WordListNode *j = i -> next; j -> next != NULL; j = j -> next)
                {
                        if(strcmp(j -> word, smallest -> word) < 0)
                        {
                                smallest = j;
                        }
                }

                if (i != smallest)
                {
                        temp.word = smallest->word;
                        smallest -> word = i -> word;
                        i -> word = temp.word;
                }
        }
}

void deleteDuplicateWords(WordListNode *head, int &n, int &u)
{
        WordListNode *i = head;
        int deleted = 0, shorter;

        while(i -> next != NULL)
        {
                if(strcmp(i -> word, i -> next -> word) == 0)
                {
                        i -> next = i -> next -> next;
                        i -> count++;
                        deleted++;
                }
                else
                {
                        i = i -> next;
                }
        }
        u = n - deleted;
}

WordListNode *searchWords(WordListNode *head)
{
        bool quit = 0;
        while (quit == 0)
        {
                WordListNode *i;
                char key[MAX_WORD_LENGTH + 1];

                cout << "Enter a word to search for: ";
                cin >> key;

                if(!goodLetter(key[0]))
                {
                        quit = 1;
                }
                else
                {
                        for(i = head; i != NULL && strcmp(i -> word, key) != 0; i = i -> next);

                        if(strncmp(i -> word, key, strlen(key + 1)) == 0)
                        {
                                cout << "\"" << key << "\" FOUND: " << i -> word << " (" << i -> count << ")" << endl;
                        }
                        else
                        {
                                cout << "\"" << key << "\" NOT FOUND." << endl;
                        }
                }
        }
}