home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / apps / educatin / typtutor / typtutor.c < prev    next >
C/C++ Source or Header  |  1990-01-23  |  5KB  |  213 lines

  1. #include <aesbind.h>
  2. #include <osbind.h>
  3. #include <gemdefs.h>
  4. #include <bios.h>
  5. #include <stdio.h>
  6. #include <strings.h>
  7.  
  8. char prefix[128],sep[]="\\",suffix[16]="*.TYP",
  9.      filename[128],name[16];
  10. char *cp;
  11.  
  12. char *text,line[80],clrscr[]="\33E\33e\33q\n",cursoff[]="\33f";
  13.  
  14. char title[]="     \33p Bob Areddy's Typing Tutor, v. 2.1  (c) 1990   Genesis Software \33q\n";
  15. char instr[]="                           please select file for test\n";
  16. int drv,result,button,fhandle,num=0,lnum,counter,counter2;
  17. int tot_char,event,key,junk1,junk2,flag,flag2,errorflag;
  18. long *ptr,time1,time2,wpm,best_wpm=0,best_wpm_er=0,
  19.     sec,error,keystrokes;
  20.  
  21. main()
  22. {
  23.     init();                        /* do initialization         */
  24.     for(;;)
  25.     {
  26.         setup();                    /* get text file, etc.        */
  27.         get_input();                /* start test                */
  28.         prnt_result();                /* give the result            */
  29.     }
  30. }    
  31.  
  32. init()
  33. {
  34.     appl_init();                /* initialize an application*/
  35.     text=(char *)Malloc(5000L); /* reserve space for text   */
  36.     if (text==NULL)
  37.     {
  38.         form_alert(1,"[1][Not enough memory!][Quit]");
  39.         quit();
  40.     }
  41. }
  42.  
  43. setup()
  44. {    
  45.     num=0;
  46.     keystrokes=0;
  47.     flag=0;
  48.     button=1;
  49.     
  50.     Cconws(clrscr);
  51.     printf("%s",title);
  52.     printf("%s",instr);
  53.     Cconws(cursoff);
  54.     strncpy(filename,"\0",128);
  55.     strncpy(name,"\0",16);
  56.     strncpy(text,"\0",5000);
  57.     
  58.     cp=prefix;                    /* build path for directory    */
  59.     drv=Dgetdrv();                /* get current drive        */
  60.     *cp++=Dgetdrv()+'A';        /* change into ASCII        */
  61.     *cp++=':';                    /* add a ':' into string    */
  62.     Dgetpath(cp,drv+1);            /* get name of curr direct    */
  63.     
  64.     strcpy(filename,prefix);    /* build string             */
  65.     strcat(filename,sep);        
  66.     strcat(filename,suffix);
  67.     while(name[0]=='\0' && button==1)
  68.         result=fsel_input(filename,name,&button);
  69.         
  70.     graf_mouse(M_OFF,0);
  71.     if (button==0)
  72.         {
  73.         quit();                /* if cancel, quit pgm         */
  74.         return;
  75.         }
  76.     fhandle=Fopen(name,0);    /* open selected file         */
  77.     tot_char=Fread(fhandle,        /* read all the data        */
  78.             5000L,text);
  79.     Fclose(fhandle);            /* close the file            */
  80.         
  81.     error=0;                    /* reset value for errors   */
  82.     counter2=0;                    /* and for total keys typed */
  83. }
  84.     
  85. printline()
  86. {
  87.     Cconws(clrscr);                /* clear screen                */
  88.     Cconws("\33b3");
  89.     for(num=0;num<tot_char;num++)
  90.     {
  91.         if (text[num]==13)
  92.             {
  93.                 printf("\n");
  94.                 num++;
  95.             }
  96.         
  97.         Cconout(text[num]);
  98.     }
  99. }
  100.  
  101. gettime()
  102. {
  103.     *ptr = (*((long *)0x4ba));    /* get value from 200Hz clock */
  104. }
  105.  
  106. get_input()
  107. {
  108.     counter=0;
  109.     counter2=0;
  110.     errorflag=1;
  111.     
  112.     printline();            /* print the text */
  113.     
  114.     num=0;
  115.     Cconws("\33H\33B\33B\33b1");
  116.  
  117.     key=(evnt_keybd());
  118.     ptr=&time1;
  119.     Supexec(gettime);        /* start timer */
  120.     flag2=1;
  121.  
  122.     while(text[num]!=0)
  123.     {
  124.         if (flag2==0)
  125.             key=(evnt_keybd());    /* get key */
  126.         
  127.         flag2=0;
  128.  
  129.         if (key==24832)        /* was UNDO pressed?        */
  130.         {
  131.             flag=1;
  132.             ptr=&time2;
  133.             Supexec(gettime);
  134.             return;        /* yes, so quit                */
  135.         }
  136.         
  137.         else
  138.             key &= 0xff; /* no, so get rid of scan code */
  139.  
  140.         if ((key>32 && key<127) ||
  141.          (counter!=0 && (key>31 && key<127)))
  142.             {
  143.             if (key!=text[num])
  144.                 {
  145.                 Cconws("\33p");    /* change color of letter         */
  146.                 error+=errorflag;            /* count the error    */
  147.                 Cconout(key);
  148.                 Cconws("\33D\7");
  149.                 errorflag=0;
  150.                 }
  151.             else
  152.             {
  153.                 errorflag=1;
  154.                 Cconws("\33q");        /* normal character color        */
  155.                 Cconout(key);        /* print the letter                */
  156.                 num++;
  157.                    counter++;            /* counter for current line        */
  158.                 counter2++;            /* total keys pressed            */
  159.                 keystrokes++;
  160.             }
  161.             
  162.             if (text[num]==13)
  163.             {
  164.                 num+=2;
  165.                 counter2++;
  166.                 counter=0;
  167.                 printf("\n\n");
  168.             }
  169.         }
  170.     }
  171.     ptr=&time2;
  172.     Supexec(gettime);            /* get the ending time            */
  173.     
  174. }
  175.  
  176. prnt_result()
  177. {
  178.     Cconws(clrscr);                /* clear the screen                */
  179.     printf("\33b3Total number of words:  %ld\n",keystrokes/5);
  180.     printf("Total number of errors: %ld\n",error);
  181.     sec=(time2-time1)/200;
  182.     printf("Total time (seconds):   %ld\n\n",sec);
  183.     wpm=(sec>0) ? (keystrokes*60/5/sec) : 0;
  184.     if (flag==0)
  185.     {
  186.         best_wpm=(wpm>best_wpm) ? wpm : best_wpm;
  187.         best_wpm_er=((wpm-error)>best_wpm_er) ? wpm-error : best_wpm_er;
  188.     }
  189.     printf("Words per minute:       %ld\n",wpm);
  190.     printf("Best words per minute:  %ld\n\n",best_wpm);
  191.     
  192.     printf("Words per minute with\n");
  193.     printf("1 wpm penalty/error     %ld\n\n",wpm-error);
  194.     
  195.     printf("Best WPM with penalty:  %ld\n\n",best_wpm_er);
  196.  
  197.     printf("\nAny key to run test again.  RETURN to exit\n");
  198.     
  199.     key=evnt_keybd() & 0xFF;
  200.     
  201.     if (key==13)
  202.         quit();
  203.     graf_mouse(M_ON,0);
  204. }
  205.  
  206. quit()
  207. {
  208.     Mfree(text);        /* free allocated memory        */
  209.     appl_exit();
  210.     printf("\33f\n");    /* turn cursor off                */
  211.     exit(0);            /* quit                            */
  212. }    
  213.