Created
March 18, 2017 11:38
-
-
Save reeFridge/ba2b4ce341882ef907efbd2fea9979a3 to your computer and use it in GitHub Desktop.
freqs.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author: reefridge <reefridgerator@gmail.com> | |
* Compilation: `g++ freqs.cpp -o freqs` | |
* Synopsis: `freqs` | |
*/ | |
#include<cstdio> | |
#include<cstdlib> | |
#include<cstring> | |
#include<cctype> | |
template<class T> class Container { | |
public: | |
T* elements = new T[0]; | |
unsigned int len = 0; | |
~Container() { | |
clear(); | |
} | |
void clear() { | |
delete[] elements; | |
elements = new T[0]; | |
len = 0; | |
} | |
void operator +=(T el) { | |
T* temp = new T[len]; | |
for (int i = 0; i < len; i++) { | |
temp[i] = elements[i]; | |
} | |
delete[] elements; | |
elements = new T[len + 1]; | |
for (int i = 0; i < len; i++) { | |
elements[i] = temp[i]; | |
} | |
delete[] temp; | |
elements[len++] = el; | |
} | |
}; | |
class Word : public Container<char> { | |
public: | |
void to_lower_case() { | |
for (int j = 0; j < len; j++) { | |
elements[j] = tolower(elements[j]); | |
} | |
} | |
void terminate() { | |
Container<char>::operator+=('\0'); | |
} | |
}; | |
bool operator ==(const Word& that, const Word& other) { | |
const char *p1 = that.elements; | |
const char *p2 = other.elements; | |
while (*p1 != '\0') { | |
if (*p1 != *p2) { | |
return false; | |
} | |
p1++; p2++; | |
} | |
return true; | |
} | |
struct FreqRecord { | |
Word word; | |
unsigned int count = 1; | |
}; | |
class Freqs : public Container<FreqRecord> { | |
public: | |
int index_record_by_word(const Word& word) { | |
int result = -1; | |
for (int j = 0; j < len; j++) { | |
if (elements[j].word == word) { | |
result = j; | |
break; | |
}; | |
} | |
return result; | |
} | |
void increment(const Word word) { | |
int idx = index_record_by_word(word); | |
if (idx != -1) { | |
elements[idx].count++; | |
} | |
else { | |
FreqRecord rec; | |
rec.word = word; | |
Container<FreqRecord>::operator+=(rec); | |
} | |
} | |
}; | |
bool is_alpha(char c) { | |
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); | |
} | |
int main(int argc, char** argv) { | |
Word word; | |
Freqs freqs; | |
char input[] = "The time has come, the walrus said, to talk of many things."; | |
char input_len = sizeof(input); | |
printf("%s %d\n", input, sizeof(input)); | |
for (int i = 0; i < input_len; i++) { | |
if (is_alpha(input[i])) { | |
word += input[i]; | |
} | |
else if (word.len != 0) { | |
word.to_lower_case(); | |
word.terminate(); | |
freqs.increment(word); | |
word.clear(); | |
} | |
} | |
for (int i = 0; i < freqs.len; i++) { | |
printf("%s %d\n", freqs.elements[i].word.elements, freqs.elements[i].count); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment