Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program lab3_2;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- const
- SetOfDigitsAndOperations = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', ':', '^', '√'];
- type
- TCharSet = set of Char;
- Function TakeSize: Integer;
- const
- MIN_SIZE = 2;
- MAX_SIZE = 30;
- var
- IsCorrect: Boolean;
- Number: Integer;
- begin
- Number := 0;
- repeat
- IsCorrect := True;
- try
- Readln(Number);
- except
- Writeln('Incorrect input!!!');
- IsCorrect := False;
- end;
- if (IsCorrect and (Number < MIN_SIZE) or (Number > MAX_SIZE)) then
- begin
- Writeln('Incorrect input!!!');
- IsCorrect := False;
- end;
- until IsCorrect;
- TakeSize := Number;
- end;
- Function TakeChar: AnsiChar;
- var
- IsCorrect: Boolean;
- InputChar: AnsiChar;
- begin
- repeat
- IsCorrect := True;
- try
- Readln(InputChar);
- except
- Writeln('Incorrect input!!!');
- IsCorrect := False;
- end;
- until IsCorrect;
- TakeChar := InputChar;
- end;
- Function TakeSource: Byte;
- const
- CHOOSING_CONSOLE = 1;
- CHOOSING_FILE = 2;
- var
- Source: Byte;
- IsCorrect: Boolean;
- begin
- Source := 0;
- repeat
- IsCorrect := True;
- try
- Readln(Source);
- except
- Write('Incorrect input!!! Select the source:' + #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:' + #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;
- InFile: TextFile;
- 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
- else
- begin
- Assign(InFile, Path);
- try
- Reset(InFile);
- except
- Write('Error!!! Program can''t open file!' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end;
- end;
- until IsCorrect;
- CloseFile(InFile);
- TakeInPath := Path;
- end;
- Function TakeOutPath: String;
- var
- Path: String;
- IsCorrect: Boolean;
- OutFile: TextFile;
- 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
- else
- begin
- AssignFile(OutFile, Path);
- try
- Rewrite(OutFile);
- except
- Write('Error!!! There are problems with file!' + #13#10 + 'Enter file path: ');
- IsCorrect := False;
- end;
- end;
- until IsCorrect;
- CloseFile(OutFile);
- TakeOutPath := Path;
- end;
- Function TakeSetFromConsole: TCharSet;
- var
- Size, i: Integer;
- CharSet: TCharSet;
- InputChar: AnsiChar;
- begin
- Write('Enter size: ');
- Size := TakeSize;
- for i := 0 to (Size - 1) do
- begin
- Write('Enter element ', (i + 1), ': ');
- InputChar := TakeChar;
- Include(CharSet, InputChar);
- end;
- TakeSetFromConsole := CharSet;
- end;
- Function TakeSetFromFile(const Path: String): TCharSet;
- const
- MIN_SIZE = 2;
- var
- InFile: TextFile;
- Sign: AnsiChar;
- IsCorrect: Boolean;
- CharSet: TCharSet;
- begin
- IsCorrect := True;
- AssignFile(InFile, Path);
- Reset(InFile);
- while(IsCorrect and (not Eof(InFile))) do
- begin
- try
- Read(InFile, Sign);
- Include(CharSet, Sign);
- except
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- end;
- if (IsCorrect and (not Eof(InFile))) then
- begin
- Writeln('Incorrect file content!!!');
- IsCorrect := False;
- end;
- if not IsCorrect then
- CharSet := [];
- CloseFile(InFile);
- TakeSetFromFile := CharSet;
- end;
- Function TakeSet(Source: Byte): TCharSet;
- var
- InPath: String;
- CharSet: TCharSet;
- begin
- if (Source = 1) then
- CharSet := TakeSetFromConsole
- else
- begin
- InPath := TakeInPath;
- CharSet := TakeSetFromFile(InPath);
- while (CharSet = []) do
- begin
- InPath := TakeInPath;
- CharSet := TakeSetFromFile(InPath);
- end;
- end;
- TakeSet := CharSet;
- end;
- Function FindAnswers(CharSet: TCharSet): TCharSet;
- var
- Answers: TCharSet;
- begin
- Answers := CharSet * SetOfDigitsAndOperations;
- FindAnswers := Answers;
- end;
- Procedure OutputToConsole(Answers: TCharSet);
- var
- Element: AnsiChar;
- begin
- for Element in Answers do
- Write(Element, ' ');
- end;
- Procedure OutputToFile(const Path: String; Answers: TCharSet);
- var
- OutFile: TextFile;
- Element: AnsiChar;
- begin
- AssignFile(OutFile, Path);
- Rewrite(OutFile);
- for Element in Answers do
- Write(OutFile, Element, ' ');
- CloseFile(OutFile);
- Writeln('Done');
- end;
- Procedure Output(const Source: Byte; Answers: TCharSet);
- var
- OutPath: String;
- begin
- if (Source = 1) then
- OutputToConsole(Answers)
- else
- begin
- OutPath := TakeOutPath;
- OutputToFile(OutPath,Answers);
- end;
- end;
- var
- InputSource, OutputSource: Byte;
- InputSet: TCharSet;
- Answers: TCharSet;
- begin
- Write('Welcome to the program that will print a set, whose elements are signs of arithmetic operations and digits from a given set' + #13#10+ 'Select the source for entering the array:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- InputSource := TakeSource;
- InputSet := TakeSet(InputSource);
- Answers := FindAnswers(InputSet);
- Write('Select the source for output:' + #13#10 + '1:Console' + #13#10 + '2:File' + #13#10 + 'Enter 1 or 2: ');
- OutputSource := TakeSource;
- Output(OutputSource, Answers);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement