/*
* ============================================================================
*
* Filename: wordManageProgram.c
*
* Description: [일반과제 2반] 단어 관리 프로그램
*
* Version: 1.0
* Created: 2009년 01월 06일 20시 40분 03초
* Revision: none
* Compiler: gcc
*
* Author: Kyungwook, Park,[[email protected]](mailto:[email protected])
*
* ============================================================================
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define WORD_SIZE 80 /* 단어 크기 */
#define WORD_CNT 20 /* 단어 개수 */
/*
* === FUNCTION =============================================================
* Name: search
* Description: 문자열 검색
* ============================================================================
*/
int search(char* set, int* wordCnt, char* keyword)
{
int results, i;
for (results = 1, i = 0; i < *wordCnt && results; i++) {
if (strlen((char*)(set + i * WORD_SIZE)))
results = strcmp((char*)(set + i * WORD_SIZE), keyword);
}
return (results == 0) ? i : 0;
}
/*
* === FUNCTION =============================================================
* Name: add
* Description: 문자열 추가
* ============================================================================
*/
int add(char* set, int* wordCnt, char* keyword)
{
int results = search(set, wordCnt, keyword);
/* 문자열이 없고, 현재 문자열의 개수가 WORD_CNT 미만일때 */
if (!results && *wordCnt < WORD_CNT) {
strcpy((char*)(set + (*wordCnt) * WORD_SIZE), keyword); /* 문자열 복사 */
(*wordCnt)++;
}
return !results;
}
/*
* === FUNCTION =============================================================
* Name: delete
* Description: 문자열 삭제
* ============================================================================
*/
int delete (char* set, int* wordCnt, char* keyword)
{
int results = search(set, wordCnt, keyword);
int i;
if (results) {
/* 앞으로 당겨서 복사한다. */
for (i = results; i < (*wordCnt) - 1; i++) {
strcpy(
((char*)(set + (i)*WORD_SIZE)), /* i 번째 */
((char*)(set + (i + 1) * WORD_SIZE)) /* i+1번째 */
);
}
/* 문자열이 검색되었을때 문자열 개수를 하나 줄이는 것으로 삭제를 한다. */
(*wordCnt)--;
}
return results;
}
/*
* === FUNCTION =============================================================
* Name: print
* Description:
* ============================================================================
*/
void print(char* set, int* wordCnt)
{
int i;
printf("nnt** 저장된 단어 목록 **n");
if (*wordCnt > 0) {
for (i = 0; i < *wordCnt; i++)
printf("t단어 %2d : %sn", i + 1, (char*)(set + i * WORD_SIZE));
}
else
printf("t!! 저장된 단어가 없습니다. !!n");
printf("n");
}
/*
* === FUNCTION =============================================================
* Name: run
* Description: 메뉴 입력 및 실행
* ============================================================================
*/
void run()
{
char word[WORD_CNT][WORD_SIZE]; /* WORD LIST */
char inp[WORD_SIZE]; /* WORD 입력에 쓰일 입력 변수 */
char yesOrNo; /* Y/N을 물어볼때 쓰는 변수 */
int wordCnt = 0; /* WORD COUNT */
int i, results, job; /* results : 함수 리턴에 쓰는 변수, job : 메뉴에 쓰일 변수 */
printf("[ 단어 관리 프로그램 ]n");
do {
fflush(stdin);
printf("1.입력하기 / 2.출력하기 / 3.검색하기 / 4.삭제하기 / 5.종 료 : ");
scanf("%d", &job);
if (job < 1 && job > 5) {
printf("n !! 1과 5 사이 값만 가능합니다. !!n");
continue;
}
switch (job) {
case 1: /* 단어 추가 */
printf("t* 단어 입력시 제한게수 20개 이하n");
printf("t (입력 종료 조건 : " end " 문자열 입력)n");
for (i = 0, results = 1; i < 20 && results; i++) {
printf("t# 영문 단어를 입력하세요 : ");
scanf("%s", inp);
results = strcmp(inp, "end");
if (results) {
printf("t!! %s 문자열이 ", inp);
if (add((char*)word, &wordCnt, inp)) /* 단어를 넣을 수 있으면 */
printf("정상적으로 입력되었습니다. !!n");
else if (search((char*)word, &wordCnt, inp)) /* 단어를 넣을 수 없으면 검색 */
printf("존재합니다. !!n");
else
printf("입력되지 않았습니다.(문자열 개수 : %d) !!n", wordCnt);
}
}
break;
case 2: /* 단어 출력 */
print((char*)word, &wordCnt); /* 저장된 문자열 출력 */
break;
case 3: /* 단어 검색 */
do {
int wordRank = 0; /* 저장된 문자열 상대적 순서 */
printf("t# 찾는 단어를 입력하세요 : ");
scanf("%s", inp);
results = strcmp(inp, "end");
if (results) {
/* 문자열의 상대적 위치를 리턴함 */
wordRank = search((char*)word, &wordCnt, inp);
printf("t@%s 단어는 ", inp);
if (wordRank)
printf("%d번째에 존재합니다.n", wordRank);
else
printf("존재하지 않습니다.n");
}
} while (results);
break;
case 4:
do {
printf("t# 삭제할 단어를 입력하세요 : ");
scanf("%s", inp);
results = strcmp(inp, "end");
if (results != 0) {
/* 입력받은 단어의 위치를 리턴받는다. */
int wordRank = search((char*)word, &wordCnt, inp);
printf("t@%s 단어는 ", inp);
if (wordRank) {
printf("%d번째에 존재합니다.n", wordRank);
printf("t# 정말로 삭제하시겠습니까?(Y/N) : ");
scanf("%c%c", &yesOrNo, &yesOrNo);
/* Y일 경우에만 삭제, 그 외에 문자는 삭제 취소 */
if (yesOrNo == 'y' || yesOrNo == 'Y') {
if (delete ((char*)word, &wordCnt, inp))
printf("t!! 삭제되었습니다. !!n");
}
else
printf("t!! 삭제가 취소되었습니다. !!n");
}
else
printf("존재하지 않습니다.n");
}
} while (results);
break;
case 5:
break;
}
} while (job != 5);
}
int main()
{
run(); /* 실행 함수 호출 */
return 0;
}