Created
October 23, 2015 12:40
-
-
Save dralletje/7d0a35964efdcd5744b8 to your computer and use it in GitHub Desktop.
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
#include <string> | |
#include <iostream> | |
#include <fstream> | |
#include <assert.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
using namespace std; | |
const int max_word_length = 60; | |
const int max_words_length = 300000; | |
const int max_command_length = 100; | |
const int max_filename_length = 80; | |
typedef char Word [max_word_length]; | |
typedef Word Words [max_words_length]; | |
void copy_word(Word target, Word source) { | |
for (int i = 0; i < max_word_length; i = i + 1) { | |
target[i] = source[i]; | |
} | |
} | |
void print(string msg) { | |
cout << msg << "\n"; | |
} | |
bool compare_word(Word one, Word two) { | |
string one_string(one); | |
string two_string(two); | |
return one_string == two_string; | |
} | |
int find_occurence(Word needle, Words haystack, int words_count, int offset) { | |
for(int i = offset; i < words_count; i = i + 1) { | |
if(compare_word(haystack[i], needle)) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
// enter Vogon poem.txt | |
bool ensure_secuence(Words input, int input_length, Words words, int words_index, int input_offset) { | |
int real_length = input_length - input_offset; | |
if (real_length == 0) { | |
return words_index; | |
} | |
int input_index = 0; | |
while(compare_word(input[input_index + input_offset], words[words_index + input_index])) { | |
input_index = input_index + 1; | |
if(real_length == input_index) { | |
return true; | |
} | |
} | |
return false; | |
} | |
int find_sequence( | |
Words input, int input_length, Words words, | |
int words_count, int input_offset, int offset | |
) { | |
int index = find_occurence(input[input_offset], words, words_count, offset); | |
if (index != -1 && ensure_secuence(input, input_length, words, index, input_offset)) { | |
return index; | |
} else { | |
return -1; | |
} | |
} | |
bool read_word (ifstream& infile, Word word) { | |
infile >> word; | |
return word; | |
} | |
int read_words (ifstream& infile, Words words) { | |
int word_counter = 0; | |
Word word; | |
while (read_word(infile, word) == true) { | |
if (!infile) { | |
break; | |
} | |
copy_word(words[word_counter], word); | |
word_counter = word_counter + 1; | |
} | |
return word_counter; | |
} | |
bool enter_filename(ifstream& infile, string b) { | |
infile.open(b); | |
if (infile.is_open()) { | |
return true; | |
} | |
cout << "Failed to open file " << b << endl; | |
return false; | |
} | |
Words words_read; | |
Words words_input; | |
Word empty_word; | |
int main() { | |
int words_count; | |
for(int i = 0; i < max_word_length; i = i + 1) { | |
empty_word[i] = '\0'; | |
} | |
while (true) { | |
// Reset the array of command words | |
for (int i = 0; i < max_words_length; i = i + 1) { | |
copy_word(words_input[i], empty_word); | |
} | |
// Request new command | |
cout << "\n> "; | |
char input[max_command_length]; | |
for(int i = 0; i < max_command_length; i = i + 1) { | |
input[i] = '\0'; | |
} | |
cin.getline(input, max_command_length); | |
cin.clear(); | |
// Split it up in words | |
int words_index = 0; | |
int char_index = 0; | |
for (int i = 0; i < max_command_length; i = i + 1) { | |
char current = input[i]; | |
if (current != ' ') { | |
words_input[words_index][char_index] = current; | |
char_index = char_index + 1; | |
} else { | |
words_index = words_index + 1; | |
char_index = 0; | |
} | |
} | |
int input_length = words_index + 1; | |
string command = words_input[0]; | |
if(command == "enter") { | |
ifstream infile; | |
string filename; | |
// Construct filename from args | |
for (int i = 1; i < input_length; i = i + 1) { | |
if (i == 1) { | |
filename = words_input[i]; | |
} else { | |
filename = filename + " " + words_input[i]; | |
} | |
} | |
// Open and read it | |
assert(enter_filename(infile, filename) == true); | |
words_count = read_words(infile, words_read); | |
// Notify the user | |
print("Sucessfully opened file " + filename); | |
print("Total amount of counted words in " + filename + " = " + to_string(words_count)); | |
continue; | |
} | |
if (command == "content") { | |
// Print every word | |
for(int i = 0; i < words_count; i = i + 1) { | |
cout << words_read[i] << " "; | |
} | |
print(""); | |
continue; | |
} | |
if (command == "stop") { | |
print("Bye!"); | |
exit(0); | |
} | |
if (command == "count") { | |
if (input_length < 2) { | |
print("`count` requires at least one argument;"); | |
continue; | |
} | |
int count = 0; | |
int index = find_sequence(words_input, input_length, words_read, words_count, 1, 0); | |
while(index != -1) { | |
count = count + 1; | |
int position = index + input_length; | |
index = find_sequence(words_input, input_length, words_read, words_count, 1, position); | |
} | |
print("Found the secuence " + to_string(count) + " times."); | |
continue; | |
} | |
if (command == "where") { | |
if (input_length < 2) { | |
print("`count` requires at least one argument;"); | |
continue; | |
} | |
int count = 0; | |
int index = find_sequence(words_input, input_length, words_read, words_count, 1, 0); | |
while(index != -1) { | |
print("Found at position " + to_string(index) + "."); | |
count = count + 1; | |
int position = index + input_length; | |
index = find_sequence(words_input, input_length, words_read, words_count, 1, position); | |
} | |
print("Found the secuence " + to_string(count) + " times."); | |
continue; | |
} | |
if (command == "context") { | |
int m = atoi(words_input[1]); | |
if (input_length < 3) { | |
print("`content` requires at least two arguments;"); | |
continue; | |
} | |
int count = 0; | |
int index = find_sequence(words_input, input_length, words_read, words_count, 2, 0); | |
while(index != -1) { | |
cout << "|"; | |
int begin = max(index - m, 0); | |
int end = min(index + input_length + m - 2, words_count); | |
for(int i = begin; i < end; i = i + 1) { | |
cout << " " << words_read[i]; | |
} | |
print(""); | |
count = count + 1; | |
int position = index + input_length; | |
index = find_sequence(words_input, input_length, words_read, words_count, 2, position); | |
} | |
print("Found the secuence " + to_string(count) + " times."); | |
continue; | |
} | |
cout << "please enter a valid choice" << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i++; ipv i=i+1;