Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- realtime-search.cpp::
- #include <Windows.h>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include "resource.h"
- using namespace std;
- // Structure to represent a dictionary entry
- struct DictionaryEntry {
- wstring word;
- wstring definition;
- };
- // Structure to represent a node in the linked list
- struct Node {
- DictionaryEntry data;
- Node* next;
- };
- Node* dictionary = nullptr;
- vector<wstring> words;
- // Function prototypes
- void insertNode(Node*& head, const DictionaryEntry& entry);
- void addWord(Node*& head, const wstring& word, const wstring& definition);
- void deleteWord(Node*& head, const wstring& keyword);
- wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title);
- void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition);
- void editWord(Node* head, const wstring& oldWord, const wstring& newWord, const wstring& newDefinition);
- void listWords(const Node* head, vector<wstring>& words);
- void serializeDictionary(const Node* head, const wstring& filename);
- void deserializeDictionary(Node*& head, const wstring& filename);
- void searchWordRealtime(const Node* head, const wstring& keyword, vector<wstring>& suggestions);
- void updateListBox(HWND hwnd, const vector<wstring>& words);
- // Function implementations
- void insertNode(Node*& head, const DictionaryEntry& entry) {
- Node* newNode = new Node{ entry, nullptr };
- if (!head) {
- head = newNode;
- }
- else {
- Node* current = head;
- while (current->next) {
- current = current->next;
- }
- current->next = newNode;
- }
- }
- void addWord(Node*& head, const wstring& word, const wstring& definition) {
- insertNode(head, { word, definition });
- }
- void deleteWord(Node*& head, const wstring& keyword) {
- if (!head) return;
- if (head->data.word == keyword) {
- Node* temp = head;
- head = head->next;
- delete temp;
- return;
- }
- Node* current = head;
- while (current->next && current->next->data.word != keyword) {
- current = current->next;
- }
- if (current->next) {
- Node* temp = current->next;
- current->next = current->next->next;
- delete temp;
- }
- }
- wstring InputBox(HWND hwnd, const wstring& prompt, const wstring& title) {
- wchar_t input[256] = L"";
- if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_INPUT), hwnd,
- [](HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -> INT_PTR {
- switch (message) {
- case WM_INITDIALOG:
- SetWindowText(GetDlgItem(hDlg, IDC_INPUT), (LPCWSTR)lParam);
- return (INT_PTR)TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
- EndDialog(hDlg, LOWORD(wParam));
- return (INT_PTR)TRUE;
- }
- break;
- }
- return (INT_PTR)FALSE;
- }, (LPARAM)prompt.c_str()) == IDOK) {
- GetDlgItemText(hwnd, IDC_INPUT, input, 256);
- return input;
- }
- return L"";
- }
- void DisplayDefinition(HWND hwnd, const wstring& word, const wstring& definition) {
- wstring message = L"Definition of '" + word + L"':\n\n" + definition;
- MessageBox(hwnd, message.c_str(), L"Word Definition", MB_OK | MB_ICONINFORMATION);
- }
- void editWord(Node* head, const wstring& oldWord, const wstring& newWord, const wstring& newDefinition) {
- Node* current = head;
- while (current) {
- if (current->data.word == oldWord) {
- current->data.word = newWord;
- current->data.definition = newDefinition;
- return;
- }
- current = current->next;
- }
- }
- void listWords(const Node* head, vector<wstring>& words) {
- words.clear();
- const Node* current = head;
- while (current) {
- words.push_back(current->data.word);
- current = current->next;
- }
- }
- void serializeDictionary(const Node* head, const wstring& filename) {
- wofstream file(filename);
- const Node* current = head;
- while (current) {
- file << current->data.word << L";" << current->data.definition << endl;
- current = current->next;
- }
- file.close();
- }
- void deserializeDictionary(Node*& head, const wstring& filename) {
- wifstream file(filename);
- wstring line;
- while (getline(file, line)) {
- size_t pos = line.find(L';');
- if (pos != wstring::npos) {
- wstring word = line.substr(0, pos);
- wstring definition = line.substr(pos + 1);
- addWord(head, word, definition);
- }
- }
- file.close();
- }
- void searchWordRealtime(const Node* head, const wstring& keyword, vector<wstring>& suggestions) {
- suggestions.clear();
- const Node* current = head;
- while (current && suggestions.size() < 5) {
- if (current->data.word.find(keyword) != wstring::npos) {
- suggestions.push_back(current->data.word);
- }
- current = current->next;
- }
- }
- void updateListBox(HWND hwnd, const vector<wstring>& words) {
- HWND hListBox = GetDlgItem(hwnd, IDC_LISTBOX);
- SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
- for (const auto& word : words) {
- SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)word.c_str());
- }
- }
- // Dialog procedure
- INT_PTR CALLBACK DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
- switch (uMsg) {
- case WM_INITDIALOG:
- deserializeDictionary(dictionary, L"dictionary.txt");
- listWords(dictionary, words);
- updateListBox(hwnd, words);
- return TRUE;
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case IDC_ADD_WORD: {
- wchar_t word[256], definition[1024];
- GetDlgItemText(hwnd, IDC_WORD_INPUT, word, 256);
- GetDlgItemText(hwnd, IDC_DEFINITION_INPUT, definition, 1024);
- if (wcslen(word) > 0 && wcslen(definition) > 0) {
- addWord(dictionary, word, definition);
- listWords(dictionary, words);
- updateListBox(hwnd, words);
- serializeDictionary(dictionary, L"dictionary.txt");
- SetDlgItemText(hwnd, IDC_WORD_INPUT, L"");
- SetDlgItemText(hwnd, IDC_DEFINITION_INPUT, L"");
- }
- return TRUE;
- }
- case IDC_DELETE_WORD: {
- int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
- if (index != LB_ERR) {
- wchar_t word[256];
- SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
- deleteWord(dictionary, word);
- listWords(dictionary, words);
- updateListBox(hwnd, words);
- serializeDictionary(dictionary, L"dictionary.txt");
- }
- return TRUE;
- }
- case IDC_EDIT_WORD: {
- wstring wordToEdit = InputBox(hwnd, L"Enter the word to edit:", L"Edit Word");
- if (!wordToEdit.empty()) {
- Node* current = dictionary;
- while (current) {
- if (current->data.word == wordToEdit) {
- wstring newWord = InputBox(hwnd, L"Enter the new word:", L"Edit Word");
- if (!newWord.empty()) {
- wstring newDefinition = InputBox(hwnd, L"Enter the new definition:", L"Edit Word");
- if (!newDefinition.empty()) {
- editWord(dictionary, wordToEdit, newWord, newDefinition);
- listWords(dictionary, words);
- updateListBox(hwnd, words);
- serializeDictionary(dictionary, L"dictionary.txt");
- }
- }
- break;
- }
- current = current->next;
- }
- if (!current) {
- MessageBox(hwnd, L"Word not found in the dictionary.", L"Error", MB_OK | MB_ICONERROR);
- }
- }
- return TRUE;
- }
- case IDC_VIEW_DEFINITION: {
- int index = SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETCURSEL, 0, 0);
- if (index != LB_ERR) {
- wchar_t word[256];
- SendDlgItemMessage(hwnd, IDC_LISTBOX, LB_GETTEXT, index, (LPARAM)word);
- Node* current = dictionary;
- while (current) {
- if (current->data.word == word) {
- DisplayDefinition(hwnd, current->data.word, current->data.definition);
- break;
- }
- current = current->next;
- }
- }
- return TRUE;
- }
- case IDC_SEARCH_INPUT: {
- if (HIWORD(wParam) == EN_CHANGE) {
- wchar_t query[256];
- GetDlgItemText(hwnd, IDC_SEARCH_INPUT, query, 256);
- vector<wstring> suggestions;
- searchWordRealtime(dictionary, query, suggestions);
- updateListBox(hwnd, suggestions);
- }
- return TRUE;
- }
- case IDCANCEL:
- EndDialog(hwnd, 0);
- return TRUE;
- }
- break;
- case WM_CLOSE:
- EndDialog(hwnd, 0);
- return TRUE;
- }
- return FALSE;
- }
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
- HWND hDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), 0, DialogProc, 0);
- if (!hDlg) {
- MessageBox(NULL, L"Failed to create dialog", L"Error", MB_ICONERROR | MB_OK);
- return 1;
- }
- ShowWindow(hDlg, nCmdShow);
- MSG msg;
- while (GetMessage(&msg, 0, 0, 0)) {
- if (!IsDialogMessage(hDlg, &msg)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int)msg.wParam;
- }
- Resource.rc::
- #include <windows.h>
- #include "resource.h"
- IDD_MAINDIALOG DIALOGEX 0, 0, 320, 240
- STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
- CAPTION "Dictionary Application"
- FONT 8, "MS Shell Dlg", 400, 0, 0x1
- BEGIN
- LTEXT "Word:", -1, 10, 10, 30, 8
- EDITTEXT IDC_WORD_INPUT, 50, 8, 100, 14, ES_AUTOHSCROLL
- LTEXT "Definition:", -1, 10, 30, 40, 8
- EDITTEXT IDC_DEFINITION_INPUT, 50, 28, 200, 14, ES_AUTOHSCROLL
- PUSHBUTTON "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
- PUSHBUTTON "Delete Word", IDC_DELETE_WORD, 260, 28, 50, 14
- PUSHBUTTON "Edit Word", IDC_EDIT_WORD, 260, 48, 50, 14
- PUSHBUTTON "View Definition", IDC_VIEW_DEFINITION, 200, 8, 50, 14
- LTEXT "Search:", -1, 10, 50, 30, 8
- EDITTEXT IDC_SEARCH_INPUT, 50, 48, 200, 14, ES_AUTOHSCROLL
- LISTBOX IDC_LISTBOX, 10, 70, 300, 160, LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
- END
- IDD_INPUT DIALOGEX 0, 0, 200, 100
- STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
- CAPTION "Input"
- FONT 8, "MS Shell Dlg"
- BEGIN
- LTEXT "Input:", -1, 7, 14, 50, 8
- EDITTEXT IDC_INPUT, 7, 24, 186, 12, ES_AUTOHSCROLL
- DEFPUSHBUTTON "OK", IDOK, 139, 7, 50, 14
- PUSHBUTTON "Cancel", IDCANCEL, 139, 24, 50, 14
- END
- /*IDD_INPUT DIALOGEX 0, 0, 200, 100
- STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
- CAPTION "Input"
- FONT 8, "MS Shell Dlg"
- BEGIN
- LTEXT "Input:", -1, 7, 14, 50, 8
- EDITTEXT IDC_INPUT, 7, 24, 186, 12, ES_AUTOHSCROLL
- DEFPUSHBUTTON "OK", IDOK, 139, 7, 50, 14
- PUSHBUTTON "Cancel", IDCANCEL, 139, 24, 50, 14
- END*/
- //PUSHBUTTON "Add Word", IDC_ADD_WORD, 260, 8, 50, 14
- //PUSHBUTTON "View Definition", IDC_VIEW_DEFINITION, 260, 68, 50, 14
- Resource.h::
- #ifndef RESOURCE_H
- #define RESOURCE_H
- #define IDD_MAINDIALOG 101
- #define IDC_WORD_INPUT 1001
- #define IDC_DEFINITION_INPUT 1002
- #define IDC_ADD_WORD 1003
- #define IDC_DELETE_WORD 1004
- #define IDC_SEARCH_INPUT 1005
- #define IDC_LISTBOX 1006
- #define IDC_INPUT 1007 // Add this line if you're using IDC_INPUT
- #define IDC_EDIT_WORD 1008
- #define IDC_VIEW_DEFINITION 1009
- #define IDD_INPUT 1010
- #endif // RESOURCE_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement