Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Laba3_1;
- Uses
- System.SysUtils;
- Const
- Min = 1;
- Max = 50;
- Procedure Condition();
- Begin
- Writeln('This program converts a string according to the rule: ');
- Writeln('1)If the string length > N, then the program will discard the first characters.');
- Writeln('2)If the string length < N, then the program will add "."');
- Writeln;
- End;
- Function ChoiseInput(): Char;
- Var
- Input: Char;
- IsCorrect: Boolean;
- Begin
- Repeat
- Readln(Input);
- Input := UpCase(Input);
- if ((Input = 'Y') or (Input = 'N')) then
- IsCorrect := True
- else
- Begin
- IsCorrect := False;
- Writeln('Incorrect input. Enter Y(Yes) or N(No): ');
- End;
- Until IsCorrect;
- ChoiseInput := Input;
- End;
- Function InputFromFile(): String;
- Var
- Sentence: String;
- FileName: String;
- IsCorrect: Boolean;
- InputFile: Text;
- begin
- Repeat
- Writeln('Enter the name of the file you want to read from: ');
- Readln(FileName);
- FileName := FileName + '.txt';
- IsCorrect := True;
- Assign(InputFile, FileName);
- {$I-}
- Reset(InputFile);
- {$I+}
- if IOResult <> 0 then
- begin
- Writeln('File ',FileName,' does not exist!');
- IsCorrect := False;
- end
- else
- begin
- Read(InputFile, Sentence);
- if length(Sentence) = 0 then
- begin
- Writeln('File ',FileName,' is empty!');
- IsCorrect := False;
- end;
- if ((Length(Sentence) < Min) or (Length(Sentence) > Max)) then
- Begin
- Writeln('Check the file! String length must be in the interval [',Min,'...',Max,']: ');
- IsCorrect := False;
- End;
- end;
- Until IsCorrect;
- Writeln('Source string: ',Sentence);
- Close(InputFile);
- InputFromFile := Sentence;
- end;
- Function CheckInputString(): String;
- Var
- IsCorrect: Boolean;
- Sentence: String;
- Begin
- Repeat
- Readln(Sentence);
- if ((Length(Sentence) >= Min) and (Length(Sentence) <= Max)) then
- IsCorrect := True
- else
- Begin
- Writeln('Enter string length from interval [',Min,'...',Max,']: ');
- IsCorrect := False;
- End;
- Until IsCorrect;
- CheckInputString := Sentence;
- End;
- Function InputFromConsole(): String;
- Var
- Sentence: String;
- Begin
- Writeln('Please, enter a string: ');
- Sentence := CheckInputString();
- InputFromConsole := Sentence;
- End;
- Function CheckInput(): Integer;
- Var
- N: Integer;
- IsCorrect: Boolean;
- Begin
- IsCorrect := False;
- Repeat
- Try
- Readln(N);
- if ((N >= Min) and (N <= Max)) then
- IsCorrect := True
- else
- Writeln('Enter number from interval [',Min,'...',Max,']: ');
- Except
- Writeln('Check entered data. Enter number from interval [',Min,'...',Max,']: ');
- End;
- Until IsCorrect;
- CheckInput := N;
- End;
- Procedure StringConversion(Var Sentence: String; N: integer);
- Var
- i, Len: integer;
- Begin
- Len := Length(Sentence);
- if ((Len > N) and (Len mod 2 = 0)) then
- Begin
- Sentence := copy (Sentence,len - N + 1,Len - n );
- Writeln ('Final string: ',Sentence);
- End
- else
- if ((Len > N) and (Len mod 2 <> 0)) then
- Begin
- Sentence := copy(Sentence, Len - N + 1, Len - N + 1);
- Writeln ('Final string: ',Sentence);
- End;
- if Len < N then
- Begin
- for i := Len + 1 to N do
- Insert('.',Sentence,1);
- Writeln ('Final string: ',Sentence);
- End;
- if Len = N then
- Writeln ('Final string: ',Sentence);
- End;
- Procedure OutputInFile(Sentence: String);
- Var
- NameOfFile: String;
- OutputFile: Text;
- Begin
- Writeln('Enter the file name for the entry: ');
- Readln(NameOfFile);
- NameOfFile := NameOfFile + '.txt';
- Assign(OutputFile,NameOfFile);
- Rewrite(OutputFile);
- Writeln(OutputFile,'Final string: ');
- Writeln(OutputFile,Sentence);
- Close(OutputFile);
- Writeln('String successfully written to file ',NameOfFile);
- End;
- Procedure Main();
- Var
- Sentence: String;
- N: Integer;
- begin
- Condition();
- Writeln('Would you like to use File input instead of Console input?',
- 'Enter Y(Yes) or N(No): ');
- if ChoiseInput = 'Y' then
- Sentence := InputFromFile()
- else
- Sentence := InputFromConsole();
- Writeln('Enter N: ');
- N := CheckInput();
- StringConversion(Sentence,N);
- Write('Would you like to write down the answer to File? Enter Y(Yes) or N(No): ');
- if ChoiseInput = 'Y' then
- OutputInFile(Sentence);
- Writeln('Press "Enter" to exit.');
- Readln;
- end;
- Begin
- Main();
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement