Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Lab3task1;
- {$APPTYPE CONSOLE}
- Uses
- SysUtils;
- 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
- IsCorrect: Boolean;
- Str: String;
- I: Integer;
- Begin
- Repeat
- Writeln ('Input the string consisting of numbers, letters of the English alphabet and symbols "-", "+", "." ');
- IsCorrect := True;
- Readln(Str);
- For I:=1 To Length(Str) Do
- Case Ord(Str[I]) Of
- 1..42, 44, 47, 58..64, 91..96, 123..255: IsCorrect := False;
- End;
- If Not(IsCorrect) Then
- Writeln('Incorrect string format');
- Until (IsCorrect);
- 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;
- I: Integer;
- 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;
- If (IsCorrect) Then
- Begin
- For I:=1 To Length(Str) Do
- Case Ord(Str[I]) Of
- 1..42, 44, 47, 58..64, 91..96, 123..255: IsCorrect := False;
- End;
- If Not(IsCorrect) Then
- Writeln('Incorrect string format');
- 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, Ñ:\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;
- Procedure FindConsoleSubStr(Str: String);
- Var
- SubStr: String;
- I: Integer;
- IsFound: Boolean;
- Begin
- IsFound := False;
- For I := 1 to Length(Str) Do
- Case Ord(Str[I]) Of
- 43, 45: Begin
- If (Length(SubStr)>1) Then
- Writeln(Substr)
- Else
- If (Str[I-1]='0') Then
- Writeln('0');
- SubStr := ''+Str[I];
- IsFound := True;
- End;
- 46, 65..90, 97..122: Begin
- IsFound := False;
- If (Length(SubStr)>1) Then
- Writeln (SubStr)
- Else
- If (Str[I-1]='0') Then
- Writeln('0');
- SubStr := '';
- End;
- 48: If (IsFound) And Not((Substr[Length(Substr)]='+') Or (Substr[Length(Substr)]='-')) Then
- SubStr:= SubStr+Str[I];
- 49..57: If (IsFound) Then
- SubStr:= SubStr+Str[I];
- End;
- If (IsFound) And (Length(SubStr)>1) Then
- Writeln (SubStr);
- End;
- Procedure FindFileSubStr(Str: String);
- Var
- SubStr, Path: String;
- I: Integer;
- IsFound: Boolean;
- OutputFile: TextFile;
- Begin
- Path:=CheckOutputFilePath();
- AssignFile (OutputFile, Path);
- Rewrite(OutputFile);
- IsFound := False;
- For I := 1 to Length(Str) Do
- Case Ord(Str[I]) Of
- 43, 45: Begin
- If (Length(SubStr)>1) Then
- Writeln (OutputFile, SubStr)
- Else
- If (Str[I-1]='0') Then
- Writeln(OutputFile, '0');
- SubStr := ''+Str[I];
- IsFound := True;
- End;
- 46, 65..90, 97..122: Begin
- IsFound := False;
- If (Length(SubStr)>1) Then
- Writeln (OutputFile, SubStr)
- Else
- If (Str[I-1]='0') Then
- Writeln(OutputFile, '0');
- SubStr := '';
- End;
- 48: If (IsFound) And Not((Substr[Length(Substr)]='+') Or (Substr[Length(Substr)]='-')) Then
- SubStr:= SubStr+Str[I];
- 49..57: If (IsFound) Then
- SubStr:= SubStr+Str[I];
- End;
- If (IsFound) And (Length(SubStr)>1) Then
- Writeln (OutputFile, SubStr);
- Close(OutputFile);
- Writeln('Successful output');
- End;
- Procedure OutputChoice (Str: String);
- Var
- Choice: Integer;
- Begin
- Writeln('Choose output option: ', #10, '1.Output through console', #10,
- '2.Output through file');
- Choice := ChoiceCheck();
- If (Choice = 1) Then
- FindConsoleSubStr(Str)
- Else
- FindFileSubStr(Str);
- End;
- Var
- Str: String;
- Begin
- Str := InputChoice();
- OutputChoice(Str);
- Readln
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement