CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 06:14:44 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=c61d2fc7886fd73f4b02daef0c2dab59c32381b7334281fc1ee5607f074ceadda%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%220uLNeBc62lfAy8GR8plDSNP7g9XOZxRL%22%3B%7D; HttpOnly; Path=/
cf-ray: 98cc380dabd10bc9-BLR
Lab2Challenge2Delphi - Pastebin.com
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Lab2Challenge2;
- {$APPTYPE CONSOLE}
- {$R *.res}
- Uses
- System.SysUtils;
- Const
- FRIEND_NUMBERS_FIND_MAX = 32000;
- FRIEND_ARRAY_SIZE = 100;
- PRINT_TYPE_MIN = 0;
- PRINT_TYPE_MAX = 2;
- DEFAULT_INPUT_FILE = './input.txt';
- DEFAULT_OUTPUT_FILE = './output.txt';
- Type
- TFriends = Array[0..1] Of Array[0..FRIEND_ARRAY_SIZE] Of Integer;
- Function IsTextFile(Const FilePath: String): Boolean;
- Var
- IsTxt: Boolean;
- Len: Integer;
- Begin
- Len := Length(FilePath);
- IsTxt := ((Len > 2) And (FilePath[Len - 3] = '.') And (FilePath[Len - 2] = 't') And (FilePath[Len - 1] = 'x') And
- (FilePath[Len] = 't'));
- IsTextFile := IsTxt;
- End;
- Function CheckFileAvailability(Const FilePath: String; Read: Boolean): Boolean;
- Var
- IsAvailable: Boolean;
- Checkable: TextFile;
- Begin
- IsAvailable := True;
- AssignFile(Checkable, FilePath);
- Try
- If (Read) Then
- Reset(Checkable)
- Else
- Begin
- If (FileExists(FilePath)) Then
- Append(Checkable)
- Else
- Rewrite(Checkable);
- End;
- Close(Checkable);
- Except
- IsAvailable := False;
- End;
- If (IsAvailable And Not IsTextFile(FilePath)) Then
- IsAvailable := False;
- CheckFileAvailability := IsAvailable;
- End;
- Function TakeIntValueFromConsole(Const Description: String): Integer;
- Var
- Value: Integer;
- IsCorrect: Boolean;
- Begin
- IsCorrect := False;
- Value := 0;
- Repeat
- Write(Description);
- Try
- Readln(Value);
- IsCorrect := True;
- Except
- Writeln('Enter number, not string or anything else!');
- End;
- Until IsCorrect;
- TakeIntValueFromConsole := Value;
- End;
- Function TakeIntValueInRangeFromConsole(Const Description: String; Min: Integer; Max: Integer): Integer;
- Var
- Value: Integer;
- IsCorrect: Boolean;
- Begin
- Repeat
- Value := TakeIntValueFromConsole(Description);
- IsCorrect := True;
- If ((Value < Min) Or (Value > Max)) Then
- Begin
- Writeln('Value must be in range from ', Min, ' to ', Max, '!');
- IsCorrect := False;
- End;
- Until IsCorrect;
- TakeIntValueInRangeFromConsole := Value;
- End;
- Procedure TakeCorrectFile(Var FileToAssign: TextFile; Input: Boolean);
- Var
- FilePath: String;
- IsCorrect: Boolean;
- Begin
- Repeat
- Var
- DefaultFilePath := DEFAULT_OUTPUT_FILE;
- If (Input) Then
- Begin
- Write('Enter path to input file (when empty - ', DEFAULT_INPUT_FILE, '): ');
- DefaultFilePath := DEFAULT_INPUT_FILE;
- End
- Else
- Write('Enter path to output file (when empty - ', DEFAULT_OUTPUT_FILE, '): ');
- ReadLn(FilePath);
- If (FilePath = '') Then
- FilePath := DefaultFilePath;
- IsCorrect := True;
- If (Not CheckFileAvailability(FilePath, Input)) Then
- Begin
- IsCorrect := False;
- WriteLn('This path contains wrong file or file, which cannot be accessed! Enter another path!');
- End;
- Until IsCorrect;
- AssignFile(FileToAssign, FilePath);
- If (Input) Then
- Reset(FileToAssign)
- Else
- Rewrite(FileToAssign);
- End;
- Function GetSumOfDivers(Num: Integer): Integer;
- Var
- Sum, I: Integer;
- Begin
- Sum := 0;
- For I := 1 To (Num - 1) Do
- Begin
- if (Num Mod I = 0) Then
- Sum := Sum + I;
- End;
- GetSumOfDivers := Sum;
- End;
- Function Calculate(Var Friends: TFriends): Integer;
- Var
- I, J, PotFriend: Integer;
- Begin
- J := 0;
- For I := 2 To FRIEND_NUMBERS_FIND_MAX Do
- Begin
- PotFriend := GetSumOfDivers(I);
- If (I <> PotFriend) And (I = GetSumOfDivers(PotFriend)) Then
- Begin
- Friends[0][J] := I;
- Friends[1][J] := PotFriend;
- J := J + 1;
- End;
- End;
- Calculate := J;
- End;
- Function SaveResultIntoFile(Const Friends: TFriends; Length: Integer): Boolean;
- Var
- Saved: Boolean;
- FileToSave: TextFile;
- I: Integer;
- Begin
- Saved := True;
- TakeCorrectFile(FileToSave, False);
- For I := 0 To Length - 1 Do
- Begin
- Write(FileToSave, Friends[0][I]);
- Write(FileToSave, ' ');
- WriteLn(FileToSave, Friends[1][I]);
- End;
- Close(FileToSave);
- SaveResultIntoFile := Saved;
- End;
- Procedure PrintResultIntoConsole(Const Friends: TFriends; Length: Integer);
- Var
- I: Integer;
- Begin
- WriteLn;
- WriteLn('Friendly numbers up to ', FRIEND_NUMBERS_FIND_MAX, ':');
- For I := 0 To Length - 1 Do
- Begin
- Write(Friends[0][I]);
- Write(' ');
- WriteLn(Friends[1][I]);
- End;
- WriteLn;
- End;
- Procedure PrintResult(Const Friends: TFriends; Length: Integer);
- Var
- WriteType: Integer;
- Saved: Boolean;
- Begin
- Saved := False;
- WriteLn;
- WriteLn('Where post result?');
- WriteLn('0 - Only into console');
- WriteLn('1 - Only into file');
- WriteLn('2 - Into console and into file');
- WriteType := TakeIntValueInRangeFromConsole('Enter write type: ', PRINT_TYPE_MIN, PRINT_TYPE_MAX);
- Case WriteType Of
- 0:
- Begin
- PrintResultIntoConsole(Friends, Length);
- End;
- 1:
- Begin
- Saved := SaveResultIntoFile(Friends, Length);
- End;
- 2:
- Begin
- Saved := SaveResultIntoFile(Friends, Length);
- PrintResultIntoConsole(Friends, Length);
- End;
- End;
- If (Saved) Then
- WriteLn('Result saved into file!');
- End;
- Begin
- Var Friends: TFriends;
- Var N: Integer;
- WriteLn('2. Output all friendly numbers up to 32000.');
- N := Calculate(Friends);
- PrintResult(Friends, N);
- WriteLn('Press [ENTER] to close program...');
- ReadLn;
- End.
Tags:
delphi
Advertisement
Add Comment
Please, Sign In to add comment
-
⭐✅ Trading Profit Method ✅ NEVER SEEN BEFORE...
JavaScript | 4 sec ago | 0.24 KB
-
⭐ Instant BTC Profit Method ✅ NEVER SEEN BEFO...
JavaScript | 13 sec ago | 0.24 KB
-
⭐✅ MAKE $2000 INSTANTLY ✅ NEVER SEEN BEFORE ⭐...
JavaScript | 23 sec ago | 0.24 KB
-
⭐ Free Crypto Method ✅ NEVER SEEN BEFORE ⭐⭐⭐
JavaScript | 34 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ✅ NEVER SEEN...
JavaScript | 45 sec ago | 0.24 KB
-
⭐✅ Marketplace Glitch ✅ Working ✅ NEVER SEEN...
JavaScript | 54 sec ago | 0.24 KB
-
✅⭐ Make $2500 in 15 minutes ✅ NEVER SEEN BEFO...
JavaScript | 1 min ago | 0.24 KB
-
⭐ Instant BTC Profit Method ✅ NEVER SEEN BEFO...
JavaScript | 1 min ago | 0.24 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand