-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTyping Tutor
91 lines (78 loc) · 2.31 KB
/
Typing Tutor
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_WORD_LENGTH 20
// Node structure for linked list
typedef struct WordNode {
char text[MAX_WORD_LENGTH];
struct WordNode *next;
} WordNode;
// Structure to track user scores
typedef struct Score {
int correct;
int total;
time_t start_time;
time_t end_time;
} Score;
// Function to add a word to the linked list
void addWord(WordNode **head, const char *word) {
WordNode *newNode = (WordNode *)malloc(sizeof(WordNode));
strcpy(newNode->text, word);
newNode->next = *head;
*head = newNode;
}
// Function to free the linked list
void freeWords(WordNode *head) {
WordNode *tmp;
while (head != NULL) {
tmp = head;
head = head->next;
free(tmp);
}
}
// Function to load words into the linked list
void loadWords(WordNode **head) {
char *sampleWords[] = {"apple", "banana", "cherry", "date", "elderberry"};
int sampleCount = sizeof(sampleWords) / sizeof(sampleWords[0]);
for (int i = 0; i < sampleCount; i++) {
addWord(head, sampleWords[i]);
}
}
// Function to start the typing test
void startTypingTest(WordNode *head, Score *score) {
char input[MAX_WORD_LENGTH];
WordNode *current = head;
score->correct = 0;
score->total = 0;
score->start_time = time(NULL);
while (current != NULL) {
printf("Type the word: %s\n", current->text);
scanf("%s", input);
if (strcmp(input, current->text) == 0) {
score->correct++;
}
score->total++;
current = current->next;
}
score->end_time = time(NULL);
}
// Function to display the results
void displayResults(Score score) {
double time_taken = difftime(score.end_time, score.start_time);
printf("Correct words: %d\n", score.correct);
printf("Total words: %d\n", score.total);
printf("Accuracy: %.2f%%\n", (double)score.correct / score.total * 100);
printf("Time taken: %.2f seconds\n", time_taken);
printf("Words per minute: %.2f WPM\n", (double)score.correct / time_taken * 60);
}
// Main function
int main() {
WordNode *wordList = NULL;
Score score;
loadWords(&wordList);
startTypingTest(wordList, &score);
displayResults(score);
freeWords(wordList);
return 0;
}