Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <string.h>
- #include <conio.h>
- #include <Windows.h>
- #include <iomanip>
- using namespace std;
- struct NodeE{
- string *data[5];
- int amount; // so luong vd
- };
- struct NodeM{
- string data;
- NodeM *pNext;
- };
- struct ListMean{
- NodeM *pFirst;
- };
- struct NodeW{
- string word;
- string type;
- ListMean mean;
- NodeE ex;
- };
- struct NodeD{
- NodeW data;
- NodeD *pLeft;
- NodeD *pRight;
- };
- struct HashTable{
- NodeD *pHead;
- NodeD *pTail;
- };
- HashTable *Dic = new HashTable[26];
- int HamBam(string str){
- return str[0]-97;
- }
- void MakeHashTable(){
- for (int i = 0; i < 26; i++){
- Dic[i].pHead = NULL;
- Dic[i].pTail = NULL;
- }
- }
- // Ham Do Hoa
- void SetColor(WORD color){
- HANDLE hConsoleOutput;
- hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
- GetConsoleScreenBufferInfo(hConsoleOutput, &screen_buffer_info);
- WORD wAttributes = screen_buffer_info.wAttributes;
- color &= 0x000f;
- wAttributes &= 0xfff0;
- wAttributes |= color;
- SetConsoleTextAttribute(hConsoleOutput, wAttributes);
- }
- void gotoxy(int x, int y){
- HANDLE hConsoleOutput;
- COORD Cursor_an_Pos = { x, y };
- hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
- }
- void textcolor(int x){
- HANDLE mau;
- mau = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(mau, x);
- }
- void ShowCur(bool CursorVisibility)
- {
- HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cursor = { 1, CursorVisibility };
- SetConsoleCursorInfo(handle, &cursor);
- }
- void resizeConsole(int width, int height) {
- HWND console = GetConsoleWindow();
- RECT r;
- GetWindowRect(console, &r);
- MoveWindow(console, r.left, r.top, width, height, TRUE);
- }
- void xoamanhinh{
- CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
- HANDLE hConsoleOut;
- COORD Home = {0, 0};
- DWORD dummy;
- hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
- GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
- FillConsoleOutputCharacter(hConsoleOut, ' ' , csbiInfo.dwSize.X * csbiInfo.dwSize.Y,Home,&dummy);
- csbiInfo.dwCursorPosition.X = 0;
- csbiInfo.dwCursorPosition.Y = 0;
- SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);
- }
- void viet(int x, int y, int z){
- gotoxy(x,y);
- printf("%c",z);
- }
- void vietchuoi(int x, int y, string s, int mau){
- gotoxy(x,y);
- textcolor(mau);
- cout<<s;
- }
- void AddLastMeanList(ListMean &l, string m){ // them cuoi ds nghia
- NodeM *p = new NodeM;
- p->pNext = NULL;
- p->data = m;
- NodeM *k = l.pFirst;
- if (l.pFirst == NULL){
- l.pFirst = p;
- }
- else{
- while (true){
- if(k->pNext == NULL){
- k->pNext = p;
- break;
- }
- k = k->pNext;
- }
- }
- }
- void DelFirstMeanList(ListMean &l){ // xoa dau ds nghia cua tu
- if (l.pFirst == NULL){
- return;
- }
- NodeM *p = l.pFirst;
- l.pFirst = l.pFirst->pNext;
- delete p;
- }
- // them vao ds tu dien
- void InsertFirstListDic(HashTable &ds, NodeW w){ // them dau ds tu dien
- NodeD *p = new NodeD;
- p->pLeft = NULL;
- p->pRight = NULL;
- p->data = w;
- if (ds.pHead == NULL){
- ds.pHead = p;
- ds.pTail = p;
- }
- else{
- p->pRight = ds.pHead;
- ds.pHead->pLeft = p;
- ds.pHead = p;
- }
- }
- void InsertLastListDic(HashTable &ds, NodeW w){ // them cuoi ds tu dien
- NodeD *p = new NodeD;
- p->pLeft = NULL;
- p->pRight = NULL;
- p->data = w;
- if (ds.pHead == NULL){
- ds.pHead = p;
- ds.pTail = p;
- }
- else{
- ds.pTail->pRight = p;
- p->pLeft = ds.pTail;
- ds.pTail = p;
- }
- }
- void InsertAfterListDic(HashTable &ds, NodeW w, NodeW temp){ // them sau ds tu dien
- NodeD *p = new NodeD;
- p->pLeft = NULL;
- p->pRight = NULL;
- p->data = w;
- NodeD *k = Dic[HamBam(w.word)].pHead;
- for (NodeD *k = Dic[HamBam(w.word)].pHead; k != NULL; k = k->pRight){
- if (k->data.word == temp.word){
- p->pRight = k->pRight;
- k->pRight->pLeft = p;
- p->pLeft = k;
- k->pRight = p;
- }
- }
- }
- void AddHashTable(NodeW w){ // them hashtable
- if (Dic[HamBam(w.word)].pHead == NULL){
- InsertFirstListDic(Dic[HamBam(w.word)], w);
- }
- else if (Dic[HamBam(w.word)].pHead->data.word > w.word){
- InsertFirstListDic(Dic[HamBam(w.word)], w);
- }
- else if (Dic[HamBam(w.word)].pTail->data.word < w.word){
- InsertLastListDic(Dic[HamBam(w.word)], w);
- }
- else{
- for (NodeD *k = Dic[HamBam(w.word)].pTail; k != NULL; k = k->pLeft){
- if(k->data.word < w.word){
- InsertAfterListDic(Dic[HamBam(w.word)], w, k->data);
- break;
- }
- }
- }
- }
- bool KT(int KiemTra)// kiem tra xem con tu nao o sau nua khong
- {
- for (int i = KiemTra; i < 26; i++)
- {
- if (Dic[i].pHead != NULL)
- {
- return true;
- }
- }
- return false;
- }
- void tachlayvd(string line, NodeW &w){
- string temp;
- for (int i = 0; i < line.length(); i++){
- if(line[i] == ','){
- w.ex.data[w.ex.amount] = new string;
- *w.ex.data[w.ex.amount] = temp;
- w.ex.amount++;
- temp.clear();
- }
- else
- temp.push_back(line[i]);
- }
- w.ex.data[w.ex.amount] = new string;
- *w.ex.data[w.ex.amount] = temp;
- w.ex.amount++;
- }
- void tachlaynghia(string x, NodeW &w){
- string temp;
- for (int i = 0; i < x.length(); i++){
- if (x[i] == ','){
- AddLastMeanList(w.mean, temp);
- temp.clear();
- }
- else{
- temp.push_back(x[i]);
- }
- }
- AddLastMeanList(w.mean, temp);
- }
- // xoa tu
- void DelWord(HashTable &ds,NodeD* NodeNow){
- // xoa Vd
- for (int i = 0; i < NodeNow->data.ex.amount; i++){
- delete NodeNow->data.ex.data[i];
- }
- //xoa nghia
- while (NodeNow->data.mean.pFirst != NULL){
- DelFirstMeanList(NodeNow->data.mean);
- }
- //xoa node nut DL list
- if (NodeNow == ds.pHead){
- if (ds.pHead->pRight == NULL)
- {
- ds.pHead = NULL;
- }
- else{
- ds.pHead = ds.pHead->pRight;
- ds.pHead->pLeft = NULL;
- }
- }
- else if (NodeNow == ds.pTail){
- ds.pTail = ds.pTail->pLeft;
- ds.pTail->pRight = NULL;
- }
- else{
- NodeNow->pRight->pLeft = NodeNow->pLeft;
- NodeNow->pLeft->pRight = NodeNow->pRight;
- }
- delete NodeNow;
- }
- void bangmenu(int x, int y, int m, int n, int mau){
- int i,j;
- textcolor(mau);
- for (i = x; i <= x + m; i++){
- for(j = y; j <= y + n; j++){
- if(i == x || i == x+m){
- if(j == y && i != x + m)
- viet(i, j, 218);
- else if( j == y && i != x)
- viet(i, j, 191);
- else if( j == y + n && i != x + m)
- viet(i, j, 192);
- else if( j == y + n && i != x)
- viet(i, j, 217);
- else
- viet(i, j, 179);
- }
- else if(j == y || j == y + n)
- viet(i, j, 196);
- }
- }
- }
- void menutimkiem(int x, int y){
- int i, j;
- for (i = x; i <= x + 80; i++){
- for (j = y; j <= y + 25; j++){
- if (i == x || i == x + 30 || i == x + 80){
- if (j == y && i != x && i != x + 80)
- viet(i, j, 203);
- else if (j == y && i != x + 80)
- viet(i, j, 201);
- else if (j == y && i != x)
- viet(i, j, 187);
- else if (j == y + 25 && i != x && i != x + 80)
- viet(i, j, 202);
- else if (j == y + 25 && i != x + 80)
- viet(i, j, 200);
- else if (j == y + 25 && i != x)
- viet(i, j, 188);
- else if (j == y + 4 && i != x + 30 && i != x + 80)
- viet(i, j, 204);
- else if (j == y + 4 && i != x && i != x + 30)
- viet(i, j, 185);
- else if (j == y + 4 && i != x && i != x + 80)
- viet(i, j, 206);
- else
- viet(i, j, 186);
- }
- else if (j == y || j == y + 25 || j == y + 4)
- viet(i, j, 205);
- }
- }
- }
- int batphim()
- {
- int c=getch();
- if(c==8) //phim Backspace
- return 1;
- else if(c==9) //phim Tab
- return 2;
- else if(c==13) //phim Enter
- return 3;
- else if(c==27) //phim Esc
- return 4;
- else if(c==224)
- {
- c=getch();
- if(c==72) //Keyup
- return 5;
- else if(c==80) //KeyDown
- return 6;
- else if(c==75) //KeyLeft
- return 7;
- else if(c==77) //KeyRight
- return 8;
- else if(c==83) //Delete
- return 9;
- }
- else if(c==0)
- {
- c=getch();
- if(c==60) //F2
- return 12;
- if(c==62) //F4
- return 13;
- }
- }
- int menuXOA(int x, int y){
- //NodeW w;
- vietchuoi(x+20,y+5,"Ban co muon xoa tu nay khong? Chon [CO] hoac [KHONG].",12);
- bangmenu(x+51,y+8,10,4,2);
- vietchuoi(x+55,y+10,"CO",14);
- vietchuoi(x+73,y+10,"KHONG",15);
- int kt=1;
- gotoxy(x+80,y+7);
- for(;;){
- gotoxy(x+80,y+7);
- int h=batphim();
- if(h==8){ //phim sang phai
- if(kt==1){
- kt=2;
- bangmenu(x+51,y+8,10,4,0);
- vietchuoi(x+55,y+10,"CO",15);
- bangmenu(x+70,y+8,10,4,2);
- vietchuoi(x+73,y+10,"KHONG",14);
- }
- else if(kt==2){
- kt=1;
- bangmenu(x+51,y+8,10,4,2);
- vietchuoi(x+55,y+10,"CO",14);
- bangmenu(x+70,y+8,10,4,0);
- vietchuoi(x+73,y+10,"KHONG",15);
- }
- }
- else if(h==7){ //phim sang trai
- if(kt==1){
- kt=2;
- bangmenu(x+51,y+8,10,4,0);
- vietchuoi(x+55,y+10,"CO",15);
- bangmenu(x+70,y+8,10,4,2);
- vietchuoi(x+73,y+10,"KHONG",14);
- }
- else if(kt==2){
- kt=1;
- bangmenu(x+51,y+8,10,4,2);
- vietchuoi(x+55,y+10,"CO",14);
- bangmenu(x+70,y+8,10,4,0);
- vietchuoi(x+73,y+10,"KHONG",15);
- }
- }
- else if(h==3){ //phim enter
- if(kt==1){
- return 1;
- }
- else if(kt==2){
- return 2;
- }
- }
- }
- }
- void menumain();
- void menuthemtu(int x, int y){
- int i,j;
- for (i = x; i <= x + 80; i++){
- for (j = y; j <= y + 25; j++){
- if (i == x || i == x + 80){
- if (j == y && i != x + 80)
- viet(i, j, 201);
- else if (j == y && i != x)
- viet(i, j, 187);
- else if (j == y + 25 && i != x && i != x + 80)
- viet(i, j, 202);
- else if (j == y + 25 && i != x + 80)
- viet(i, j, 200);
- else if (j == y + 25 && i != x)
- viet(i, j, 188);
- else
- viet(i, j, 186);
- }
- else if (j == y || j == y + 25)
- viet(i, j, 205);
- }
- }
- vietchuoi(x+5,y+2,"1> Nhap tu moi: ",6);
- vietchuoi(x+5,y+4,"2> Tu loai: ",6);
- vietchuoi(x+5,y+6,"3> Nghia: ",6);
- vietchuoi(x+5,y+14,"4> Vi du: ",6);
- vietchuoi(x+10,y+16,"a> vd1: ",6);
- }
- int MenuChucNang(int x, int y){
- int kt=0;
- HashTable ds;
- NodeD *NodeNow = new NodeD;
- bangmenu(x,y+21,80,4,10);
- bangmenu(x+2,y+22,10,2,14);
- vietchuoi(x+4,y+23,"THEM TU",15);
- vietchuoi(x+20,y+23,"SUA TU",6);
- vietchuoi(x+35,y+23,"XOA TU",6);
- vietchuoi(x+50,y+23,"THOAT",6);
- gotoxy(x+50,28);
- for(;;){
- int h=batphim();
- gotoxy(x+50,28);
- if(h==8){ //right
- if(kt==0){
- kt=1;
- bangmenu(x+2,y+22,10,2,0);
- vietchuoi(x+4,y+23,"THEM TU",6);
- bangmenu(x+18,y+22,10,2,14);
- vietchuoi(x+20,y+23,"SUA TU",15);
- vietchuoi(x+35,y+23,"XOA TU",6);
- vietchuoi(x+50,y+23,"THOAT",6);
- }
- else if(kt==1){
- kt=2;
- vietchuoi(x+4,y+23,"THEM TU",6);
- bangmenu(x+18,y+22,10,2,0);
- vietchuoi(x+20,y+23,"SUA TU",6);
- bangmenu(x+33,y+22,10,2,14);
- vietchuoi(x+35,y+23,"XOA TU",15);
- vietchuoi(x+50,y+23,"THOAT",6);
- }
- else if(kt==2){
- kt=3;
- vietchuoi(x+4,y+23,"THEM TU",6);
- vietchuoi(x+20,y+23,"SUA TU",6);
- bangmenu(x+33,y+22,10,2,0);
- vietchuoi(x+35,y+23,"XOA TU",6);
- bangmenu(x+48,y+22,10,2,14);
- vietchuoi(x+50,y+23,"THOAT",15);
- }
- else if(kt==3){
- kt=0;
- bangmenu(x+2,y+22,10,2,14);
- vietchuoi(x+4,y+23,"THEM TU",15);
- vietchuoi(x+20,y+23,"SUA TU",6);
- vietchuoi(x+35,y+23,"XOA TU",6);
- bangmenu(x+48,y+22,10,2,0);
- vietchuoi(x+50,y+23,"THOAT",6);
- }
- }
- else if(h==7){ //left
- if(kt==0){
- kt=3;
- bangmenu(x+2,y+22,10,2,0);
- vietchuoi(x+4,y+23,"THEM TU",6);
- vietchuoi(x+20,y+23,"SUA TU",6);
- vietchuoi(x+35,y+23,"XOA TU",6);
- bangmenu(x+48,y+22,10,2,14);
- vietchuoi(x+50,y+23,"THOAT",15);
- }
- else if(kt==3){
- kt=2;
- vietchuoi(x+4,y+23,"THEM TU",6);
- vietchuoi(x+20,y+23,"SUA TU",6);
- bangmenu(x+33,y+22,10,2,14);
- vietchuoi(x+35,y+23,"XOA TU",15);
- bangmenu(x+48,y+22,10,2,0);
- vietchuoi(x+50,y+23,"THOAT",6);
- }
- else if(kt==2){
- kt=1;
- vietchuoi(x+4,y+23,"THEM TU",6);
- bangmenu(x+18,y+22,10,2,14);
- vietchuoi(x+20,y+23,"SUA TU",15);
- bangmenu(x+33,y+22,10,2,0);
- vietchuoi(x+35,y+23,"XOA TU",6);
- vietchuoi(x+50,y+23,"THOAT",6);
- }
- else if(kt==1){
- kt=0;
- bangmenu(x+2,y+22,10,2,14);
- vietchuoi(x+4,y+23,"THEM TU",15);
- bangmenu(x+18,y+22,10,2,0);
- vietchuoi(x+20,y+23,"SUA TU",6);
- vietchuoi(x+35,y+23,"XOA TU",6);
- vietchuoi(x+50,y+23,"THOAT",6);
- }
- }
- else if(h==3){ //enter
- if(kt==0){
- xoamanhinh();
- menuthemtu(x,y);
- system("pause");
- xoamanhinh();
- menumain();
- }
- else if(kt==1){
- xoamanhinh();
- menuthemtu(x,y);
- system("pause");
- xoamanhinh();
- menumain();
- }
- else if(kt==2){
- xoamanhinh();
- menuXOA(x,y);
- system("pause");
- xoamanhinh();
- menumain();
- }
- else if(kt==3){
- gotoxy(8,22);
- MessageBox(0, "Dang thoat chuong trinh!", "THONG BAO", MB_OK);
- gotoxy(x+5,y+26);
- exit(0);
- }
- }
- }
- }
- //YEU CAU
- //TIM KIEM
- bool SoSanh (string input, string y){
- string x = input;
- if (x.length() > y.length()){
- return false;
- }
- else{
- for (int i =0;i<x.length();i++){
- if (x[i]!=y[i]){
- return false;
- }
- }
- return true;
- }
- }
- NodeD* Search(string lookup){
- int x=HamBam(lookup);
- if(x<0){
- return NULL;
- }
- else{
- NodeD *p=Dic[x].pHead;
- while(p!=NULL)
- {
- // if(p->data.word.compare(0,lookup.size(),lookup)==0)
- if (SoSanh(lookup,p->data.word))
- {
- return p;
- }
- p=p->pRight;
- }
- return NULL;
- }
- }
- // xoa man hinh
- void DelScreen(int X, int Y, int cd, int cr){
- ShowCur(0);
- textcolor(7);
- for (int i = Y; i < cr + Y; i++){
- for (int j = X; j < X + cd; j++){
- gotoxy(j, i);
- cout << " ";
- }
- }
- }
- void Xuat1Tu(NodeW w){
- DelScreen(37, 15, 48, 12);
- gotoxy(37, 17);
- cout << w.word << "(" << w.type << ")" << ":";
- for (NodeM *k = w.mean.pFirst; k != NULL; k = k->pNext){
- cout << k->data;
- if (k->pNext != NULL){
- cout << ",";
- }
- }
- gotoxy(37, 18);
- cout << "VD:";
- int j = 19;
- for (int i = 0; i < w.ex.amount; i++){
- gotoxy(37, j++);
- cout << *w.ex.data[i];
- }
- gotoxy(40, 26);
- system("pause");
- DelScreen(37, 15, 48, 12);
- }
- void XuLyNhapChu(string &x, int X, int &Y){
- ShowCur(1);
- while (true){
- char c = _getch();
- if (((c >= 97 && c <= 122) || c==44 )&& x.length() < X + 47){
- x.push_back(c);
- cout << c;
- }
- else if (((c >= 65 && c <= 90) || c==44) && x.length() < X + 47){
- c = c + 32;
- x.push_back(c);
- cout << c;
- }
- else if (c == 8 && x.length() > 0){
- x.erase (x.begin() + x.length()-1);
- cout << "\b";
- cout << " ";
- cout << "\b";
- }
- else if (c == 32){
- x.push_back(' ');
- cout << " ";
- }
- else if (c == 13 && x.length() > 0){ // n?u là phím enter
- Y = Y + 3;
- return ;
- }
- else if (c == -32){
- c = getch();
- if (c == 80){
- Y = Y + 3;
- break;
- }
- else if (c == 72){
- Y = Y - 3;
- break;
- }
- }
- else if (c == 27)
- {
- return ;
- }
- }
- }
- void GhiFile(){
- string mean, ex;
- ifstream filein;
- filein.open("TU-DIEN-2017.txt", ios_base::in);
- if (filein.fail() == true){
- cout << "\nFile ko ton tai\n";
- system("pause");
- }
- while (true){
- NodeW w;
- w.ex.amount = 0;
- w.mean.pFirst = NULL;
- getline(filein, w.word, '/');
- cout<<w.word<<" "<<endl;
- getline(filein, w.type, '/');
- getline(filein, mean, '/');
- getline(filein, ex, '/');
- if(w.word.empty()){
- break;
- }
- tachlaynghia(mean, w);
- tachlayvd(ex, w);
- AddHashTable(w);
- filein.ignore();
- }
- filein.close();
- }
- void DocFile(){
- ofstream fileout("TU-DIEN-2017.txt", ios::out);
- for (int i = 0; i < 26; i++){
- for (NodeD *k = Dic[i].pHead; k != NULL; k = k->pRight){
- fileout << k->data.word << "/" << k->data.type << "/";
- for (NodeM* h = k->data.mean.pFirst; h != NULL; h = h->pNext){
- if (h->pNext != NULL){
- fileout << h->data << "/";
- }
- else
- fileout << h->data << "/";
- }
- for (int j = 0; j < k->data.ex.amount; j++){
- if (j == k->data.ex.amount - 1){
- fileout << *k->data.ex.data[j] << ";";
- }
- else
- fileout << *k->data.ex.data[j] << ",";
- }
- fileout << endl;
- }
- }
- fileout.close();
- }
- void GiaiPhongBoNho(){
- NodeD* k;
- for (int i = 0; i < 26; i++){
- while(Dic[i].pHead!=NULL){
- k = Dic[i].pHead;
- // xoa Vd
- for (int j = 0; j < k->data.ex.amount; j++){
- delete k->data.ex.data[j];
- }
- //xoa nghia
- while (k->data.mean.pFirst != NULL){
- DelFirstMeanList(k->data.mean);
- }
- // xoa tu
- if (Dic[i].pHead->pRight == NULL){
- Dic[i].pHead = NULL;
- }
- else
- {
- Dic[i].pHead = Dic[i].pHead->pRight;
- Dic[i].pHead->pLeft = NULL;
- }
- delete k;
- }
- }
- delete[]Dic;
- }
- void menumain(){
- int x, y;
- bangmenu(37,1,67,2,11);
- vietchuoi(65, 2, "TRA TU DIEN", 9);
- textcolor(2);
- menutimkiem(x = 30, y = 6);
- gotoxy(x+5, y+26);
- cout << endl;
- MenuChucNang(x,y+7);
- }
- int main(){
- resizeConsole(1100,650);
- menumain();
- // MakeHashTable();
- // GhiFile();
- /*
- for(int i=0;i<26;i++){
- for(NodeD* k = Dic[i].pHead;k!=NULL;k=k->pRight){
- cout<<k->data.word<<endl;
- }
- }*/
- // DocFile();
- // system ("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement