Skip to content

Instantly share code, notes, and snippets.

@tjb0607
Created April 17, 2015 22:33
Show Gist options
  • Save tjb0607/0e062263b6bc977ca321 to your computer and use it in GitHub Desktop.
Save tjb0607/0e062263b6bc977ca321 to your computer and use it in GitHub Desktop.
meme.cpp
/*****************************************************
* Name: Tyler Beatty
* Date Created: 2015-04-16
* Date Modified: 2015-04-17
* Lab Number: Programming Exercise 2
* Filename: PE 2 Tyler Beatty.cpp
*
* Input:
* A text file named "cosby.txt"
*
* Output:
* The counts of the occurances of each word in said
* text file, formatted like so:
* Word Frequency
* I 1
* don't 1
* know 1
* the 2
* key 2
* [...]
*****************************************************/
#define _CRT_SECURE_NO_WARNINGS // allow visual studio to use strcpy
// Memory Leak Checker
#include <crtdbg.h>
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstring>
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::strcpy;
using std::setw;
using std::left;
const int BUFFER_LENGTH = 256;
// NOTE: variables are passed by POINTER
// adds the word to the dynamic arrays storing the words & the word counts
void ProcessWord(char * buffer, int wordLength, char *** words, int ** wordCounts, int * numWords);
// frees the memory used by the arrays
void Purge(char *** words, int ** wordCounts, int * numWords);
// reads a word into a buffer, returns the length of the word
int ReadWord(ifstream & file, char * buffer, int bufferLength);
// prints the contents of the arrays
void PrintArrays(char ** words, int * wordCounts, int numWords);
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
ifstream file("cosby.txt");
if (!file.is_open())
{
cerr << "Error: Couldn't open cosby.txt!" << endl;
return -1;
}
char ** words = nullptr;
int * wordCounts = nullptr;
int numWords = 0;
char buffer[BUFFER_LENGTH];
buffer[0] = '\0';
while (!file.eof())
{
int wordLength = ReadWord(file, buffer, BUFFER_LENGTH);
if (wordLength > 0)
ProcessWord(buffer, wordLength, &words, &wordCounts, &numWords);
}
PrintArrays(words, wordCounts, numWords);
Purge(&words, &wordCounts, &numWords);
return 0;
}
void ProcessWord(char * buffer, int wordLength, char *** words, int ** wordCounts, int * numWords)
{
// find the index of where a word matches with an already stored word if that's the case
int matchIndex = -1;
for (int i = 0; i < *numWords; i++)
{
if (_stricmp(buffer, (*words)[i]) == 0)
matchIndex = i;
}
if (matchIndex != -1)
{
(*wordCounts)[matchIndex] += 1; // increment the count of the word
}
else // insert the word
{
char ** temp_words = new char *[*numWords + 1]; // resize the arrays
int * temp_wordCounts = new int[*numWords + 1];
for (int i = 0; i < *numWords; i++) // transfer the old words & word counts into the new array
{
temp_words[i] = (*words)[i];
temp_wordCounts[i] = (*wordCounts)[i];
}
delete[] * words; // delete the old, smaller arrays
delete[] * wordCounts;
*words = temp_words; // let the new arrays replace the (now deleted) old ones
*wordCounts = temp_wordCounts;
(*words)[*numWords] = new char[wordLength + 1]; // create the new string
strcpy((*words)[*numWords], buffer);
(*wordCounts)[*numWords] = 1; // set the new string's associated count at 1
(*numWords)++;
}
}
void Purge(char *** words, int ** wordCounts, int * numWords)
{
for (int i = 0; i < *numWords; i++) // deallocate the strings referenced by the pointers
delete[](*words)[i];
delete[] * words; // deallocate the array of pointers
delete[] * wordCounts; // deallocate the array of ints
*words = nullptr; // reset the values
*wordCounts = nullptr;
*numWords = 0;
}
int ReadWord(ifstream & file, char * buffer, int bufferLength)
{
char currentChar;
int stringLength = 0;
while ( stringLength < (bufferLength - 1) && // Checks if there's enough room in the buffer
file.get(currentChar) && // Reads a character from the file
((currentChar >= 'a' && currentChar <= 'z') || // Checks if the character is part of a word
(currentChar >= 'A' && currentChar <= 'Z') ||
(currentChar == '-') ||
(currentChar == '\'')) )
{
buffer[stringLength] = currentChar; // Appends the character to the buffer string
stringLength++;
}
buffer[stringLength] = '\0'; // Terminates the buffer string
return stringLength;
}
void PrintArrays(char ** words, int * wordCounts, int numWords)
{
cout << left
<< "Word Frequency Analysis" << endl << endl
<< setw(10) << "Word" << " Frequency" << endl;
for (int i = 0; i < numWords; i++)
cout << setw(10) << words[i] << " " << wordCounts[i] << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment