/* +------------------------------------------------------------------------+
   |                                                                        |
   |                 hwcorrect.c : correction automatique de copies         |
   |                                                                        |
   +------------------------------------------------------------------------+ */

/* M. Quercia, le 20/09/98 */
/* compilation : gcc -O2 -o hwcorrect hwcorrect.c -lm */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <time.h>

/* paramètres statistiques */
long  s0 = 0;
float s1 = 0;
float s2 = 0;
float min = 0;
float max = 20;


/* autres paramètres */
char *scanner = "/dev/scanner";
char *printer = "/dev/lp0";    
char *color   = "red";         
char *matter  = "maths";


                      /* +----------------------------+
                         |  Affiche les statistiques  |
                         +----------------------------+ */


void affiche_stats() {

float avg,var;

    printf("homeworks processed : %d\n",s0);
    if (s0 > 0) {
      avg = s1/s0;
      var = s2/s0 - avg*avg;
      if (var <= 0) var = 0;
      printf("average             : %f\n",avg);
      printf("standard deviation  : %f\n",sqrt(var));
    }
    exit(0);
}


                          /* +---------------------+
                             |  Attend une touche  |
                             +---------------------+ */


void ready() {

char ans[5];

  printf(" and hit ENTER when ready.");
  fgets(ans,5,stdin);
  if (strcmp(ans,"stop") == 0) affiche_stats();

}


                               /* +----------+
                                  |  Hasard  |
                                  +----------+ */


float hasard(float a, float b) {

  return(a + (b-a)*((float)random())/RAND_MAX);

}

                         /* +-----------------------+
                            |  Lecture d'une copie  |
                            +-----------------------+ */



void lit_copie() {

float d,n;

  printf("Enter new homework in %s",scanner); ready();
  if (hasard(0,10) < 1) {
    printf("Oops, the sheet seems to be upside-down ! Check the orientation");
    ready();
    printf("Ah, That's better ! ");
  }
  if (hasard(0,10) < 1) {
    printf("fatal error : division by zero on first page.\n");
    exit(1);
  }

  printf("Correcting ");
  d = hasard(1,10);
  while (d > 0) {printf("."); fflush(stdout); sleep(1); d -= 1;}
  n = hasard(min,max);
  printf("\nThis is worth %f. Put homework in %s",n,printer); ready();
  if (hasard(1,10) < 5) {
    printf("Ooh, there is no more %s ink. Replace cadridge",color); ready();
  }

  s0++;
  s1 += n;
  s2 += n*n;
}

                            /* +-----------------+
                               |  Mode d'emploi  |
                               +-----------------+ */


void usage() {

  printf("hwcorrect [-m matter] [-i input-device] [-o printer]
         [-color color] [-range a:b]

defaults : matter = maths
           input-device = /dev/scanner
           printer      = /dev/lp0
           color        = red
           range        = 0:20

bugs : random crash if there is a division by zero on first page.
");
  exit(0);
}

                         /* +-----------------------+
                            |  Programme principal  |
                            +-----------------------+ */


void main(int argc, char **argv) {

  argv++; argc--;
  while (argc > 1) {
    if      (strcmp(argv[0],"-m") == 0)     {matter  = argv[1];}
    else if (strcmp(argv[0],"-i") == 0)     {scanner = argv[1];}
    else if (strcmp(argv[0],"-o") == 0)     {printer = argv[1];}
    else if (strcmp(argv[0],"-color") == 0) {color   = argv[1];}
    else if (strcmp(argv[0],"-range") == 0) {sscanf(argv[1],"%f:%f",&min,&max);}
    else {printf("unrecognized keyword : %s\n",argv[0]); usage();}
    argv += 2;
    argc -= 2;
  }
  if (argc > 0) {printf("bad syntax.\n"); usage();}

  printf("Beginning %s correction. Enter \"stop\" to end the job.\n",matter);
  srandom(time(NULL));
  while(1) lit_copie();

}

