~fkfd/fcc

bed8b5a783f83b68c9385c1981b5e25420df4db7 — Frederick Yin 11 months ago 9763af0
const everything
4 files changed, 14 insertions(+), 12 deletions(-)

M json.c
M json.h
M quiz.c
M quiz.h
M json.c => json.c +4 -4
@@ 2,7 2,7 @@
#include <json-c/json_object.h>
#include <json-c/json_types.h>

question_t get_question_by_idx(json_object *pool, const int idx) {
question_t get_question_by_idx(const json_object *pool, const int idx) {
	const json_object *obj = json_object_array_get_idx(pool, idx);
	const json_object *answers = json_object_object_get(obj, "answers");
	const question_t   question = {


@@ 19,7 19,7 @@ question_t get_question_by_idx(json_object *pool, const int idx) {
	return question;
}

question_t get_question(json_object *pool, const char *id) {
question_t get_question(const json_object *pool, const char *id) {
	for (int idx = 0; idx < json_object_array_length(pool); idx++) {
		const question_t question = get_question_by_idx(pool, idx);
		if (strcmp(question.id, id) == 0) return question;


@@ 40,7 40,7 @@ void create_log_if_not_exist(const char *fp) {
	fprintf(stderr, "Created empty log file at %s\n", fp);
}

history_t get_history_by_idx(json_object *histories, const int idx) {
history_t get_history_by_idx(const json_object *histories, const int idx) {
	const json_object *hist = json_object_array_get_idx(histories, idx);
	const history_t history = {
		json_object_get_string(json_object_object_get(hist, "id")),


@@ 50,7 50,7 @@ history_t get_history_by_idx(json_object *histories, const int idx) {
	return history;
}

history_t get_history(json_object *histories, const char *id) {
history_t get_history(const json_object *histories, const char *id) {
	for (int idx = 0; idx < json_object_array_length(histories); idx++) {
		const history_t hist = get_history_by_idx(histories, idx);
		if (strcmp(hist.id, id) == 0) return hist;

M json.h => json.h +4 -4
@@ 23,10 23,10 @@ struct history {

typedef struct history history_t;

question_t get_question_by_idx(json_object *pool, const int idx);
question_t get_question(json_object *pool, const char *id);
question_t get_question_by_idx(const json_object *pool, const int idx);
question_t get_question(const json_object *pool, const char *id);
void create_log_if_not_exist(const char *fp);
history_t get_history_by_idx(json_object *histories, const int idx);
history_t get_history(json_object *pool, const char *id);
history_t get_history_by_idx(const json_object *histories, const int idx);
history_t get_history(const json_object *pool, const char *id);
void update_history(json_object *histories, history_t hist);
#endif

M quiz.c => quiz.c +4 -2
@@ 32,7 32,7 @@ void quiz(const char *pool_fp, const char *log_fp, const bool random_mode,
		const question_t question = get_question_by_idx(pool, i);
		history_t history = get_history(histories, question.id);
		if (history.id == NULL) {
			// no related history yet
			// no related history yet; create one
			history.id = question.id;
			history.attempts = 0;
			history.hits = 0;


@@ 51,6 51,8 @@ void quiz(const char *pool_fp, const char *log_fp, const bool random_mode,
	json_object_put(log);
}

void quiz_sequential();

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


@@ 82,7 84,7 @@ int ask_question(const question_t question) {
	}
}

void print_question(question_t question) {
void print_question(const question_t question) {
	printf("%s\n%s\n\n", question.id, question.question);
	printf("A. %s\n", question.answers[0]);
	printf("B. %s\n", question.answers[1]);

M quiz.h => quiz.h +2 -2
@@ 21,6 21,6 @@ enum {

void quiz(const char *pool_fp, const char *log_fp, const bool random_mode,
          const bool show_answer, const bool weight_enable);
int ask_question(question_t question);
void print_question(question_t question);
int ask_question(const question_t question);
void print_question(const question_t question);
#endif