home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume36
/
formes
/
part02
/
formes.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-01
|
8KB
|
422 lines
static char *copyright =
{
" FORMES 1.5 Copyright (C) 1992-1993 Jeffrey Chilton \
\
Permission is granted to anyone to make or distribute copies of \
this program, in any medium, provided that the copyright notice \
and permission notice are preserved, and that the distributor \
grants the recipient permission for further redistribution as \
permitted by this notice. \
\
Author's E-mail address: 172-9221@mcimail.com "
};
static char *whatstring = "@(#)formes.c 2.6 JWC";
#include <stdio.h>
#include "class.h"
#include "exstr.h"
#include "form.h"
#include "subject.h"
#include "subjset.h"
#include "quiz.h"
#include "quizset.h"
#include "random.h"
#include "report.h"
#include "verb.h"
#include "verbset.h"
SubjectSet *AllSubjects;
VerbSet *AllVerbs;
QuizSet *AllQuizzes;
ReportCard *VerbScores;
ReportCard *QuizScores;
Random *Squizzer;
int GotRight;
extern Verb *Aller;
#define SETS 5
#define REPS 5
static void loadObjects();
static void loadScores();
static void displayReport();
static void saveScores();
main()
{
register int i, j;
int erreurs;
int questions;
Quiz *oneQuiz;
int quizClass;
int lastClass;
float percentage;
char *t;
int rc;
/* Initialize globals */
Squizzer = Random_new();
VerbScores = ReportCard_new("VERBSCORES");
QuizScores = ReportCard_new("QUIZSCORES");
AllSubjects = SubjectSet_new();
AllVerbs = VerbSet_new();
AllQuizzes = QuizSet_new();
/* Load up! */
printf("\n");
loadObjects();
loadScores();
displayReport(VerbScores);
/* Present questions */
printf("\n");
lastClass = -1;
questions = 0;
erreurs = 0;
for (j = 0; j < SETS; j++)
{
quizClass = ReportCard_pickClass(QuizScores);
oneQuiz = QuizSet_getOneFromClass(AllQuizzes, quizClass);
t = quizClass == lastClass ? " (encore!)" : "";
printf("\n%s%s:\n\n", Quiz_getName(oneQuiz), t);
for (i = 0; i < REPS; i++)
{
questions++;
rc = Quiz_perform(oneQuiz);
ReportCard_record(QuizScores, quizClass, rc == PERFECT ? 1 : 0);
if (rc < PERFECT)
{
erreurs++;
}
}
lastClass = quizClass;
}
/* Dump out */
saveScores();
/* Calculate and report score */
percentage = ((float )questions - (float )erreurs) / (float )questions;
printf("\nVotre compte est %3.1f par cent. ", percentage * 100.0);
if (percentage > 0.95)
{
printf("Fantastique!\n");
}
else if (percentage > 0.80)
{
printf("Tres bien!\n");
}
else if (percentage > 0.70)
{
printf("Pas tres bien.\n");
}
else if (percentage > 0.55)
{
printf("Pauvre, pauvre.\n");
}
else /* rilly low */
{
printf("Un compte affreux!\n");
}
}
static void
loadScores()
{
FILE *sfp;
char line[128];
char class[32];
int zoneTotal;
sfp = fopen("xscores", "r");
if (!sfp)
{
printf("No score file yet...");
goto out;
}
printf("Loading scores...\n");
fgets(line, 128, sfp);
while (!feof(sfp))
{
if (line[0] == '\n' || line[0] == '#')
{
fgets(line, 128, sfp);
continue;
}
sscanf(line, "%[^:]: (%d)", class, &zoneTotal);
if (0 == strcmp(class, "VERBSCORES"))
{
ReportCard_loadFromFile(VerbScores, sfp, zoneTotal);
}
else if (0 == strcmp(class, "QUIZSCORES"))
{
ReportCard_loadFromFile(QuizScores, sfp, zoneTotal);
}
else
{
fprintf(stderr, "loadScores: unknown: %s\n", class);
exit(1);
}
fgets(line, 128, sfp);
}
fclose(sfp);
out:
return;
}
static void
loadObjects()
{
register int i, j;
char line[128];
char class[32];
char type[128];
char text[128];
Form *conjug;
int contype;
Verb *verb;
Subject *subj;
Quiz *quiz;
FILE *sfp;
int rc;
sfp = fopen("xobjects", "r");
if (!sfp)
{
printf("xobjects file is missing (cannot continue)\n\n");
exit(1);
}
printf("Loading objects...\n");
fgets(line, 128, sfp);
while (!feof(sfp))
{
if (line[0] == '\n' || line[0] == '#')
{
fgets(line, 128, sfp);
continue;
}
sscanf(line, "%[^:]", class);
if (0 == strcmp(class, "VERB"))
{
j = sscanf(line, "%[^:]: %s (%d)", class, text, &contype);
verb = Verb_newFromFile(text, sfp);
if (!verb)
{
fprintf(stderr, "loadObjects: Verb_newFromFile fails\n");
goto out;
}
Verb_setConjugationClass(verb, contype);
ReportCard_enroll(VerbScores, contype);
VerbSet_addOne(AllVerbs, verb);
}
else if (0 == strcmp(class, "SUBJECT"))
{
j = sscanf(line, "%[^:]: %s %[^\n]", class, type, text);
conjug = Form_newFromImage(type);
subj = Subject_new((ExtendString *)text, conjug);
if (!subj)
{
fprintf(stderr, "loadObjects: Subject_new fails\n");
goto out;
}
SubjectSet_addOne(AllSubjects, subj);
}
else if (0 == strcmp(class, "QUIZ"))
{
j = sscanf(line, "%[^:]: <%[^>]> (%d)", class, text, &contype);
quiz = Quiz_newFromFile(text, sfp);
if (!quiz)
{
fprintf(stderr, "loadObjects: Quiz_newFromFile fails\n");
goto out;
}
Quiz_setClass(quiz, contype);
ReportCard_enroll(QuizScores, contype);
QuizSet_addOne(AllQuizzes, quiz);
}
else if (0 == strcmp(class, "END\n"))
{
goto out;
}
else
{
fprintf(stderr, "loadObjects: unknown class: %s\n", class);
goto out;
}
fgets(line, 128, sfp);
#if MDEBUG
if (!malloc_verify())
{
printf("loadObjects: the heap has been corrupted\n");
exit(7);
}
#endif
}
rc = VerbSet_locateRequired(AllVerbs);
if (rc < PERFECT)
{
exit(1);
}
out:
fclose(sfp);
}
static void
saveScores()
{
FILE *dfp;
dfp = fopen("xscores", "w");
if (dfp)
{
printf("\nSaving scores...\n");
ReportCard_printOn(VerbScores, dfp);
ReportCard_printOn(QuizScores, dfp);
}
}
static int compareGrade();
static void
displayReport(report)
ReportCard *report;
{
register int i, j;
int nInList;
long tries, hits;
long totalTries;
long totalMissed;
struct oneGrade **sortList;
struct oneGrade *t;
int missed;
int bar, col;
Verb *example;
ExtendString *text;
int n;
nInList = 0;
totalTries = 0;
totalMissed = 0;
n = Collection_size(report->allGrades);
sortList = (struct oneGrade **)malloc(n * sizeof (struct oneGrade *));
for (i = 0; i < n; i++)
{
t = (struct oneGrade *)Collection_atGet(report->allGrades, i);
if (t)
{
t->class = i;
tries = hits = 0;
for (j = 0; j < NBUCKETS; j++)
{
tries += t->tries[j];
hits += t->hits[j];
}
totalTries += tries;
totalMissed += tries - hits;
sortList[nInList] = t;
nInList++;
}
}
qsort((char *)sortList, nInList, sizeof (struct oneGrade *), compareGrade);
printf("\n");
for (i = 0; i < nInList; i++)
{
t = sortList[i];
tries = hits = 0;
for (j = 0; j < NBUCKETS; j++)
{
tries += t->tries[j];
hits += t->hits[j];
}
missed = tries - hits;
if (missed == 0)
{
break;
}
printf("#%-3d ", t->class);
bar = missed * 25 / tries;
for (n = 0; n < bar; n++)
{
printf("X");
}
col = 10 + bar;
printf(" (%ld/%ld)", hits, tries);
for (n = 0; n < Collection_size(VerbSet_getAll(AllVerbs)); n++)
{
example = (Verb *)Collection_atGet(VerbSet_getAll(AllVerbs), n);
if (Verb_getConjugationClass(example) != t->class)
{
continue;
}
text = Verb_infinitive(example);
if (col > 60)
{
printf("...");
break;
}
printf(" %s", text);
col += strlen(text) + 1;
}
printf("\n");
}
if (totalTries > 0)
{
printf("\n");
printf("Tout ");
bar = totalMissed * 25 / totalTries;
for (n = 0; n < bar; n++)
{
printf("X");
}
printf(" (%ld/%ld)", totalTries - totalMissed, totalTries);
}
}
static int
compareGrade(a, b)
struct oneGrade **a;
struct oneGrade **b;
{
return (*b)->zone - (*a)->zone;
}