Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GUI
- #include <Windows.h>
- #include "Damerau_Levenstein.h"
- #include "Dictionary.h"
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM );
- #define ID_BUTTON1 1
- #define ID_TEXTBOX 2
- #define ID_BUTTON2 3
- #define ID_LISTBOX 4
- #define ID_BUTTON_SEARCH 5
- int showWindow()
- {
- LPCWSTR szClassName = L"MyWindow";
- MSG msg;
- WNDCLASSEX wcex;
- memset(&wcex, 0, sizeof(wcex));
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = NULL;
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW - 2);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = szClassName;
- wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
- int res = RegisterClassEx(&wcex);
- HWND hMainWnd = CreateWindow(szClassName, // имя класса
- L"Полноценная оконная процедура", // имя окна (то что сверху)
- WS_OVERLAPPED| WS_CAPTION| WS_SYSMENU| WS_MINIMIZEBOX, // режимы отображения окошка
- 100, // положение окна по оси х (по умолчанию)
- 100, // позиция окна по оси у (раз дефолт в х, то писать не нужно)
- 295, // ширина окошка (по умолчанию)
- 370, // высота окна (раз дефолт в ширине, то писать не нужно)
- NULL, // дескриптор родительского окошка (у нас нет род. окон)
- NULL, // дескриптор меню (у нас его нет)
- NULL, // .... экземпляра приложения
- NULL); // ничего не передаём из WndProc
- ShowWindow(hMainWnd, SW_SHOWNORMAL);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);// Функция трансляции кодов нажатой клавиши
- DispatchMessage(&msg); // Посылает сообщение функции WndProc()
- }
- return 0;
- }
- HWND hEdit;
- LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
- {
- switch (Message)
- {
- case WM_CREATE:
- CreateWindow(L"button", L"Направление перевода", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 5, 30, 270, 35, hwnd, (HMENU)ID_BUTTON1, NULL, NULL);
- hEdit = CreateWindowEx(WS_EX_STATICEDGE, L"edit", NULL, WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL, 5, 10, 270, 25, hwnd, (HMENU)ID_TEXTBOX, NULL, NULL);
- CreateWindow(L"button", L"Перевести", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 5, 290, 270, 35, hwnd, (HMENU)ID_BUTTON2, NULL, NULL);
- CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("listbox"), L"", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL, 5, 70, 270, 220, hwnd, (HMENU)ID_LISTBOX, NULL, NULL);
- return 0;
- case WM_COMMAND:
- {
- int Id = LOWORD(wparam);
- int Event = HIWORD(wparam);
- switch (Id)
- {
- case ID_TEXTBOX:
- {
- switch (Event)
- {
- case EN_CHANGE:
- {
- int length_of_textbox = GetWindowTextLength(hEdit);
- TCHAR* buf = new TCHAR[length_of_textbox + 1];
- GetWindowText(hEdit, buf, length_of_textbox + 1);
- break;
- }
- }
- break;
- }
- case ID_LISTBOX:
- {
- switch (Event)
- {
- case LBN_DBLCLK:
- {
- //MessageBox(hwnd, )
- }
- }
- }
- case ID_BUTTON1:
- case ID_BUTTON2:
- }
- return 0;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- }
- return DefWindowProc(hwnd, Message, wparam, lparam);
- }
- int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
- {
- return showWindow();
- }
- //Logic
- #include <Windows.h>
- #include <iostream>
- #include <map>
- #include <vector>
- #include <fstream>
- #include <string>
- #include <memory>
- #include "Damerau_Levenstein.h"
- namespace Dictionary
- {
- typedef std::map<std::wstring, std::wstring> dictionary_t;
- typedef std::shared_ptr<dictionary_t> dict_ptr;//умный поинтер
- ////////////////////////////////////////////////////////
- enum TranslateDirection
- {
- ENG2RU,
- RU2ENG
- };
- class IParser
- {
- public:
- virtual std::vector<dict_ptr> parse() = 0;
- };
- class IDictionary
- {
- public:
- virtual bool load() = 0;
- virtual std::vector<std::wstring> find(std::wstring, int, TranslateDirection) = 0;
- virtual std::wstring translate(std::wstring str, TranslateDirection transDir) = 0;
- virtual ~IDictionary() = 0;
- };
- ////////////////////////////////////////////////////////
- class Parser : IParser
- {
- private:
- std::wstring m_filename;
- public:
- Parser(std::wstring filename) : m_filename(filename) {}
- std::vector<dict_ptr> parse()
- {
- dict_ptr rus_dict = std::make_shared<dictionary_t>();
- dict_ptr eng_dict = std::make_shared<dictionary_t>();
- std::vector<dict_ptr> dicts;
- std::wifstream file;
- file.open(m_filename);
- if (!file.is_open())
- std::cout << "Файл не может быть открыт";
- else{
- std::wstring wstring;
- while (std::getline(file, wstring))
- {
- size_t sign = 0; // итератор на конец слова
- sign = wstring.find('<', 0);
- eng_dict->operator[](wstring.substr(0, sign)) = wstring.substr(sign + 2, wstring.size());
- rus_dict->operator[](wstring.substr(sign + 2, wstring.size())) = wstring.substr(0, sign);
- }
- dicts.push_back(rus_dict);
- dicts.push_back(eng_dict);
- }
- return dicts;
- }
- };
- class Dictionary : IDictionary
- {
- dict_ptr m_ru2engDic;
- dict_ptr m_eng2ruDic;
- public:
- Dictionary() {}
- bool load(IParser &parser)
- {
- std::vector<dict_ptr> vec = parser.parse();
- auto dict = std::move(vec.at(0));
- m_ru2engDic = std::move(vec.at(0));
- m_eng2ruDic = std::move(vec.at(1));
- };
- std::vector<std::wstring> find(std::wstring word, int d, TranslateDirection transDir) override // проверяет есть ли это в родительском классе
- {
- std::vector<std::wstring> words;
- if (transDir == RU2ENG)
- {
- for (auto i : *m_ru2engDic)
- {
- if (Damerau_Levenstein(word, i.first) <= d)
- {
- words.push_back(i.first);
- }
- }
- }
- else
- {
- for (auto i : *m_eng2ruDic)
- {
- if (Damerau_Levenstein(word, i.first) <= d)
- {
- words.push_back(i.first);
- }
- }
- }
- return words;
- }
- std::wstring translate(std::wstring str, TranslateDirection transDir)
- {
- if (transDir == TranslateDirection::ENG2RU)
- {
- m_eng2ruDic->at(str);
- }
- else
- {
- m_ru2engDic->at(str);
- }
- return std::wstring();
- };
- };
- }
- // Damerau Levenstein
- #include "Damerau_Levenstein.h"
- #include <windows.h>
- size_t Damerau_Levenstein(const std::wstring& str1, const std::wstring& str2)
- {
- const size_t m = str1.length(), n = str2.length();
- if (m == 0) {
- return n;
- }
- if (n == 0) {
- return m;
- }
- std::vector<std::vector<size_t>> matrix(m + 1);
- for (size_t i = 0; i <= m; ++i) {
- matrix[i].resize(n + 1);
- matrix[i][0] = i;
- }
- for (size_t i = 0; i <= n; ++i) {
- matrix[0][i] = i;
- }
- size_t up_elem, left_elem, diag_elem, cost;
- for (size_t i = 1; i <= m; ++i) {
- for (size_t j = 1; j <= n; ++j) {
- diag_elem = matrix[i - 1][j - 1];
- left_elem = matrix[i][j - 1];
- up_elem = matrix[i - 1][j];
- cost = str1[i - 1] == str2[j - 1] ? 0 : 1;
- matrix[i][j] = min(min(up_elem + 1, left_elem + 1), diag_elem + cost);
- if (i > 1 & j > 1 & str1[i] == str2[j - 1] & str1[i - 1] == str2[j])
- matrix[i][j] = min(matrix[i][j], matrix[i - 2][j - 2] + 1);
- }
- }
- return matrix[m][n];
- }
- //.h алгоритма
- #pragma once
- #include "string.h"
- #include <vector>
- size_t Damerau_Levenstein(const std::wstring& str1, const std::wstring& str2);
- // Controller
- #include "Dictionary.h"
- using namespace Dictionary;
- class Controller{
- std::wstring cur_translated_str;
- IDictionary *m_dic;
- public:
- void init();
- std::wstring getTranslatedWord();
- void setTranslatedWord(std::wstring);
- void translate();
- void setTranslateCallBack(std::wstring (CallBack)());
- void setFilterCallBack(std::vector<std::wstring>(CallBack));
- };
- //.h часть
- #include "Controller.h"
- std::wstring Controller::getTranslatedWord(){}
- void Controller::setTranslatedWord(std::wstring word){
- cur_translated_str = word;
- }
- void Controller::translate(){
- }
- void Controller::setTranslateCallBack(std::wstring(CallBack)())
- void Controller::setFilterCallBack(std::vector<std::wstring>(CallBack))
- //Ещё не реализовано
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement