-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.c
More file actions
122 lines (92 loc) · 2.67 KB
/
Main.c
File metadata and controls
122 lines (92 loc) · 2.67 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <Analysis.h>
#define ESC 0x27
#define COUNTDOWN 0x03
#define BUFFER_SIZE 1024
#define PROMPTS 25
static char INPUT_BUFFER[BUFFER_SIZE];
static char PROMPT_BUFFER[BUFFER_SIZE];
static Analysis *currentAnalysis_ptr;
void readFileIntoBuffer(const char *file_name, char *buffer, int line_number)
{
FILE *file = fopen(file_name, "r");
for (int i = 0; i < line_number; i++)
{
fgets(PROMPT_BUFFER, BUFFER_SIZE, file);
}
rewind(file);
return;
}
void prompt()
{
int n = (rand() % PROMPTS) + 1;
readFileIntoBuffer("prompts.txt", PROMPT_BUFFER, n);
analysePrompt(PROMPT_BUFFER, currentAnalysis_ptr);
printf("Prompt: %s\n", PROMPT_BUFFER);
return;
}
void input()
{
struct timeval tv1;
struct timeval tv2;
unsigned long long timeElapsed;
printf("Type: ");
gettimeofday(&tv1, NULL);
fgets(INPUT_BUFFER, 2047, stdin);
gettimeofday(&tv2, NULL);
timeElapsed =
tv2.tv_sec * 1000 + tv2.tv_usec / 1000 -
tv1.tv_sec * 1000 + tv2.tv_usec / 1000;
currentAnalysis_ptr->speed = (float)timeElapsed / 1000;
printf("Elapsed: [%fs]\nTyped: %s\n", (float)timeElapsed / 1000, INPUT_BUFFER);
return;
}
int main(void)
{
srand(time(NULL));
int rounds = 0;
char string[5];
printf("How many rounds? ");
fgets(string, 4, stdin);
rounds = atoi(string);
printf("Get ready!");
fflush(stdout);
sleep(1);
for (int i = 1; i <= COUNTDOWN; i++)
{
printf(" %d", i);
fflush(stdout);
sleep(1);
}
printf(" Go!\n");
int elapsedRounds = 0;
Analysis cumulative;
initAnalysis(&cumulative);
while (rounds > 0)
{
Analysis analysis;
initAnalysis(&analysis);
currentAnalysis_ptr = &analysis;
prompt();
input();
scoreAttempt(INPUT_BUFFER, PROMPT_BUFFER, currentAnalysis_ptr);
cumulative.accuracy += analysis.accuracy;
cumulative.score += analysis.score;
cumulative.speed += analysis.speed;
printf("Speed: %f, Accuracy: %f, Score: %f\n",
analysis.speed, analysis.accuracy, analysis.score);
printf("Characters: %d, Words: %d, Difficulty: %f\n\n",
analysis.characters, analysis.words, analysis.difficulty);
rounds--;
elapsedRounds++;
}
cumulative.accuracy /= elapsedRounds;
cumulative.score /= elapsedRounds;
cumulative.speed /= elapsedRounds;
printf("Average Speed: %f, Average Accuracy: %f, Average Score: %f\n",
cumulative.speed, cumulative.accuracy, cumulative.score);
return 0;
}