~fkfd/fcc

fcc899ec1f52131eeb807f56534f2f1984a4914f — Frederick Yin 11 months ago a42aeb2 main
Random mode
2 files changed, 32 insertions(+), 7 deletions(-)

M quiz.c
M quiz.h
M quiz.c => quiz.c +31 -7
@@ 26,10 26,29 @@ void quiz(const char *pool_fp, const char *log_fp, const bool simulate_test,
		    log_fp);
		exit(1);
	}
	
	const int size = json_object_array_length(pool);
	for (int i = 0; i < size; i++) {
		const question_t question = get_question_by_idx(pool, i);
	json_object *progress = json_object_object_get(log, "progress");
	if (progress == NULL) {
		fprintf(
		    stderr,
		    "\"progress\" field in %s is not valid or does not exist\n",
		    log_fp);
		exit(1);
	}

	const int pool_size = json_object_array_length(pool);
	int idx;
	if (random_mode) {
		srand(time(NULL));
		idx = rand() % pool_size;
	} else {
		idx = json_object_get_int(progress);
		if (idx < 0 || idx >= pool_size) {
			fprintf(stderr, "Invalid progress index: %d\n", idx);
			idx = 0;
		}
	}
	while (1) {
		const question_t question = get_question_by_idx(pool, idx);
		history_t history = get_history(histories, question.id);
		if (history.id == NULL) {
			// no related history yet; create one


@@ 44,6 63,14 @@ void quiz(const char *pool_fp, const char *log_fp, const bool simulate_test,
			history.hits++;
		}
		update_history(histories, history);
		
		// decide next idx
		if (random_mode) {
			idx = rand() % pool_size;
		} else {
			idx++;
			json_object_int_inc(progress, 1);
		}
		json_object_to_file_ext(log_fp, log, JSON_C_TO_STRING_PRETTY);
	}



@@ 51,9 78,6 @@ void quiz(const char *pool_fp, const char *log_fp, const bool simulate_test,
	json_object_put(log);
}

void quiz_sequential(const json_object *pool, const json_object *log) {
}

int ask_question(const question_t question) {
	print_question(question);
	printf("\nYou choose: ");

M quiz.h => quiz.h +1 -0
@@ 3,6 3,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <json-c/json.h>
#include <json-c/json_object.h>
#include "json.h"