Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project1;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- TSetOfChars = set Of Char;
- Function CheckExtension(Path: String): Boolean; forward;
- Function InputNumber(MinNumber, MaxNumber: Integer): Integer;
- var
- IsCorrect: Boolean;
- Number: Integer;
- begin
- repeat
- IsCorrect := true;
- try
- Readln(Number);
- except
- Writeln('You need to write an integer number, which is not less than ',
- MinNumber, ' and more than ', MaxNumber);
- IsCorrect := false;
- end;
- if IsCorrect and ((Number < MinNumber) or (Number > MaxNumber)) then
- begin
- Writeln('You need to write a number, which is not less than ',
- MinNumber, ' and more than ', MaxNumber);
- IsCorrect := false;
- end;
- until IsCorrect;
- InputNumber := Number;
- end;
- Function ChooseWayOfInputOrOutput(): Integer;
- const
- CONSOLE_WAY = 1;
- FILE_WAY = 2;
- var
- UserWay: Integer;
- begin
- Repeat
- UserWay := InputNumber(1, 2);
- Until (UserWay = CONSOLE_WAY) or (UserWay = FILE_WAY);
- ChooseWayOfInputOrOutput := UserWay;
- end;
- Function InputPathToFile(): String;
- var
- Path: String;
- IsCorrect: Boolean;
- NewFile: TextFile;
- begin
- Writeln('Input path to file:');
- repeat
- Readln(Path);
- IsCorrect := CheckExtension(Path);
- until IsCorrect;
- InputPathToFile := Path;
- end;
- Function CheckPermissionForReading(Path: String): Boolean;
- var
- OutputFile: TextFile;
- RightPermission: Boolean;
- begin
- Assign(OutputFile, Path);
- RightPermission := true;
- try
- Reset(OutputFile);
- Close(OutputFile);
- except
- Writeln('File is not available for reading.');
- RightPermission := false;
- end;
- CheckPermissionForReading := RightPermission;
- end;
- Function ReceiveTextFromConsole(): String;
- var
- XStart, XEnd: Integer;
- Text: String;
- begin
- Writeln('Input string: ');
- Readln(Text);
- ReceiveTextFromConsole := Text;
- end;
- Function CheckText(Text: String; RequiredChars: TSetOfChars): Boolean;
- var
- IsCorrect: Boolean;
- EndNumber, i: Integer;
- begin
- isCorrect:= false;
- EndNumber:= Length(Text) + 1;
- i:= 1;
- while (i < EndNumber) and (not IsCorrect) do
- begin;
- if ansichar(Text[i]) in RequiredChars then
- IsCorrect:= true;
- Inc(i);
- end;
- CheckText := IsCorrect;
- end;
- Function ReceiveTextFromFile(Path: String): String;
- var
- InputFile: TextFile;
- Text: String;
- begin;
- Assign(InputFile, Path);
- Reset(InputFile);
- Read(InputFile, Text);
- Close(InputFile);
- ReceiveTextFromFile := Text;
- end;
- Function ReceiveString(RequiredChars: TSetOfChars): String;
- const
- CONSOLE_WAY = 1;
- FILE_WAY = 2;
- var
- Path: String;
- Text: String;
- UserWay: Integer;
- IsTextCorrect: Boolean;
- IsCorrect: Boolean;
- begin;
- repeat
- begin
- Writeln('Choose way of input: '#13#10'Type ''1'' if you want to receive string from console.'#13#10'Type ''2'' if you want to receieve string from file:');
- UserWay := ChooseWayOfInputOrOutput();
- case UserWay of
- CONSOLE_WAY:
- begin
- Text := ReceiveTextFromConsole();
- end;
- FILE_WAY:
- begin
- repeat
- Path := InputPathToFile();
- IsCorrect := CheckPermissionForReading(Path);
- until IsCorrect;
- Text := ReceiveTextFromFile(Path);
- end;
- end;
- IsTextCorrect := CheckText(Text, RequiredChars);
- if not IsTextCorrect then
- Writeln('There are no reqired elements.');
- end;
- until IsTextCorrect;
- ReceiveString := Text;
- end;
- Function ReceiveResultSet(Text: String; RequiredChars: TSetOfChars): TSetOfChars;
- var
- i, EndNumber: Integer;
- ResultNumber: String;
- IsCorrect: Boolean;
- ResultSet: TSetOfChars;
- ElementChar: String;
- Elem: Char;
- IntegerElem: Integer;
- begin
- EndNumber := length(Text);
- ResultSet := [];
- for i := 1 to EndNumber do
- begin
- if (Text[i] in RequiredChars) then
- Include(ResultSet, AnsiChar(Text[i]))
- end;
- ReceiveResultSet := ResultSet;
- end;
- Function CheckExtension(Path: String): Boolean;
- var
- RigthExtension: Boolean;
- begin
- if (ExtractFileExt(Path) = '.txt') then
- RigthExtension := true
- else
- begin
- Writeln('Wrong file extension.');
- RigthExtension := false;
- end;
- CheckExtension := RigthExtension;
- end;
- Function CheckPermissionForWriting(Path: String): Boolean;
- var
- OutputFile: TextFile;
- RightPermission: Boolean;
- begin
- Assign(OutputFile, Path);
- RightPermission := true;
- try
- Rewrite(OutputFile);
- Close(OutputFile);
- except
- Writeln('File is not available for writing.');
- RightPermission := false;
- end;
- CheckPermissionForWriting := RightPermission;
- end;
- Procedure PrintResultToFile(Path: String; ResultSet: TSetOfChars);
- const
- START_INDEX = 40;
- END_INDEX = 125;
- var
- OutputFile: TextFile;
- i: Integer;
- begin
- Assign(OutputFile, Path);
- Rewrite(OutputFile);
- Write(OutputFile, 'RecievedSet: ');
- for i := START_INDEX to END_INDEX do
- if AnsiChar(i) in ResultSet then
- Write(OutputFile, AnsiChar(i), ' ');
- Close(OutputFile);
- end;
- Procedure PrintResultInConsole(ResultSet: TSetOfChars);
- const
- START_INDEX = 40;
- END_INDEX = 125;
- var
- i: Integer;
- begin;
- Write('RecievedSet: ');
- for i := START_INDEX to END_INDEX do
- if AnsiChar(i) in ResultSet then
- Write(AnsiChar(i), ' ');
- end;
- Procedure PrintResult(ResultString: String; ResultSet: TSetOfChars);
- const
- CONSOLE_WAY = 1;
- FILE_WAY = 2;
- var
- UserWay: Integer;
- Path: String;
- IsCorrect: Boolean;
- begin;
- Writeln('Choose way of output: '#13#10'Type ''1'' if you want to print result in console.'#13#10'Type ''2'' if you want to print result in file.');
- UserWay := ChooseWayOfInputOrOutput();
- case UserWay of
- CONSOLE_WAY:
- begin
- PrintResultInConsole(ResultSet);
- end;
- FILE_WAY:
- begin
- repeat
- Path := InputPathToFile;
- IsCorrect := CheckPermissionForWriting(Path);
- until IsCorrect;
- PrintResultToFile(Path, ResultSet);
- end;
- end;
- end;
- Procedure Main();
- var
- RequiredChars, ResultSet: TSetOfChars;
- Text, ResultNumber: String;
- begin
- Writeln('Program receives and prints even numbers, brackets and characters of arithmetic operations from the string you inserted');
- RequiredChars := ['+', '-', '*', '/', '[', ']', '{', '}', '(', ')', '0',
- '2', '4', '6', '8'];
- Text := ReceiveString(RequiredChars);
- ResultSet := ReceiveResultSet(Text, RequiredChars);
- PrintResult(Text, ResultSet);
- Writeln(''#13#10'Program is completed.');
- Readln;
- end;
- begin
- Main();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement