2005. 3. 15. 14:53

src

OldPapers/linux 2005. 3. 15. 14:53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

#include "dic.h"
void print_prefix();
void print_postfix();
void prompt_command(UINT type);
void start_dic();
void insert_dic();
void search_dic();
void update_dic();
void get_input();
USHORT get_func();
UINT read_data(DIC_DATA *target);
UINT insert_data(int langType);// 1: eng, 2. kor
UINT search_data(int langType);

char temp[MAXLEN];

void init()
{
        master_info.eng_nData=0;
        master_info.kor_nData=0;
        master_info.eng_head=NULL;
        master_info.kor_head=NULL;
        master_info.e_Current=NULL;
        master_info.k_Current=NULL;
}

int main()
{
        int end=0;
        int func_sel=-1;

        init();

        print_prefix();
        while(1)
        {
                prompt_command(1);
                func_sel=get_func();
                switch(func_sel)
                {
                        case 1:
                                search_dic();
                                break;
                        case 2:
                                insert_dic();
                                break;
                        case 3:
                                end=1;
                                break;
                }
                if(end==1)break;
        }

        print_postfix();
}

UINT read_data(DIC_DATA *target)
{
        printf("# Data insert\n");
        printf(" Word : ");
        fflush(stdout);
        read(0, temp, MAXWORDLEN);
        strncpy(target->word, temp, strlen(temp));
        printf(" Meaning : ");
        fflush(stdout);
        read(0, temp, MAXLEN);
        strncpy(target->meaning, temp, strlen(temp));
        return 1;
}

UINT insert_data(int langType)// 1: eng, 2. kor
{
        DIC_DATA *data;
        data = (DIC_DATA *)malloc(sizeof(DIC_DATA));

        if(data==NULL)
        {
                printf("Memory Allocation Failure!\n");
                exit(0);
        }

        memset(data, 0, sizeof(DIC_DATA));

        if(1!=read_data(data))printf("Data insert Failure!\n");

        if(langType==1)
        {
                if(master_info.eng_head==NULL)
                {
                        master_info.eng_head=data;
                        data->next=NULL;
                        master_info.e_Current=data;
                }
                else
                {
                        data->next=master_info.eng_head;
                        master_info.eng_head=data;
                }
                master_info.eng_nData++;
                printf("New data is inserted\n");
                return 1;
        }
        if(langType==2)
        {
                if(master_info.kor_head==NULL)
                {
                        master_info.kor_head=data;
                        data->next=NULL;
                        master_info.k_Current=data;
                }
                else
                {
                        data->next=master_info.kor_head;
                        master_info.kor_head=data;
                }
                master_info.kor_nData++;
                printf("New data is inserted\n");
                return 1;
        }
        return 0;
}

UINT search_data(int langType)
{
        char select;
        int count, check, matchcount=0;

        if(langType==1)
        {
                master_info.e_Current=master_info.eng_head;
                while(master_info.e_Current!=NULL)
                {
                        check=strncmp(temp, master_info.e_Current->word, strlen(master_info.e_Current->word));
                        if(check==0)
                        {
                                printf(" meaning : %s\n", master_info.e_Current->meaning);
                                matchcount++;
                        }
                        master_info.e_Current=master_info.e_Current->next;
                }
                printf(" %i개의 의미가 검색되었습니다.\n", matchcount);
                if(matchcount > 0 )return 1;
        }
        if(langType==2)
        {
                master_info.k_Current=master_info.kor_head;
                while(master_info.k_Current!=NULL)
                {
                        check=strncmp(temp, master_info.k_Current->word, strlen(master_info.k_Current->word));
                        if(check==0)
                        {
                                printf(" meaning : %s\n", master_info.k_Current->meaning);
                                matchcount++;
                        }
                        master_info.k_Current=master_info.k_Current->next;
                }
                printf(" %i개의 의미가 검색되었습니다.\n", matchcount);
                if(matchcount > 0)return 1;
        }

        if(matchcount == 0)
        {
                printf("사전에 없는 단어입니다. 등록하시겠습니까 ? (y/n)");
                getchar();
                select=getchar();
                if(select == 'y' || select == 'Y')
                {
                        insert_data(langType);
                }
        }

}





void start_dic()
{
        USHORT end=0;
        USHORT func_sel=-1;

        while(1)
        {
                prompt_command(1);
                func_sel=get_func();
                switch(func_sel)
                {
                        case 1:
                                search_dic();
                                break;
                        case 2:
                                break;
                        case 3:
                                end=1;
                                break;
                }
                if(end)break;
        }
}

void search_dic()
{
        int i=0, noeng=0, lang_flag=0;
        get_input();
        
        for(i=0 ; i<strlen(temp) ; i++)
        {
                if( (temp[i] >= 'a' && temp[i] <= 'z') || (temp[i] >= 'A' && temp[i] <= 'Z') )continue;
                else noeng++;
        }
        if(noeng > 0)lang_flag=2;// 한글 단어의 의미분석
        else if(noeng == 0) lang_flag=1;// 영문 단어의 의미 분석
        printf("lang = %i\n", lang_flag);
        search_data(lang_flag);
}

void insert_dic()
{
        int i, noeng=0, lang_flag, langType;
        DIC_DATA *data;
        printf("# New data Insert \n");
        printf("# word : ");
        fflush(stdout);
        read(0, temp, MAXLEN);
        for(i=0 ; i<strlen(temp) ; i++)
        {
                if( (temp[i] >= 'a' && temp[i] <= 'z') || (temp[i] >= 'A' && temp[i] <= 'Z') )continue;
                else noeng++;
        }
        if(noeng > 0)langType=2;// 한글 단어의 의미분석
        else langType=1;// 영문 단어의 의미 분석

        printf("lang = %i\n", langType);

        data=(DIC_DATA *)malloc(sizeof(DIC_DATA));

        strncpy(data->word, temp, strlen(temp));
        printf("# meaning :");
        fflush(stdout);
        read(0, temp, MAXLEN);
        strncpy(data->meaning, temp, strlen(temp));
        
        if(langType==1)
        {
                if(master_info.eng_head==NULL)
                {
                        master_info.eng_head=data;
                        data->next=NULL;
                        master_info.e_Current=data;
                }
                else
                {
                        data->next=master_info.eng_head;
                        master_info.eng_head=data;
                        master_info.e_Current=data;
                }
                master_info.eng_nData++;
                printf("New data is inserted\n");
        }
        if(langType==2)
        {
                if(master_info.kor_head==NULL)
                {
                        master_info.kor_head=data;
                        data->next=NULL;
                        master_info.k_Current=data;
                }
                else
                {
                        data->next=master_info.kor_head;
                        master_info.kor_head=data;
                }
                master_info.kor_nData++;
                printf("New data is inserted\n");
        }
}

void get_input()
{
        int chk, i=0, noeng=0;
        
        prompt_command(2);
        fflush(stdout);
        chk=read(0, temp, MAXLEN);
}

USHORT get_func()
{
        int chk;

        scanf("%i", &chk);

        if(chk < 0 || chk > 3 )return 0;
        return chk;
}

void prompt_command(UINT type)
{
        switch(type)
        {
                case 1: //menu
                        printf("[ Select Menu ]: ");
                        break;
                case 2: // search
                        printf("[ Search Word ]: ");
                        break;
                case 3:
                        break;
        }
}

void print_prefix()
{
        printf("________________________________________\n\n");
        printf("+  [ my Dictionary ] :::::::::::::::::::\n");
        printf("+  Author : eph      :::::::::::::::::::\n");
        printf("+  Last Modified : 2005. 03. 14 ::::::::\n");
        printf("+  :::::::::::::::::::::::::::::::::::::\n\n");
        printf("+  1. Search  2. Update 3. Exit\n");
        printf("________________________________________\n");

        return;
}
void print_postfix()
{
        printf("________________________________________\n\n");
        printf("+  :::::::  Good Bye  ::::::::::::::::::\n\n");
        printf("________________________________________\n");
}