-
Notifications
You must be signed in to change notification settings - Fork 0
/
vocab_wiz_quiz v1.c
132 lines (106 loc) · 3.5 KB
/
vocab_wiz_quiz v1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct{
char type[10];
char eng_mean[50];
char tr_mean[50];
} Word;
void wordSave() {
char file_name[50];
printf("Enter the name of the file to be saved: ");
scanf("%s", file_name);
FILE *opened_file = fopen(file_name, "a");
if (opened_file == NULL) {
printf("File could not be opened!\n");
return;
}
while(1){
Word new_word;
printf("Enter the type of the word (N, Adj, etc.) or 'exit' to finish: ");
scanf("%s", new_word.type);
if (strcmp(new_word.type, "exit") == 0) break;
printf("Enter the word in English: ");
scanf("%s", new_word.eng_mean);
printf("Enter the meaning of the word in Turkish: ");
scanf("%s", new_word.tr_mean);
getchar();
//Check
printf("You have entered: %s %s %s. Save? (Y/N): ", new_word.type, new_word.eng_mean, new_word.tr_mean);
char confirmation;
scanf(" %c", &confirmation);
if (confirmation == 'Y' || confirmation == 'y') {
fprintf(opened_file, "%s %s %s\n", new_word.type, new_word.eng_mean, new_word.tr_mean);
printf("Word saved successfully.\n");
}
else {
printf("Word not saved. Please re-enter the word.\n");
}
}
fclose(opened_file);
}
int loadWords(const char* fileName, Word words[], int maxWords) {
FILE *file = fopen(fileName, "r");
if (file == NULL) {
printf("File could not be opened!\n");
return 0;
}
int count = 0;
while (count < maxWords && fscanf(file, "%s %s %s", words[count].type, words[count].eng_mean, words[count].tr_mean) == 3) {
count++;
}
fclose(file);
return count;
}
void quiz(Word words[], int wordCount) {
srand(time(NULL)); // rand start
int i;
for (i = 0; i < wordCount; i++) {
int correctAnswer = rand() % 3;
int wrongAnswer1, wrongAnswer2;
do {
wrongAnswer1 = rand() % wordCount;
} while (wrongAnswer1 == i);
do {
wrongAnswer2 = rand() % wordCount;
} while (wrongAnswer2 == i || wrongAnswer2 == wrongAnswer1);
printf("What is the meaning of '%s'?\n", words[i].eng_mean);
int j;
for (j = 0; j < 3; j++) {
if (j == correctAnswer) {
printf("%c) %s\n", 'A' + j, words[i].tr_mean);
} else if (j == (correctAnswer + 1) % 3) {
printf("%c) %s\n", 'A' + j, words[wrongAnswer1].tr_mean);
} else {
printf("%c) %s\n", 'A' + j, words[wrongAnswer2].tr_mean);
}
}
char userAnswer;
printf("Your answer: ");
scanf(" %c", &userAnswer);
getchar();
if (userAnswer - 'A' == correctAnswer) {
printf("Correct!\n\n");
} else {
printf("Wrong! The correct answer was %c) %s\n\n", 'A' + correctAnswer, words[i].tr_mean);
}
}
}
void wordLearn() {
char fileName[50];
printf("Enter the name of the file to learn from: ");
scanf("%s", fileName);
Word words[100]; // Max 100 Word
int wordCount = loadWords(fileName, words, 100);
if (wordCount > 0) {
quiz(words, wordCount);
} else {
printf("No words to learn.\n");
}
}
int main(){
//wordSave();
wordLearn();
return 0;
}