2019-01-28

Hyeonu_Chun·2021년 6월 21일
0

HW1

#include <stdio.h>
#include <string.h>
#pragma warning (disable : 4996)

typedef struct dic {
	char word[20] = "";
	char mean[80] = "";
	int len = 0;
}Dic;

void inputString(const char*, char*);
void dicChange(Dic*, Dic*);
void input(Dic*, int*);
void output(Dic*, int);
void dicFind(Dic*, int);
void dicDelete(Dic*, int*);
void myflush();

int main() {
	Dic ary[10];
	int num, sum = 0;
	while (1) {
		printf("1.입력하기 2.출력하기 3.검색하기 4.삭제하기 5.종료 : ");
		scanf("%d", &num);
		myflush();
		switch (num) {
		case 1: input(ary, &sum); printf("\n"); break;
		case 2: output(ary, sum); printf("\n"); break;
		case 3: dicFind(ary, sum); printf("\n"); break;
		case 4: dicDelete(ary, &sum); printf("\n"); break;
		case 5: return 0;
		}
	}
	return 0;
}

void input(Dic*sp, int*sum) {
	for (int i = 0;i < 11;i++) {
		if (((sp+i)->word)[0] == 0) {
			inputString("#단어를 입력하시오 : ", (sp+i)->word);
			if (strcmp((sp + i)->word, "end") == 0) {
				strcpy((sp + i)->word, "");
				break;
			}
			inputString("#뜻을 입력하시오 : ", (sp+i)->mean);
			printf("\n");
			(sp+i)->len = strlen((sp+i)->word);
			(*sum)++;
		}
	}
	return;
}

void output(Dic*sp, int sum) {
	for (int i = 0;i < 10;i++) {
		if (((sp + i)->word)[0] >= 97 && ((sp + i)->word)[0] <= 122) {
			for (int j = i + 1; j < sum;j++) {
				if (((sp + i)->word)[0] >= 97 && ((sp + i)->word)[0] <= 122) {
					if (((sp + i)->word)[0] > ((sp + j)->word)[0]) {
						dicChange((sp + i), (sp + j));
					}
				}
			}
			printf("%d.%20s(%d) : %-s", i + 1, (sp + i)->word, (sp + i)->len, (sp + i)->mean);
			if (strlen((sp + i)->mean) <= 50) printf("\n");
			else {
				for (size_t k = 0; k < strlen((sp + i)->mean) - 50; k++) {
					printf(" \b\b");
				}
				printf("~\n"); 
			}
		}
	}
	return;
}

void dicFind(Dic*sp, int sum) {
	char fData[20];
	while (1) {
		int num = 0;
		inputString("# 찾을 단어를 입력하시오 : ", fData);
		for (int i = 0;i < sum;i++) {
			if (strcmp(fData, "end") == 0)return;
			if (strcmp(fData, (sp + i)->word) == 0) {
				printf("  단어의 뜻 : %s\n", (sp + i)->mean);
				break;
			}
			num++;
		}
		if (num == sum) printf("해당 단어는 존재하지 않습니다.\n");
	}
}

void dicDelete(Dic*sp, int* sum) {
	char dData[20];
	char ch;
	while (1) {
		int num = *sum;
		inputString("# 삭제할 단어를 입력하시오 : ", dData);
		for (int i = 0;i < *sum;i++) {
			if (strcmp(dData, "end") == 0)return;
			if (strcmp(dData, (sp + i)->word) == 0) {
				printf("# 정말로 삭제하시겠습니까?(y/n) : ");
				scanf(" %c", &ch);
				myflush();
				if (ch == 'y') {
					dicChange(sp + i, sp + (*sum - 1));
					strcpy((sp + (*sum - 1))->word, "");
					strcpy((sp + (*sum - 1))->mean, "");
					(sp + (*sum - 1))->len = 0;
					(*sum)--;
					printf("삭제되었습니다.\n");
				}
				else {
					printf("삭제가 취소되었습니다.\n");
					num++;
				}
				break;
			}
		}
		if(num == *sum) printf("해당 단어는 존재하지 않습니다.\n");
	}
}

void dicChange(Dic*ary1, Dic*ary2) {
	Dic num = { "","",0 };
	Dic *temp = &num;

	strcpy(temp->word, ary1->word);
	strcpy(ary1->word, ary2->word);
	strcpy(ary2->word, temp->word);

	strcpy(temp->mean, ary1->mean);
	strcpy(ary1->mean, ary2->mean);
	strcpy(ary2->mean, temp->mean);

	temp->len = ary1->len;
	ary1->len = ary2->len;
	ary2->len = temp->len;
	return;
}

void inputString(const char *msg, char*ary) {
	printf(msg);
	fgets(ary, 80 , stdin);
	ary[strlen(ary) - 1] = '\0';
	return;
}

void myflush()
{
	while (getchar() != '\n');
}
profile
Stay hungry, stay foolish

0개의 댓글