Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Lab3task2;
- {$APPTYPE CONSOLE}
- Uses
- SysUtils;
- Type
- TSet = Set Of Char;
- Function ChoiceCheck() : Integer;
- Var
- Choice: Integer;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect:=True;
- Try
- Readln(Choice);
- Except
- Writeln('Error! Input a number');
- IsCorrect := False;
- End;
- If (IsCorrect And ((Choice < 1) Or (Choice > 2))) Then
- Begin
- Writeln('Error! Input 1 or 2');
- IsCorrect := False;
- End;
- Until (IsCorrect);
- ChoiceCheck := Choice;
- End;
- Function InputCheck(): String;
- Var
- Str: String;
- Begin
- Writeln ('Input a string');
- Readln(Str);
- InputCheck := Str;
- End;
- Function CheckInputFilePath(): String;
- Var
- Path: String;
- IsCorrect: Boolean;
- InputFile: File;
- Begin
- Repeat
- Writeln('Input path to the file');
- IsCorrect := True;
- Readln(Path);
- If (Not(FileExists(Path))) Then
- Begin
- IsCorrect := False;
- Writeln('Could not find the file');
- End;
- If (IsCorrect) Then
- Begin
- Try
- AssignFile(InputFile, Path);
- ReSet(InputFile);
- Except
- IsCorrect := False;
- Writeln('Could not open the file');
- End;
- End;
- Until (IsCorrect);
- CloseFile(InputFile);
- CheckInputFilePath := Path;
- End;
- Function FileCheckString() : String;
- Var
- InputFile: TextFile;
- Path, Str: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- Path := CheckInputFilePath();
- IsCorrect := True;
- AssignFile(InputFile, Path);
- Reset(Inputfile);
- Read(InputFile, Str);
- If (IsCorrect And (Length(Str) < 1)) Then
- Begin
- IsCorrect := False;
- Writeln ('The string is empty');
- End;
- If ((IsCorrect) And Not(EoF(InputFile))) Then
- Begin
- IsCorrect := False;
- Writeln ('There should be only one string in the file');
- End;
- Until (IsCorrect);
- FileCheckString := Str;
- End;
- Function InputChoice (): String;
- Var
- Str: String;
- Choice: Integer;
- Begin
- Writeln('Choose input option: ', #10, '1.Input from console', #10,
- '2.Input from file');
- Choice := ChoiceCheck();
- If (Choice = 1) Then
- Str := InputCheck()
- Else
- Str:= FileCheckString();
- InputChoice := Str;
- End;
- Function CheckOutputFilePath(): String;
- Var
- Path: String;
- IsCorrect: Boolean;
- OutputFile: File;
- Begin
- Writeln('Input file path and the name of the file for', #10, 'example, C:\Projects\Number\FileName.txt. If the ', #10, 'file does not exist, then it will be created', #10,'automatically in the root folder of the program');
- IsCorrect := True;
- Readln(Path);
- If (Not(FileExists(Path))) Then
- Begin
- IsCorrect := False;
- Writeln('Could not find the file');
- End;
- If (IsCorrect)Then
- Try
- AssignFile(OutputFile, Path);
- ReSet(OutputFile);
- Except
- IsCorrect := False;
- Writeln('Could not open the file');
- End;
- If (Not(IsCorrect)) Then
- Begin
- Writeln ('File will be created in the root folder of the program');
- Path := 'Result.txt';
- End
- Else
- CloseFile(OutputFile);
- CheckOutputFilePath := Path;
- End;
- Function CreateSet(S: String; Set0: TSet): TSet;
- Var
- Set1: Set of Char;
- I: Integer;
- Begin
- Set1 := [];
- For I := 1 To Length(S) Do
- If (S[I] In Set0) Then
- Begin
- Include (Set1, S[I]);
- Exclude (Set0, S[I]);
- End;
- CreateSet := Set1;
- End;
- Procedure OutputConsoleSet(Set1: TSet);
- Var
- I: Integer;
- Begin
- Writeln ('Here is the created set:');
- For I:= 32 To 255 Do
- If (Chr(I) In Set1) Then
- Writeln(Chr(I));
- End;
- Procedure OutputFileSet(Set1: TSet);
- Var
- I: Integer;
- OutputFile: TextFile;
- Path: String;
- Begin
- Path:=CheckOutputFilePath();
- AssignFile (OutputFile, Path);
- Rewrite(OutputFile);
- For I:= 32 To 255 Do
- If (Chr(I) In Set1) Then
- Writeln(OutputFile, Chr(I));
- Close(OutputFile);
- Writeln('Successful output');
- End;
- Procedure OutputChoice (Set1: TSet);
- Var
- Choice: Integer;
- Begin
- Writeln('Choose output option: ', #10, '1.Output through console', #10,
- '2.Output through file');
- Choice := ChoiceCheck();
- If (Choice = 1) Then
- OutputConsoleSet(Set1)
- Else
- OutputFileSet(Set1);
- End;
- Var
- Set0, Set1: Set Of Char;
- S: String;
- Begin
- Set0 := ['+', '-', '*', '/', '2', '4', '6', '8', '0', '(', ')', '[', ']', '{', '}'];
- S := InputChoice();
- Set1 := CreateSet(S, Set0);
- OutputChoice(Set1);
- Readln
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement