Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program LabWork31;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- type
- TCharSet = set of Char;
- const
- LenModOrDiv = 2; // because 5-3 = 2 +1 = 3 (+1 because we include third index)
- function UserChooce(): Boolean;
- var
- IsCorrect: Boolean;
- Choice: Char;
- begin
- IsCorrect := False;
- repeat
- Write('Y/N: ');
- Readln(Choice);
- Choice := UpCase(Choice);
- case Ord(Choice) of
- Ord('Y'):
- begin
- IsCorrect := True;
- UserChooce := True;
- end;
- Ord('N'):
- begin
- IsCorrect := True;
- UserChooce := False;
- end
- else
- Writeln('You made an incorrect choice, please, try again clearly following the instructions.');
- end;
- until IsCorrect;
- end;
- function CorrectFormatOfTheFile(FileName: String): String;
- const
- FormatLen = 4;
- begin
- if AnsiCompareStr(copy(FileName, length(FileName) - FormatLen, FormatLen), '.txt') <> 0 then
- FileName := FileName + '.txt';
- CorrectFormatOfTheFile := FileName;
- end;
- function InputFileName(): 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);
- FileName := CorrectFormatOfTheFile(FileName);
- if FileExists(FileName) then
- begin
- IsCorrect := True;
- InputFileName := FileName;
- end
- else
- Writeln('File with the same name does not exist, please, enter name of the file again.');
- until IsCorrect;
- end;
- function EnterStringFromFile(): String;
- var
- Input: TextFile;
- UserString: String;
- begin
- AssignFile(Input, InputFileName);
- Reset(Input);
- while seekEOF(Input) do
- begin
- Writeln('Your file haven''t strings.');
- Writeln;
- AssignFile(Input, InputFileName);
- Reset(Input);
- end;
- Readln(Input, UserString);
- Close(Input);
- EnterStringFromFile := UserString;
- end;
- function EnterStringFromConsole(): String;
- var
- UserString: String;
- begin
- Write('Please, enter your string: ');
- Readln(UserString);
- EnterStringFromConsole := UserString;
- end;
- function ModOrDiv(const UserString: String; const Index: Integer): Boolean;
- var
- StringLen: Integer;
- Assumption: String;
- IsCorrectAssumption: Boolean;
- begin
- IsCorrectAssumption := False;
- StringLen := length(UserString);
- if StringLen - Index >= LenModOrDiv then
- Assumption := copy(UserString, Index, LenModOrDiv + 1);
- if (AnsiCompareStr(Assumption, 'mod') = 0)or(AnsiCompareStr(Assumption, 'div') = 0) then
- IsCorrectAssumption := True;
- ModOrDiv := IsCorrectAssumption;
- end;
- function CreateSet(const UserString: String): TCharSet;
- var
- Symbol: Char;
- Symbols: TCharSet;
- StringIndex, StringLen: Integer;
- const
- SetNumbers = ['0', '2', '4', '6', '8'];
- OperationsAndParentheses =
- ['(', ')', '{', '}', '[', ']', '+', '-', '*', '/'];
- begin
- Symbols := [];
- StringLen := Length(UserString);
- StringIndex := 1;
- while StringIndex <= StringLen do
- begin
- Symbol := UserString[StringIndex];
- if not (Symbol in Symbols) then
- if Symbol in OperationsAndParentheses then
- Include(Symbols, Symbol)
- else
- if Symbol in SetNumbers then
- Include(Symbols, Symbol)
- else
- if ModOrDiv(UserString, StringIndex) then
- begin
- Include(Symbols, UpCase(Symbol));
- StringIndex := StringIndex + LenModOrDiv; // +2 because we inc(stringIndex) in next line
- end;
- inc(StringIndex);
- end;
- CreateSet := Symbols;
- end;
- function SetToStr(const Symbols: TCharSet): String;
- var
- CurrentChar: Char;
- StringFromSet: String;
- i: Integer;
- begin
- StringFromSet := '';
- for i := 0 to 255 do
- begin
- CurrentChar := Chr(i);
- if CurrentChar in Symbols then
- case Ord(CurrentChar) of
- Ord('M'):
- StringFromSet := StringFromSet + 'mod ';
- Ord('D'):
- StringFromSet := StringFromSet + 'div ';
- else
- StringFromSet := StringFromSet + CurrentChar + ' ';
- end;
- end;
- SetToStr := StringFromSet;
- end;
- procedure OutputInf(const Symbols: TCharSet);
- var
- Output: TextFile;
- FileName, StringFromSet: String;
- begin
- Writeln;
- StringFromSet := SetToStr(Symbols);
- if AnsiCompareStr(StringFromSet, '') = 0 then
- Writeln('Your string haven''t symbols which can be append to set')
- else
- begin
- Writeln('Your set: ', StringFromSet);
- Writeln('Do you want to output data to a file?');
- if UserChooce() then
- begin
- Writeln('Please, enter name of the file.');
- Writeln('Example: Text.txt');
- Readln(FileName);
- FileName := CorrectFormatOfTheFile(FileName);
- AssignFile(Output, FileName);
- try
- if FileExists(FileName) then
- begin
- Write('A file with this name already exists, ');
- Writeln('do you want to append information(otherwise rewrite)?');
- if UserChooce() then
- Append(Output)
- else
- Rewrite(Output);
- end
- else
- Rewrite(Output);
- except
- Writeln('Error writing to file.');
- end;
- Write(Output, 'Set from your string: ', StringFromSet);
- CloseFile(Output);
- Writeln('Write was successfully.');
- end;
- end;
- end;
- procedure Main();
- var
- UserString: String;
- begin
- Writeln('Hello, this program extracts integers from a string.');
- repeat
- Writeln('Do you want to enter data through a file? (otherwise via console).');
- if UserChooce then
- begin
- UserString := EnterStringFromFile();
- Writeln('Your string: ', UserString);
- end
- else
- UserString := EnterStringFromConsole();
- UserString := StringReplace(UserString, ' ', '', [rfReplaceAll]);
- if UserString = '' then
- Writeln('Your string is empty.')
- else
- OutputInf(CreateSet(UserString));
- Writeln('Do you want enter another string?');
- until not UserChooce();
- Writeln('Press enter to exit...');
- Readln;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement