Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project2;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- const
- Amount = 5;
- ErrorMessage = 'Error! Enter name (nonempty string, starts with uppercase letter)';
- //function that validates the name
- function IsName(Name: string) : Boolean;
- var
- NameLength, i, j, AlphabetSize : Integer;
- Letters : string;
- HasSuchLetter : Boolean;
- Alphabet : set of 'a'..'z';
- begin
- Alphabet := ['a'..'z'];
- NameLength := Length(Name);
- Letters := 'qwertyuiopasdfghjklzxcvbnm';
- AlphabetSize := Length(Letters);
- IsName := True;
- if Name <> '' then
- if Name[1] <> LowerCase(name[1]) then //if first letter is capital
- for i := 2 to NameLength do //start with 2nd - first is checked by previous condition
- begin
- if not (Name[i] in Alphabet) then
- begin
- //writeln(Name[i], ' is absent in the alphabet');
- isName := False;
- end;
- {
- HasSuchLetter := False;
- for j := 1 to AlphabetSize do
- begin
- if Name[i] = Letters[j] then
- begin
- HasSuchLetter := True;
- end;
- end;
- if not HasSuchLetter then
- IsName := False;
- }
- end
- else
- IsName := False
- else
- IsName := False;
- end;
- var
- MyName : string;
- i, Counter, Index : Integer;
- HasStarted, IsInvalid : Boolean;
- List: array [0 .. 4] of string = ('Artem', 'Maxim', 'Arseniy', 'Vasya', 'Merlin');
- begin
- Counter := 0;
- Index := -1;
- HasStarted := False;
- IsInvalid := True;
- Writeln('This program can recognize and greet one of 5 friends');
- repeat
- Counter := 0;
- Index := -1;
- // validation if name
- while IsInvalid do
- begin
- try
- if not HasStarted then
- Writeln('Enter name:')
- else
- Writeln('Please, clarify the name:');
- Readln(MyName);
- if (not IsName(MyName)) then
- raise Exception.Create('');
- IsInvalid := False;
- except
- Writeln(ErrorMessage);
- end;
- end;
- // validation end
- //on next iteration of repeat-until validation will start again
- IsInvalid := True;
- for i := 0 to Amount - 1 do
- begin
- if Pos(MyName, List[i]) = 1 then
- begin
- Counter := Counter + 1; // счетчик совпадений
- Index := i;
- // assign last match to index
- end
- end;
- Writeln('Matches found: ', Counter);
- if Counter = 0 then
- Counter := 1;
- // ask to 'enter name' ONCE
- HasStarted := True;
- until Counter = 1;
- if Index = -1 then
- Writeln('I DON''T KNOW YOU!')
- else
- Writeln(List[Index], ', nice to meet you!');
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement