Skip to content

Instantly share code, notes, and snippets.

@mharju
Created July 7, 2010 05:25
Show Gist options
  • Save mharju/466339 to your computer and use it in GitHub Desktop.
Save mharju/466339 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define QUESTIONS_FILE "hello.txt"
#define BUFFER_SIZE 255
#define OPTSEPARATOR ';'
#define QUESTIONSEPARATOR '\n'
struct option_t
{
char* option;
};
struct question_t
{
int num_options;
int answer_number;
char* question;
struct option_t** options;
};
char* read_until(FILE *f, int ch)
{
int n=0, c=0;
char* buffer = (char*) malloc( BUFFER_SIZE * sizeof(char) );
while( c = fgetc(f) )
{
if( c == ch )
{
*(buffer + n) = '\0';
} else if( n < BUFFER_SIZE )
{
*(buffer + n++) = c;
continue;
}
break;
}
return buffer;
}
struct option_t* read_option( FILE *f )
{
struct option_t* option = (struct option_t *) malloc(sizeof(struct option_t));
option->option = read_until( f, OPTSEPARATOR );
return option;
}
struct question_t* read_question( FILE *f )
{
char *buffer = NULL;
struct question_t* question = (struct question_t *) malloc(sizeof(struct question_t));
buffer = read_until( f, OPTSEPARATOR );
question->num_options = atoi(buffer);
free(buffer);
buffer = read_until( f, OPTSEPARATOR );
question->answer_number = atoi(buffer);
question->question = read_until( f, OPTSEPARATOR );
question->options = (struct option_t **) malloc( question->num_options * sizeof(struct option_t *));
int i=0;
for(i=0;i<question->num_options;i++)
{
question->options[i] = read_option(f);
}
return question;
}
void print_question(const struct question_t* question, int n)
{
int i=0;
printf("\e[0;36m");
for(i=0;i<strlen(question->question)+16;i++) printf("=");
printf("\n| Kysymys %02d. %s |\n", n, question->question);
for(i=0;i<strlen(question->question)+16;i++) printf("=");
printf("\n\n\e[0;33m");
for(i=0;i<question->num_options;i++)
printf(" %d. %s\n", i+1, question->options[i]->option);
printf(" %d. Lopeta\e[0;37m\n", i+1);
}
void do_questionnaire( struct question_t** questions, int num_questions )
{
int score = 0;
int total = 0;
int done = 0;
int answer = 0;
int ordering[ num_questions ], pos = 0, i=0;
for(i=0;i<num_questions;i++)
ordering[i] = -1;
printf("Arvotaan kysymysten järjestely ");
for(i=0;i<num_questions;i++)
{
pos = rand() % (num_questions-1);
while( ordering[ pos ] != -1 ) { pos++; if(pos > num_questions) pos = 0; }
printf("%d ", pos);
ordering[ pos ] = i;
}
printf("valmis\n\n");
while( !done )
{
int question_number = ordering[ total ];
print_question( questions[ question_number ], total+1);
answer = 0;
while( answer < 1 || answer > questions[question_number]->num_options+1 )
{
printf("\nVastauksesi (Pisteesi: %d / %d): ", score, total);
char buffer[255]={0};
fgets(buffer, sizeof(buffer), stdin);
answer = atoi(buffer);
}
if(answer == questions[question_number]->answer_number)
{
printf("\e[0;32mOikein!\n\n");
score++;
}
else if( answer <= questions[question_number]->num_options)
printf("\e[0;31mVäärin!\n\n");
if( total >= num_questions-1 || answer > questions[question_number]->num_options )
done = 1;
total++;
}
printf("\e[0;36mPeli päättyi. Pisteesi: %d\n\n\e[0;37mKiitos pelaamisesta!\n", score);
}
int main()
{
FILE *f = fopen(QUESTIONS_FILE, "rt");
int num_questions = 0;
int i=0, j=0;
char *buffer = read_until(f, 0x0a);
num_questions = atoi(buffer);
free(buffer);
printf("Kysymyspeli versio 1.0 (c) 2010 Mikko Harju / Osakeyhtiö Taiste\n");
printf("Ladataan %d kysymystä", num_questions);
struct question_t** questions = (struct question_t**) malloc( num_questions * sizeof(struct question_t *));
for(i=0;i<num_questions;i++)
{
printf(".");
questions[i] = read_question(f);
fgetc(f);
}
printf("valmis\n");
do_questionnaire( questions, num_questions );
for(i=0;i<num_questions;i++)
{
for(j=0;j<questions[i]->num_options;j++)
{
free(questions[i]->options[j]);
}
free(questions[i]->options);
free(questions[i]);
}
free(questions);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment