Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program FinalLaba3zad3;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- SysUtils;
- const
- CMaxNameSize = 30;
- CMaxNumPart = 15;
- CMinNumPart = 2;
- CMinScore = 0;
- CMaxScore = 1000;
- type
- TGrade = Integer;
- TRec = record
- ParticipantName : string[CMaxNameSize];
- Score : TGrade;
- end;
- TArrOfRec = array of TRec;
- function GetUserAcception: Boolean;
- type
- TCharSet = Set of Char;
- var
- UserChoice: Char;
- UserCommandForAcception: TCharSet;
- UserCommandForRejection: TCharSet;
- begin
- repeat
- Write('Enter ''Y'' / 1 for acception or ''N'' / 0 for rejection: ');
- UserCommandForAcception := ['Y','y','1'];
- UserCommandForRejection := ['N','n','0'];
- ReadLn(UserChoice);
- until UserChoice in (UserCommandForAcception + UserCommandForRejection);
- GetUserAcception := UserChoice in UserCommandForAcception;
- end;
- function GetRightInputPath(): string;
- var
- PathToFile: string;
- FileIsAvailable: Boolean;
- TextFile: Text;
- begin
- Write('Enter path to input file: ');
- repeat
- FileIsAvailable := True;
- ReadLn(PathToFile);//C:\ucheba\Лабы\Win32\Debug\laba3zad3In.txt
- if not FileExists(PathToFile) then
- Write('File doesnt exists or specified path is wrong.Try again: ')
- else
- try
- Assign(TextFile, PathToFile);
- Reset(TextFile);
- Close(TextFile);
- except
- FileIsAvailable := False;
- Write('File is unavailable.Unclock it for reading or choose',
- 'another file: ');
- end;
- until FileExists(PathToFile) and FileIsAvailable;
- GetRightInputPath := PathToFile;
- end;
- function GetValidNumPart(): Integer;
- var
- NumPart: Integer;
- NumPartIsCorr: Boolean;
- begin
- NumPart := 0;
- Write('Enter number of participants: ');
- repeat
- try
- NumPartIsCorr := True;
- Read(NumPart);
- if (NumPart > CMaxNumPart) or (NumPart < CMinNumPart) then
- begin
- NumPartIsCorr := False;
- Write('Number of participants can be from ', CMinNumPart,' to ',
- CMaxNumPart,'.Try again: ');
- end;
- except
- NumPartIsCorr := False;
- Write ('Enter integer number from ', CMinNumPart,' to ',
- CMaxNumPart,'.Try again: ');
- end;
- until NumPartIsCorr;
- GetValidNumPart := NumPart;
- end;
- function InPutFromFile(PathToFile: string;
- var DataOfFileIsCorr: Boolean): TArrOfRec;
- var
- TextFile: Text;
- List: TArrOfRec;
- i, NumPart: Integer;
- TempCh: Char;
- DistChSet: set of Char;
- begin
- DistChSet := [':','|'];
- Write('Enter number of participants: ');
- NumPart := GetValidNumPart();
- SetLength(List, NumPart);
- repeat
- try
- DataOfFileIsCorr := True;
- Assign(TextFile, PathToFile);
- Reset(TextFile);
- for i := 0 to Length(List) - 1 do
- begin
- with List[i] do
- begin
- TempCh := #0;
- repeat
- ParticipantName := ParticipantName + TempCh;
- Read(TextFile, TempCh);
- until EOLn(TextFile) or (TempCh in DistChSet);
- while Length(ParticipantName) < CMaxNameSize do
- ParticipantName := ParticipantName + ' ';
- ReadLn(TextFile, Score);
- if (Score < CMinScore) or (Score > CMaxScore) then
- DataOfFileIsCorr := False;
- end;
- end;
- except
- WriteLn('File data is not valid!');
- DataOfFileIsCorr := False;
- end;
- until DataOfFileIsCorr;
- Close(TextFile);
- InPutFromFile := List;
- end;
- function InPutConsole(): TArrOfRec;
- var
- NumOfPart: Integer;
- ListIsCorrect: Boolean;
- List: TArrOfRec;
- i: Integer;
- begin
- i := 0;
- NumOfPart := GetValidNumPart();
- SetLength(List, NumOfPart);
- repeat
- try
- ListIsCorrect := True;
- while i < Length(List) do
- begin
- with List[i] do
- begin
- Write('Enter name of ', i + 1,' participant: ');
- Flush(Input);
- ReadLn(ParticipantName);
- while Length(ParticipantName) < CMaxNameSize do
- ParticipantName := ParticipantName + ' ';
- Write('Enter score of ', i + 1,' participant: ');
- ReadLn(Score);
- Inc(i);
- end;
- end;
- except
- begin
- ListIsCorrect := False;
- WriteLn('You have entered not valid data. Try again!');
- end;
- end;
- until ListIsCorrect;
- InPutConsole := List;
- end;
- function GetAvailableOutputPath(): string;
- var
- OutPutPath: string;
- OutPutFile: Text;
- OutPutFileIsAvailable: Boolean;
- begin
- WriteLn('Warning!!!Choosed file will be rewriten.You may lose yor''s data.');
- repeat
- OutPutFileIsAvailable := True;
- Write('Enter output path: ');
- try
- Read(OutPutPath);
- Assign(OutPutFile, OutputPath);
- ReWrite(OutPutFile);
- Close(OutPutFile);
- except
- OutPutFileIsAvailable := False;
- Write('File is unavailable!try again.');
- end;
- until OutPutFileIsAvailable;
- GetAvailableOutputPath := OutputPath
- end;
- procedure OutPutFile(List: TArrOfRec; PathToOutputFile: String);
- var
- OutPutFile: Text;
- i: Integer;
- begin
- Assign(OutPutFile, PathToOutputFile);
- Rewrite(OutPutFile);
- WriteLn(OutPutFile, 'Result List: ');
- for i := 0 to Length(List) - 1 do
- begin
- with List[i] do
- begin
- Write(OutPutFile, ParticipantName, ' | ');
- WriteLn(OutPutFile, Score);
- end;
- end;
- WriteLn('Result has been successfully saved!');
- Close(OutPutFile);
- end;
- procedure OutPutArrOfRec(List: TArrOfRec);
- var
- i: Integer;
- begin
- for i := 0 to Length(List) - 1 do
- begin
- with List[i] do
- begin
- Write(ParticipantName, ' | ');
- WriteLn(Score);
- end;
- end;
- end;
- function Sort(List: TArrOfRec): TArrOfRec;
- const
- CFirstSymbolPos = 1;
- var
- i, j: Integer;
- TempRec: TRec;
- begin
- for i := Length(List) - 1 downto 0 do
- for j:= i downto 0 do
- begin
- if List[j].Score < List[i].Score then
- begin
- TempRec := List[i];
- List[i] := List[j];
- List[j] := TempRec;
- end;
- if (List[j].Score = List[i].Score) and
- (Ord(List[i].ParticipantName[CFirstSymbolPos]) <
- Ord(List[j].ParticipantName[CFirstSymbolPos])) then
- begin
- TempRec := List[i];
- List[i] := List[j];
- List[j] := TempRec;
- end;
- end;
- Sort := List;
- end;
- procedure main;
- var
- List: TArrOfRec;
- PathToInputFile, PathToOutputFile: string;
- DataOfFileIsCorr: Boolean;
- begin
- repeat
- Write('Do you want to get list of participans from file?');
- if GetUserAcception then
- repeat
- PathToInputFile := GetRightInputPath;
- DataOfFileIsCorr := True;
- List := InPutFromFile(PathToInputFile, DataOfFileIsCorr)
- until DataOfFileIsCorr
- else
- List := InPutConsole();
- List := Sort(List);
- OutPutArrOfRec(List);
- Write('Save results to file?');
- if GetUserAcception then
- begin
- PathToOutputFile := GetAvailableOutputPath();
- OutPutFile(List, PathToOutputFile);
- end;
- Write('Restart program?');
- until not GetUserAcception;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement