검색한 단어와 동일한 단어가 존재할 경우 : 한글->영어, 영어->한글 로 변환

없을 경우 : 재검색 하면서 검색단어와 사전의 단어 앞글자가 일치하는 경우를 출력


실행 환경 : 실행파일과 같은 폴더에 dictionaly.txt 파일이 존재할 것

 

1
2
3
4
5
6
7
8
9
학교
school
학생
student
사랑
love
반복
loop
-1
cs

 

▲dictionaly.txt format


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main() {
    
    ifstream in("dictionaly.txt");
    string strDictionaly[100][2], seachWord;
    char strTemp[100];
    int t = 0, cnt = 0;
    //사전 불러오기
    while (1)
    {
        in.getline(strTemp, 100);
        if (!strcmp(strTemp, "-1")) break;
        if (!t)
        {
            strDictionaly[cnt][t] = strTemp;
            t = 1;
        }
        else {
            strDictionaly[cnt][t] = strTemp;
            cnt++;
            t = 0;
        }
    }
    int i, j;
    bool isSerach;
    while (1) {
        cout << "검색할 단어를 입력하세요 (종료 : -1)" << endl;
        cin >> strTemp;
        seachWord = strTemp;
        //종료 메시지일 경우 반복문 탈출
        if (seachWord =="-1")
        {
            cout << "사전 프로그램을 종료합니다." << endl;
            break;
        }
        //사전 검색
        isSerach = false;
        for (i = 0; i <= cnt; i++)
        {
            for (j = 0; j < 2; j++) {
                //동일한 단어 찾을 경우 탈출
                if (strDictionaly[i][j] == seachWord)
                {
                    isSerach = true;
                    goto a10;
                }
            }
        }
    a10:;
        //동일 단어 찾을 경우
        if (isSerach)
        {
            //영어->한글 한글->영어 로 배열 인덱스 변환
            j = (j == 0 ? 1 : 0);
            cout << strDictionaly[i][j] << endl;
        }
        else {
            //동일 단어 없을시 앞글자가 동일한 단어 출력
            isSerach = false;
            for (i = 0; i <= cnt; i++)
            {
                for (j = 0; j < 2; j++) {
                    if (strDictionaly[i][j].find(seachWord) == 0)
                    {
                        isSerach = true;
                        cout << strDictionaly[i][j] << " ";
                    }
                }
            }
            //그런 단어도 없을 경우 검색 실패
            if (!isSerach)
            {
                cout << "검색 결과가 없습니다.";
            }
            cout << endl;
        }
    }
}
cs

 


출력 결과

 


'프로그래밍 > C++' 카테고리의 다른 글

(MFC) 메시지 처리  (0) 2016.07.09
(C++) 클래스와 객체  (0) 2016.07.09
(C++) 상속을 이용한 간단한 계산 클래스 구현  (0) 2016.07.09
(C++) 상속성  (0) 2016.07.09
(C++) 연산자 오버로딩  (0) 2016.07.09

+ Recent posts