aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/calculer.c48
1 files changed, 46 insertions, 2 deletions
diff --git a/src/calculer.c b/src/calculer.c
index 1c78197..6ed5983 100644
--- a/src/calculer.c
+++ b/src/calculer.c
@@ -17,20 +17,49 @@
#include "config.h"
+#include <assert.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
#include <readline/readline.h>
static int lower = 0;
static int upper = 100;
+static FILE *logfile;
+
/* Return a random number x such that lower <= x < upper. */
-int
-calculer_rand()
+static int
+calculer_rand(void)
{
return lower + rand()%(upper-lower);
}
+static void *
+measure_before(void)
+{
+ /* TODO: measure time in ms */
+ return (void *) time(NULL);
+}
+
+int
+measure_after(void *data)
+{
+ int t0 = (intptr_t) data;
+ time_t now = time(NULL);
+ int dt = now - t0;
+ char buf[64] = {0};
+ struct tm *tm = localtime(&now);
+ size_t bytes_written = strftime(buf, sizeof(buf), "%F\t", tm);
+ assert(bytes_written);
+ bytes_written += sprintf(buf + bytes_written, "%d\n", dt);
+
+ return bytes_written != fwrite(buf, 1, bytes_written, logfile);
+}
+
int
main(void)
{
@@ -38,19 +67,34 @@ main(void)
int x, y, res;
char *input;
+ logfile = fopen("./calculer.log", "a");
+ assert(logfile);
+
while (1)
{
x = calculer_rand();
y = calculer_rand();
+ void *measure_data = measure_before();
res = x+y;
+ assert(!measure_after(measure_data));
sprintf(prompt, "%d + %d = ", x, y);
input = readline(prompt);
+ if (strcmp(input, "q") == 0)
+ {
+ free(input);
+ fclose(logfile);
+ exit(0);
+ }
+
if (atoi(input) == res)
printf("RIGHT\n");
else
printf("WRONG\n");
+
+ free(input);
}
+ fclose(logfile);
return 0;
}