home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / borland / cb / setup / cbuilder / data.z / WPMMAIN.CPP < prev    next >
C/C++ Source or Header  |  1997-02-28  |  3KB  |  81 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1997 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include "wpmmain.h"
  10. #include <ctype.h>
  11. //---------------------------------------------------------------------------
  12. #pragma resource "*.dfm"
  13. TForm1 *Form1;
  14. //---------------------------------------------------------------------------
  15. __fastcall TForm1::TForm1(TComponent* Owner)
  16.   : TForm(Owner)
  17. {
  18. }
  19. //---------------------------------------------------------------------------
  20. void __fastcall TForm1::StartTimer(TObject *Sender, Word &Key,
  21.       TShiftState Shift)
  22. {
  23.     if (RichEdit1->ReadOnly == false)
  24.     {
  25.         Timer1->Enabled = True;
  26.         Label1->Caption = "Test Started";
  27.         Label1->Font->Color = clGreen;
  28.     }
  29. }
  30. //---------------------------------------------------------------------
  31. void __fastcall TForm1::IncrementBar(TObject *Sender)
  32. {
  33.     if (ProgressBar1->Position == 59)
  34.     {
  35.         Timer1->Enabled = False;
  36.         Label1->Caption = "Test Complete";
  37.         Label1->Font->Color = clRed;
  38.         RichEdit1->ReadOnly = True;
  39.         Label2->Caption = IntToStr(CalculateWPM()) + " Words Per Minute";
  40.     }
  41.     ProgressBar1->StepIt();
  42. }
  43. //---------------------------------------------------------------------
  44. void __fastcall TForm1::Button2Click(TObject *Sender)
  45. {
  46.     Application->Terminate();
  47. }
  48. //---------------------------------------------------------------------
  49. void __fastcall TForm1::Button1Click(TObject *Sender)
  50. {
  51.         Timer1->Enabled = False;
  52.         Label1->Caption = "Start typing to begin test";
  53.         Label1->Font->Color = clBlack;
  54.         Label2->Caption = "";
  55.         RichEdit1->SetTextBuf("");
  56.         RichEdit1->ReadOnly = False;
  57.         ProgressBar1->Position = 0;
  58. }
  59. //---------------------------------------------------------------------
  60. int __fastcall TForm1::CalculateWPM(void)
  61. {
  62.     int character=0;            // for iterating through text by character
  63.     int count=0;                // for keeping track of the number of words
  64.     bool betweenwords=true;     // for state information while scanning
  65.  
  66.     while ((RichEdit1->Lines->Text.c_str()[character++] != NULL))
  67.     {
  68.         if (isalnum(RichEdit1->Lines->Text.c_str()[character]))
  69.         {
  70.             if (betweenwords)
  71.             {
  72.                 betweenwords = false;
  73.                 count++;
  74.             }
  75.         } else {
  76.             betweenwords = true;
  77.         }
  78.     }
  79.     return count;
  80. }
  81. //---------------------------------------------------------------------