Skip to content

Instantly share code, notes, and snippets.

@lavantien
Created September 30, 2018 08:29
Show Gist options
  • Save lavantien/f4033522d3b1652a0b70fd0f7f5c8393 to your computer and use it in GitHub Desktop.
Save lavantien/f4033522d3b1652a0b70fd0f7f5c8393 to your computer and use it in GitHub Desktop.
Simple Dictionary console program.
/*
Warning: The following application was written in ISO C++11 standard, so it may incompatible with older C++ standards
Compiled by GCC with -std=c++11 flag
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib> // for using rand() and srand()
#include <ctime> // for using time()
using namespace std;
#define FPATH "Dictionary.txt" // file path
struct Word {
string English;
string Vietnamese;
string Type;
};
void quicksort(Word [], const int, const int); // sort by English attribute of Word
int binsearch(const Word [], const int, const int, const string); // search for position of string kstr in Word array, by English attribute
string formalize(const string); // make the string to compatible with Dictionary database's format
int main() {
int n = 0;
Word *arr = nullptr; // nullptr represent for NULL pointer, which is a new feature in C++11 standard
string tstr;
// read and store database in Word array
ifstream f(FPATH);
if (!f) { // error handle
cout << "Error on loading database! Process terminated.\n";
return -1;
}
while (!(f >> ws).eof()) { // count the number of lines in the file - in a safety way
getline(f, tstr);
if (f.fail()) {
break;
}
n++;
}
n /= 3;
arr = new Word[n]; // allocating an array of Word which has n members
f.seekg(f.beg); // return the buffer to the beginning of the file
for (int i = 0; i < n; i++) { // actually read each Word from the begin to the end of the file
getline(f, tstr);
arr[i].English = formalize(tstr);
getline(f, arr[i].Vietnamese);
getline(f, arr[i].Type);
}
f.close();
// sort Word array by Quick Sort algorithm and by English attribute
srand((unsigned int)time(nullptr)); // create a random seed
quicksort(arr, 0, n - 1);
// user interface
cout << "List of " << n << " English words available in Dictionary database (sorted):\n";
for (int i = 0; i < n; i++) {
cout << "- " << arr[i].English << '\n';
}
cout << '\n';
do {
cout << "Enter an English word to search for its meaning in Vietnamese and type: ";
getline(cin, tstr);
tstr = formalize(tstr);
int k = binsearch(arr, 0, n - 1, tstr);
if (k == -1) {
cout << "Unable to find this English word in the Dictionary database!\n";
}
else {
cout << "Here is it:\n";
cout << "- English word: " << arr[k].English << '\n';
cout << "- Vietnamese meaning: " << arr[k].Vietnamese << '\n';
cout << "- Type of the word: " << arr[k].Type << '\n';
}
cout << "Continue? (Y/N): ";
getline(cin, tstr);
tstr = formalize(tstr);
} while (!(tstr.size() == 1 && tstr[0] == 'n')); // the program exit only if user enter 'n' or 'N'
// clean up garbages and exit application
delete[] arr;
arr = nullptr;
return 0;
}
void quicksort(Word arr[], const int l, const int r) {
if (l >= r) {
return;
}
string p = arr[l + rand() % (r - l + 1)].English;
int i = l, j = r;
do {
while (arr[i].English < p) {
i++;
}
while (arr[j].English > p) {
j--;
}
if (i <= j) {
if (i < j) {
Word t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
i++;
j--;
}
} while (i <= j);
quicksort(arr, l, j);
quicksort(arr, i, r);
}
int binsearch(const Word arr[], const int l, const int r, const string kstr) {
if (r == -1 || (l == r && kstr != arr[l].English)) {
return -1;
}
int mid = l + (r - l) / 2;
if (kstr == arr[mid].English) {
return mid;
}
if (kstr > arr[mid].English) {
return binsearch(arr, mid + 1, r, kstr);
}
else {
return binsearch(arr, l, mid - 1, kstr);
}
}
string formalize(const string str) {
string tstr = str;
int n = tstr.size();
// normalize all letters in tstr
for (int i = 0; i < n; i++) {
if (tstr[i] > 'A' && tstr[i] < 'Z')
tstr[i] += 32;
}
// truncating spaces at the end of tstr
int i = n - 1;
while (tstr[i] == ' ') {
i--;
}
tstr.resize(i + 1);
return tstr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment