리눅스에서 영단어 퀴즈 프로그램 만들기 with C (단일연결리스트)
리눅스에서 영단어 퀴즈 프로그램 만들기 with C (단일연결리스트)
직접 코딩하면서 공부하기 리눅스 우분투 환경에서 C언어 gcc 컴파일러로 구현하여 타운영체제나 프로그램에서 실행되지 않을 수 있습니다.
영어 단어 퀴즈 프로그램
영어 단어 퀴즈 프로그램을 구현합니다.
기능
- 영어단어 n개가 저장된 dic.txt를 준비합니다. (단어 뜻)
- 프로그램은 자기참조구초제(단일 연결리스트)와 동적 할당을 통해서 단어를 불러옵니다.
- 단어의 개수 상관없이 동적으로 실행되어야합니다.
- 불러온 단어는 사전순으로 리스트가 바뀌어야 합니다.
- 프로그램을 실행하면 퀴즈를 시작하는 1옵션, 프로그램을 종료하는 2옵션을 입력받습니다.
- 1을 입력하면 영단어의 한글 뜻이 출력되고 그에 맞는 영단어를 입력합니다.
- 맞으면 correct, 틀리면 incorrect가 출력됩니다.
- 프로그램은 끝까지 입력하거나 .quit을 입력하면 백분율로 점수가 출력되고 엔터키를 기다립니다.
- 엔터키가 입력되면 다시 1,2 선택 메뉴가 나오고 2를 입력하면 화면이 clear 되고 프로그램이 종료됩니다.
English.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct list { //자기 참조 구초제 이므로 이름이 같아야함!
char eng[15];
char kor[30];
struct list* next;
} list;
list* makeNode(char eng[], char kor[]);
void addNode(list** head, char eng[], char kor[]);
void swapNode(list** first, list** second);
void sortNode(list** head, int count);
void printWord(list* head);
int main()
{
FILE* txt;
int count = 0;
char eng[15], kor[30];
txt = fopen("dic.txt", "r");
list* head = 0;
while (fscanf(txt, "%s %s", eng, kor) != EOF) {
addNode(&head, eng, kor);
count++;
}
fclose(txt);
//printWord(head);
sortNode(&head, count);
printWord(head);
bool isTwo = false;
while (!isTwo) {
printf(">>영어 단어 맞추기 프로그램<<\n1. 영어 단어 맞추기\t2. 프로그램 종료\n\n번호를 선택하세요: ");
char num = getchar();
if (num == '1') {
list* word = head;
char answer[15];
float rightCount = 0, wrongCount = 0;
bool isQuit = false;
while (!isQuit) {
printf("%s -> ", word->kor);
scanf("%s", answer);
if (strcmp(answer, word->eng) == 0) {
printf("correct!\n");
rightCount++;
}
else if (strcmp(answer, ".quit") == 0) {
break;
}
else {
printf("incorrect!\n");
wrongCount++;
}
if (rightCount + wrongCount == count)
isQuit = true;
word = word->next;
}
float point = rightCount / (rightCount + wrongCount) * 100;
printf("당신의 점수는 %.2f점 입니다.\n", point);
getchar();
while (getchar() != '\n');
system("clear");
}
else if (num == '2') {
isTwo = true;
system("clear);
}
}
return 0;
}
list* makeNode(char eng[], char kor[])
{
list* word = (list*)malloc(sizeof(list));
strcpy(word->eng, eng);
strcpy(word->kor, kor);
word->next = NULL;
return word;
}
void addNode(list** head, char eng[], char kor[])
{
list* word = makeNode(eng, kor);
if (!(*head)) {
*head = word;
}
else if ((*head)->next == NULL) {
(*head)->next = word;
}
else {
addNode(&(*head)->next, eng, kor);
}
}
void printWord(list* head)
{
list* word = head;
while (word != NULL) {
printf("%s %s\n", word->eng, word->kor);
word = word->next;
}
printf("\n");
}
void swapNode(list** first, list** second) {
char engTemp[15], korTemp[30];
strcpy(engTemp, (*first)->eng);
strcpy((*first)->eng, (*second)->eng);
strcpy((*second)->eng, engTemp);
strcpy(korTemp, (*first)->kor);
strcpy((*first)->kor, (*second)->kor);
strcpy((*second)->kor, korTemp);
}
void sortNode(list** head, int count) {
list* word = *head;
list* word2 = (*head)->next;
for (int i = 0; i < count; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (strcmp((*word).eng, (*word2).eng) == 1) {
swapNode(&word, &word2);
}
word = word->next;
word2 = word2->next;
}
word = *head;
word2 = (*head)->next;
}
}