Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program lab3_2;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils;
- type
- TChar = set of char;
- Function InputFileLocation(): string;
- Var
- IsCorrect: Boolean;
- Location: String;
- Begin
- Repeat
- IsCorrect := false;
- WriteLn('Enter file location:');
- ReadLn(Location);
- If FileExists(Location) then
- IsCorrect := true
- Else
- Begin
- WriteLn('Please enter the correct location');
- WriteLn('');
- End;
- Until IsCorrect;
- InputFileLocation := Location;
- End;
- Function ChooseInput(): integer;
- Var
- Line: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := true;
- WriteLn('Do you want to input from file? (y/n)');
- ReadLn(line);
- Line := Line.ToLower();
- If(Line <> '') and (Line <> 'y') and (Line <> 'n') then
- Begin
- IsCorrect := false;
- WriteLn('Enter valid answer');
- End;
- Until IsCorrect;
- If (Line = '') or (Line = 'y') then
- ChooseInput := 0
- Else
- ChooseInput := 1;
- End;
- Procedure OutputToFile(Str: String);
- var
- TFile: TextFile;
- begin
- AssignFile(TFile, InputFileLocation());
- Rewrite(TFile);
- Write(TFile, Str);
- CloseFile(TFile);
- end;
- Function CheckUnique(Str: String; I: Integer; LetSet: TChar): Boolean;
- Var
- IsNotUnique: Boolean;
- Begin
- isNotUnique := false;
- If Str[I] in LetSet then
- isNotUnique := true
- else
- isNotUnique := false;
- CheckUnique := isNotUnique;
- End;
- Procedure ChangeSentence(var Str: String);
- var
- Temp, I: Integer;
- IsNotUnique: Boolean;
- LetSet: TChar;
- Begin
- LetSet := [];
- I := 1;
- Temp := High(Str);
- while I <= Temp do
- Begin
- isNotUnique := true;
- IsNotUnique := CheckUnique(Str, I, LetSet);
- if IsNotUnique then
- Begin
- Delete(Str, I, 1);
- Dec(Temp);
- End
- Else
- Begin
- LetSet := LetSet + [Str[I]];
- Inc(I);
- End;
- End;
- End;
- Function GetLineFromConsole(): String;
- var
- IsCorrect: Boolean;
- Str: String;
- begin
- repeat
- IsCorrect := true;
- ReadLn(Str);
- if Length(Str) = 0 then
- begin
- WriteLn('The line is empty, please, try to enter it one more time');
- IsCorrect := false;
- end;
- until IsCorrect;
- GetLineFromConsole := Str;
- end;
- Procedure GetLineFromFile(Str: String);
- var IsCorrect: Boolean; TFile: TextFile;
- begin
- repeat
- IsCorrect := true;
- AssignFile(TFile, InputFileLocation());
- Reset(TFile);
- ReadLn(TFile, Str);
- CloseFile(TFile);
- if (Length(Str) = 0) then
- begin
- WriteLn('String is empty. Enter another file location');
- IsCorrect := false;
- end;
- until IsCorrect;
- end;
- Procedure Main();
- Var
- ChosenInput: Integer;
- Str: String;
- Size, I : Integer;
- isNotUnique : Boolean;
- LetSet : TChar;
- Begin
- WriteLn('Программа удаляет одинаковые буквы в предложении.');
- ChosenInput := ChooseInput();
- If (ChosenInput = 0) then
- GetLineFromFile(Str)
- Else
- Begin
- writeln('Введите строку');
- Str := GetLineFromConsole();
- End;
- ChangeSentence(Str);
- writeln('Modified string');
- writeln(Str);
- WriteLn;
- OutputToFile(Str);
- ReadLn;
- End;
- Begin
- Main();
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement