Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Laba_3_2;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- type
- TInputMode = (WithFile, Console);
- TSet = set of Char;
- resourcestring
- InstructionMessage
- = 'This program composes a set consisting of arithmetic operands and even digits';
- WayOfInputMessage = 'Where do you want to input data from? [F]ile or [C]onsole';
- IncorrectInputFilePathMessage = 'Incorrect input file path, check if file exists and try again';
- IncorrectWayOfInputMessage = 'Incorrect way of input, choose [F]ile or [C]onsole';
- OutputFileExistsErrorMessage = 'Incorrect output file, such file already exists, try again';
- ShouldCreateMessage = 'Do you want to create output file? [Y]es or [N]o';
- ShouldCreateErrorMessage = 'Incorrect answer, choose [Y]es or [N]o';
- EmptySequenceMessage = 'Your sequence doesn''t contain any of assumed symbols';
- OutputMessage = 'Composed set based on your sequence';
- Mask = '-+*/02468';
- function GetInputFilePath: string;
- var
- Path: string;
- begin
- Writeln('Enter input file path');
- Readln(Path);
- while not FileExists(Path) do
- begin
- Writeln(IncorrectInputFilePathMessage);
- Readln(Path);
- end;
- GetInputFilePath := Path;
- end;
- function ShouldCreateOutputFile: Boolean;
- var
- Answer: string;
- begin
- Writeln(ShouldCreateMessage);
- Readln(Answer);
- Answer := LowerCase(Answer);
- while (Answer <> 'y') and (Answer <> 'n') do
- begin
- Writeln(ShouldCreateErrorMessage);
- Readln(Answer);
- end;
- ShouldCreateOutputFile := Answer = 'y';
- end;
- function GetOutputFilePath: string;
- var
- Path: string;
- begin
- Writeln('Enter output file path');
- Readln(Path);
- while FileExists(Path) do
- begin
- Writeln(OutputFileExistsErrorMessage);
- Readln(Path);
- end;
- GetOutputFilePath := Path;
- end;
- function ChooseInputMode(var OutputFilePath: string): TInputMode;
- var
- Mode: string;
- begin
- Writeln(WayOfInputMessage);
- Readln(Mode);
- Mode := LowerCase(Mode);
- while (Mode <> 'c') and (Mode <> 'f') do
- begin
- Writeln(IncorrectWayOfInputMessage);
- Readln(Mode);
- end;
- if ShouldCreateOutputFile() then
- OutputFilePath := GetOutputFilePath()
- else
- OutputFilePath := '';
- if Mode = 'f' then
- ChooseInputMode := WithFile
- else
- ChooseInputMode := Console;
- end;
- function ReadFromFile(InputFilePath: string): string;
- var
- InputFile: TextFile;
- InputString: string;
- begin
- AssignFile(InputFile, InputFilePath);
- Reset(InputFile);
- Readln(InputFile, InputString);
- CloseFile(InputFile);
- ReadFromFile := InputString;
- end;
- function ReadFromConsole(): string;
- var
- InputString: string;
- begin
- Writeln('Input sequence of symbols');
- Readln(InputString);
- ReadFromConsole := InputString;
- end;
- function GetSetFromString(InputString: string): TSet;
- var
- StringLength, i: Integer;
- Sequence: TSet;
- begin
- Sequence := [];
- StringLength := Length(InputString);
- for i := 1 to StringLength do
- if (Pos(InputString[i], Mask) <> 0) and not(InputString[i] in Sequence) then
- Include(Sequence, InputString[i]);
- GetSetFromString := Sequence;
- end;
- procedure PrintSetToConsole(Sequence: TSet);
- var
- i: Integer;
- begin
- if Sequence = [] then
- Writeln(EmptySequenceMessage)
- else
- begin
- Writeln(OutputMessage);
- for i := 0 to 255 do
- if Chr(i) in Sequence then
- Write(Chr(i), ' ');
- end;
- end;
- procedure PrintSetToFile(Sequence: TSet; OutputFilePath: string);
- var
- i: Integer;
- OutputFile: TextFile;
- begin
- AssignFile(OutputFile, OutputFilePath);
- Rewrite(OutputFile);
- if Sequence = [] then
- Writeln(OutputFile, EmptySequenceMessage)
- else
- begin
- Writeln(OutputFile, OutputMessage);
- for i := 0 to 255 do
- if Chr(i) in Sequence then
- Write(OutputFile, Chr(i), ' ');
- end;
- CloseFile(OutputFile);
- end;
- procedure Main;
- var
- InputFilePath, OutputFilePath, InputString: string;
- Sequence: TSet;
- begin
- Writeln(InstructionMessage);
- case ChooseInputMode(OutputFilePath) of
- WithFile:
- begin
- InputFilePath := GetInputFilePath();
- InputString := ReadFromFile(InputFilePath);
- end;
- Console:
- InputString := ReadFromConsole();
- end;
- Sequence := GetSetFromString(InputString);
- if OutputFilePath <> '' then
- PrintSetToFile(Sequence, OutputFilePath);
- PrintSetToConsole(Sequence);
- Readln;
- end;
- begin
- Main();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement