Advertisement
green1ant

1_1

Sep 24th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.95 KB | None | 0 0
  1. program Project2;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. uses
  7.    System.SysUtils;
  8.  
  9. const
  10.    Amount = 5;
  11.    ErrorMessage = 'Error! Enter name (nonempty string, starts with uppercase letter)';
  12.  
  13.    //function that validates the name
  14. function IsName(Name: string) : Boolean;
  15. var
  16.    NameLength, i, j, AlphabetSize : Integer;
  17.    Letters : string;
  18.    HasSuchLetter : Boolean;
  19.    Alphabet : set of 'a'..'z';
  20. begin
  21.    Alphabet := ['a'..'z'];
  22.    NameLength := Length(Name);
  23.    Letters := 'qwertyuiopasdfghjklzxcvbnm';
  24.    AlphabetSize := Length(Letters);
  25.    IsName := True;
  26.  
  27.    if Name <> '' then
  28.       if Name[1] <> LowerCase(name[1]) then  //if first letter is capital
  29.          for i := 2 to NameLength do //start with 2nd - first is checked by previous condition
  30.          begin
  31.             if not (Name[i] in Alphabet) then
  32.             begin
  33.                //writeln(Name[i], ' is absent in the alphabet');
  34.                isName := False;
  35.             end;
  36.  
  37.             {
  38.             HasSuchLetter := False;
  39.             for j := 1 to AlphabetSize do
  40.             begin
  41.                if Name[i] = Letters[j] then
  42.                begin
  43.                   HasSuchLetter := True;
  44.                end;
  45.             end;
  46.  
  47.             if not HasSuchLetter then
  48.                IsName := False;
  49.             }
  50.          end
  51.       else
  52.          IsName := False
  53.    else
  54.       IsName := False;
  55. end;
  56.  
  57. var
  58.    MyName : string;
  59.    i, Counter, Index : Integer;
  60.    HasStarted, IsInvalid : Boolean;
  61.    List: array [0 .. 4] of string = ('Artem', 'Maxim', 'Arseniy', 'Vasya', 'Merlin');
  62.  
  63. begin
  64.    Counter := 0;
  65.    Index := -1;
  66.    HasStarted := False;
  67.    IsInvalid := True;
  68.  
  69.    Writeln('This program can recognize and greet one of 5 friends');
  70.  
  71.    repeat
  72.       Counter := 0;
  73.       Index := -1;
  74.  
  75.       // validation if name
  76.       while IsInvalid do
  77.       begin
  78.          try
  79.             if not HasStarted then
  80.                Writeln('Enter name:')
  81.             else
  82.                Writeln('Please, clarify the name:');
  83.             Readln(MyName);
  84.             if (not IsName(MyName)) then
  85.                raise Exception.Create('');
  86.             IsInvalid := False;
  87.          except
  88.             Writeln(ErrorMessage);
  89.                  
  90.          end;
  91.       end;
  92.       // validation end
  93.  
  94.       //on next iteration of repeat-until validation will start again
  95.       IsInvalid := True;
  96.  
  97.       for i := 0 to Amount - 1 do
  98.       begin
  99.          if Pos(MyName, List[i]) = 1 then
  100.          begin
  101.             Counter := Counter + 1; // счетчик совпадений
  102.             Index := i;
  103.             // assign last match to index
  104.          end
  105.       end;
  106.  
  107.       Writeln('Matches found: ', Counter);
  108.       if Counter = 0 then
  109.          Counter := 1;
  110.       // ask to 'enter name' ONCE
  111.       HasStarted := True;
  112.    until Counter = 1;
  113.  
  114.    if Index = -1 then
  115.       Writeln('I DON''T KNOW YOU!')
  116.    else
  117.       Writeln(List[Index], ', nice to meet you!');
  118.    Readln;
  119. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement