Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program LabWork31;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- type
- TStringArray = array of String;
- function UserChoise(): Char;
- var
- IsCorrect: Boolean;
- Choise: Char;
- begin
- IsCorrect := False;
- repeat
- Write('Y/N: ');
- Readln(Choise);
- Choise := UpCase(Choise);
- if (Choise = 'Y') or (Choise = 'N') then
- begin
- IsCorrect := True;
- UserChoise := Choise;
- end
- else
- Writeln('You made an incorrect choice, please, try again clearly following the instructions');
- until IsCorrect;
- end;
- function InputName(): String;
- var
- FileName: String;
- IsCorrect: Boolean;
- begin
- Writeln('Please, enter name of the file or way to the file.');
- Writeln('Example: F:\Programming\Lab\3_1\Delphi\Input.txt');
- IsCorrect := False;
- repeat
- Readln(FileName);
- if FileExists(FileName) then
- begin
- IsCorrect := True;
- InputName := FileName;
- end
- else
- Writeln('File with the same name does not exist, please, enter name of the file again');
- until IsCorrect;
- end;
- function EnterStringFromFile(FileName: String): String;
- var
- Input: TextFile;
- UserString: String;
- begin
- AssignFile(Input, FileName);
- Reset(Input);
- Readln(Input, UserString);
- EnterStringFromFile := UserString;
- end;
- function CountTheDimension(UserString: String): Integer;
- var
- StringIndex, Iteration : Integer;
- Dimension: Integer;
- NumberLikeString: set of Char;
- begin
- NumberLikeString := ['0'..'9'];
- Iteration := length(UserString) - 1;
- StringIndex := 1;
- Dimension := 0;
- while StringIndex < Iteration do
- begin
- if (UserString[StringIndex] = '+') or (UserString[StringIndex] = '-') then
- if UserString[StringIndex + 1] in NumberLikeString then
- Dimension := Dimension + 1;
- inc(StringIndex);
- end;
- CountTheDimension := Dimension;
- end;
- function FindIntegers(UserString: String): TStringArray;
- var
- IntegerLikeString: String;
- NumbersFromString: TStringArray;
- StringIndex, StringLen, ArrayIndex, Dimension: Integer;
- NumberLikeString : set of Char;
- begin
- NumberLikeString := ['0'..'9'];
- StringLen := Length(UserString);
- Dimension := CountTheDimension(UserString);
- SetLength(NumbersFromString, Dimension);
- if Dimension <> 0 then
- begin
- StringIndex := 1;
- ArrayIndex := 0;
- while StringIndex < StringLen do
- begin
- IntegerLikeString := '';
- if (UserString[StringIndex] = '+') or (UserString[StringIndex] = '-') then
- begin
- if UserString[StringIndex] = '-' then
- IntegerLikeString := '-';
- inc(StringIndex);
- while UserString[StringIndex] in NumberLikeString do
- begin
- IntegerLikeString := IntegerLikeString + UserString[StringIndex];
- if StringIndex <= StringLen then
- inc(StringIndex);
- end;
- if (IntegerLikeString <> '-') and (IntegerLikeString <> '') then
- begin
- NumbersFromString[ArrayIndex] := IntegerLikeString;
- inc(ArrayIndex);
- end;
- end
- else
- inc(StringIndex);
- end;
- end;
- FindIntegers := NumbersFromString;
- end;
- procedure OutputInf(NumbersFromString: TStringArray);
- var
- Output: TextFile;
- FileName: String;
- Iteration, Index: Integer;
- OutputToFile, AppendOrRewrite: Char;
- begin
- if Length(NumbersFromString) <> 0 then
- begin
- Writeln('There are ', length(NumbersFromString), ' integers in your string.');
- Iteration := Length(NumbersFromString) - 1;
- for Index := 0 to Iteration do
- Writeln(NumbersFromString[Index]);
- Writeln('Do you want to output data to a file?');
- OutputToFile := UserChoise();
- if OutputToFile = 'Y' then
- begin
- Writeln('Please, enter name of the file');
- Readln(FileName);
- AssignFile(Output, FileName);
- if FileExists(FileName) then
- begin
- Write('A file with this name already exists, ');
- Writeln('do you want to append information');
- AppendOrRewrite := UserChoise();
- if AppendOrRewrite = 'Y' then
- Append(Output)
- else
- Rewrite(Output);
- end
- else
- Rewrite(Output);
- Writeln(Output, 'There are ', length(NumbersFromString), ' integers in your string.');
- for Index := 0 to Iteration do
- Writeln(Output, NumbersFromString[Index]);
- CloseFile(Output);
- Writeln('Write was successfully');
- end;
- end
- else
- Writeln('Your string have not integers.')
- end;
- procedure Main();
- var
- ChoiseInput, RepeatProgram: Char;
- FileName, UserString: String;
- NumbersFromString: TStringArray;
- begin
- Writeln('Hello, this program extracts integers from a string');
- repeat
- Writeln('Do you want to enter data through a file? (otherwise via console)');
- ChoiseInput := UserChoise;
- case Ord(ChoiseInput) of
- Ord('Y'):
- begin
- FileName := InputName();
- UserString := EnterStringFromFile(FileName);
- end;
- Ord('N'):
- begin
- Writeln('Please, enter your string');
- Readln(UserString);
- end;
- end;
- UserString := Trim(UserString);
- if UserString = '' then
- Writeln('Your string is empty')
- else
- begin
- NumbersFromString := FindIntegers(userString);
- OutputInf(NumbersFromString);
- end;
- Writeln('Do you want enter another string?');
- RepeatProgram := UserChoise();
- until (RepeatProgram = 'N');
- Writeln('Press enter to exit');
- Readln;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement