Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program lab2_4;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- Arr = array of Double;
- Function TakeSource(const Purpose: String): Byte;
- const
- CHOOSING_CONSOLE = 1;
- CHOOSING_FILE = 2;
- var
- Source: Byte;
- IsCorrect: Boolean;
- begin
- Write('Select the source for '+ Purpose + ':' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- Source := 0;
- repeat
- IsCorrect := True;
- try
- Readln(Source);
- except
- Write('Incorrect input!!! Select the source for '+ Purpose + ':' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- IsCorrect := False;
- end;
- if (IsCorrect and (Source <> CHOOSING_CONSOLE) and (Source <> CHOOSING_FILE)) then
- begin
- Write('Incorrect input!!! Select the source for '+ Purpose + ':' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- IsCorrect := False;
- end;
- until IsCorrect;
- TakeSource := Source;
- end;
- Function TakeInPath: String;
- var
- Path: String;
- IsCorrect: Boolean;
- begin
- Write('Enter file path: ');
- repeat
- IsCorrect := True;
- Readln(Path);
- if not FileExists(Path) then
- begin
- Write('File is not found' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end
- else
- if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
- (Path[Length(Path)- 2] <> 't') or (Path[Length(Path)- 3] <> '.')) then
- begin
- Write('File is found, but it is not ".txt" type file' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end;
- until IsCorrect;
- TakeInPath := Path;
- end;
- Function TakeOutPath: String;
- var
- Path: String;
- IsCorrect: Boolean;
- begin
- Write('Enter file path: ');
- repeat
- IsCorrect := True;
- Readln(Path);
- if ((Path[Length(Path)] <> 't') or (Path[Length(Path) - 1] <> 'x') or
- (Path[Length(Path)- 2] <> 't') or (Path[Length(Path) - 3] <> '.')) then
- begin
- Write('It should be a ".txt" type file' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end;
- until IsCorrect;
- TakeOutPath := Path;
- end;
- Function TakeSequenceFromConsole: Arr;
- const
- MIN_SIZE = 2;
- var
- Size, i: Integer;
- IsCorrect: Boolean;
- Sequence: Arr;
- begin
- Write('Enter size: ');
- Size := 0;
- repeat
- IsCorrect := True;
- try
- Readln(Size);
- except
- Write('Incorrect input!!!' + #13#10 + 'Enter size:: ');
- IsCorrect := False;
- end;
- if (IsCorrect and (Size < MIN_SIZE)) then
- begin
- Write('The size must be higher than 1', #13#10, 'Enter size:: ');
- IsCorrect := False;
- end;
- until IsCorrect;
- SetLength(Sequence, Size);
- for i := 0 to (Length(Sequence) - 1) do
- begin
- Write('Enter element ', (i + 1), ': ');
- repeat
- IsCorrect := True;
- try
- Readln(Sequence[i]);
- except
- Write('Incorrect input!!!', #13#10, 'Enter element ', (i + 1), ': ');
- IsCorrect := False;
- end;
- until IsCorrect;
- end;
- TakeSequenceFromConsole := Sequence;
- end;
- Function TakeSequenceFromFile(const Path: String): Arr;
- const
- MIN_SIZE = 2;
- var
- InFile: TextFile;
- Size, i: Integer;
- IsCorrect: Boolean;
- Sequence: Arr;
- begin
- IsCorrect := True;
- AssignFile(InFile, Path);
- Reset(InFile);
- Size := 0;
- try
- Read(InFile,Size);
- except
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- if (IsCorrect and (Size < MIN_SIZE)) then
- begin
- Write('Incorrect file content!!! The size must be higher than 1');
- IsCorrect := False;
- end;
- i := 0;
- if IsCorrect then
- begin
- SetLength(Sequence,Size);
- While (IsCorrect and (i < Size) and (not Eof(InFile))) do
- begin
- try
- Read(InFile, Sequence[i]);
- except
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- i := i + 1;
- end;
- end;
- if (IsCorrect and((i < Size) or ((not Eof(InFile))))) then
- begin
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- if IsCorrect then
- begin
- Writeln('Sequence is: ');
- For i := 0 to (Length(Sequence) - 1) do
- begin
- Write(Sequence[i]:10:7, ';');
- end;
- Writeln('');
- end
- else
- SetLength(Sequence, 0);
- TakeSequenceFromFile := Sequence;
- CloseFile(InFile);
- end;
- Function TakeSequence(Source: Byte): Arr;
- var
- InPath: String;
- Sequence: Arr;
- begin
- if (Source = 1) then
- Sequence := TakeSequenceFromConsole
- else
- begin
- InPath := TakeInPath;
- Sequence := TakeSequenceFromFile(InPath);
- while (Length(Sequence) = 0) do
- begin
- InPath := TakeInPath;
- Sequence := TakeSequenceFromFile(InPath);
- end;
- end;
- TakeSequence := Sequence;
- end;
- Function FindAnswer(Sequence: Arr): String;
- var
- i: Integer;
- IsIncorrect: Boolean;
- begin
- i := 0;
- repeat
- IsInCorrect := Sequence[i] < Sequence[i + 1];
- i := i + 1;
- until IsIncorrect or (i = Length(Sequence) - 1);
- if not IsIncorrect then
- FindAnswer := 'The sequence is non-growing'
- else
- FindAnswer := 'The sequence is not non-growing';
- end;
- Procedure OutputToFile(const Path, Answer: String);
- var
- OutFile: TextFile;
- begin
- AssignFile(OutFile, Path);
- Rewrite(OutFile);
- Writeln(OutFile, Answer);
- CloseFile(OutFile);
- Writeln('Complete');
- end;
- Procedure Output(const Source: Byte; Answer: String);
- var
- OutPath: String;
- begin
- if (Source = 1) then
- Writeln(Answer)
- else
- begin
- OutPath := TakeOutPath;
- OutputToFile(OutPath,Answer);
- end;
- end;
- var
- InputSource, OutputSource: Byte;
- Answer: String;
- Sequence: Arr;
- begin
- Writeln('Welcome to the program that checks is a given sequence of numbers non-growing');
- InputSource := TakeSource('entering the sequence of numbers');
- Sequence := TakeSequence(InputSource);
- Answer := FindAnswer(Sequence);
- OutputSource := TakeSource('output the answer');
- Output(OutputSource, Answer);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement