Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program lab2_5;
- {$APPTYPE CONSOLE}
- uses
- System.SysUtils;
- Type
- TMassivs = array of array of Integer;
- 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 Print(Triangle: TMassivs; Size: Integer);
- Var
- I, J, Temp : Integer;
- Begin
- Temp := Size - 1;
- For I := 0 to Temp do
- Begin
- For J := 0 to I do
- Write(Triangle[I,J], ' ');
- writeln;
- End;
- End;
- Function GetSizeFromFile(Size : Integer): Integer;
- Var
- TxtFile: TextFile;
- Begin
- AssignFile(TxtFile, InputFileLocation());
- Reset(TxtFile);
- Read(TxtFile, Size);
- ReadLn(TxtFile);
- CloseFile(TxtFile);
- GetSizeFromFile := Size;
- End;
- Procedure OutputToFile(Triangle: TMassivs; Size: Integer);
- Var
- I, J, Temp: Integer;
- TxtFile: TextFile;
- Line: String;
- Begin
- Temp := Size - 1;
- AssignFile(TxtFile, InputFileLocation());
- Rewrite(TxtFile);
- Begin
- For I := 0 to Temp do
- Begin
- For J := 0 to I do
- Write(TxtFile, Triangle[I,J], ' ');
- writeln(TxtFile);
- End;
- CloseFile(TxtFile);
- End;
- End;
- procedure CreateTriangle(Size: Integer ; Triangle: TMassivs);
- Var
- N, K:Integer;
- Begin
- N := 0;
- K := 0;
- for n := 0 to size do
- Begin
- triangle[n,0] := 1;
- triangle[n,n] := 1;
- for k := 1 to n do
- triangle[n][k] := triangle[n - 1][k - 1] + triangle[n - 1][k];
- End;
- End;
- Function GetSizeFromConsole(IsNatural: Boolean = false): Integer;
- Var
- IsCorrect: Boolean;
- Size: Integer;
- Begin
- Size := 0;
- WriteLn('Enter the matrix size');
- Repeat
- IsCorrect := false;
- Try
- ReadLn(Size);
- IsCorrect := true;
- Except
- WriteLn('Enter the Number')
- End;
- If IsCorrect and IsNatural and (Size < 1) then
- Begin
- IsCorrect := false;
- WriteLn('Please enter a natural value');
- End;
- Until IsCorrect ;
- GetSizeFromConsole := Size;
- End;
- Procedure Main();
- Var
- ChosenInput: Integer;
- Triangle: TMassivs;
- Size : Integer;
- Begin
- Size := 0;
- WriteLn('Удалить строки массива , содержащие нулевые элементы.');
- ChosenInput := ChooseInput();
- If (ChosenInput = 0) then
- Size := GetSizeFromFile(size)
- Else
- Size := GetSizeFromConsole(true);
- writeln('Pascal`s Triangle');
- SetLength(Triangle, Size + 1, Size + 1);
- CreateTriangle(Size, Triangle);
- Print(Triangle, Size);
- WriteLn;
- OutputToFile(Triangle, Size);
- ReadLn;
- End;
- Begin
- Main();
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement