Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project1;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- Function CheckExtension(Path: String): Boolean; forward;
- Function InputNumber(MinNumber, MaxNumber: Integer): Integer;
- var
- IsCorrect: Boolean;
- Number: Integer;
- begin
- repeat
- IsCorrect := true;
- try
- Readln(Number);
- except
- Writeln('Нужно ввести целое число, которое не меньше ', MinNumber,
- ' и не больше ', MaxNumber);
- IsCorrect := false;
- end;
- if IsCorrect and ((Number < MinNumber) or (Number > MaxNumber)) then
- begin
- Writeln('Нужно ввести целое число, которое не меньше ', MinNumber,
- ' и не больше ', MaxNumber);
- IsCorrect := false;
- end;
- until IsCorrect;
- InputNumber := Number;
- end;
- Function ChooseWayOfInputOrOutput(): Integer;
- const
- CONSOLE_WAY = 1;
- FILE_WAY = 2;
- var
- UserWay: Integer;
- begin
- Repeat
- UserWay := InputNumber(1, 2);
- Until (UserWay = CONSOLE_WAY) or (UserWay = FILE_WAY);
- ChooseWayOfInputOrOutput := UserWay;
- end;
- Function InputPathToFile(): String;
- var
- Path: String;
- IsCorrect: Boolean;
- NewFile: TextFile;
- begin
- Writeln('Введите путь к файлу:');
- repeat
- Readln(Path);
- IsCorrect := CheckExtension(Path);
- until IsCorrect;
- InputPathToFile := Path;
- end;
- Function CheckPermissionForReading(Path: String): Boolean;
- var
- OutputFile: TextFile;
- RightPermission: Boolean;
- begin
- Assign(OutputFile, Path);
- RightPermission := true;
- try
- Reset(OutputFile);
- Close(OutputFile);
- except
- Writeln('Файл закрыт для чтения.');
- RightPermission := false;
- end;
- CheckPermissionForReading := RightPermission;
- end;
- Function ReceiveTextFromConsole(): String;
- var
- XStart, XEnd: Integer;
- Text: String;
- begin
- Writeln('Введите строку:');
- Readln(Text);
- ReceiveTextFromConsole := Text;
- end;
- Function ReceiveTextFromFile(Path: String): String;
- var
- InputFile: TextFile;
- Text: String;
- IsCorrect: Boolean;
- begin;
- IsCorrect := true;
- Assign(InputFile, Path);
- Reset(InputFile);
- Read(InputFile, Text);
- Close(InputFile);
- ReceiveTextFromFile := Text;
- end;
- Function FindPlusOrMinus(Text: String): Integer;
- var
- i: Integer;
- NumberOfElement: Integer;
- IsCorrect: Boolean;
- begin
- NumberOfELement := 0;
- IsCorrect := True;
- i := 1;
- while (i < length(Text)) and IsCorrect do
- begin
- if (Text[i] = '+') or (Text[i] = '-') then
- begin
- NumberOfElement := i;
- IsCorrect := False;
- end;
- Inc(i);
- end;
- FindPlusOrMinus := NumberOfElement;
- end;
- Function CheckText(Text: String): Boolean;
- var
- IsCorrect, IsStringCorrect: Boolean;
- i, Index, EndNumber: Integer;
- begin
- IsCorrect := True;
- Index := FindPlusOrMinus(Text);
- if Index = 0 then
- IsStringCorrect := false
- else
- begin
- IsStringCorrect := true;
- Inc(Index);
- i := Index;
- EndNumber:= length(Text) + 1;
- while (i < EndNumber) and IsCorrect do
- begin
- if (Ord(Text[i]) > 47) and (Ord(Text[i]) < 58) then
- begin
- IsCorrect := false;
- IsStringCorrect := true;
- end
- else
- IsStringCorrect := false;
- Inc(i);
- end;
- end;
- CheckText := IsStringCorrect;
- end;
- Function ReceiveString(): String;
- const
- CONSOLE_WAY = 1;
- FILE_WAY = 2;
- var
- Path: String;
- Text: String;
- UserWay: Integer;
- IsTextCorrect: Boolean;
- IsCorrect: Boolean;
- begin;
- repeat
- begin
- Writeln('Выберите способ ввода:'#13#10'Нажмите "1", если хотите ввести число с консоли.'#13#10'Нажмите "2", если хотите считать строку из файла.');
- UserWay := ChooseWayOfInputOrOutput();
- case UserWay of
- CONSOLE_WAY:
- begin
- Text := ReceiveTextFromConsole();
- end;
- FILE_WAY:
- begin
- repeat
- Path := InputPathToFile();
- IsCorrect:= CheckPermissionForReading(Path);
- until IsCorrect;
- Text := ReceiveTextFromFile(Path);
- end;
- end;
- IsTextCorrect := CheckText(Text);
- if not IsTextCorrect then
- Writeln('Нет знака или последующих за ним цифр.');
- end;
- until IsTextCorrect;
- ReceiveString := Text;
- end;
- Function ReceiveNumber(Text: String): String;
- var
- i, EndNumber: Integer;
- ResultNumber: String;
- IsCorrect : Boolean;
- begin
- IsCorrect := False;
- i := FindPlusOrMinus(Text);
- ResultNumber := Text[i];
- Inc(i);
- EndNumber := Length(Text) + 1;
- while i < EndNumber do
- begin
- if (Ord(Text[i]) > 47) and (Ord(Text[i]) <58) then
- begin
- ResultNumber := ResultNumber + Copy(Text, I, 1);
- end;
- Inc(i);
- end;
- ReceiveNumber := ResultNumber;
- end;
- Function CheckExtension(Path: String): Boolean;
- var
- RigthExtension: Boolean;
- begin
- if (ExtractFileExt(Path) = '.txt') then
- RigthExtension := true
- else
- begin
- Writeln('Неверное расширение файла.');
- RigthExtension := false;
- end;
- CheckExtension := RigthExtension;
- end;
- Function CheckPermissionForWriting(Path: String): Boolean;
- var
- OutputFile: TextFile;
- RightPermission: Boolean;
- begin
- Assign(OutputFile, Path);
- RightPermission := true;
- try
- Rewrite(OutputFile);
- Close(OutputFile);
- except
- Writeln('Файл закрыт для записи.');
- RightPermission := false;
- end;
- CheckPermissionForWriting := RightPermission;
- end;
- Procedure PrintResultToFile(Path: String; ResultString: String);
- var
- OutputFile: TextFile;
- begin
- Assign(OutputFile, Path);
- Rewrite(OutputFile);
- Writeln(OutputFile,'Полученное число: ', ResultString);
- Close(OutputFile);
- end;
- Procedure PrintResultInConsole(ResultString: String);
- begin;
- Writeln('Полученное число: ', ResultString);
- end;
- Procedure PrintResult(ResultString: String);
- const
- CONSOLE_WAY = 1;
- FILE_WAY = 2;
- var
- UserWay: Integer;
- Path: String;
- IsCorrect: Boolean;
- begin;
- Writeln('Выберите способ вывода:'#13#10'Нажмите "1", если хотите вывести число в консоль.'#13#10'Нажмите "2", если хотите вывести число в файл.');
- UserWay := ChooseWayOfInputOrOutput();
- case UserWay of
- CONSOLE_WAY:
- begin
- PrintResultInConsole(ResultString);
- end;
- FILE_WAY:
- begin
- repeat
- Path := InputPathToFile;
- IsCorrect := CheckPermissionForWriting(Path);
- until IsCorrect;
- PrintResultToFile(Path, ResultString);
- end;
- end;
- end;
- Procedure Main();
- var
- Text, ResultNumber: String;
- MaxFunctionValue: Double;
- begin
- Writeln('Программа выделяет из строки подстроку, которая соответствует целому числу, начинающемуся с ''+'' или ''-''.');
- Text := ReceiveString();
- ResultNumber := ReceiveNumber(Text);
- PrintResult(ResultNumber);
- Writeln('Программа завершена.');
- Readln;
- end;
- begin
- Main();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement