home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / edu / DITOdev.lha / DITOdev / Library / Examples / TEACH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-23  |  2.5 KB  |  156 lines

  1. /*
  2.  *   Teach © Dirk Holtwick, 1996
  3.  *
  4.  *   Small teaching tool to demonstrates, how
  5.  *   it works.
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/dito.h>
  16.  
  17. #define SUMKNOWN(data) (data->KnownWeight[0]+data->KnownWeight[1]+data->KnownWeight[2])
  18. #define SUMASKED(data) (data->AskedWeight[0]+data->AskedWeight[1]+data->AskedWeight[2])
  19.  
  20. struct Library *DitoBase;
  21.  
  22. struct DITO_Database *data;
  23. struct DITO_Entry    *entry;
  24.  
  25. /*
  26. ** Easy routine for error handling
  27. */
  28.  
  29. void HandleDITOError(int error)
  30. {
  31.     switch(error)
  32.     {
  33.         case DITO_Error_FileNotFound:
  34.             puts("File not found!");
  35.             break;
  36.         case DITO_Error_NoDitoDatabase:
  37.             puts("No DITO Database!");
  38.             break;
  39.         case DITO_Error_WrongLanguage:
  40.             puts("Wrong Language!");
  41.             break;
  42.         case DITO_Error_Memory:
  43.             puts("Not enough memory!");
  44.             break;
  45.     }
  46. }
  47.  
  48. /*
  49. ** Teach(), return quote of answers
  50. */
  51.  
  52. LONG Teach(void){
  53.     struct DITO_Entry *entry;
  54.     char answ[2];
  55.  
  56.     /*
  57.     ** Lets start from the beginning, but this is not
  58.     ** obligatory.
  59.     */
  60.  
  61.     DITO_TeachInit(data);
  62.  
  63.     /*
  64.     ** The "question loop"
  65.     */
  66.  
  67.     do
  68.     {
  69.         /*
  70.         ** Get pointer to question
  71.         */
  72.  
  73.         entry = DITO_TeachRandom(data, 0);
  74.  
  75.         /*
  76.         ** Ask.
  77.         */
  78.  
  79.         printf("\fKNOWN WORDS: %d%\n\n", (SUMKNOWN(data) * 100 / data->sum));
  80.  
  81.         printf("What means '%s'? Say it loud!\n", DITO_GetStr(entry, DITO_Str_Word));
  82.  
  83.         gets(answ);
  84.  
  85.         /*
  86.         ** Show answer
  87.         */
  88.  
  89.         printf("It means '%s'. Did you know that (y/n)? ", DITO_GetStr(entry, DITO_Str_Translation));
  90.  
  91.         gets(answ);
  92.  
  93.         /*
  94.         ** Set Known-Flag and get information, if we reached the end.
  95.         */
  96.     }
  97.     while(!DITO_TeachKnown(data, (tolower(answ[0]) == 'y')));
  98.  
  99.     /*
  100.     ** Calculate quote.
  101.     */
  102.  
  103.     return(SUMKNOWN(data) * 100 / SUMASKED(data));
  104. }
  105.  
  106.  
  107. /*
  108. ** Main program
  109. */
  110.  
  111.  
  112. main(int argc, char *argv[])
  113. {
  114.  
  115.     if(argc==2)
  116.     {
  117.         if(DitoBase = OpenLibrary(DITO_NAME, DITO_VMIN))
  118.         {
  119.  
  120.             /*
  121.             ** You have to create a database before adding or
  122.             ** loading entries to the structure. Do not try to
  123.             ** init the structure by yourself!
  124.             */
  125.  
  126.             if(data = DITO_CreateDatabase())
  127.             {
  128.  
  129.                 /*
  130.                 ** You may now add entries to the database, e.g.
  131.                 ** with DITO_LoadDatabase()
  132.                 */
  133.  
  134.                 HandleDITOError(DITO_LoadDatabase(data,argv[1]));
  135.  
  136.                 /*
  137.                 ** Jump to Teach();
  138.                 */
  139.  
  140.                 printf("\fYou have reached a quote of %d%.\n", Teach());
  141.  
  142.                 /*
  143.                 ** Do not forget to dispose the DITO_Database!
  144.                 */
  145.  
  146.                 DITO_DisposeDatabase(data);
  147.             }
  148.  
  149.             CloseLibrary(DitoBase);
  150.  
  151.         }else puts("Couldn't open actual version of dito.library.");
  152.  
  153.     }else puts("USAGE: Teach [file.voc]");
  154. }
  155.  
  156.