unit Question3_U;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, StdCtrls, ExtCtrls, ComCtrls;

type
  TfrmQuest3 = class(TForm)
    redOutput: TRichEdit;
    pnlOptions: TPanel;
    btnA: TButton;
    btnB: TButton;
    BitBtn1: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure btnAClick(Sender: TObject);
    procedure btnBClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmQuest3: TfrmQuest3;
  arrInfo  : array[1..12] of String;
implementation



{$R *.dfm}

procedure TfrmQuest3.FormCreate(Sender: TObject);
begin
  arrInfo[1]:= 'HDRDBRHRHDdRHHDDrhbRR';
  arrInfo[2]:= 'RrBHDrBrRHBBDRHDdBRRhb';
  arrInfo[3]:= 'BBRRRbDBRrBBHHRBBB';
  arrInfo[4]:= 'RBBRBRdBRDBDRRRBbRRRRbBDHHH';
  arrInfo[5]:= 'RHDBBBHRBB';
  arrInfo[6]:= 'RbrBrBDdRrBb';
  arrInfo[7]:= 'bRRbhbdB';
  arrInfo[8]:= 'hbB';
  arrInfo[9]:= 'BHRrB';
  arrInfo[10]:= 'HhbrbBHhRrdrbBDRRRBB';
  arrInfo[11]:= 'BBBbrrRRdrHRBbBRdDBRBDRrBBDRdB';
  arrInfo[12]:= 'BRBrHRBBRrhHHrrRBDbBDbRRrRRBBDRBDDRHhHD';

  redOutput.Paragraph.TabCount := 3;
  redOutput.Paragraph.Tab[0] := 1;
  redOutput.Paragraph.Tab[1] := 30;
  redOutput.Paragraph.Tab[2] := 300;

end;

procedure TfrmQuest3.btnAClick(Sender: TObject);
var
  K, M, iCount, iTotalRob, iTotalCrime  :integer;
  sNewString, sMonth    :String;
  rPercentage :real;
begin
  redOutput.Lines.Add('Month' + #9 + 'Statistics' + #9 + 'Armed Robberies');
  iTotalRob := 0;
  iTotalCrime := 0;
  for K := 1 to 12 do
    begin
      iCount := 0;
      sNewString := IntToStr(K)+ #9;
      for M := 1 to length(arrInfo[K]) do
        begin
        case  upcase(arrInfo[K][M]) of
        'B', 'D', 'H' : sNewString := sNewString + lowercase(arrInfo[K][M]);
        'R':begin
              sNewString := sNewString + upcase( arrInfo[K][M]);
              iCount := iCount + 1;
            end;
        end;

          {Option 2
          if arrInfo[K][M] in ['B', 'D','H'] then
             sNewString := sNewString + lowercase(arrInfo[K][M])
          else
            if arrInfo[K][M] in ['b', 'd','h'] then
             sNewString := sNewString + arrInfo[K][M]
           else
            if  arrInfo[K][M] in ['R', 'r'] then
            begin
             sNewString := sNewString + upcase( arrInfo[K][M]);
             iCount := iCount + 1;
            end; }
        end;
        iTotalRob := iTotalRob + iCount;
        iTotalCrime := iTotalCrime + length(arrInfo[K]);
        sNewString :=  #9 + sNewString + #9 + intToStr(iCount);
        redOutput.Lines.Add(sNewString);
     end;
     rPercentage := iTotalRob / iTotalCrime * 100;
     redOutput.Lines.Add(' ');
     redOutput.Lines.Add('The percentage armed robberies: ' + floatToStrf(rPercentage, ffFixed, 0, 1));
end;

procedure TfrmQuest3.btnBClick(Sender: TObject);
var
  iLength, K, iCount, iFound, iMonth, iTheMonth  :integer;
  sString, sTheString :string;
  bFound :boolean;
  sCrime :string;
begin
  sString := 'b';
  bFound := false;
  iCount := 1;
  iFound := 1;

  while ((bFound = false) and (iFound <> 0)) do
  begin
   iMonth := 1;
   while (bFound = false) and (iMonth <=12) do
   begin
    iFound := pos(Uppercase(sString), Uppercase(arrInfo[iMonth]));
    if (iFound > 0) then
    begin
      bFound := true;
      iTheMonth := iMonth;
      sTheString := sString;
     end
    else iMonth := iMonth + 1;
   end;   // inner while
  if (bFound = true) then
    begin
      bFound := false;
      sString := sString + 'b';
    end;
end;  // outer while
redOutput.Clear;
redOutput.Lines.Add('Crime: Burglaries');
redOutput.Lines.Add('The month with the most consecutive occurences: ' + intToStr(iTheMonth));
redOutput.Lines.Add('The number of consecutive occurences during this month: ' +   intToStr(length(sTheString)));


end;

end.
