#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* rand(), strand()を使うため追加*/
#include <time.h> /* time() 変数の種を変えるため追加*/
#define MAX_WORDS 100
typedef struct{
char word[50];
char meaning[100];
char example[200];
char category[20];
int correct_count;
int wrong_count;
} WordCard;
void add_words(WordCard cards[], int *count){
if (*count >= MAX_WORDS){
printf("unavailable to add a new a word(Max_words%d)\n", MAX_WORDS);
return;
}
WordCard new_card;
printf("Word :");
fgets(new_card.word, sizeof(new_card.word), stdin);
new_card.word[strcspn(new_card.word, "\n")] = '\0';
printf("Meaning :");
fgets(new_card.meaning, sizeof(new_card.meaning), stdin);
new_card.meaning[strcspn(new_card.meaning, "\n")] = '\0';
printf("Example :");
fgets(new_card.example, sizeof(new_card.example), stdin);
new_card.example[strcspn(new_card.example, "\n")] = '\0';
printf("Category :");
fgets(new_card.category, sizeof(new_card.category), stdin);
new_card.category[strcspn(new_card.category, "\n")] = '\0';
new_card.correct_count = 0;
new_card.wrong_count = 0;
cards[*count] = new_card;
(*count)++;
printf("Added %s !\n\n", new_card.word);
}
void list_words(WordCard cards[], int count){
if (count == 0){
printf("Unregistrated word\n\n");
return;
}
printf("===Registrated Word %d ===\n", count);
for (int i = 0; i < count; i++){
printf("%d. %s - %s [%s]\n", i + 1, cards[i].word, cards[i].meaning, cards[i].category);
} printf("\n");
}
void quiz_word(WordCard cards[], int count){
if (count == 0){
printf("No words to quiz. Add some words first.\n\n");
return;
}
void save_words(WordCard cards[], int count){
FILE *fp = fopen("words.txt", "w"); /* w = 書き込みモードで開く*/
if (fp == NULL) {
printf("Failed to save the file.\n");
return;
}
fprintf(fp, "%d\n", count); /* 1件分のデータを1行にまとめて書き込む
区切り文字として | を使うことで、後で読み込むときに分割しやすくする */
for (int i = 0; i < count; i++){
fprintf(fp, "%s|%s|%s|%s|%d|%d\n",
cards[i].word,
cards[i].meaning
cards[i].example
cards[i].category
cards[i].correct_count
cards[i].wrong_count);
}
fclose(fp); /* 開いたファイルは必ず閉じる */
printf("Saved %d word to words.txt\n\n", count);
}
void load_words(WordCard cards[], int *count){
FILE *fp = fopen("words.txt", "r"); /* "r" = read(読み込みモードで開く) */
if (fp == NULL) { /* 初回起動時などファイルがまだ存在しない場合はここに来る。
エラーではないので、静かに戻るだけでよい */
printf("No saved data found. Starting freash.\n\n");
return;
}
int saved_count;
fscanf(fp, "%d\n", &saved_count; i++){
fscanf(fp, "%49[^|]|%99[^|]|%199[^|]|%19[^|]|%d|%d\n",
cards[i].word,
cards[i].meaning,
cards[i].example,
cards[i].category,
&cards[i].correct_count,
&cards[i].wrong_count);
}
*count = saved_count;
fclose(fp);
printf("Loaded %d word(s) from words.txt\n\n", saved_count);
}
/* 0 ~ (count-1)の範囲でランダムな数字を作る。*/
/* rand()は0以上の大きい整数を返すので% count(countで割った余り)使って0~count-1に範囲を収める*/
int index = rand() % count;
WordCard *quiz = &cards[index];
char answer[100];
printf("Q. What does \"%s\" mean?\n", quiz->word); /*ポインタが指している構造体のメンバーにアクセスする*/
printf("Your answer :");
fgets(answer, sizeof(answer), stdin);
answer[strcspn(answer, "\n")] = '\0';
/* 文字列の比較は == ではできない。strcmp() を使う。
strcmp(a, b) は a と b が完全に同じなら 0 を返す */
if (strcmp(answer, quiz->meaning) == 0){ /*文字の中身そのものを比較したいときは strcmp() を使い、戻り値が 0 なら「完全に一致」という意味*/
printf("Correct!\n\n");
quiz->correct_count++;
}else{
printf("Incorrect. Correct answer: %s\n\n", quiz->meaning);
quiz->wrong_count++;
}
}
int main(void){
WordCard cards[MAX_WORDS];
int word_count = 0;
int choice;
srand(time(NULL)); /* 乱数の種をプログラム起動時の時刻で初期化, これがないと毎回同じ順番で出題されてしまう*/
/*time(NULL)(現在時刻)を種にすることで、毎回違う結果*/
load_words(cards, &word_count);
while (1) {
printf("=== Words ===\n");
printf("1 : Add a word\n");
printf("2 : Look up the list\n");
printf("3 :Quiz\n");
printf("4 : End\n");
printf("Choose :");
scanf("%d", &choice);
getchar();
if (choice == 1){
add_words(cards, &word_count);
} else if (choice == 2) {
list_words(cards, word_count);
} else if (choice == 3){
quiz_word(cards, word_count);
} else if (choice == 4){
save_words(cards, word_count);
printf("Bye\n");
break;
} else {
printf("Put a number 1 ~ 4\n");
}
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: